Skip to content

Instantly share code, notes, and snippets.

@michzio
Last active December 12, 2021 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michzio/1f8aa4a74129ba2b1b78d06a54f871db to your computer and use it in GitHub Desktop.
Save michzio/1f8aa4a74129ba2b1b78d06a54f871db to your computer and use it in GitHub Desktop.
SwiftUI MVVM architecture - ViewModel
public protocol WordsetCategoriesViewModelProtocol: ObservableObject {
var error: Error? { get set }
var isLoading: Bool { get set }
func syncWordsetCategories()
}
public class WordsetCategoriesViewModel: WordsetCategoriesViewModelProtocol {
@Inject private var repository: WordsetCategoryRepositoryProtocol
private var disposables = Set<AnyCancellable>()
@Published var error: Error?
@Published var isLoading: Bool
public func syncWordsetCategories() {
guard !isLoading else { return }
isLoading = true
repo.syncWordsetCategories()
.sink(receiveCompletion: { [weak self] completion in
switch completion {
case .failure(let error):
self?.error = error
case .finished:
break
}
self?.isLoading = false
}) { result in
print("Synced wordset categories: \(result)")
}
.store(in: &disposables)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment