Skip to content

Commit

Permalink
Release 26.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
robot-divkit authored and root committed Jul 25, 2023
1 parent 4cdd2c8 commit 0ba8b33
Show file tree
Hide file tree
Showing 46 changed files with 775 additions and 183 deletions.
8 changes: 8 additions & 0 deletions Core/BaseTinyPublic/iOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ public typealias SystemShadow = NSShadow

public typealias EdgeInsets = UIEdgeInsets

public typealias UserInterfaceLayoutDirection = UIUserInterfaceLayoutDirection

extension UIUserInterfaceLayoutDirection {
public static var system: UIUserInterfaceLayoutDirection {
return UIApplication.shared.userInterfaceLayoutDirection
}
}

extension NSShadow {
public var cgColor: CGColor? { (shadowColor as? UIColor)?.cgColor }
}
Expand Down
4 changes: 3 additions & 1 deletion DivKit/Actions/DivActionHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public final class DivActionHandler {
patchProvider: DivPatchProvider,
variablesStorage: DivVariablesStorage = DivVariablesStorage(),
updateCard: @escaping DivActionURLHandler.UpdateCardAction,
showTooltip: @escaping DivActionURLHandler.ShowTooltipAction,
showTooltip: DivActionURLHandler.ShowTooltipAction?,
tooltipActionPerformer: TooltipActionPerformer? = nil,
logger: DivActionLogger = EmptyDivActionLogger(),
trackVisibility: @escaping TrackVisibility = { _, _ in },
trackDisappear: @escaping TrackVisibility = { _, _ in },
Expand All @@ -47,6 +48,7 @@ public final class DivActionHandler {
variableUpdater: variablesStorage,
updateCard: updateCard,
showTooltip: showTooltip,
tooltipActionPerformer: tooltipActionPerformer,
performTimerAction: performTimerAction
),
logger: logger,
Expand Down
21 changes: 16 additions & 5 deletions DivKit/Actions/DivActionURLHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public final class DivActionURLHandler {
private let patchProvider: DivPatchProvider
private let variableUpdater: DivVariableUpdater
private let updateCard: UpdateCardAction
private let showTooltip: ShowTooltipAction
private let showTooltip: ShowTooltipAction?
private let tooltipActionPerformer: TooltipActionPerformer?
private let performTimerAction: PerformTimerAction

public init(
Expand All @@ -40,7 +41,8 @@ public final class DivActionURLHandler {
patchProvider: DivPatchProvider,
variableUpdater: DivVariableUpdater,
updateCard: @escaping UpdateCardAction,
showTooltip: @escaping ShowTooltipAction,
showTooltip: ShowTooltipAction?,
tooltipActionPerformer: TooltipActionPerformer?,
performTimerAction: @escaping PerformTimerAction = { _, _, _ in }
) {
self.stateUpdater = stateUpdater
Expand All @@ -49,6 +51,7 @@ public final class DivActionURLHandler {
self.variableUpdater = variableUpdater
self.updateCard = updateCard
self.showTooltip = showTooltip
self.tooltipActionPerformer = tooltipActionPerformer
self.performTimerAction = performTimerAction
}

Expand All @@ -67,9 +70,17 @@ public final class DivActionURLHandler {

switch intent {
case let .showTooltip(id, multiple):
showTooltip(TooltipInfo(id: id, showsOnStart: false, multiple: multiple))
case .hideTooltip:
return false
let tooltipInfo = TooltipInfo(id: id, showsOnStart: false, multiple: multiple)
guard showTooltip == nil else {
showTooltip?(tooltipInfo)
break
}
tooltipActionPerformer?.showTooltip(info: tooltipInfo)
case let .hideTooltip(id):
guard showTooltip == nil else {
break
}
tooltipActionPerformer?.hideTooltip(id: id)
case let .download(patchUrl):
patchProvider.getPatch(
url: patchUrl,
Expand Down
29 changes: 18 additions & 11 deletions DivKit/DivBlockModelingContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,28 @@ public struct DivBlockModelingContext {
public let extensionHandlers: [String: DivExtensionHandler]
public let stateInterceptors: [String: DivStateInterceptor]
private let variables: DivVariables
public let layoutDirection: LayoutDirection
public let layoutDirection: UserInterfaceLayoutDirection
public let debugParams: DebugParams
public let playerFactory: PlayerFactory?
public var childrenA11yDescription: String?
public weak var parentScrollView: ScrollView?
public let errorsStorage: DivErrorsStorage
private let variableTracker: DivVariableTracker?

var overridenWidth: DivOverridenSize?
var overridenHeight: DivOverridenSize?

public var expressionResolver: ExpressionResolver {
ExpressionResolver(
variables: variables,
errorTracker: { [weak errorsStorage] error in
errorsStorage?.add(DivBlockModelingRuntimeError(error, path: parentPath))
},
variableTracker: { [weak variableTracker] variables in
variableTracker?.onVariablesUsed(cardId: cardId, variables: variables)
}
)
}
public let debugParams: DebugParams
public let playerFactory: PlayerFactory?
public var childrenA11yDescription: String?
public weak var parentScrollView: ScrollView?
public let errorsStorage: DivErrorsStorage
var overridenWidth: DivOverridenSize?
var overridenHeight: DivOverridenSize?

public init(
cardId: DivCardID,
Expand All @@ -60,7 +66,8 @@ public struct DivBlockModelingContext {
childrenA11yDescription: String? = nil,
parentScrollView: ScrollView? = nil,
errorsStorage: DivErrorsStorage = DivErrorsStorage(errors: []),
layoutDirection: LayoutDirection = .system
layoutDirection: UserInterfaceLayoutDirection = UserInterfaceLayoutDirection.system,
variableTracker: DivVariableTracker? = nil
) {
self.cardId = cardId
self.cardLogId = cardLogId
Expand All @@ -74,14 +81,14 @@ public struct DivBlockModelingContext {
self.divCustomBlockFactory = divCustomBlockFactory
self.flagsInfo = flagsInfo
self.fontProvider = fontProvider ?? DefaultFontProvider()
self.variables = variables
self.playerFactory = playerFactory
self.debugParams = debugParams
self.childrenA11yDescription = childrenA11yDescription
self.parentScrollView = parentScrollView
self.errorsStorage = errorsStorage
self.layoutDirection = layoutDirection

self.variables = variables
self.variableTracker = variableTracker

var extensionsHandlersDictionary = [String: DivExtensionHandler]()
extensionHandlers.forEach {
Expand Down
63 changes: 45 additions & 18 deletions DivKit/DivKitComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ public final class DivKitComponents {
public let flagsInfo: DivFlagsInfo
public let fontProvider: DivFontProvider
public let imageHolderFactory: ImageHolderFactory
public let layoutDirection: LayoutDirection
public let layoutDirection: UserInterfaceLayoutDirection
public let patchProvider: DivPatchProvider
public let playerFactory: PlayerFactory?
public let safeAreaManager: DivSafeAreaManager
public let showToolip: DivActionURLHandler.ShowTooltipAction
public let stateManagement: DivStateManagement
public let showToolip: DivActionURLHandler.ShowTooltipAction?
public let tooltipManager: TooltipManager
public let triggersStorage: DivTriggersStorage
public let urlOpener: UrlOpener
public let variablesStorage: DivVariablesStorage
public let visibilityCounter = DivVisibilityCounter()

private let timerStorage: DivTimerStorage
private let updateAggregator: RunLoopCardUpdateAggregator
private let updateCard: DivActionURLHandler.UpdateCardAction
private let variableTracker = DivVariableTracker()
private let disposePool = AutodisposePool()

public init(
Expand All @@ -37,11 +40,12 @@ public final class DivKitComponents {
flagsInfo: DivFlagsInfo = .default,
fontProvider: DivFontProvider? = nil,
imageHolderFactory: ImageHolderFactory? = nil,
layoutDirection: LayoutDirection = .system,
layoutDirection: UserInterfaceLayoutDirection = UserInterfaceLayoutDirection.system,
patchProvider: DivPatchProvider? = nil,
requestPerformer: URLRequestPerforming? = nil,
showTooltip: @escaping DivActionURLHandler.ShowTooltipAction = { _ in },
showTooltip: DivActionURLHandler.ShowTooltipAction? = nil,
stateManagement: DivStateManagement = DefaultDivStateManagement(),
tooltipManager: TooltipManager? = nil,
trackVisibility: @escaping DivActionHandler.TrackVisibility = { _, _ in },
trackDisappear: @escaping DivActionHandler.TrackVisibility = { _, _ in },
updateCardAction: UpdateCardAction?,
Expand All @@ -62,6 +66,9 @@ public final class DivKitComponents {

safeAreaManager = DivSafeAreaManager(storage: variablesStorage)

updateAggregator = RunLoopCardUpdateAggregator(updateCardAction: updateCardAction ?? { _ in })
updateCard = updateAggregator.aggregate(_:)

let requestPerformer = requestPerformer ?? URLRequestPerformer(urlTransform: nil)

self.imageHolderFactory = imageHolderFactory
Expand All @@ -70,12 +77,19 @@ public final class DivKitComponents {
self.patchProvider = patchProvider
?? DivPatchDownloader(requestPerformer: requestPerformer)

let updateAggregator = RunLoopCardUpdateAggregator(updateCardAction: updateCardAction ?? { _ in
})
self.updateAggregator = updateAggregator
let updateCard: DivActionURLHandler.UpdateCardAction = updateAggregator.aggregate(_:)

weak var weakTimerStorage: DivTimerStorage?
weak var weakActionHandler: DivActionHandler?

self.tooltipManager = tooltipManager ?? DefaultTooltipManager(
shownDivTooltips: .init(),
handleAction: {
switch $0.payload {
case let .divAction(params: params):
weakActionHandler?.handle(params: params, urlOpener: urlOpener)
default: break
}
}
)

actionHandler = DivActionHandler(
stateUpdater: stateManagement,
Expand All @@ -84,6 +98,7 @@ public final class DivKitComponents {
variablesStorage: variablesStorage,
updateCard: updateCard,
showTooltip: showTooltip,
tooltipActionPerformer: self.tooltipManager,
logger: DefaultDivActionLogger(
requestPerformer: requestPerformer
),
Expand All @@ -104,14 +119,12 @@ public final class DivKitComponents {
urlOpener: urlOpener,
updateCard: updateCard
)

weakActionHandler = actionHandler
weakTimerStorage = timerStorage
variablesStorage.changeEvents.addObserver { change in
switch change.kind {
case .global:
updateCard(.variable(.all))
case let .local(cardId, _):
updateCard(.variable(.specific([cardId])))
}

variablesStorage.changeEvents.addObserver { [weak self] event in
self?.onVariablesChanged(event: event)
}.dispose(in: disposePool)
}

Expand Down Expand Up @@ -175,7 +188,8 @@ public final class DivKitComponents {
debugParams: DebugParams = DebugParams(),
parentScrollView: ScrollView? = nil
) -> DivBlockModelingContext {
DivBlockModelingContext(
variableTracker.onModelingStarted(cardId: cardId)
return DivBlockModelingContext(
cardId: cardId,
stateManager: stateManagement.getStateManagerForCard(cardId: cardId),
blockStateStorage: blockStateStorage,
Expand All @@ -190,7 +204,8 @@ public final class DivKitComponents {
playerFactory: playerFactory,
debugParams: debugParams,
parentScrollView: parentScrollView,
layoutDirection: layoutDirection
layoutDirection: layoutDirection,
variableTracker: variableTracker
)
}

Expand All @@ -217,6 +232,18 @@ public final class DivKitComponents {
public func setTimers(divData: DivData, cardId: DivCardID) {
timerStorage.set(cardId: cardId, timers: divData.timers ?? [])
}

private func onVariablesChanged(event: DivVariablesStorage.ChangeEvent) {
switch event.kind {
case let .global(variables):
let cardIds = variableTracker.getAffectedCards(variables: variables)
if (!cardIds.isEmpty) {
updateCard(.variable(.specific(cardIds)))
}
case let .local(cardId, _):
updateCard(.variable(.specific([cardId])))
}
}
}

func makeImageHolderFactory(requestPerformer: URLRequestPerforming) -> ImageHolderFactory {
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 = "26.1.0"
public static let version = "26.2.0"
}
8 changes: 7 additions & 1 deletion DivKit/Expressions/CalcExpression/CalcExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ final class CalcExpression: CustomStringConvertible {
func makeOutputMessage(for expression: String) -> String {
switch self {
case let .shortMessage(message):
return "Failed to evaluate [\(expression)]. \(message)"
return "Failed to evaluate [\(expression.escaped)]. \(message)"
default:
return self.description
}
Expand Down Expand Up @@ -1436,3 +1436,9 @@ extension UnicodeScalarView {
}
}
}

extension String {
fileprivate var escaped: String {
replacingOccurrences(of: "\\", with: "\\\\")
}
}
18 changes: 2 additions & 16 deletions DivKit/Expressions/ExpressionLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,10 @@ public struct ExpressionLink<T> {
let validator: ExpressionValueValidator<T>?
let errorTracker: ExpressionErrorTracker?

init?(
expression: String,
validator: ExpressionValueValidator<T>?,
errorTracker: ExpressionErrorTracker? = nil,
resolveNested: Bool = true
) throws {
try self.init(
rawValue: "@{\(expression)}",
validator: validator,
errorTracker: errorTracker,
resolveNested: resolveNested
)
}

@usableFromInline
init?(
rawValue: String,
validator: ExpressionValueValidator<T>?,
validator: ExpressionValueValidator<T>? = nil,
errorTracker: ExpressionErrorTracker? = nil,
resolveNested: Bool = true
) throws {
Expand Down Expand Up @@ -63,10 +49,10 @@ public struct ExpressionLink<T> {
let value = String(currentValue[start...end])
if resolveNested, let link = try ExpressionLink<String>(
rawValue: value,
validator: nil,
errorTracker: errorTracker
) {
items.append(.nestedCalcExpression(link))
variablesNames.append(contentsOf: link.variablesNames)
} else {
let parsedCalcExpression = CalcExpression.parse(value)
items.append(.calcExpression(parsedCalcExpression))
Expand Down
Loading

0 comments on commit 0ba8b33

Please sign in to comment.