metatext/Views/AddIdentityView.swift

99 lines
3.8 KiB
Swift
Raw Normal View History

// Copyright © 2020 Metabolist. All rights reserved.
2020-09-10 09:38:21 +00:00
import KingfisherSwiftUI
import SwiftUI
2020-09-01 07:33:49 +00:00
import ViewModels
struct AddIdentityView: View {
@StateObject var viewModel: AddIdentityViewModel
2020-09-12 07:36:59 +00:00
@Environment(\.accessibilityReduceMotion) var accessibilityReduceMotion
2020-08-07 01:41:59 +00:00
@EnvironmentObject var rootViewModel: RootViewModel
2020-09-12 07:36:59 +00:00
2020-09-11 09:55:06 +00:00
@State private var navigateToRegister = false
var body: some View {
Form {
2020-09-10 09:38:21 +00:00
Section {
TextField("add-identity.instance-url", text: $viewModel.urlFieldText)
2020-09-12 07:36:59 +00:00
.textContentType(.URL)
2020-09-10 09:38:21 +00:00
.autocapitalization(.none)
.disableAutocorrection(true)
.keyboardType(.URL)
2020-09-12 19:09:54 +00:00
if let instance = viewModel.instance {
2020-09-10 09:38:21 +00:00
VStack(alignment: .center) {
2020-09-11 00:57:22 +00:00
KFImage(instance.thumbnail)
.placeholder {
ProgressView()
}
.resizable()
.aspectRatio(16 / 9, contentMode: .fill)
2020-09-11 01:59:43 +00:00
.background(Color.blue)
Spacer()
2020-09-10 09:38:21 +00:00
Text(instance.title)
.font(.headline)
Text(instance.uri)
.font(.subheadline)
.foregroundColor(.secondary)
2020-09-11 01:59:43 +00:00
Spacer()
2020-09-10 09:38:21 +00:00
}
.frame(maxWidth: .infinity, alignment: .center)
.listRowInsets(EdgeInsets())
}
2020-09-11 01:59:43 +00:00
Group {
if viewModel.loading {
ProgressView()
} else {
Button("add-identity.log-in",
action: viewModel.logInTapped)
2020-09-12 19:09:54 +00:00
if let instance = viewModel.instance, let url = viewModel.url,
2020-09-11 09:55:06 +00:00
instance.registrations {
ZStack {
NavigationLink(
destination: RegistrationView(
viewModel: viewModel.registrationViewModel(
instance: instance,
url: url)),
isActive: $navigateToRegister) {
EmptyView()
}
.hidden()
Button(instance.approvalRequired
? "add-identity.request-invite"
: "add-identity.join") {
navigateToRegister.toggle()
}
}
}
2020-09-11 01:59:43 +00:00
if viewModel.isPublicTimelineAvailable {
Button("add-identity.browse", action: viewModel.browseTapped)
}
}
}
.frame(maxWidth: .infinity, alignment: .center)
}
}
2020-09-12 07:36:59 +00:00
.animation(.default, if: !accessibilityReduceMotion)
.alertItem($viewModel.alertItem)
2020-09-07 04:56:18 +00:00
.onAppear(perform: viewModel.refreshFilter)
}
}
2020-09-10 00:52:46 +00:00
extension AddIdentityError: LocalizedError {
public var errorDescription: String? {
NSLocalizedString("add-identity.unable-to-connect-to-instance", comment: "")
}
}
2020-07-31 21:40:57 +00:00
#if DEBUG
2020-09-01 07:33:49 +00:00
import PreviewViewModels
struct AddAccountView_Previews: PreviewProvider {
static var previews: some View {
2020-09-11 09:55:06 +00:00
NavigationView {
AddIdentityView(viewModel: RootViewModel.preview.addIdentityViewModel())
.navigationBarTitleDisplayMode(.inline)
}
}
}
2020-07-31 21:40:57 +00:00
#endif