metatext/Development Assets/DevelopmentModels.swift

126 lines
4.1 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
extension Secrets {
2020-08-02 22:23:01 +00:00
static func fresh() -> Secrets { Secrets(keychain: FakeKeychain()) }
2020-07-31 21:40:57 +00:00
static let development: Secrets = {
2020-08-02 22:23:01 +00:00
let secrets = Secrets.fresh()
2020-07-31 21:40:57 +00:00
try! secrets.set("DEVELOPMENT_CLIENT_ID", forItem: .clientID, forIdentityID: devIdentityID)
try! secrets.set("DEVELOPMENT_CLIENT_SECRET", forItem: .clientSecret, forIdentityID: devIdentityID)
2020-08-02 22:23:01 +00:00
try! secrets.set(devAccessToken, forItem: .accessToken, forIdentityID: devIdentityID)
2020-07-31 21:40:57 +00:00
return secrets
}()
}
2020-08-06 06:45:57 +00:00
extension Defaults {
static func fresh() -> Defaults { Defaults(userDefaults: FakeUserDefaults()) }
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-02 22:23:01 +00:00
secrets: Secrets = .fresh(),
webAuthSessionType: WebAuthSession.Type = SuccessfulStubbingWebAuthSession.self) -> AppEnvironment {
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-02 22:23:01 +00:00
secrets: secrets,
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-02 22:23:01 +00:00
secrets: .development,
webAuthSessionType: SuccessfulStubbingWebAuthSession.self)
}
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