Files
divkit/client/ios/DivKit/Actions/DownloadActionHandler.swift
T
pkurchatov 8292e43354 Updated generated files
commit_hash:1442bce2fc538aaeab3448cd87fa3a59db0bde37
2025-01-27 21:17:26 +03:00

63 lines
1.5 KiB
Swift

import Foundation
import VGSL
final class DownloadActionHandler {
private let patchProvider: DivPatchProvider
private let updateCard: DivActionHandler.UpdateCardAction
init(
patchProvider: DivPatchProvider,
updateCard: @escaping DivActionHandler.UpdateCardAction
) {
self.patchProvider = patchProvider
self.updateCard = updateCard
}
func handle(_ action: DivActionDownload, context: DivActionHandlingContext) {
guard let url = action.resolveUrl(context.expressionResolver) else {
return
}
handle(
url: url,
context: context,
onSuccessActions: action.onSuccessActions,
onFailActions: action.onFailActions
)
}
func handle(
url: URL,
context: DivActionHandlingContext,
onSuccessActions: [DivAction]?,
onFailActions: [DivAction]?
) {
let info = context.info
patchProvider.getPatch(
url: url,
info: info,
completion: { [weak self] in
guard let self else {
return
}
let callbackActions: [DivAction]
switch $0 {
case let .success(patch):
updateCard(.patch(info.cardId, patch))
callbackActions = onSuccessActions ?? []
case .failure:
callbackActions = onFailActions ?? []
}
for action in callbackActions {
context.actionHandler.handle(
action,
path: info.path,
source: .callback,
sender: nil
)
}
}
)
}
}