metatext/Views/AddIdentityView.swift

96 lines
3.6 KiB
Swift
Raw Normal View History

// Copyright © 2020 Metabolist. All rights reserved.
2021-01-08 02:29:08 +00:00
import Kingfisher
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
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)
if viewModel.isPublicTimelineAvailable {
Button("add-identity.browse", action: viewModel.browseTapped)
}
2021-01-05 21:34:14 +00: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 01:59:43 +00:00
}
}
.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? {
2021-01-17 20:24:50 +00: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 00:52:46 +00:00
}
}
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