Created
December 13, 2023 09:54
-
-
Save thematver/e67753664e8e27b93e21cc3e2b20ec9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In view | |
// Init | |
self.components = DivKitComponents( | |
extensionHandlers: [ | |
ShimmerImagePreviewExtension(), | |
], flagsInfo: DivFlagsInfo(imageLoadingOptimizationEnabled: true), | |
urlHandler: DivUrlHandlerDelegate { UIApplication.shared.open($0) }, variablesStorage: DivVariablesStorage() | |
) | |
self.preloader = DivViewPreloader(divKitComponents: components) | |
self.content = content | |
// Show content | |
divView = DivView(divKitComponents: components) | |
let cardId = DivCardID(rawValue: content.logId) | |
let source = DivViewSource(kind: .divData(content), cardId: cardId) | |
let params = DebugParams(isDebugInfoEnabled: true) | |
divView.setSource(source, debugParams: params) | |
view.addSubview(divView) | |
divView?.showCardId(DivCardID(rawValue: content.logId), shouldResetPreviousCardData: true) | |
DebugParams.showDebugInfo(divView) | |
// Deserializer itself | |
public struct DivJson: Deserializable { | |
public let templates: [String: Any] | |
public let card: [String: Any] | |
public init(dictionary: [String: Any]) throws { | |
templates = try dictionary.getOptionalField("templates") ?? [:] | |
card = try dictionary.getField("card") | |
} | |
static func load(_ content: String) throws -> DivData { | |
guard let data = content.data(using: .utf8) else { throw JSONParsingError() } | |
let divJson = try DivJson(JSONData: data) | |
guard let divData = | |
DivData.resolve( | |
card: divJson.card, | |
templates: divJson.templates | |
).value else { | |
throw JSONParsingError() | |
} | |
return divData | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment