Delete identities

This commit is contained in:
Justin Mazzocchi 2020-08-08 00:43:06 -07:00
parent 8d584b55da
commit 97de884213
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
5 changed files with 53 additions and 11 deletions

View file

@ -5,6 +5,7 @@
"oauth.error.code-not-found" = "OAuth error: code not found";
"secondary-navigation.accounts" = "Accounts";
"secondary-navigation.preferences" = "Preferences";
"identities.add" = "Add";
"preferences" = "Preferences";
"preferences.posting-reading" = "Posting and Reading";
"preferences.posting" = "Posting";

View file

@ -41,6 +41,12 @@ extension IdentityDatabase {
.eraseToAnyPublisher()
}
func deleteIdentity(id: UUID) -> AnyPublisher<Void, Error> {
return databaseQueue.writePublisher(updates: StoredIdentity.filter(Column("id") == id).deleteAll)
.map { _ in () }
.eraseToAnyPublisher()
}
func updateLastUsedAt(identityID: UUID) -> AnyPublisher<Void, Error> {
databaseQueue.writePublisher {
try StoredIdentity
@ -110,8 +116,8 @@ extension IdentityDatabase {
.eraseToAnyPublisher()
}
func identitiesObservation(excluding: UUID) -> AnyPublisher<[Identity], Error> {
ValueObservation.tracking(Self.identitiesRequest(excluding: excluding).fetchAll)
func identitiesObservation() -> AnyPublisher<[Identity], Error> {
ValueObservation.tracking(Self.identitiesRequest().fetchAll)
.removeDuplicates()
.publisher(in: databaseQueue, scheduling: .immediate)
.map { $0.map(Identity.init(result:)) }
@ -119,7 +125,11 @@ extension IdentityDatabase {
}
func recentIdentitiesObservation(excluding: UUID) -> AnyPublisher<[Identity], Error> {
ValueObservation.tracking(Self.identitiesRequest(excluding: excluding).limit(9).fetchAll)
ValueObservation.tracking(
Self.identitiesRequest()
.filter(Column("id") != excluding)
.limit(9)
.fetchAll)
.removeDuplicates()
.publisher(in: databaseQueue, scheduling: .immediate)
.map { $0.map(Identity.init(result:)) }
@ -132,9 +142,8 @@ extension IdentityDatabase {
}
private extension IdentityDatabase {
private static func identitiesRequest(excluding: UUID) -> QueryInterfaceRequest<IdentityResult> {
private static func identitiesRequest() -> QueryInterfaceRequest<IdentityResult> {
StoredIdentity
.filter(Column("id") != excluding)
.order(Column("lastUsedAt").desc)
.including(optional: StoredIdentity.instance)
.including(optional: StoredIdentity.account)

View file

@ -69,7 +69,7 @@ extension IdentityRepository {
}
func identitiesObservation() -> AnyPublisher<[Identity], Error> {
appEnvironment.identityDatabase.identitiesObservation(excluding: identity.id)
appEnvironment.identityDatabase.identitiesObservation()
}
func recentIdentitiesObservation() -> AnyPublisher<[Identity], Error> {

View file

@ -24,6 +24,12 @@ extension RootViewModel {
.store(in: &cancellables)
}
func deleteIdentity(id: UUID) {
environment.identityDatabase.deleteIdentity(id: id)
.sink(receiveCompletion: { _ in }, receiveValue: {})
.store(in: &cancellables)
}
func addIdentityViewModel() -> AddIdentityViewModel {
AddIdentityViewModel(environment: environment)
}

View file

@ -1,10 +1,12 @@
// Copyright © 2020 Metabolist. All rights reserved.
import SwiftUI
import KingfisherSwiftUI
struct IdentitiesView: View {
@StateObject var viewModel: IdentitiesViewModel
@EnvironmentObject var rootViewModel: RootViewModel
@Environment(\.displayScale) var displayScale: CGFloat
var body: some View {
Form {
@ -12,18 +14,42 @@ struct IdentitiesView: View {
NavigationLink(
destination: AddIdentityView(viewModel: rootViewModel.addIdentityViewModel()),
label: {
Label("add new account", systemImage: "plus")
Label("identities.add", systemImage: "plus.circle")
})
}
Section {
List(viewModel.identities) { identity in
Button(identity.handle) {
List {
ForEach(viewModel.identities) { identity in
Button {
withAnimation {
rootViewModel.newIdentitySelected(id: identity.id)
}
} label: {
HStack {
KFImage(identity.image,
options: .downsampled(dimension: 28, scaleFactor: displayScale))
Text(identity.handle)
Spacer()
if identity.id == viewModel.identity.id {
Image(systemName: "checkmark.circle")
}
}
}
.disabled(identity.id == viewModel.identity.id)
.buttonStyle(PlainButtonStyle())
}
.onDelete {
guard let index = $0.first else { return }
rootViewModel.deleteIdentity(id: viewModel.identities[index].id)
}
}
}
}
.toolbar {
ToolbarItem(placement: ToolbarItemPlacement.navigationBarTrailing) {
EditButton()
}
}
}
}