metatext/Views/IdentitiesView.swift

95 lines
3.1 KiB
Swift
Raw Normal View History

2020-08-04 20:26:09 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
2021-01-08 02:29:08 +00:00
import Kingfisher
2020-09-05 02:31:43 +00:00
import SwiftUI
2020-09-01 07:33:49 +00:00
import ViewModels
2020-08-04 20:26:09 +00:00
struct IdentitiesView: View {
@StateObject var viewModel: IdentitiesViewModel
@EnvironmentObject var rootViewModel: RootViewModel
2020-08-08 07:43:06 +00:00
@Environment(\.displayScale) var displayScale: CGFloat
2020-08-04 20:26:09 +00:00
var body: some View {
Form {
Section {
NavigationLink(
2021-01-27 20:31:32 +00:00
destination: AddIdentityView(
viewModelClosure: { rootViewModel.addIdentityViewModel() },
displayWelcome: false),
2020-08-04 20:26:09 +00:00
label: {
2020-08-29 03:50:58 +00:00
Label("add", systemImage: "plus.circle")
2020-08-04 20:26:09 +00:00
})
}
2021-01-28 02:28:12 +00:00
section(title: "identities.accounts",
identities: viewModel.identities.filter { $0.authenticated && !$0.pending })
section(title: "identities.browsing",
identities: viewModel.identities.filter { !$0.authenticated && !$0.pending })
section(title: "identities.pending",
identities: viewModel.identities.filter { $0.pending })
2020-09-09 05:40:49 +00:00
}
2021-01-28 01:31:12 +00:00
.navigationTitle(Text("secondary-navigation.accounts"))
2020-09-09 05:40:49 +00:00
.toolbar {
ToolbarItem(placement: ToolbarItemPlacement.navigationBarTrailing) {
EditButton()
}
}
}
}
private extension IdentitiesView {
@ViewBuilder
func section(title: LocalizedStringKey, identities: [Identity]) -> some View {
if identities.isEmpty {
EmptyView()
} else {
Section(header: Text(title)) {
2020-08-08 07:43:06 +00:00
List {
2020-09-09 05:40:49 +00:00
ForEach(identities) { identity in
2020-08-08 07:43:06 +00:00
Button {
withAnimation {
2020-09-09 12:05:43 +00:00
rootViewModel.identitySelected(id: identity.id)
2020-08-08 07:43:06 +00:00
}
} label: {
2020-09-09 06:17:35 +00:00
row(identity: identity)
2020-08-07 01:41:59 +00:00
}
2020-10-05 22:50:05 +00:00
.disabled(identity.id == viewModel.currentIdentityId)
2020-08-08 07:43:06 +00:00
.buttonStyle(PlainButtonStyle())
}
.onDelete {
guard let index = $0.first else { return }
2020-09-09 05:40:49 +00:00
rootViewModel.deleteIdentity(id: identities[index].id)
2020-08-04 20:26:09 +00:00
}
}
}
}
}
2020-09-09 06:17:35 +00:00
@ViewBuilder
func row(identity: Identity) -> some View {
HStack {
2021-01-28 01:31:12 +00:00
Label {
Text(verbatim: identity.handle)
} icon: {
KFImage(identity.image)
.downsampled(dimension: .barButtonItemDimension, scaleFactor: displayScale)
2020-09-09 06:17:35 +00:00
}
Spacer()
2020-10-05 22:50:05 +00:00
if identity.id == viewModel.currentIdentityId {
2020-09-09 06:17:35 +00:00
Image(systemName: "checkmark.circle")
}
}
}
2020-08-04 20:26:09 +00:00
}
2020-08-12 07:37:14 +00:00
#if DEBUG
2020-09-01 07:33:49 +00:00
import PreviewViewModels
2020-08-04 20:26:09 +00:00
struct IdentitiesView_Previews: PreviewProvider {
static var previews: some View {
2021-01-26 00:06:35 +00:00
IdentitiesView(viewModel: .init(identityContext: .preview))
2020-09-08 02:12:38 +00:00
.environmentObject(RootViewModel.preview)
2020-08-04 20:26:09 +00:00
}
}
2020-08-12 07:37:14 +00:00
#endif