Skip to content

Commit

Permalink
Release 30.14.0
Browse files Browse the repository at this point in the history
2617baea3792929fad5b7489f67b00c547075332
  • Loading branch information
robot-divkit committed Aug 19, 2024
1 parent 8a25863 commit 4b8c4c7
Show file tree
Hide file tree
Showing 18 changed files with 253 additions and 49 deletions.
5 changes: 5 additions & 0 deletions .mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,7 @@
"Specs/DivKit/30.11.0/DivKit.podspec":"divkit/public-ios/Specs/DivKit/30.11.0/DivKit.podspec",
"Specs/DivKit/30.12.0/DivKit.podspec":"divkit/public-ios/Specs/DivKit/30.12.0/DivKit.podspec",
"Specs/DivKit/30.13.0/DivKit.podspec":"divkit/public-ios/Specs/DivKit/30.13.0/DivKit.podspec",
"Specs/DivKit/30.14.0/DivKit.podspec":"divkit/public-ios/Specs/DivKit/30.14.0/DivKit.podspec",
"Specs/DivKit/30.2.0/DivKit.podspec":"divkit/public-ios/Specs/DivKit/30.2.0/DivKit.podspec",
"Specs/DivKit/30.3.0/DivKit.podspec":"divkit/public-ios/Specs/DivKit/30.3.0/DivKit.podspec",
"Specs/DivKit/30.4.0/DivKit.podspec":"divkit/public-ios/Specs/DivKit/30.4.0/DivKit.podspec",
Expand Down Expand Up @@ -960,6 +961,7 @@
"Specs/DivKitExtensions/30.11.0/DivKitExtensions.podspec":"divkit/public-ios/Specs/DivKitExtensions/30.11.0/DivKitExtensions.podspec",
"Specs/DivKitExtensions/30.12.0/DivKitExtensions.podspec":"divkit/public-ios/Specs/DivKitExtensions/30.12.0/DivKitExtensions.podspec",
"Specs/DivKitExtensions/30.13.0/DivKitExtensions.podspec":"divkit/public-ios/Specs/DivKitExtensions/30.13.0/DivKitExtensions.podspec",
"Specs/DivKitExtensions/30.14.0/DivKitExtensions.podspec":"divkit/public-ios/Specs/DivKitExtensions/30.14.0/DivKitExtensions.podspec",
"Specs/DivKitExtensions/30.2.0/DivKitExtensions.podspec":"divkit/public-ios/Specs/DivKitExtensions/30.2.0/DivKitExtensions.podspec",
"Specs/DivKitExtensions/30.3.0/DivKitExtensions.podspec":"divkit/public-ios/Specs/DivKitExtensions/30.3.0/DivKitExtensions.podspec",
"Specs/DivKitExtensions/30.4.0/DivKitExtensions.podspec":"divkit/public-ios/Specs/DivKitExtensions/30.4.0/DivKitExtensions.podspec",
Expand Down Expand Up @@ -1007,6 +1009,7 @@
"Specs/DivKit_LayoutKit/30.11.0/DivKit_LayoutKit.podspec":"divkit/public-ios/Specs/DivKit_LayoutKit/30.11.0/DivKit_LayoutKit.podspec",
"Specs/DivKit_LayoutKit/30.12.0/DivKit_LayoutKit.podspec":"divkit/public-ios/Specs/DivKit_LayoutKit/30.12.0/DivKit_LayoutKit.podspec",
"Specs/DivKit_LayoutKit/30.13.0/DivKit_LayoutKit.podspec":"divkit/public-ios/Specs/DivKit_LayoutKit/30.13.0/DivKit_LayoutKit.podspec",
"Specs/DivKit_LayoutKit/30.14.0/DivKit_LayoutKit.podspec":"divkit/public-ios/Specs/DivKit_LayoutKit/30.14.0/DivKit_LayoutKit.podspec",
"Specs/DivKit_LayoutKit/30.2.0/DivKit_LayoutKit.podspec":"divkit/public-ios/Specs/DivKit_LayoutKit/30.2.0/DivKit_LayoutKit.podspec",
"Specs/DivKit_LayoutKit/30.3.0/DivKit_LayoutKit.podspec":"divkit/public-ios/Specs/DivKit_LayoutKit/30.3.0/DivKit_LayoutKit.podspec",
"Specs/DivKit_LayoutKit/30.4.0/DivKit_LayoutKit.podspec":"divkit/public-ios/Specs/DivKit_LayoutKit/30.4.0/DivKit_LayoutKit.podspec",
Expand Down Expand Up @@ -1054,6 +1057,7 @@
"Specs/DivKit_LayoutKitInterface/30.11.0/DivKit_LayoutKitInterface.podspec":"divkit/public-ios/Specs/DivKit_LayoutKitInterface/30.11.0/DivKit_LayoutKitInterface.podspec",
"Specs/DivKit_LayoutKitInterface/30.12.0/DivKit_LayoutKitInterface.podspec":"divkit/public-ios/Specs/DivKit_LayoutKitInterface/30.12.0/DivKit_LayoutKitInterface.podspec",
"Specs/DivKit_LayoutKitInterface/30.13.0/DivKit_LayoutKitInterface.podspec":"divkit/public-ios/Specs/DivKit_LayoutKitInterface/30.13.0/DivKit_LayoutKitInterface.podspec",
"Specs/DivKit_LayoutKitInterface/30.14.0/DivKit_LayoutKitInterface.podspec":"divkit/public-ios/Specs/DivKit_LayoutKitInterface/30.14.0/DivKit_LayoutKitInterface.podspec",
"Specs/DivKit_LayoutKitInterface/30.2.0/DivKit_LayoutKitInterface.podspec":"divkit/public-ios/Specs/DivKit_LayoutKitInterface/30.2.0/DivKit_LayoutKitInterface.podspec",
"Specs/DivKit_LayoutKitInterface/30.3.0/DivKit_LayoutKitInterface.podspec":"divkit/public-ios/Specs/DivKit_LayoutKitInterface/30.3.0/DivKit_LayoutKitInterface.podspec",
"Specs/DivKit_LayoutKitInterface/30.4.0/DivKit_LayoutKitInterface.podspec":"divkit/public-ios/Specs/DivKit_LayoutKitInterface/30.4.0/DivKit_LayoutKitInterface.podspec",
Expand Down Expand Up @@ -1101,6 +1105,7 @@
"Specs/DivKit_Serialization/30.11.0/DivKit_Serialization.podspec":"divkit/public-ios/Specs/DivKit_Serialization/30.11.0/DivKit_Serialization.podspec",
"Specs/DivKit_Serialization/30.12.0/DivKit_Serialization.podspec":"divkit/public-ios/Specs/DivKit_Serialization/30.12.0/DivKit_Serialization.podspec",
"Specs/DivKit_Serialization/30.13.0/DivKit_Serialization.podspec":"divkit/public-ios/Specs/DivKit_Serialization/30.13.0/DivKit_Serialization.podspec",
"Specs/DivKit_Serialization/30.14.0/DivKit_Serialization.podspec":"divkit/public-ios/Specs/DivKit_Serialization/30.14.0/DivKit_Serialization.podspec",
"Specs/DivKit_Serialization/30.2.0/DivKit_Serialization.podspec":"divkit/public-ios/Specs/DivKit_Serialization/30.2.0/DivKit_Serialization.podspec",
"Specs/DivKit_Serialization/30.3.0/DivKit_Serialization.podspec":"divkit/public-ios/Specs/DivKit_Serialization/30.3.0/DivKit_Serialization.podspec",
"Specs/DivKit_Serialization/30.4.0/DivKit_Serialization.podspec":"divkit/public-ios/Specs/DivKit_Serialization/30.4.0/DivKit_Serialization.podspec",
Expand Down
18 changes: 16 additions & 2 deletions DivKit/DivBlockStateStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,21 @@ public struct IdAndCardId: Hashable {
}

