Goals

✅ Adopt Identifiable protocol

✅ Connect with ReSwift architecture

Progress

Adopting the Identifiable protocol

I’ve added a ViewModel protocol that extends Identifiable. I also adopted UUID to any Model and ViewModel.

By the way, should a view model be named after its model or its view? 🤔 I’ve changed mine to use the view name! (By the way, another blog post idea)

Connecting with ReSwift architecture

I was thinking how can I use the UDF that comes with ReSwift combined with the Data Flow of Combine and SwiftUI.

It occurred to me that if I connect a ViewModel, that is also a ObservableObject to the AppState using a @Published wrapped property, I would get the result I was looking for. It is the perfect match.

By the way, I had to resume using String as Self.ID for Identifiable as I was loading the entries from a file.

Updating entry!

Using the actions to update the entries make it way more convenient now! The struct view has access to yearvuStore so it is just a matter of dispatching the action and that’s it!

But using an array in the state looks a bit messy, specially when looking for the index by the ID. Perhaps I need to create a hash map so the search of items is faster.

I got the following error while testing:

ForEach<Range<Int>, Int, ModifiedContent<Text, _TraitWritingModifier<TagValueTraitKey<Int>>>> count (4) != its initial count (2). `ForEach(_:content:)` should only be used for *constant* data. Instead conform data to `Identifiable` or use `ForEach(_:id:content:)` and provide an explicit `id`!

So I modified

ForEach(0 ..< currencies.count) {
    Text(currencies[$0]).tag($0)
}

To be

ForEach(0 ..< currencies.count, id: \.self) {
    Text(currencies[$0]).tag($0)
}

To index the changes of the currency list, given it was being changed when the view model got modified.

Conclusion

Yearvu source code can be found in my GitHub.

Check out the commit in the public repository.