Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user info to assetViewModel (Breaking Change) #201

Merged
merged 6 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to update the userInfo when we get the model from the cache?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call

decoder.logger?.t("Creating model for \(data.id)")
modelCache?.set(model, forKey: data.id, codingPath: decoder.codingPath)
}
Expand Down