Source code for the WriteFreely SwiftUI app for iOS, iPadOS, and macOS
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

100 righe
3.7 KiB

  1. import SwiftUI
  2. struct AccountLoginView: View {
  3. @EnvironmentObject var model: WriteFreelyModel
  4. @EnvironmentObject var errorHandling: ErrorHandling
  5. @State private var alertMessage: String = ""
  6. @State private var username: String = ""
  7. @State private var password: String = ""
  8. @State private var server: String = ""
  9. var body: some View {
  10. VStack {
  11. Text("Log in to publish and share your posts.")
  12. .font(.caption)
  13. .foregroundColor(.secondary)
  14. HStack {
  15. Image(systemName: "person.circle")
  16. .foregroundColor(.gray)
  17. #if os(iOS)
  18. TextField("Username", text: $username)
  19. .autocapitalization(.none)
  20. .disableAutocorrection(true)
  21. .textFieldStyle(RoundedBorderTextFieldStyle())
  22. #else
  23. TextField("Username", text: $username)
  24. #endif
  25. }
  26. HStack {
  27. Image(systemName: "lock.circle")
  28. .foregroundColor(.gray)
  29. #if os(iOS)
  30. SecureField("Password", text: $password)
  31. .autocapitalization(.none)
  32. .disableAutocorrection(true)
  33. .textFieldStyle(RoundedBorderTextFieldStyle())
  34. #else
  35. SecureField("Password", text: $password)
  36. #endif
  37. }
  38. HStack {
  39. Image(systemName: "link.circle")
  40. .foregroundColor(.gray)
  41. #if os(iOS)
  42. TextField("Server URL", text: $server)
  43. .keyboardType(.URL)
  44. .autocapitalization(.none)
  45. .disableAutocorrection(true)
  46. .textFieldStyle(RoundedBorderTextFieldStyle())
  47. #else
  48. TextField("Server URL", text: $server)
  49. #endif
  50. }
  51. Spacer()
  52. if model.isLoggingIn {
  53. ProgressView("Logging in...")
  54. .padding()
  55. } else {
  56. Button(action: {
  57. #if os(iOS)
  58. hideKeyboard()
  59. #endif
  60. // If the server string is not prefixed with a scheme, prepend "https://" to it.
  61. if !(server.hasPrefix("https://") || server.hasPrefix("http://")) {
  62. server = "https://\(server)"
  63. }
  64. // We only need the protocol and host from the URL, so drop anything else.
  65. let url = URLComponents(string: server)
  66. if let validURL = url {
  67. let scheme = validURL.scheme
  68. let host = validURL.host
  69. var hostURL = URLComponents()
  70. hostURL.scheme = scheme
  71. hostURL.host = host
  72. server = hostURL.string ?? server
  73. model.login(
  74. to: URL(string: server)!,
  75. as: username, password: password
  76. )
  77. } else {
  78. self.errorHandling.handle(error: AccountError.invalidServerURL)
  79. }
  80. }, label: {
  81. Text("Log In")
  82. })
  83. .disabled(
  84. model.account.isLoggedIn || (username.isEmpty || password.isEmpty || server.isEmpty)
  85. )
  86. .padding()
  87. }
  88. }
  89. }
  90. }
  91. struct AccountLoginView_Previews: PreviewProvider {
  92. static var previews: some View {
  93. AccountLoginView()
  94. .environmentObject(WriteFreelyModel())
  95. }
  96. }