metatext/Development Assets/DevelopmentModels.swift

125 lines
4.2 KiB
Swift
Raw Normal View History

2020-07-31 21:40:57 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
import Combine
// swiftlint:disable force_try
private let decoder = MastodonDecoder()
private var cancellables = Set<AnyCancellable>()
2020-08-02 22:23:01 +00:00
private let devInstanceURL = URL(string: "https://mastodon.social")!
private let devIdentityID = UUID(uuidString: "E621E1F8-C36C-495A-93FC-0C247A3E6E5F")!
2020-08-02 22:23:01 +00:00
private let devAccessToken = "DEVELOPMENT_ACCESS_TOKEN"
2020-07-31 21:40:57 +00:00
2020-08-09 02:52:41 +00:00
func freshKeychainService() -> KeychainServiceType { MockKeychainService() }
2020-08-02 22:23:01 +00:00
2020-08-09 02:52:41 +00:00
let developmentKeychainService: KeychainServiceType = {
let keychainService = MockKeychainService()
let secretsService = SecretsService(identityID: devIdentityID, keychainService: keychainService)
2020-07-31 21:40:57 +00:00
2020-08-09 02:52:41 +00:00
try! secretsService.set("DEVELOPMENT_CLIENT_ID", forItem: .clientID)
try! secretsService.set("DEVELOPMENT_CLIENT_SECRET", forItem: .clientSecret)
try! secretsService.set(devAccessToken, forItem: .accessToken)
2020-07-31 21:40:57 +00:00
2020-08-09 02:52:41 +00:00
return keychainService
}()
2020-07-31 21:40:57 +00:00
2020-08-06 06:45:57 +00:00
extension Defaults {
2020-08-09 01:29:05 +00:00
static func fresh() -> Defaults { Defaults(userDefaults: MockUserDefaults()) }
2020-08-02 22:23:01 +00:00
2020-08-06 06:45:57 +00:00
static let development: Defaults = {
let preferences = Defaults.fresh()
2020-08-02 22:23:01 +00:00
2020-08-08 01:16:04 +00:00
// Do future setup here
2020-08-02 22:23:01 +00:00
return preferences
}()
}
2020-07-31 21:40:57 +00:00
extension Account {
static let development = try! decoder.decode(Account.self, from: Data(officialAccountJSON.utf8))
}
extension Instance {
static let development = try! decoder.decode(Instance.self, from: Data(officialInstanceJSON.utf8))
}
extension IdentityDatabase {
2020-08-02 22:23:01 +00:00
static func fresh() -> IdentityDatabase { try! IdentityDatabase(inMemory: true) }
2020-07-31 21:40:57 +00:00
static var development: IdentityDatabase = {
2020-08-02 22:23:01 +00:00
let db = IdentityDatabase.fresh()
2020-07-31 21:40:57 +00:00
2020-08-02 22:23:01 +00:00
db.createIdentity(id: devIdentityID, url: devInstanceURL)
2020-07-31 21:40:57 +00:00
.receive(on: ImmediateScheduler.shared)
.sink(receiveCompletion: { _ in }, receiveValue: { _ in })
.store(in: &cancellables)
db.updateAccount(.development, forIdentityID: devIdentityID)
.receive(on: ImmediateScheduler.shared)
.sink(receiveCompletion: { _ in }, receiveValue: { _ in })
.store(in: &cancellables)
db.updateInstance(.development, forIdentityID: devIdentityID)
.receive(on: ImmediateScheduler.shared)
.sink(receiveCompletion: { _ in }, receiveValue: { _ in })
.store(in: &cancellables)
return db
}()
}
2020-08-02 22:23:01 +00:00
extension AppEnvironment {
static func fresh(
2020-08-03 15:20:51 +00:00
URLSessionConfiguration: URLSessionConfiguration = .stubbing,
2020-08-02 22:23:01 +00:00
identityDatabase: IdentityDatabase = .fresh(),
2020-08-06 06:45:57 +00:00
defaults: Defaults = .fresh(),
2020-08-09 02:52:41 +00:00
keychainService: KeychainServiceType = freshKeychainService(),
2020-08-09 01:37:46 +00:00
webAuthSessionType: WebAuthSessionType.Type = SuccessfulMockWebAuthSession.self) -> AppEnvironment {
2020-08-02 22:23:01 +00:00
AppEnvironment(
2020-08-03 15:20:51 +00:00
URLSessionConfiguration: URLSessionConfiguration,
2020-08-02 22:23:01 +00:00
identityDatabase: identityDatabase,
2020-08-06 06:45:57 +00:00
defaults: defaults,
2020-08-09 02:52:41 +00:00
keychainService: keychainService,
2020-08-02 22:23:01 +00:00
webAuthSessionType: webAuthSessionType)
}
static let development = AppEnvironment(
2020-08-03 15:20:51 +00:00
URLSessionConfiguration: .stubbing,
2020-07-31 21:40:57 +00:00
identityDatabase: .development,
2020-08-06 06:45:57 +00:00
defaults: .development,
2020-08-09 02:52:41 +00:00
keychainService: developmentKeychainService,
2020-08-09 01:37:46 +00:00
webAuthSessionType: SuccessfulMockWebAuthSession.self)
2020-08-02 22:23:01 +00:00
}
2020-08-08 23:08:47 +00:00
extension IdentityService {
static let development = try! IdentityService(identityID: devIdentityID, appEnvironment: .development)
2020-08-08 06:01:45 +00:00
}
2020-08-03 15:20:51 +00:00
extension RootViewModel {
static let development = RootViewModel(environment: .development)
}
extension MainNavigationViewModel {
2020-08-08 04:08:57 +00:00
static let development = RootViewModel.development.mainNavigationViewModel(identityID: devIdentityID)!
2020-08-03 15:20:51 +00:00
}
2020-08-08 06:01:45 +00:00
#if os(iOS)
2020-08-07 03:57:52 +00:00
extension SecondaryNavigationViewModel {
2020-08-08 04:08:57 +00:00
static let development = MainNavigationViewModel.development.secondaryNavigationViewModel()
2020-07-31 21:40:57 +00:00
}
2020-08-04 20:26:09 +00:00
extension IdentitiesViewModel {
2020-08-08 23:08:47 +00:00
static let development = IdentitiesViewModel(identityService: .development)
2020-08-04 20:26:09 +00:00
}
2020-08-08 06:01:45 +00:00
#endif
2020-08-04 20:26:09 +00:00
2020-08-07 10:59:48 +00:00
extension PreferencesViewModel {
2020-08-08 23:08:47 +00:00
static let development = PreferencesViewModel(identityService: .development)
2020-08-07 10:59:48 +00:00
}
extension PostingReadingPreferencesViewModel {
2020-08-08 23:08:47 +00:00
static let development = PostingReadingPreferencesViewModel(identityService: .development)
2020-08-07 10:14:14 +00:00
}
2020-07-31 21:40:57 +00:00
// swiftlint:enable force_try