From 23760cfb8936310500077beb1c935d2a24f7dd60 Mon Sep 17 00:00:00 2001 From: zwu01 Date: Tue, 24 Oct 2023 16:11:09 -0400 Subject: [PATCH 1/4] add user info to assetViewModel --- .../reference-assets/Sources/SwiftUI/InputAsset.swift | 5 +++-- .../swiftui/Sources/types/assets/ControlledAsset.swift | 9 +++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/ios/packages/reference-assets/Sources/SwiftUI/InputAsset.swift b/ios/packages/reference-assets/Sources/SwiftUI/InputAsset.swift index a709735d2..c94a01370 100644 --- a/ios/packages/reference-assets/Sources/SwiftUI/InputAsset.swift +++ b/ios/packages/reference-assets/Sources/SwiftUI/InputAsset.swift @@ -47,9 +47,10 @@ open class InputAssetViewModel: AssetViewModel { Constructs the `InputAssetViewModel` - parameters: - data: The `InputData` decoded from the core player + - userInfo: The `userInfo` from the decoder */ - public required init(_ data: InputData) { - super.init(data) + public required init(_ data: InputData, userInfo: [CodingUserInfoKey: Any]? = nil) { + super.init(data, userInfo: userInfo) $data.sink { [weak self] (newData) in (newData.value?.stringValue).map { self?.text = $0 } }.store(in: &bag) diff --git a/ios/packages/swiftui/Sources/types/assets/ControlledAsset.swift b/ios/packages/swiftui/Sources/types/assets/ControlledAsset.swift index 2267a2d99..19ec9699a 100644 --- a/ios/packages/swiftui/Sources/types/assets/ControlledAsset.swift +++ b/ios/packages/swiftui/Sources/types/assets/ControlledAsset.swift @@ -15,6 +15,9 @@ open class AssetViewModel: ObservableObject { /// The decoded data @Published public var data: T + /// Any contextual information set by the user + public var userInfo: [CodingUserInfoKey: Any]? + /// Set to store subscriptions in for cancellation public var bag = Set() @@ -22,9 +25,11 @@ open class AssetViewModel: ObservableObject { Constructs an instance of this ViewModel with the given data - parameters: - data: The data to publish from this ViewModel + - userInfo: Any contextual information set by the user */ - public required init(_ data: T) { + public required init(_ data: T, userInfo: [CodingUserInfoKey: Any]? = nil) { self._data = Published(initialValue: data) + self.userInfo = userInfo } } @@ -75,7 +80,7 @@ open class ControlledAsset: SwiftUIAsset where M decoder.logger?.t("Updating model for \(data.id)") model.data = data } else { - self.model = ModelType(data) + self.model = ModelType(data, userInfo: decoder.userInfo) decoder.logger?.t("Creating model for \(data.id)") modelCache?.set(model, forKey: data.id, codingPath: decoder.codingPath) } From 33f9928b4e19f586120a9e0fbab39fe01c0b49c4 Mon Sep 17 00:00:00 2001 From: zwu01 Date: Wed, 25 Oct 2023 11:11:38 -0400 Subject: [PATCH 2/4] address comments --- .../reference-assets/Sources/SwiftUI/InputAsset.swift | 2 +- .../swiftui/Sources/types/assets/ControlledAsset.swift | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/ios/packages/reference-assets/Sources/SwiftUI/InputAsset.swift b/ios/packages/reference-assets/Sources/SwiftUI/InputAsset.swift index c94a01370..e1c1406b3 100644 --- a/ios/packages/reference-assets/Sources/SwiftUI/InputAsset.swift +++ b/ios/packages/reference-assets/Sources/SwiftUI/InputAsset.swift @@ -49,7 +49,7 @@ open class InputAssetViewModel: AssetViewModel { - data: The `InputData` decoded from the core player - userInfo: The `userInfo` from the decoder */ - public required init(_ data: InputData, userInfo: [CodingUserInfoKey: Any]? = nil) { + public required init(_ data: InputData, userInfo: [CodingUserInfoKey: Any]) { super.init(data, userInfo: userInfo) $data.sink { [weak self] (newData) in (newData.value?.stringValue).map { self?.text = $0 } diff --git a/ios/packages/swiftui/Sources/types/assets/ControlledAsset.swift b/ios/packages/swiftui/Sources/types/assets/ControlledAsset.swift index 19ec9699a..b6c1e3f7f 100644 --- a/ios/packages/swiftui/Sources/types/assets/ControlledAsset.swift +++ b/ios/packages/swiftui/Sources/types/assets/ControlledAsset.swift @@ -16,7 +16,7 @@ open class AssetViewModel: ObservableObject { @Published public var data: T /// Any contextual information set by the user - public var userInfo: [CodingUserInfoKey: Any]? + public var userInfo: [CodingUserInfoKey: Any] /// Set to store subscriptions in for cancellation public var bag = Set() @@ -25,9 +25,9 @@ open class AssetViewModel: ObservableObject { Constructs an instance of this ViewModel with the given data - parameters: - data: The data to publish from this ViewModel - - userInfo: Any contextual information set by the user + - userInfo: Any contextual information set by the user */ - public required init(_ data: T, userInfo: [CodingUserInfoKey: Any]? = nil) { + public required init(_ data: T, userInfo: [CodingUserInfoKey: Any] = [:]) { self._data = Published(initialValue: data) self.userInfo = userInfo } @@ -79,6 +79,7 @@ open class ControlledAsset: SwiftUIAsset where M self.model = model decoder.logger?.t("Updating model for \(data.id)") model.data = data + model.userInfo = decoder.userInfo } else { self.model = ModelType(data, userInfo: decoder.userInfo) decoder.logger?.t("Creating model for \(data.id)") From 122d146ed067c7e361f49d298b2024893d6a132a Mon Sep 17 00:00:00 2001 From: zwu01 Date: Wed, 25 Oct 2023 13:05:46 -0400 Subject: [PATCH 3/4] update tests --- .../ViewInspector/SwiftUI/InputAssetTests.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ios/packages/reference-assets/ViewInspector/SwiftUI/InputAssetTests.swift b/ios/packages/reference-assets/ViewInspector/SwiftUI/InputAssetTests.swift index 0d483a5e6..69dde7542 100644 --- a/ios/packages/reference-assets/ViewInspector/SwiftUI/InputAssetTests.swift +++ b/ios/packages/reference-assets/ViewInspector/SwiftUI/InputAssetTests.swift @@ -67,7 +67,7 @@ class InputAssetTests: SwiftUIAssetUnitTestCase { validation: nil ) - let model = InputAssetViewModel(data) + let model = InputAssetViewModel(data, userInfo: [:]) let update1expect = expectation(description: "Initial value") model.$data.sink { (data) in guard data.value?.stringValue == "a" else { return } @@ -110,7 +110,7 @@ class InputAssetTests: SwiftUIAssetUnitTestCase { let modelRef = ModelReference(rawValue: val) let data = InputData(id: "input", type: "input", placeholder: nil, value: modelRef, label: nil, set: nil, dataType: nil, validation: nil) - let model = InputAssetViewModel(data) + let model = InputAssetViewModel(data, userInfo: [:]) let view = InputAssetView(model: model) @@ -140,7 +140,7 @@ class InputAssetTests: SwiftUIAssetUnitTestCase { validation: nil ) - let model = InputAssetViewModel(data) + let model = InputAssetViewModel(data, userInfo: [:]) let view = await InputAssetView(model: model) @@ -176,7 +176,7 @@ class InputAssetTests: SwiftUIAssetUnitTestCase { validation: validation ) - let model = InputAssetViewModel(data) + let model = InputAssetViewModel(data, userInfo: [:]) let view = InputAssetView(model: model) @@ -204,7 +204,7 @@ class InputAssetTests: SwiftUIAssetUnitTestCase { let data = InputData(id: "input", type: "input", placeholder: nil, value: nil, label: nil, set: wrapper, dataType: nil, validation: nil) - let model = InputAssetViewModel(data) + let model = InputAssetViewModel(data, userInfo: [:]) let view = InputAssetView(model: model) @@ -226,7 +226,7 @@ class InputAssetTests: SwiftUIAssetUnitTestCase { let data = InputData(id: "input", type: "input", placeholder: nil, value: nil, label: nil, set: wrapper, dataType: nil, validation: nil) - let model = InputAssetViewModel(data) + let model = InputAssetViewModel(data, userInfo: [:]) let view = InputAssetView(model: model) From cf0b76cd4c8cf4477cc369bbcd6ce7be38685191 Mon Sep 17 00:00:00 2001 From: zwu01 Date: Wed, 25 Oct 2023 18:35:51 -0400 Subject: [PATCH 4/4] update doc --- docs/site/pages/assets/custom.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/site/pages/assets/custom.mdx b/docs/site/pages/assets/custom.mdx index 197d7599e..0beb94484 100644 --- a/docs/site/pages/assets/custom.mdx +++ b/docs/site/pages/assets/custom.mdx @@ -170,12 +170,12 @@ class ActionAsset: UncontrolledAsset { #### ControlledAsset -The `ControlledAsset` lets you define the viewModel type, as long as it subclasses `AssetViewModel`, this way you still receive updated data whenever Player changes state, but you can add other functionality to the `viewModel`. +The `ControlledAsset` lets you define the viewModel type, as long as it subclasses `AssetViewModel`, this way you still receive updated data and user info whenever Player changes state, but you can add other functionality to the `viewModel`. ```swift class ActionViewModel: AssetViewModel { - public required init(_ data: ActionData) { - super.init(data) + public required init(_ data: ActionData, userInfo: [CodingUserInfoKey: Any]) { + super.init(data, userInfo: userInfo) } } class ActionAsset: ControlledAsset {