public final class DivBlockStateStorage {
public struct ChangeEvent {
public let id: IdAndCardId
public let state: ElementState
}

public private(set) var states: BlocksState
private var statesById: [IdAndCardId: ElementState] = [:]
private var focusedElement: UIElementPath?
private var focusedElementById: IdAndCardId?
private let lock = AllocatedUnfairLock()
private let stateUpdatesPipe = SignalPipe<ChangeEvent>()

var stateUpdates: Signal<ChangeEvent> {
stateUpdatesPipe.signal
}

public init(states: BlocksState = [:]) {
self.states = states
Expand Down Expand Up @@ -53,16 +63,20 @@ public final class DivBlockStateStorage {
}

public func setState(path: UIElementPath, state: ElementState) {
let id = IdAndCardId(path: path)
lock.withLock {
statesById[IdAndCardId(path: path)] = nil
statesById[id] = nil
states[path] = state
}
stateUpdatesPipe.send(ChangeEvent(id: id, state: state))
}

public func setState(id: String, cardId: DivCardID, state: ElementState) {
let id = IdAndCardId(id: id, cardId: cardId)
lock.withLock {
statesById[IdAndCardId(id: id, cardId: cardId)] = state
statesById[id] = state
}
stateUpdatesPipe.send(ChangeEvent(id: id, state: state))
}

public func setFocused(isFocused: Bool, element: IdAndCardId) {
Expand Down
1 change: 1 addition & 0 deletions DivKit/DivKitComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public final class DivKitComponents {

triggersStorage = DivTriggersStorage(
variablesStorage: variablesStorage,
stateUpdates: blockStateStorage.stateUpdates,
actionHandler: actionHandler,
persistentValuesStorage: persistentValuesStorage,
reporter: reporter
Expand Down
2 changes: 1 addition & 1 deletion DivKit/DivKitInfo.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
public enum DivKitInfo {
public static let version = "30.13.0"
public static let version = "30.14.0"
}
2 changes: 1 addition & 1 deletion DivKit/StoredValues/DivPersistentValuesStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ extension DivStoredValue {
} else {
nil
}
case .bool:
case .bool, .boolean:
switch value.lowercased() {
case "0", "false":
.bool(false)
Expand Down
3 changes: 2 additions & 1 deletion DivKit/StoredValues/DivStoredValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ struct DivStoredValue {
case string
case number
case integer
case bool
case boolean
case bool // invalid value, used for backward compatibility
case color
case url
}
Expand Down
59 changes: 45 additions & 14 deletions DivKit/Variables/DivTriggersStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public final class DivTriggersStorage {

public init(
variablesStorage: DivVariablesStorage,
stateUpdates: Signal<DivBlockStateStorage.ChangeEvent> = .empty,
actionHandler: DivActionHandler,
persistentValuesStorage: DivPersistentValuesStorage,
reporter: DivReporter? = nil
Expand All @@ -42,6 +43,10 @@ public final class DivTriggersStorage {
self.actionHandler = actionHandler
self.persistentValuesStorage = persistentValuesStorage
self.reporter = reporter ?? DefaultDivReporter()

stateUpdates.addObserver { [weak self] stateEvent in
self?.handleStateEvent(stateEvent)
}.dispose(in: disposePool)
}

public func set(
Expand Down Expand Up @@ -114,19 +119,30 @@ public final class DivTriggersStorage {
}

func enableTriggers(path: UIElementPath) {
let item = lock.withLock {
let item = triggersByPath[path]
item?.active = true
return item
enableTriggers(predicate: { $0.starts(with: path) })
}

func disableTriggers(path: UIElementPath) {
disableTriggers(predicate: { $0.starts(with: path) })
}

private func enableTriggers(predicate: (UIElementPath) -> Bool) {
let items = lock.withLock {
let triggers = triggersByPath.filter { predicate($0.key) }
triggers.forEach { $0.value.active = true }
return triggers
}
guard let item else { return }

runActions(path: path, item: item, changedVariablesNames: nil)
items.forEach { path, item in
runActions(path: path, item: item, changedVariablesNames: nil)
}
}

func disableTriggers(path: UIElementPath) {
private func disableTriggers(predicate: (UIElementPath) -> Bool) {
lock.withLock {
triggersByPath[path]?.active = false
triggersByPath
.filter { predicate($0.key) }
.forEach { $0.value.active = false }
}
}

Expand Down Expand Up @@ -175,20 +191,24 @@ public final class DivTriggersStorage {
}
}
}
}

extension DivTriggersStorage: ElementStateObserver {
public func elementStateChanged(_ state: ElementState, forPath path: UIElementPath) {
if let tabState = state as? TabViewState {
private func handleStateEvent(_ stateEvent: DivBlockStateStorage.ChangeEvent) {
if let tabState = stateEvent.state as? TabViewState {
let activeTab = Int(tabState.selectedPageIndex)

for index in 0..<tabState.countOfPages {
if index != activeTab {
disableTriggers(path: path + index)
disableTriggers(predicate: {
$0.cardId == stateEvent.id.cardId
&& $0.findTabId(stateEvent.id.id) == String(index)
})
}
}

enableTriggers(path: path + activeTab)
enableTriggers(predicate: {
$0.cardId == stateEvent.id.cardId
&& $0.findTabId(stateEvent.id.id) == String(activeTab)
})
}
}
}
Expand All @@ -215,4 +235,15 @@ extension UIElementPath {
}
return false
}

fileprivate func findTabId(_ tabsId: String) -> String? {
var currPath: UIElementPath? = self
while let path = currPath {
if path.parent?.leaf == tabsId {
return path.leaf
}
currPath = currPath?.parent
}
return nil
}
}
1 change: 0 additions & 1 deletion DivKit/Views/DivView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ public final class DivView: VisibleBoundsTrackingView {
extension DivView: ElementStateObserver {
public func elementStateChanged(_ state: ElementState, forPath path: UIElementPath) {
divKitComponents.blockStateStorage.elementStateChanged(state, forPath: path)
divKitComponents.triggersStorage.elementStateChanged(state, forPath: path)
blockProvider?.update(withStates: [path: state])
}

Expand Down
4 changes: 2 additions & 2 deletions LayoutKit/LayoutKit/Blocks/GalleryBlock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public final class GalleryBlock: BlockWithTraits {
}
}

public func intrinsicContentHeight(forWidth _: CGFloat) -> CGFloat {
public func intrinsicContentHeight(forWidth width: CGFloat) -> CGFloat {
switch heightTrait {
case let .fixed(value):
return value
case let .intrinsic(_, minSize, maxSize):
let height = contentSize.height
let height = model.intrinsicSize(width: width).height
return clamp(height, min: minSize, max: maxSize)
case .weighted:
return 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension ScrollableContentPager: ScrollDelegate {
: targetContentOffset.pointee.y
if let resultOffset = targetPageOffset(
forProposedOffset: proposedOffset,
velocity: velocity.projection(isHorizontal)
isHorizontal: isHorizontal
) {
if isHorizontal {
targetContentOffset.pointee.x = resultOffset
Expand Down
68 changes: 42 additions & 26 deletions LayoutKit/LayoutKit/UI/Base/ScrollableContentPager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,69 @@ public final class ScrollableContentPager: NSObject {
currentPageIndex = nil
}
}

public func setInitialOffset(_ offset: CGFloat) {
guard isPagingEnabled else { return }

currentPageIndex = pageIndex(forOffset: offset)

if currentPageIndex == nil, lastContentOffset == nil, indexedPageOrigins.count > 0 {
let initialPageIndex = pageIndex(forOffset: offset)
let pageOrigin = indexedPageOrigins[initialPageIndex].origin
let initialContentOffset = isHorizontal ? CGPoint(x: pageOrigin, y: 0) : CGPoint(x: 0, y: pageOrigin)

currentPageIndex = initialPageIndex
lastContentOffset = initialContentOffset
}
}

public private(set) var currentPageIndex: Int?
public private(set) var lastContentOffset: CGPoint?

public func targetPageOffset(forProposedOffset offset: CGFloat, velocity: CGFloat) -> CGFloat? {
guard let startPageIndex = currentPageIndex else { return nil }

public func targetPageOffset(forProposedOffset offset: CGFloat, isHorizontal: Bool) -> CGFloat? {
guard currentPageIndex != nil,
var newLastContentOffset = lastContentOffset else {
return nil
}

var resultPageIndex = pageIndex(forOffset: offset)

if resultPageIndex == startPageIndex, velocity.isApproximatelyNotEqualTo(0) {
let forcedIndexOffset = (velocity > 0 ? 1 : -1)
resultPageIndex += forcedIndexOffset
var forcedIndexOffset = 0
if newLastContentOffset.forceIndexOffset(isHorizontal: isHorizontal).isApproximatelyNotEqualTo(offset) {
forcedIndexOffset = newLastContentOffset.forceIndexOffset(isHorizontal: isHorizontal) < offset ? 1 : 0
}
resultPageIndex += forcedIndexOffset

resultPageIndex = clamp(
resultPageIndex,
min: 0,
max: indexedPageOrigins.count - 1
)


let targetPageOffset = indexedPageOrigins[resultPageIndex].origin
if isHorizontal {
newLastContentOffset.x = targetPageOffset
} else {
newLastContentOffset.y = targetPageOffset
}
lastContentOffset = newLastContentOffset
currentPageIndex = resultPageIndex
return indexedPageOrigins[resultPageIndex].origin
return targetPageOffset
}

private func pageIndex(forOffset offset: CGFloat) -> Int {
Int(round(intermediatePageIndex(forOffset: offset)))
}

private func intermediatePageIndex(forOffset offset: CGFloat) -> Float {
guard let rightBound = indexedPageOrigins.first(where: {
guard indexedPageOrigins.first(where: {
$0.origin.isApproximatelyGreaterOrEqualThan(offset)
}) else { return Float(indexedPageOrigins.count - 1) }

}) != nil else { return indexedPageOrigins.count - 1 }
guard let leftBound = indexedPageOrigins.reversed().first(where: {
$0.origin.isApproximatelyLessOrEqualThan(offset)
}) else { return 0 }

return leftBound.index
}
}

guard rightBound.index != leftBound.index,
!rightBound.origin.isApproximatelyEqualTo(leftBound.origin) else {
return Float(leftBound.index)
extension CGPoint {
fileprivate func forceIndexOffset(isHorizontal: Bool) -> CGFloat {
isHorizontal ? self.x : self.y
}

let relativePart = (offset - leftBound.origin) / (rightBound.origin - leftBound.origin)
return Float(leftBound.index) + Float(relativePart)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ private final class GestureView: BlockView {
action: #selector(handleSwipe(_:))
)
recognizer.direction = direction
recognizer.delegate = self
return recognizer
}
}
Expand All @@ -136,3 +137,12 @@ extension GestureView: VisibleBoundsTrackingContainer {
[childView]
}
}

extension GestureView: UIGestureRecognizerDelegate {
func gestureRecognizer(
_ gestureRecognizer: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer
) -> Bool {
return true
}
}
11 changes: 11 additions & 0 deletions LayoutKit/LayoutKit/ViewModels/GalleryViewLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ extension GalleryViewModel {
let frames = self.frames(fitting: nil)
return contentSize(for: frames, fitting: nil)
}

func intrinsicSize(width: CGFloat) -> CGSize {
switch direction {
case .horizontal:
return intrinsicSize
case .vertical:
let size = CGSize(width: width, height: .infinity)
let frames = self.frames(fitting: size)
return contentSize(for: frames, fitting: size)
}
}
}

extension GalleryViewModel {
Expand Down
Loading

0 comments on commit 4b8c4c7

Please sign in to comment.