Refactoring

This commit is contained in:
Justin Mazzocchi 2020-09-08 23:17:35 -07:00
parent 335a006f45
commit 44f6e5d551
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
5 changed files with 50 additions and 41 deletions

View file

@ -9,21 +9,19 @@ import Secrets
public struct AllIdentitiesService {
public let mostRecentlyUsedIdentityID: AnyPublisher<UUID?, Never>
public let instanceFilterService: InstanceFilterService
private let database: IdentityDatabase
private let environment: AppEnvironment
private let database: IdentityDatabase
public init(environment: AppEnvironment) throws {
self.environment = environment
self.database = try environment.fixtureDatabase ?? IdentityDatabase(
inMemory: environment.inMemoryContent,
keychain: environment.keychain)
self.environment = environment
mostRecentlyUsedIdentityID = database.mostRecentlyUsedIdentityIDObservation()
.replaceError(with: nil)
.eraseToAnyPublisher()
instanceFilterService = InstanceFilterService(environment: environment)
}
}

View file

@ -9,7 +9,7 @@ public struct InstanceFilterService {
private let httpClient: HTTPClient
private var userDefaultsClient: UserDefaultsClient
init(environment: AppEnvironment) {
public init(environment: AppEnvironment) {
httpClient = HTTPClient(session: environment.session, decoder: JSONDecoder())
userDefaultsClient = UserDefaultsClient(userDefaults: environment.userDefaults)
}

View file

@ -11,11 +11,13 @@ public final class AddIdentityViewModel: ObservableObject {
public let addedIdentityID: AnyPublisher<UUID, Never>
private let allIdentitiesService: AllIdentitiesService
private let instanceFilterService: InstanceFilterService
private let addedIdentityIDSubject = PassthroughSubject<UUID, Never>()
private var cancellables = Set<AnyCancellable>()
init(allIdentitiesService: AllIdentitiesService) {
init(allIdentitiesService: AllIdentitiesService, instanceFilterService: InstanceFilterService) {
self.allIdentitiesService = allIdentitiesService
self.instanceFilterService = instanceFilterService
addedIdentityID = addedIdentityIDSubject.eraseToAnyPublisher()
}
}
@ -81,7 +83,7 @@ public extension AddIdentityViewModel {
}
func refreshFilter() {
allIdentitiesService.instanceFilterService.updateFilter()
instanceFilterService.updateFilter()
.sink { _ in }
.store(in: &cancellables)
}
@ -102,7 +104,7 @@ private extension AddIdentityViewModel {
throw URLError(.badURL)
}
if allIdentitiesService.instanceFilterService.isFiltered(url: url) {
if instanceFilterService.isFiltered(url: url) {
return Self.filteredURL
}

View file

@ -8,6 +8,7 @@ public final class RootViewModel: ObservableObject {
@Published public private(set) var identification: Identification?
@Published private var mostRecentlyUsedIdentityID: UUID?
private let environment: AppEnvironment
private let allIdentitiesService: AllIdentitiesService
private let userNotificationService: UserNotificationService
private let registerForRemoteNotifications: () -> AnyPublisher<Data, Error>
@ -15,6 +16,7 @@ public final class RootViewModel: ObservableObject {
public init(environment: AppEnvironment,
registerForRemoteNotifications: @escaping () -> AnyPublisher<Data, Error>) throws {
self.environment = environment
allIdentitiesService = try AllIdentitiesService(environment: environment)
userNotificationService = UserNotificationService(environment: environment)
self.registerForRemoteNotifications = registerForRemoteNotifications
@ -77,6 +79,8 @@ public extension RootViewModel {
}
func addIdentityViewModel() -> AddIdentityViewModel {
AddIdentityViewModel(allIdentitiesService: allIdentitiesService)
AddIdentityViewModel(
allIdentitiesService: allIdentitiesService,
instanceFilterService: InstanceFilterService(environment: environment))
}
}

View file

@ -44,6 +44,23 @@ private extension IdentitiesView {
rootViewModel.newIdentitySelected(id: identity.id)
}
} label: {
row(identity: identity)
}
.disabled(identity.id == viewModel.currentIdentityID)
.buttonStyle(PlainButtonStyle())
}
.onDelete {
guard let index = $0.first else { return }
rootViewModel.deleteIdentity(id: identities[index].id)
}
}
}
}
}
@ViewBuilder
func row(identity: Identity) -> some View {
HStack {
KFImage(identity.image,
options: .downsampled(dimension: 40, scaleFactor: displayScale))
@ -77,18 +94,6 @@ private extension IdentitiesView {
}
}
}
.disabled(identity.id == viewModel.currentIdentityID)
.buttonStyle(PlainButtonStyle())
}
.onDelete {
guard let index = $0.first else { return }
rootViewModel.deleteIdentity(id: identities[index].id)
}
}
}
}
}
}
#if DEBUG