metatext-app-ios-iphone-ipad/Views/IdentitiesView.swift

114 lines
3.8 KiB
Swift
Raw Normal View History

2020-08-04 22:26:09 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
2021-01-08 03:29:08 +01:00
import Kingfisher
2020-09-09 07:40:49 +02:00
import struct ServiceLayer.Identity
2020-09-05 04:31:43 +02:00
import SwiftUI
2020-09-01 09:33:49 +02:00
import ViewModels
2020-08-04 22:26:09 +02:00
struct IdentitiesView: View {
@StateObject var viewModel: IdentitiesViewModel
@EnvironmentObject var rootViewModel: RootViewModel
2020-08-08 09:43:06 +02:00
@Environment(\.displayScale) var displayScale: CGFloat
2020-08-04 22:26:09 +02:00
var body: some View {
Form {
Section {
NavigationLink(
destination: AddIdentityView(viewModel: rootViewModel.addIdentityViewModel()),
label: {
2020-08-29 05:50:58 +02:00
Label("add", systemImage: "plus.circle")
2020-08-04 22:26:09 +02:00
})
}
2020-09-09 07:40:49 +02:00
section(title: "identities.accounts", identities: viewModel.authenticated)
2020-09-11 03:59:43 +02:00
section(title: "identities.browsing", identities: viewModel.unauthenticated)
2020-09-13 10:03:08 +02:00
section(title: "identities.pending", identities: viewModel.pending)
2020-09-09 07:40:49 +02: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 09:43:06 +02:00
List {
2020-09-09 07:40:49 +02:00
ForEach(identities) { identity in
2020-08-08 09:43:06 +02:00
Button {
withAnimation {
2020-09-09 14:05:43 +02:00
rootViewModel.identitySelected(id: identity.id)
2020-08-08 09:43:06 +02:00
}
} label: {
2020-09-09 08:17:35 +02:00
row(identity: identity)
2020-08-07 03:41:59 +02:00
}
2020-10-06 00:50:05 +02:00
.disabled(identity.id == viewModel.currentIdentityId)
2020-08-08 09:43:06 +02:00
.buttonStyle(PlainButtonStyle())
}
.onDelete {
guard let index = $0.first else { return }
2020-09-09 07:40:49 +02:00
rootViewModel.deleteIdentity(id: identities[index].id)
2020-08-04 22:26:09 +02:00
}
}
}
}
}
2020-09-09 08:17:35 +02:00
@ViewBuilder
func row(identity: Identity) -> some View {
HStack {
2021-01-08 03:29:08 +01:00
KFImage(identity.image)
.downsampled(dimension: 40, scaleFactor: displayScale)
2020-09-09 08:17:35 +02:00
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 {
if let instance = identity.instance {
2020-09-09 09:28:14 +02:00
CustomEmojiText(
text: instance.title,
emoji: [],
textStyle: .headline)
2020-09-09 08:17:35 +02:00
Text(instance.uri)
.font(.subheadline)
.foregroundColor(.secondary)
2020-09-09 09:28:14 +02:00
} else {
Text(identity.handle)
.font(.headline)
2020-09-09 08:17:35 +02:00
}
}
Spacer()
}
Spacer()
2020-10-06 00:50:05 +02:00
if identity.id == viewModel.currentIdentityId {
2020-09-09 08:17:35 +02:00
Image(systemName: "checkmark.circle")
}
}
}
2020-08-04 22:26:09 +02:00
}
2020-08-12 09:37:14 +02:00
#if DEBUG
2020-09-01 09:33:49 +02:00
import PreviewViewModels
2020-08-04 22:26:09 +02:00
struct IdentitiesView_Previews: PreviewProvider {
static var previews: some View {
2020-09-08 04:12:38 +02:00
IdentitiesView(viewModel: .init(identification: .preview))
.environmentObject(RootViewModel.preview)
2020-08-04 22:26:09 +02:00
}
}
2020-08-12 09:37:14 +02:00
#endif