diff --git a/Shared/Account/AccountLoginView.swift b/Shared/Account/AccountLoginView.swift index c7ea5ea..64e7971 100644 --- a/Shared/Account/AccountLoginView.swift +++ b/Shared/Account/AccountLoginView.swift @@ -1,109 +1,99 @@ import SwiftUI struct AccountLoginView: View { @EnvironmentObject var model: WriteFreelyModel @EnvironmentObject var errorHandling: ErrorHandling @State private var alertMessage: String = "" @State private var username: String = "" @State private var password: String = "" @State private var server: String = "" var body: some View { VStack { Text("Log in to publish and share your posts.") .font(.caption) .foregroundColor(.secondary) HStack { Image(systemName: "person.circle") .foregroundColor(.gray) #if os(iOS) TextField("Username", text: $username) .autocapitalization(.none) .disableAutocorrection(true) .textFieldStyle(RoundedBorderTextFieldStyle()) #else TextField("Username", text: $username) #endif } HStack { Image(systemName: "lock.circle") .foregroundColor(.gray) #if os(iOS) SecureField("Password", text: $password) .autocapitalization(.none) .disableAutocorrection(true) .textFieldStyle(RoundedBorderTextFieldStyle()) #else SecureField("Password", text: $password) #endif } HStack { Image(systemName: "link.circle") .foregroundColor(.gray) #if os(iOS) TextField("Server URL", text: $server) .keyboardType(.URL) .autocapitalization(.none) .disableAutocorrection(true) .textFieldStyle(RoundedBorderTextFieldStyle()) #else TextField("Server URL", text: $server) #endif } Spacer() if model.isLoggingIn { ProgressView("Logging in...") .padding() } else { Button(action: { #if os(iOS) hideKeyboard() #endif // If the server string is not prefixed with a scheme, prepend "https://" to it. if !(server.hasPrefix("https://") || server.hasPrefix("http://")) { server = "https://\(server)" } // We only need the protocol and host from the URL, so drop anything else. let url = URLComponents(string: server) if let validURL = url { let scheme = validURL.scheme let host = validURL.host var hostURL = URLComponents() hostURL.scheme = scheme hostURL.host = host server = hostURL.string ?? server model.login( to: URL(string: server)!, as: username, password: password ) } else { self.errorHandling.handle(error: AccountError.invalidServerURL) } }, label: { Text("Log In") }) .disabled( model.account.isLoggedIn || (username.isEmpty || password.isEmpty || server.isEmpty) ) .padding() } } - .onChange(of: model.hasError) { value in - if value { - if let error = model.currentError { - self.errorHandling.handle(error: error) - } else { - self.errorHandling.handle(error: AppError.genericError) - } - model.hasError = false - } - } } } struct AccountLoginView_Previews: PreviewProvider { static var previews: some View { AccountLoginView() .environmentObject(WriteFreelyModel()) } } diff --git a/Shared/Account/AccountView.swift b/Shared/Account/AccountView.swift index 9241026..0a2546e 100644 --- a/Shared/Account/AccountView.swift +++ b/Shared/Account/AccountView.swift @@ -1,27 +1,39 @@ import SwiftUI struct AccountView: View { @EnvironmentObject var model: WriteFreelyModel + @EnvironmentObject var errorHandling: ErrorHandling var body: some View { if model.account.isLoggedIn { HStack { Spacer() AccountLogoutView() Spacer() } .padding() } else { AccountLoginView() .withErrorHandling() .padding(.top) } + EmptyView() + .onChange(of: model.hasError) { value in + if value { + if let error = model.currentError { + self.errorHandling.handle(error: error) + } else { + self.errorHandling.handle(error: AppError.genericError) + } + model.hasError = false + } + } } } struct AccountLogin_Previews: PreviewProvider { static var previews: some View { AccountView() .environmentObject(WriteFreelyModel()) } } diff --git a/iOS/Settings/SettingsView.swift b/iOS/Settings/SettingsView.swift index e64bc94..28c55a6 100644 --- a/iOS/Settings/SettingsView.swift +++ b/iOS/Settings/SettingsView.swift @@ -1,66 +1,67 @@ import SwiftUI struct SettingsView: View { @EnvironmentObject var model: WriteFreelyModel var body: some View { VStack { SettingsHeaderView() Form { Section(header: Text("Login Details")) { AccountView() + .withErrorHandling() } Section(header: Text("Appearance")) { PreferencesView(preferences: model.preferences) } Section(header: Text("External Links")) { HStack { Spacer() Link("View the Guide", destination: model.howToURL) Spacer() } HStack { Spacer() Link("Visit the Help Forum", destination: model.helpURL) Spacer() } HStack { Spacer() Link("Write a Review on the App Store", destination: model.reviewURL) Spacer() } } Section(header: Text("Acknowledgements")) { VStack { VStack(alignment: .leading) { Text("This application makes use of the following open-source projects:") .padding(.bottom) Text("• Lora typeface") .padding(.leading) Text("• Open Sans typeface") .padding(.leading) Text("• Hack typeface") .padding(.leading) } .padding(.bottom) .foregroundColor(.secondary) HStack { Spacer() Link("View the licenses", destination: model.licensesURL) Spacer() } } .padding() } } } // .preferredColorScheme(preferences.selectedColorScheme) // See PreferencesModel for info. } } struct SettingsView_Previews: PreviewProvider { static var previews: some View { SettingsView() .environmentObject(WriteFreelyModel()) } }