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 { diff --git a/ios/packages/reference-assets/Sources/SwiftUI/InputAsset.swift b/ios/packages/reference-assets/Sources/SwiftUI/InputAsset.swift index a709735d2..e1c1406b3 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]) { + 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/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) diff --git a/ios/packages/swiftui/Sources/types/assets/ControlledAsset.swift b/ios/packages/swiftui/Sources/types/assets/ControlledAsset.swift index 2267a2d99..b6c1e3f7f 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] = [:]) { self._data = Published(initialValue: data) + self.userInfo = userInfo } } @@ -74,8 +79,9 @@ 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) + self.model = ModelType(data, userInfo: decoder.userInfo) decoder.logger?.t("Creating model for \(data.id)") modelCache?.set(model, forKey: data.id, codingPath: decoder.codingPath) }