Refactoring

This commit is contained in:
Justin Mazzocchi 2020-08-07 15:18:54 -07:00
parent 9659085ca6
commit ce3618dcbe
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
10 changed files with 59 additions and 51 deletions

View file

@ -118,8 +118,8 @@ extension AppEnvironment {
webAuthSessionType: SuccessfulStubbingWebAuthSession.self)
}
extension IdentifiedEnvironment {
static let development = try! IdentifiedEnvironment(identityID: devIdentityID, appEnvironment: .development)
extension IdentityRepository {
static let development = try! IdentityRepository(identityID: devIdentityID, appEnvironment: .development)
}
extension RootViewModel {
@ -127,7 +127,7 @@ extension RootViewModel {
}
extension MainNavigationViewModel {
static let development = MainNavigationViewModel(environment: .development)
static let development = MainNavigationViewModel(identityRepository: .development)
}
extension SecondaryNavigationViewModel {

View file

@ -15,8 +15,8 @@
D0091B6F24DD68090040E8D2 /* PreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0091B6D24DD68090040E8D2 /* PreferencesView.swift */; };
D0091B7124DD68220040E8D2 /* PreferencesViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0091B7024DD68220040E8D2 /* PreferencesViewModel.swift */; };
D0091B7224DD68220040E8D2 /* PreferencesViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0091B7024DD68220040E8D2 /* PreferencesViewModel.swift */; };
D0091B7424DDF4860040E8D2 /* IdentifiedEnvironment.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0091B7324DDF4860040E8D2 /* IdentifiedEnvironment.swift */; };
D0091B7524DDF4860040E8D2 /* IdentifiedEnvironment.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0091B7324DDF4860040E8D2 /* IdentifiedEnvironment.swift */; };
D0091B7424DDF4860040E8D2 /* IdentityRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0091B7324DDF4860040E8D2 /* IdentityRepository.swift */; };
D0091B7524DDF4860040E8D2 /* IdentityRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0091B7324DDF4860040E8D2 /* IdentityRepository.swift */; };
D047FAAE24C3E21200AF17C5 /* MetatextApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = D047FA8524C3E21000AF17C5 /* MetatextApp.swift */; };
D047FAAF24C3E21200AF17C5 /* MetatextApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = D047FA8524C3E21000AF17C5 /* MetatextApp.swift */; };
D047FAB224C3E21200AF17C5 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D047FA8724C3E21200AF17C5 /* Assets.xcassets */; };
@ -173,7 +173,7 @@
D0091B6A24DC10CE0040E8D2 /* PostingReadingPreferencesViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostingReadingPreferencesViewModel.swift; sourceTree = "<group>"; };
D0091B6D24DD68090040E8D2 /* PreferencesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesView.swift; sourceTree = "<group>"; };
D0091B7024DD68220040E8D2 /* PreferencesViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesViewModel.swift; sourceTree = "<group>"; };
D0091B7324DDF4860040E8D2 /* IdentifiedEnvironment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IdentifiedEnvironment.swift; sourceTree = "<group>"; };
D0091B7324DDF4860040E8D2 /* IdentityRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IdentityRepository.swift; sourceTree = "<group>"; };
D047FA8524C3E21000AF17C5 /* MetatextApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MetatextApp.swift; sourceTree = "<group>"; };
D047FA8724C3E21200AF17C5 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
D047FA8C24C3E21200AF17C5 /* Metatext.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Metatext.app; sourceTree = BUILT_PRODUCTS_DIR; };
@ -361,9 +361,9 @@
D0666A4424C6BC0A00F3F04B /* DatabaseError.swift */,
D052BBCE24D750C000A80A7A /* Defaults.swift */,
D0666A5324C6C3E500F3F04B /* Emoji.swift */,
D0091B7324DDF4860040E8D2 /* IdentifiedEnvironment.swift */,
D0666A4A24C6C37700F3F04B /* Identity.swift */,
D0666A4124C6BB7B00F3F04B /* IdentityDatabase.swift */,
D0091B7324DDF4860040E8D2 /* IdentityRepository.swift */,
D0666A4D24C6C39600F3F04B /* Instance.swift */,
D0DC177324D0B58800A75C65 /* Keychain.swift */,
D0ED1BE224CFA84400B4899C /* MastodonError.swift */,
@ -694,7 +694,7 @@
D0BEC94724CA22C400E864C4 /* TimelineViewModel.swift in Sources */,
D0666A4E24C6C39600F3F04B /* Instance.swift in Sources */,
D0ED1BDA24CF963E00B4899C /* AppAuthorizationEndpoint.swift in Sources */,
D0091B7424DDF4860040E8D2 /* IdentifiedEnvironment.swift in Sources */,
D0091B7424DDF4860040E8D2 /* IdentityRepository.swift in Sources */,
D052BBCF24D750C000A80A7A /* Defaults.swift in Sources */,
D0ED1BE324CFA84400B4899C /* MastodonError.swift in Sources */,
D0666A6324C6DC6C00F3F04B /* AppAuthorization.swift in Sources */,
@ -769,7 +769,7 @@
D0BEC94824CA22C400E864C4 /* TimelineViewModel.swift in Sources */,
D0666A4F24C6C39600F3F04B /* Instance.swift in Sources */,
D0ED1BDB24CF963E00B4899C /* AppAuthorizationEndpoint.swift in Sources */,
D0091B7524DDF4860040E8D2 /* IdentifiedEnvironment.swift in Sources */,
D0091B7524DDF4860040E8D2 /* IdentityRepository.swift in Sources */,
D052BBD024D750C000A80A7A /* Defaults.swift in Sources */,
D0ED1BE424CFA84400B4899C /* MastodonError.swift in Sources */,
D0666A6424C6DC6C00F3F04B /* AppAuthorization.swift in Sources */,

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PreviewsEnabled</key>
<false/>
</dict>
</plist>

