More documentation

sync
Laurent 1 year ago
parent 7a9f560ccf
commit 6981ebeb23
  1. 33
      LeStorage/StoreCenter.swift

@ -10,8 +10,10 @@ import UIKit
public class StoreCenter { public class StoreCenter {
/// The main instance
public static let main: StoreCenter = StoreCenter() public static let main: StoreCenter = StoreCenter()
/// A dictionary of Stores associated to their id
fileprivate var _stores: [String : Store] = [:] fileprivate var _stores: [String : Store] = [:]
/// The URL of the django API /// The URL of the django API
@ -82,6 +84,7 @@ public class StoreCenter {
// MARK: - Settings // MARK: - Settings
/// Sets the user info given a user
func setUserInfo(user: UserBase) { func setUserInfo(user: UserBase) {
self._settingsStorage.update { settings in self._settingsStorage.update { settings in
settings.userId = user.id 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 /// Returns the stored user Id
public var userId: String? { public var userId: String? {
return self._settingsStorage.item.userId return self._settingsStorage.item.userId
@ -106,13 +102,6 @@ public class StoreCenter {
return self._settingsStorage.item.username return self._settingsStorage.item.username
} }
/// Sets the username
func setUserName(_ username: String) {
self._settingsStorage.update { settings in
settings.username = username
}
}
/// Returns the stored token /// Returns the stored token
public func token() -> String? { public func token() -> String? {
return try? self.service().keychainStore.getValue() 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 { func deviceId() -> String {
let keychainStore = KeychainStore(serverId: "lestorage.main") let keychainStore = KeychainStore(serverId: "lestorage.main")
do { do {
@ -158,6 +150,7 @@ public class StoreCenter {
// MARK: - Api Calls // MARK: - Api Calls
/// Instantiates and loads an ApiCallCollection with the provided type
func loadApiCallCollection<T: Storable>(type: T.Type) { func loadApiCallCollection<T: Storable>(type: T.Type) {
let apiCallCollection = ApiCallCollection<T>() let apiCallCollection = ApiCallCollection<T>()
self._apiCallCollections[T.resourceName()] = apiCallCollection 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 /// Returns the ApiCall collection using the resource name of the provided T type
func apiCallCollection<T: Storable>() throws -> ApiCallCollection<T> { func apiCallCollection<T: Storable>() throws -> ApiCallCollection<T> {
if let collection = self._apiCallCollections[T.resourceName()] as? ApiCallCollection<T> { if let collection = self._apiCallCollections[T.resourceName()] as? ApiCallCollection<T> {
@ -412,6 +395,7 @@ public class StoreCenter {
// MARK: - Logs // MARK: - Logs
/// Returns the logs collection and instantiates it if necessary
fileprivate func _logsCollection() -> StoredCollection<Log> { fileprivate func _logsCollection() -> StoredCollection<Log> {
if let logs = self._logs { if let logs = self._logs {
return logs return logs
@ -434,6 +418,7 @@ public class StoreCenter {
// MARK: - Migration // MARK: - Migration
/// Migrates the token from the provided service to the main Services instance
public func migrateToken(_ services: Services) throws { public func migrateToken(_ services: Services) throws {
guard let userName = self.userName() else { guard let userName = self.userName() else {
return return

Loading…
Cancel
Save