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