Last active
December 12, 2021 10:34
-
-
Save michzio/1f8aa4a74129ba2b1b78d06a54f871db to your computer and use it in GitHub Desktop.
SwiftUI MVVM architecture - ViewModel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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