NetNewsWire/Multiplatform/Shared/Add/Add Account Sheets/AddReaderAPIAccountView.swift

148 lines
3.5 KiB
Swift
Raw Normal View History

2020-12-04 02:15:37 +01:00
//
// AddReaderAPIAccountView.swift
// Multiplatform macOS
//
// Created by Stuart Breckenridge on 03/12/2020.
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import SwiftUI
import Account
2020-12-05 14:09:34 +01:00
import RSCore
import RSWeb
import Secrets
2020-12-04 02:15:37 +01:00
struct AddReaderAPIAccountView: View {
@Environment (\.presentationMode) var presentationMode
2020-12-05 14:09:34 +01:00
@StateObject private var model = AddReaderAPIViewModel()
2020-12-04 02:15:37 +01:00
public var accountType: AccountType
2020-12-05 14:09:34 +01:00
var body: some View {
2020-12-05 15:58:11 +01:00
#if os(macOS)
macBody
#else
iosBody
#endif
}
#if os(iOS)
var iosBody: some View {
Text("TBC")
}
#endif
#if os(macOS)
var macBody: some View {
2020-12-04 02:15:37 +01:00
VStack {
HStack(spacing: 16) {
VStack(alignment: .leading) {
accountType.image()
.resizable()
.frame(width: 50, height: 50)
Spacer()
}
VStack(alignment: .leading, spacing: 8) {
Text("Sign in to your \(accountType.localizedAccountName()) account.")
.font(.headline)
HStack {
if accountType == .freshRSS {
Text("Don't have a \(accountType.localizedAccountName()) instance?")
.font(.callout)
} else {
Text("Don't have an \(accountType.localizedAccountName()) account?")
.font(.callout)
}
Button(action: {
model.presentSignUpOption(accountType)
2020-12-04 02:15:37 +01:00
}, label: {
Text(accountType == .freshRSS ? "Find out more." : "Sign up here.").font(.callout)
}).buttonStyle(LinkButtonStyle())
}
HStack {
VStack(alignment: .trailing, spacing: 14) {
Text("Email")
Text("Password")
2020-12-05 14:09:34 +01:00
if accountType == .freshRSS {
Text("API URL")
}
2020-12-04 02:15:37 +01:00
}
VStack(spacing: 8) {
2020-12-05 14:09:34 +01:00
TextField("me@email.com", text: $model.username)
SecureField("•••••••••••", text: $model.password)
if accountType == .freshRSS {
TextField("https://myfreshrss.rocks", text: $model.apiUrl)
}
2020-12-04 02:15:37 +01:00
}
}
Text("Your username and password will be encrypted and stored in Keychain.")
.foregroundColor(.secondary)
.font(.callout)
.lineLimit(2)
.padding(.top, 4)
Spacer()
HStack(spacing: 8) {
Spacer()
2020-12-05 14:09:34 +01:00
ProgressView()
.scaleEffect(CGSize(width: 0.5, height: 0.5))
.hidden(!model.isAuthenticating)
2020-12-04 02:15:37 +01:00
Button(action: {
presentationMode.wrappedValue.dismiss()
}, label: {
Text("Cancel")
.frame(width: 60)
}).keyboardShortcut(.cancelAction)
2020-12-05 14:09:34 +01:00
2020-12-04 02:15:37 +01:00
Button(action: {
2020-12-05 15:18:10 +01:00
model.authenticateReaderAccount(accountType)
2020-12-04 02:15:37 +01:00
}, label: {
2020-12-05 14:09:34 +01:00
Text("Sign In")
2020-12-04 02:15:37 +01:00
.frame(width: 60)
})
.keyboardShortcut(.defaultAction)
2020-12-05 14:09:34 +01:00
.disabled(createDisabled())
2020-12-04 02:15:37 +01:00
}
}
}
}
.padding()
2020-12-05 14:09:34 +01:00
.frame(width: 400, height: height())
2020-12-04 02:15:37 +01:00
.textFieldStyle(RoundedBorderTextFieldStyle())
2020-12-05 14:09:34 +01:00
.alert(isPresented: $model.showError, content: {
Alert(title: Text("Sign In Error"), message: Text(model.accountUpdateError.description), dismissButton: .cancel())
})
2020-12-05 15:18:10 +01:00
.onReceive(model.$canDismiss, perform: { value in
if value == true {
presentationMode.wrappedValue.dismiss()
}
})
2020-12-05 14:09:34 +01:00
}
2020-12-05 15:58:11 +01:00
#endif
2020-12-04 02:15:37 +01:00
2020-12-05 14:09:34 +01:00
func createDisabled() -> Bool {
if accountType == .freshRSS {
return model.username.isEmpty || model.password.isEmpty || !model.apiUrl.mayBeURL
}
return model.username.isEmpty || model.password.isEmpty
}
func height() -> CGFloat {
if accountType == .freshRSS {
return 260
}
return 230
}
2020-12-04 02:15:37 +01:00
}
struct AddReaderAPIAccountView_Previews: PreviewProvider {
2020-12-05 14:09:34 +01:00
static var previews: some View {
2020-12-04 02:15:37 +01:00
AddReaderAPIAccountView(accountType: .freshRSS)
2020-12-05 14:09:34 +01:00
}
2020-12-04 02:15:37 +01:00
}