mirror of
https://github.com/metabolist/metatext.git
synced 2024-11-22 00:01:00 +00:00
Refactoring
This commit is contained in:
parent
335a006f45
commit
44f6e5d551
5 changed files with 50 additions and 41 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,38 +44,7 @@ private extension IdentitiesView {
|
|||
rootViewModel.newIdentitySelected(id: identity.id)
|
||||
}
|
||||
} label: {
|
||||
HStack {
|
||||
KFImage(identity.image,
|
||||
options: .downsampled(dimension: 40, scaleFactor: displayScale))
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
Spacer()
|
||||
if identity.authenticated {
|
||||
if let account = identity.account {
|
||||
CustomEmojiText(
|
||||
text: account.displayName,
|
||||
emoji: account.emojis,
|
||||
textStyle: .headline)
|
||||
}
|
||||
Text(identity.handle)
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.secondary)
|
||||
} else {
|
||||
Text(identity.handle)
|
||||
.font(.headline)
|
||||
.foregroundColor(.secondary)
|
||||
if let instance = identity.instance {
|
||||
Text(instance.uri)
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
}
|
||||
Spacer()
|
||||
}
|
||||
Spacer()
|
||||
if identity.id == viewModel.currentIdentityID {
|
||||
Image(systemName: "checkmark.circle")
|
||||
}
|
||||
}
|
||||
row(identity: identity)
|
||||
}
|
||||
.disabled(identity.id == viewModel.currentIdentityID)
|
||||
.buttonStyle(PlainButtonStyle())
|
||||
|
@ -89,6 +58,42 @@ private extension IdentitiesView {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
func row(identity: Identity) -> some View {
|
||||
HStack {
|
||||
KFImage(identity.image,
|
||||
options: .downsampled(dimension: 40, scaleFactor: displayScale))
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
Spacer()
|
||||
if identity.authenticated {
|
||||
if let account = identity.account {
|
||||
CustomEmojiText(
|
||||
text: account.displayName,
|
||||
emoji: account.emojis,
|
||||
textStyle: .headline)
|
||||
}
|
||||
Text(identity.handle)
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.secondary)
|
||||
} else {
|
||||
Text(identity.handle)
|
||||
.font(.headline)
|
||||
.foregroundColor(.secondary)
|
||||
if let instance = identity.instance {
|
||||
Text(instance.uri)
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
}
|
||||
Spacer()
|
||||
}
|
||||
Spacer()
|
||||
if identity.id == viewModel.currentIdentityID {
|
||||
Image(systemName: "checkmark.circle")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
|
|
Loading…
Reference in a new issue