mirror of
https://github.com/metabolist/metatext
synced 2024-12-14 18:35:37 +01:00
123 lines
3.8 KiB
Swift
123 lines
3.8 KiB
Swift
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
import Foundation
|
|
import Combine
|
|
|
|
// swiftlint:disable force_try
|
|
private let decoder = MastodonDecoder()
|
|
private var cancellables = Set<AnyCancellable>()
|
|
private let devInstanceURL = URL(string: "https://mastodon.social")!
|
|
private let devIdentityID = "DEVELOPMENT_IDENTITY_ID"
|
|
private let devAccessToken = "DEVELOPMENT_ACCESS_TOKEN"
|
|
|
|
extension Secrets {
|
|
static func fresh() -> Secrets { Secrets(keychain: FakeKeychain()) }
|
|
|
|
static let development: Secrets = {
|
|
let secrets = Secrets.fresh()
|
|
|
|
try! secrets.set("DEVELOPMENT_CLIENT_ID", forItem: .clientID, forIdentityID: devIdentityID)
|
|
try! secrets.set("DEVELOPMENT_CLIENT_SECRET", forItem: .clientSecret, forIdentityID: devIdentityID)
|
|
try! secrets.set(devAccessToken, forItem: .accessToken, forIdentityID: devIdentityID)
|
|
|
|
return secrets
|
|
}()
|
|
}
|
|
|
|
extension Preferences {
|
|
static func fresh() -> Preferences { Preferences(userDefaults: FakeUserDefaults()) }
|
|
|
|
static let development: Preferences = {
|
|
let preferences = Preferences.fresh()
|
|
|
|
preferences[.recentIdentityID] = devIdentityID
|
|
|
|
return preferences
|
|
}()
|
|
}
|
|
|
|
extension MastodonClient {
|
|
static func fresh() -> MastodonClient { MastodonClient(configuration: .stubbing) }
|
|
|
|
static let development: MastodonClient = {
|
|
let client = MastodonClient.fresh()
|
|
|
|
client.instanceURL = devInstanceURL
|
|
client.accessToken = devAccessToken
|
|
|
|
return client
|
|
}()
|
|
}
|
|
|
|
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 {
|
|
static func fresh() -> IdentityDatabase { try! IdentityDatabase(inMemory: true) }
|
|
|
|
static var development: IdentityDatabase = {
|
|
let db = IdentityDatabase.fresh()
|
|
|
|
db.createIdentity(id: devIdentityID, url: devInstanceURL)
|
|
.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
|
|
}()
|
|
}
|
|
|
|
extension Identity {
|
|
static let development: Identity = {
|
|
var identity: Identity?
|
|
|
|
IdentityDatabase.development.identityObservation(id: devIdentityID)
|
|
.assertNoFailure()
|
|
.sink(receiveValue: { identity = $0 })
|
|
.store(in: &cancellables)
|
|
|
|
return identity!
|
|
}()
|
|
}
|
|
|
|
extension AppEnvironment {
|
|
static func fresh(
|
|
identityDatabase: IdentityDatabase = .fresh(),
|
|
preferences: Preferences = .fresh(),
|
|
secrets: Secrets = .fresh(),
|
|
webAuthSessionType: WebAuthSession.Type = SuccessfulStubbingWebAuthSession.self) -> AppEnvironment {
|
|
AppEnvironment(
|
|
identityDatabase: identityDatabase,
|
|
preferences: preferences,
|
|
secrets: secrets,
|
|
webAuthSessionType: webAuthSessionType)
|
|
}
|
|
|
|
static let development = AppEnvironment(
|
|
identityDatabase: .development,
|
|
preferences: .development,
|
|
secrets: .development,
|
|
webAuthSessionType: SuccessfulStubbingWebAuthSession.self)
|
|
}
|
|
|
|
extension SceneViewModel {
|
|
static let development = SceneViewModel(networkClient: .development, environment: .development)
|
|
}
|
|
|
|
// swiftlint:enable force_try
|