Instance and account form and validation improvements

This commit is contained in:
Arkadiusz Fal 2022-12-17 15:57:59 +01:00
parent 9f3a94137c
commit cf0572a94b
4 changed files with 109 additions and 53 deletions

View File

@ -82,14 +82,24 @@ final class AccountValidator: Service {
}
guard !response.json.isEmpty else {
guard let app = self.appsToValidateInstance.popLast() else {
if app == .piped {
if response.text.contains("property=\"og:title\" content=\"Piped\"") {
self.isValid.wrappedValue = false
self.isValidated.wrappedValue = true
self.isValidating.wrappedValue = false
self.error?.wrappedValue = "Trying to use Piped front-end URL, you need to use URL for Piped API instead"
return
}
}
guard let nextApp = self.appsToValidateInstance.popLast() else {
self.isValid.wrappedValue = false
self.isValidated.wrappedValue = true
self.isValidating.wrappedValue = false
return
}
self.tryValidatingUsing(app)
self.tryValidatingUsing(nextApp)
return
}
@ -128,15 +138,15 @@ final class AccountValidator: Service {
switch app.wrappedValue {
case .invidious:
invidiousValidation()
validateInvidiousAccount()
case .piped:
pipedValidation()
validatePipedAccount()
default:
setValidationResult(false)
}
}
func invidiousValidation() {
func validateInvidiousAccount() {
guard let username = account?.username,
let password = account?.password
else {
@ -177,7 +187,7 @@ final class AccountValidator: Service {
}
}
func pipedValidation() {
func validatePipedAccount() {
guard let request = accountRequest else {
setValidationResult(false)

View File

@ -24,6 +24,13 @@ struct AccountForm: View {
Group {
header
form
#if os(macOS)
VStack {
validationStatus
}
.frame(minHeight: 60, alignment: .topLeading)
.padding(.horizontal, 15)
#endif
footer
}
.frame(maxWidth: 1000)
@ -34,7 +41,8 @@ struct AccountForm: View {
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.background(Color.background(scheme: colorScheme))
#else
.frame(width: 400, height: 145)
.frame(width: 400, height: 180)
.padding(.vertical)
#endif
}
@ -72,32 +80,36 @@ struct AccountForm: View {
.onChange(of: password) { _ in validate() }
}
var formFields: some View {
Group {
TextField("Username", text: $username)
SecureField("Password", text: $password)
}
@ViewBuilder var formFields: some View {
TextField("Username", text: $username)
SecureField("Password", text: $password)
#if os(tvOS)
VStack {
validationStatus
}
.frame(minHeight: 100)
#elseif os(iOS)
validationStatus
#endif
}
var usernamePrompt: String {
switch instance.app {
case .invidious:
return "SID Cookie"
default:
return "Username"
@ViewBuilder var validationStatus: some View {
if !username.isEmpty && !password.isEmpty {
Section {
AccountValidationStatus(
app: .constant(instance.app),
isValid: $isValid,
isValidated: $isValidated,
isValidating: $isValidating,
error: $validationError
)
}
}
}
var footer: some View {
HStack {
AccountValidationStatus(
app: .constant(instance.app),
isValid: $isValid,
isValidated: $isValidated,
isValidating: $isValidating,
error: $validationError
)
Spacer()
Button("Save", action: submitForm)

View File

@ -9,28 +9,41 @@ struct AccountValidationStatus: View {
@Binding var error: String?
var body: some View {
HStack(spacing: 4) {
Image(systemName: validationStatusSystemImage)
.foregroundColor(validationStatusColor)
VStack(alignment: .leading) {
HStack {
Image(systemName: validationStatusSystemImage)
.foregroundColor(validationStatusColor)
.imageScale(.medium)
.opacity(isValidating ? 1 : (isValidated ? 1 : 0))
.frame(minWidth: 35, minHeight: 35)
.opacity(isValidating ? 1 : (isValidated ? 1 : 0))
VStack(alignment: .leading) {
Text(isValid ? "Connected successfully (\(app?.name ?? "Unknown"))" : "Connection failed")
if let error, !isValid {
Text(error)
.font(.caption2)
.foregroundColor(.secondary)
.truncationMode(.tail)
.lineLimit(1)
}
.opacity(isValidated && !isValidating ? 1 : 0)
}
if errorVisible {
Text(error ?? "")
.frame(maxWidth: .infinity, alignment: .leading)
.lineLimit(nil)
.multilineTextAlignment(.leading)
.font(.footnote)
.foregroundColor(.secondary)
.padding(.top, 5)
.opacity(errorTextVisible ? 1 : 0)
}
.frame(minHeight: 35)
.opacity(isValidating ? 0 : (isValidated ? 1 : 0))
}
}
var errorVisible: Bool {
#if !os(iOS)
true
#else
errorTextVisible
#endif
}
var errorTextVisible: Bool {
error != nil && isValidated && !isValid && !isValidating
}
var validationStatusSystemImage: String {
if isValidating {
return "bolt.horizontal.fill"

View File

@ -22,9 +22,14 @@ struct InstanceForm: View {
VStack(alignment: .leading) {
Group {
header
form
#if os(macOS)
VStack {
validationStatus
}
.frame(minHeight: 60, alignment: .topLeading)
.padding(.horizontal, 15)
#endif
footer
}
.frame(maxWidth: 1000)
@ -36,7 +41,8 @@ struct InstanceForm: View {
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.background(Color.background(scheme: colorScheme))
#else
.frame(width: 400, height: 150)
.frame(width: 400, height: 180)
.padding(.vertical)
#endif
}
@ -80,19 +86,34 @@ struct InstanceForm: View {
.autocapitalization(.none)
.keyboardType(.URL)
#endif
#if os(tvOS)
VStack {
validationStatus
}
.frame(minHeight: 100)
#elseif os(iOS)
validationStatus
#endif
}
}
@ViewBuilder var validationStatus: some View {
if !url.isEmpty {
Section {
AccountValidationStatus(
app: $app,
isValid: $isValid,
isValidated: $isValidated,
isValidating: $isValidating,
error: $validationError
)
}
}
}
private var footer: some View {
HStack {
AccountValidationStatus(
app: $app,
isValid: $isValid,
isValidated: $isValidated,
isValidating: $isValidating,
error: $validationError
)
Spacer()
Button("Save", action: submitForm)