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

96 lines
3.6 KiB
Swift
Raw Normal View History

// Copyright © 2020 Metabolist. All rights reserved.
2021-01-08 03:29:08 +01:00
import Kingfisher
import SwiftUI
2020-09-01 09:33:49 +02:00
import ViewModels
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
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-09-11 03:59:43 +02:00
Group {
if viewModel.loading {
ProgressView()
} else {
Button("add-identity.log-in",
action: viewModel.logInTapped)
if viewModel.isPublicTimelineAvailable {
Button("add-identity.browse", action: viewModel.browseTapped)
}
2021-01-05 22:34:14 +01:00
if let instance = viewModel.instance,
let url = viewModel.url,
instance.registrations {
NavigationLink(
instance.approvalRequired
? "add-identity.request-invite"
: "add-identity.join",
destination: RegistrationView(
viewModel: viewModel.registrationViewModel(
instance: instance,
url: url)))
}
2020-09-11 03:59:43 +02:00
}
}
.frame(maxWidth: .infinity, alignment: .center)
}
}
2020-09-12 09:36:59 +02:00
.animation(.default, if: !accessibilityReduceMotion)
.alertItem($viewModel.alertItem)
2020-09-07 06:56:18 +02:00
.onAppear(perform: viewModel.refreshFilter)
}
}
2020-09-10 02:52:46 +02:00
extension AddIdentityError: LocalizedError {
public var errorDescription: String? {
2021-01-17 21:24:50 +01:00
switch self {
case .unableToConnectToInstance:
return NSLocalizedString("add-identity.unable-to-connect-to-instance", comment: "")
case .instanceNotSupported:
return NSLocalizedString("add-identity.instance-not-supported", comment: "")
}
2020-09-10 02:52:46 +02:00
}
}
2020-07-31 23:40:57 +02:00
#if DEBUG
2020-09-01 09:33:49 +02:00
import PreviewViewModels
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-31 23:40:57 +02:00
#endif