|
|
|
|
@ -10,8 +10,10 @@ import UIKit |
|
|
|
|
|
|
|
|
|
public class StoreCenter { |
|
|
|
|
|
|
|
|
|
/// The main instance |
|
|
|
|
public static let main: StoreCenter = StoreCenter() |
|
|
|
|
|
|
|
|
|
/// A dictionary of Stores associated to their id |
|
|
|
|
fileprivate var _stores: [String : Store] = [:] |
|
|
|
|
|
|
|
|
|
/// The URL of the django API |
|
|
|
|
@ -82,6 +84,7 @@ public class StoreCenter { |
|
|
|
|
|
|
|
|
|
// MARK: - Settings |
|
|
|
|
|
|
|
|
|
/// Sets the user info given a user |
|
|
|
|
func setUserInfo(user: UserBase) { |
|
|
|
|
self._settingsStorage.update { settings in |
|
|
|
|
settings.userId = user.id |
|
|
|
|
@ -89,13 +92,6 @@ public class StoreCenter { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Stores the user UUID |
|
|
|
|
func setUserUUID(uuidString: String) { |
|
|
|
|
self._settingsStorage.update { settings in |
|
|
|
|
settings.userId = uuidString |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Returns the stored user Id |
|
|
|
|
public var userId: String? { |
|
|
|
|
return self._settingsStorage.item.userId |
|
|
|
|
@ -106,13 +102,6 @@ public class StoreCenter { |
|
|
|
|
return self._settingsStorage.item.username |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Sets the username |
|
|
|
|
func setUserName(_ username: String) { |
|
|
|
|
self._settingsStorage.update { settings in |
|
|
|
|
settings.username = username |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Returns the stored token |
|
|
|
|
public func token() -> String? { |
|
|
|
|
return try? self.service().keychainStore.getValue() |
|
|
|
|
@ -140,6 +129,9 @@ public class StoreCenter { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Returns a generated device id |
|
|
|
|
/// If created, stores it inside the keychain to get a consistent value even if the app is deleted |
|
|
|
|
/// as UIDevice.current.identifierForVendor value changes when the app is deleted and installed again |
|
|
|
|
func deviceId() -> String { |
|
|
|
|
let keychainStore = KeychainStore(serverId: "lestorage.main") |
|
|
|
|
do { |
|
|
|
|
@ -158,6 +150,7 @@ public class StoreCenter { |
|
|
|
|
|
|
|
|
|
// MARK: - Api Calls |
|
|
|
|
|
|
|
|
|
/// Instantiates and loads an ApiCallCollection with the provided type |
|
|
|
|
func loadApiCallCollection<T: Storable>(type: T.Type) { |
|
|
|
|
let apiCallCollection = ApiCallCollection<T>() |
|
|
|
|
self._apiCallCollections[T.resourceName()] = apiCallCollection |
|
|
|
|
@ -170,16 +163,6 @@ public class StoreCenter { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Returns or create the ApiCall collection matching the provided T type |
|
|
|
|
// func getOrCreateApiCallCollection<T: Storable>() -> ApiCallCollection<T> { |
|
|
|
|
// if let apiCallCollection = self._apiCallCollections[T.resourceName()] as? ApiCallCollection<T> { |
|
|
|
|
// return apiCallCollection |
|
|
|
|
// } |
|
|
|
|
// let apiCallCollection = ApiCallCollection<T>() |
|
|
|
|
// self._apiCallCollections[T.resourceName()] = apiCallCollection |
|
|
|
|
// return apiCallCollection |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
/// Returns the ApiCall collection using the resource name of the provided T type |
|
|
|
|
func apiCallCollection<T: Storable>() throws -> ApiCallCollection<T> { |
|
|
|
|
if let collection = self._apiCallCollections[T.resourceName()] as? ApiCallCollection<T> { |
|
|
|
|
@ -412,6 +395,7 @@ public class StoreCenter { |
|
|
|
|
|
|
|
|
|
// MARK: - Logs |
|
|
|
|
|
|
|
|
|
/// Returns the logs collection and instantiates it if necessary |
|
|
|
|
fileprivate func _logsCollection() -> StoredCollection<Log> { |
|
|
|
|
if let logs = self._logs { |
|
|
|
|
return logs |
|
|
|
|
@ -434,6 +418,7 @@ public class StoreCenter { |
|
|
|
|
|
|
|
|
|
// MARK: - Migration |
|
|
|
|
|
|
|
|
|
/// Migrates the token from the provided service to the main Services instance |
|
|
|
|
public func migrateToken(_ services: Services) throws { |
|
|
|
|
guard let userName = self.userName() else { |
|
|
|
|
return |
|
|
|
|
|