metatext/Views/AddIdentityView.swift
Justin Mazzocchi fa162fde77
Filter changes
2021-01-17 13:14:24 -08:00

95 lines
3.6 KiB
Swift

// Copyright © 2020 Metabolist. All rights reserved.
import Kingfisher
import SwiftUI
import ViewModels
struct AddIdentityView: View {
@StateObject var viewModel: AddIdentityViewModel
@Environment(\.accessibilityReduceMotion) var accessibilityReduceMotion
@EnvironmentObject var rootViewModel: RootViewModel
var body: some View {
Form {
Section {
TextField("add-identity.instance-url", text: $viewModel.urlFieldText)
.textContentType(.URL)
.autocapitalization(.none)
.disableAutocorrection(true)
.keyboardType(.URL)
if let instance = viewModel.instance {
VStack(alignment: .center) {
KFImage(instance.thumbnail)
.placeholder {
ProgressView()
}
.resizable()
.aspectRatio(16 / 9, contentMode: .fill)
.background(Color.blue)
Spacer()
Text(instance.title)
.font(.headline)
Text(instance.uri)
.font(.subheadline)
.foregroundColor(.secondary)
Spacer()
}
.frame(maxWidth: .infinity, alignment: .center)
.listRowInsets(EdgeInsets())
}
Group {
if viewModel.loading {
ProgressView()
} else {
Button("add-identity.log-in",
action: viewModel.logInTapped)
if viewModel.isPublicTimelineAvailable {
Button("add-identity.browse", action: viewModel.browseTapped)
}
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)))
}
}
}
.frame(maxWidth: .infinity, alignment: .center)
}
}
.animation(.default, if: !accessibilityReduceMotion)
.alertItem($viewModel.alertItem)
.onAppear(perform: viewModel.refreshFilter)
}
}
extension AddIdentityError: LocalizedError {
public var errorDescription: String? {
switch self {
case .unableToConnectToInstance:
return NSLocalizedString("add-identity.unable-to-connect-to-instance", comment: "")
case .instanceNotSupported:
return NSLocalizedString("add-identity.instance-not-supported", comment: "")
}
}
}
#if DEBUG
import PreviewViewModels
struct AddAccountView_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
AddIdentityView(viewModel: RootViewModel.preview.addIdentityViewModel())
.navigationBarTitleDisplayMode(.inline)
}
}
}
#endif