Skip to content

Commit

Permalink
Merge pull request #201 from player-ui/add-user-info-to-assetviewmodel
Browse files Browse the repository at this point in the history
Add user info to assetViewModel (Breaking Change)
  • Loading branch information
nancywu1 authored Oct 26, 2023
2 parents d04ee7c + 5cf3e74 commit cbc2cfa
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
6 changes: 3 additions & 3 deletions docs/site/pages/assets/custom.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,12 @@ class ActionAsset: UncontrolledAsset<ActionData> {

#### ControlledAsset

The `ControlledAsset` lets you define the viewModel type, as long as it subclasses `AssetViewModel<T: AssetData>`, 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<T: AssetData>`, 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<ActionData> {
public required init(_ data: ActionData) {
super.init(data)
public required init(_ data: ActionData, userInfo: [CodingUserInfoKey: Any]) {
super.init(data, userInfo: userInfo)
}
}
class ActionAsset: ControlledAsset<ActionData, ActionViewModel> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ open class InputAssetViewModel: AssetViewModel<InputData> {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -140,7 +140,7 @@ class InputAssetTests: SwiftUIAssetUnitTestCase {
validation: nil
)

let model = InputAssetViewModel(data)
let model = InputAssetViewModel(data, userInfo: [:])

let view = await InputAssetView(model: model)

Expand Down Expand Up @@ -176,7 +176,7 @@ class InputAssetTests: SwiftUIAssetUnitTestCase {
validation: validation
)

let model = InputAssetViewModel(data)
let model = InputAssetViewModel(data, userInfo: [:])

let view = InputAssetView(model: model)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down
10 changes: 8 additions & 2 deletions ios/packages/swiftui/Sources/types/assets/ControlledAsset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@ open class AssetViewModel<T: AssetData>: 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<AnyCancellable>()

/**
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
}
}

Expand Down Expand Up @@ -74,8 +79,9 @@ open class ControlledAsset<DataType: AssetData, ModelType>: 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)
}
Expand Down

0 comments on commit cbc2cfa

Please sign in to comment.