View file

@ -3,7 +3,7 @@
import Foundation
import Combine
class IdentifiedEnvironment {
class IdentityRepository {
@Published var identity: Identity
let observationErrors: AnyPublisher<Error, Never>
@ -41,7 +41,7 @@ class IdentifiedEnvironment {
}
}
extension IdentifiedEnvironment {
extension IdentityRepository {
var isAuthorized: Bool { networkClient.accessToken != nil }
func verifyCredentials() -> AnyPublisher<Void, Error> {

View file

@ -8,14 +8,14 @@ class IdentitiesViewModel: ObservableObject {
@Published var identities = [Identity]()
@Published var alertItem: AlertItem?
private let environment: IdentifiedEnvironment
private let identityRepository: IdentityRepository
private var cancellables = Set<AnyCancellable>()
init(environment: IdentifiedEnvironment) {
self.environment = environment
identity = environment.identity
init(identityRepository: IdentityRepository) {
self.identityRepository = identityRepository
identity = identityRepository.identity
environment.identitiesObservation()
identityRepository.identitiesObservation()
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.assign(to: &$identities)
}

View file

@ -10,15 +10,15 @@ class MainNavigationViewModel: ObservableObject {
@Published var alertItem: AlertItem?
var selectedTab: Tab? = .timelines
private let environment: IdentifiedEnvironment
private let identityRepository: IdentityRepository
private var cancellables = Set<AnyCancellable>()
init(environment: IdentifiedEnvironment) {
self.environment = environment
identity = environment.identity
environment.$identity.dropFirst().assign(to: &$identity)
init(identityRepository: IdentityRepository) {
self.identityRepository = identityRepository
identity = identityRepository.identity
identityRepository.$identity.dropFirst().assign(to: &$identity)
environment.recentIdentitiesObservation()
identityRepository.recentIdentitiesObservation()
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.assign(to: &$recentIdentities)
}
@ -26,28 +26,28 @@ class MainNavigationViewModel: ObservableObject {
extension MainNavigationViewModel {
func refreshIdentity() {
if environment.isAuthorized {
environment.verifyCredentials()
if identityRepository.isAuthorized {
identityRepository.verifyCredentials()
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink(receiveValue: {})
.store(in: &cancellables)
if identity.preferences.useServerPostingReadingPreferences {
environment.refreshServerPreferences()
identityRepository.refreshServerPreferences()
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink(receiveValue: {})
.store(in: &cancellables)
}
}
environment.refreshInstance()
identityRepository.refreshInstance()
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink(receiveValue: {})
.store(in: &cancellables)
}
func settingsViewModel() -> SecondaryNavigationViewModel {
SecondaryNavigationViewModel(environment: environment)
SecondaryNavigationViewModel(identityRepository: identityRepository)
}
}

View file

@ -8,15 +8,15 @@ class PostingReadingPreferencesViewModel: ObservableObject {
@Published var alertItem: AlertItem?
let handle: String
private let environment: IdentifiedEnvironment
private let identityRepository: IdentityRepository
private var cancellables = Set<AnyCancellable>()
init(environment: IdentifiedEnvironment) {
self.environment = environment
preferences = environment.identity.preferences
handle = environment.identity.handle
init(identityRepository: IdentityRepository) {
self.identityRepository = identityRepository
preferences = identityRepository.identity.preferences
handle = identityRepository.identity.handle
environment.$identity.map(\.preferences)
identityRepository.$identity.map(\.preferences)
.dropFirst()
.removeDuplicates()
.handleEvents(receiveOutput: { [weak self] in
@ -27,7 +27,7 @@ class PostingReadingPreferencesViewModel: ObservableObject {
.assign(to: &$preferences)
$preferences.dropFirst()
.flatMap(environment.updatePreferences)
.flatMap(identityRepository.updatePreferences)
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink(receiveValue: {})
.store(in: &cancellables)
@ -36,7 +36,7 @@ class PostingReadingPreferencesViewModel: ObservableObject {
extension PostingReadingPreferencesViewModel {
private func refreshServerPreferences() {
environment.refreshServerPreferences()
identityRepository.refreshServerPreferences()
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink(receiveValue: {})
.store(in: &cancellables)

View file

@ -5,16 +5,16 @@ import Foundation
class PreferencesViewModel: ObservableObject {
let handle: String
private let environment: IdentifiedEnvironment
private let identityRepository: IdentityRepository
init(environment: IdentifiedEnvironment) {
self.environment = environment
handle = environment.identity.handle
init(identityRepository: IdentityRepository) {
self.identityRepository = identityRepository
handle = identityRepository.identity.handle
}
}
extension PreferencesViewModel {
func postingReadingPreferencesViewModel() -> PostingReadingPreferencesViewModel {
PostingReadingPreferencesViewModel(environment: environment)
PostingReadingPreferencesViewModel(identityRepository: identityRepository)
}
}

View file

@ -29,19 +29,19 @@ extension RootViewModel {
}
func mainNavigationViewModel(identityID: String) -> MainNavigationViewModel? {
let identifiedEnvironment: IdentifiedEnvironment
let identityRepository: IdentityRepository
do {
identifiedEnvironment = try IdentifiedEnvironment(identityID: identityID, appEnvironment: environment)
identityRepository = try IdentityRepository(identityID: identityID, appEnvironment: environment)
} catch {
return nil
}
identifiedEnvironment.observationErrors
identityRepository.observationErrors
.receive(on: RunLoop.main)
.map { [weak self] _ in self?.environment.identityDatabase.mostRecentlyUsedIdentityID }
.assign(to: &$identityID)
return MainNavigationViewModel(environment: identifiedEnvironment)
return MainNavigationViewModel(identityRepository: identityRepository)
}
}

View file

@ -4,21 +4,21 @@ import Foundation
class SecondaryNavigationViewModel: ObservableObject {
@Published private(set) var identity: Identity
private let environment: IdentifiedEnvironment
private let identityRepository: IdentityRepository
init(environment: IdentifiedEnvironment) {
self.environment = environment
identity = environment.identity
environment.$identity.dropFirst().assign(to: &$identity)
init(identityRepository: IdentityRepository) {
self.identityRepository = identityRepository
identity = identityRepository.identity
identityRepository.$identity.dropFirst().assign(to: &$identity)
}
}
extension SecondaryNavigationViewModel {
func identitiesViewModel() -> IdentitiesViewModel {
IdentitiesViewModel(environment: environment)
IdentitiesViewModel(identityRepository: identityRepository)
}
func preferencesViewModel() -> PreferencesViewModel {
PreferencesViewModel(environment: environment)
PreferencesViewModel(identityRepository: identityRepository)
}
}