Added user data store as an example of ObservableObject.

This commit is contained in:
Kyle Knez 2019-09-27 10:47:34 -07:00
parent 22d6e9bfe2
commit 264e5cc129
2 changed files with 46 additions and 0 deletions

View file

@ -22,6 +22,7 @@
28AC9529233C433400BFB70A /* TransactionRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28AC9528233C433400BFB70A /* TransactionRepository.swift */; };
28AC952C233C434800BFB70A /* UserRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28AC952B233C434800BFB70A /* UserRepository.swift */; };
28AC952E233C43A300BFB70A /* User.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28AC952D233C43A300BFB70A /* User.swift */; };
543ECE42233E82A40018A9D9 /* UserDataStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 543ECE41233E82A40018A9D9 /* UserDataStore.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -65,6 +66,7 @@
28AC9528233C433400BFB70A /* TransactionRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransactionRepository.swift; sourceTree = "<group>"; };
28AC952B233C434800BFB70A /* UserRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserRepository.swift; sourceTree = "<group>"; };
28AC952D233C43A300BFB70A /* User.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = User.swift; sourceTree = "<group>"; };
543ECE41233E82A40018A9D9 /* UserDataStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserDataStore.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -187,6 +189,7 @@
28AC952B233C434800BFB70A /* UserRepository.swift */,
28AC952D233C43A300BFB70A /* User.swift */,
2857EAEE233DA73B0026BC83 /* UserData.swift */,
543ECE41233E82A40018A9D9 /* UserDataStore.swift */,
);
path = User;
sourceTree = "<group>";
@ -332,6 +335,7 @@
28AC9525233C42D100BFB70A /* BudgetApiService.swift in Sources */,
2857EAED233DA30B0026BC83 /* LoadingView.swift in Sources */,
28AC9529233C433400BFB70A /* TransactionRepository.swift in Sources */,
543ECE42233E82A40018A9D9 /* UserDataStore.swift in Sources */,
28AC952E233C43A300BFB70A /* User.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;

View file

@ -0,0 +1,42 @@
import Foundation
import Combine
class UserDataStore: ObservableObject {
// Note: You can combine these into one Result type
// Result<User, Status>
var currentUser: User? = nil
var status: UserStatus = .unauthenticated
func login(username: String, password: String) {
// Changes the status and notifies any observers of the change
self.status = .authenticating
self.objectWillChange.send()
// Perform the login
_ = self.userRepository.login(username: username, password: password)
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { (status) in
switch status {
case .finished:
return
// Do nothing it means the network request just ended
case .failure(let networkError):
// Poulate your status with failed authenticating
self.status = .failedAuthentication
self.objectWillChange.send()
}
}) { (user) in
self.currentUser = user
self.objectWillChange.send()
}
}
init(_ userRepository: UserRepository) {
self.userRepository = userRepository
}
private let userRepository: UserRepository
}