Holding state in a service class in MVVM
In my SwiftUI app, I have a UpNextManager view model and a service class called UpNextService which contains many methods to read up next items from disk, write them to disk and much more. The service class is then injected into the view model. However, the issue is alot of the service class methods require isShuffled state from the view model. Would it be better to pass the state as a parameter to the functions in the service (i.e. which means having the same isShuffled parameter in over 15 functions) or make the service class observable (i.e. make it as another view model that views can watch for changes) instead and hold the isShuffled state inside it? I need to ensure the code is modular for testability purposes. @Observable class UpNextViewModel { var isShuffled: Bool = false let upNextService: UpNextService init(upNextService: UpNextService) { self.upNextService = upNextService } } class UpNextService { func writeItems() { // Need isShuffled state in the logic } func readItems() -> [UpNextItem] { // Need isShuffled state in the logic } func removeItem(at index: Int) { // Need isShuffled state in the logic } //... }

In my SwiftUI app, I have a UpNextManager view model and a service class called UpNextService which contains many methods to read up next items from disk, write them to disk and much more. The service class is then injected into the view model. However, the issue is alot of the service class methods require isShuffled state from the view model.
Would it be better to pass the state as a parameter to the functions in the service (i.e. which means having the same isShuffled parameter in over 15 functions) or make the service class observable (i.e. make it as another view model that views can watch for changes) instead and hold the isShuffled state inside it?
I need to ensure the code is modular for testability purposes.
@Observable
class UpNextViewModel {
var isShuffled: Bool = false
let upNextService: UpNextService
init(upNextService: UpNextService) {
self.upNextService = upNextService
}
}
class UpNextService {
func writeItems() {
// Need isShuffled state in the logic
}
func readItems() -> [UpNextItem] {
// Need isShuffled state in the logic
}
func removeItem(at index: Int) {
// Need isShuffled state in the logic
}
//...
}