Refactor login view out of AccountView

This commit is contained in:
Angelo Stavrow 2020-08-10 13:31:46 -04:00
parent 4de5293345
commit a7b918c14b
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
3 changed files with 111 additions and 74 deletions

View File

@ -0,0 +1,102 @@
import SwiftUI
struct AccountLoginView: View {
@State private var username: String = ""
@State private var password: String = ""
@State private var server: String = ""
@State private var isShowingAlert: Bool = false
@State private var alertMessage: String = ""
@Binding var accountModel: AccountModel
@Binding var isLoggedIn: Bool
@Binding var isLoggingIn: Bool
var body: some View {
VStack {
HStack {
Image(systemName: "person.circle")
.foregroundColor(.gray)
TextField("Username", text: $username)
}
HStack {
Image(systemName: "lock.circle")
.foregroundColor(.gray)
SecureField("Password", text: $password)
}
HStack {
Image(systemName: "link.circle")
.foregroundColor(.gray)
TextField("Server URL", text: $server)
}
Spacer()
if isLoggingIn {
ProgressView("Logging in...")
} else {
Button(action: {
accountModel.login(
to: server,
as: username, password: password,
completion: loginHandler
)
isLoggingIn = true
}, label: {
Text("Login")
}).disabled(isLoggedIn)
}
}
.alert(isPresented: $isShowingAlert) {
Alert(
title: Text("Error Logging In"),
message: Text(alertMessage),
dismissButton: .default(Text("OK"))
)
}
}
func loginHandler(result: Result<UUID, AccountError>) {
do {
_ = try result.get()
isLoggedIn = true
isLoggingIn = false
} catch AccountError.serverNotFound {
alertMessage = """
The server could not be found. Please check that you've entered the information correctly and try again.
"""
isLoggedIn = false
isLoggingIn = false
isShowingAlert = true
} catch AccountError.invalidCredentials {
alertMessage = """
Invalid username or password. Please check that you've entered the information correctly and try again.
"""
isLoggedIn = false
isLoggingIn = false
isShowingAlert = true
} catch {
alertMessage = "An unknown error occurred. Please try again."
isLoggedIn = false
isLoggingIn = false
isShowingAlert = true
}
}
}
struct AccountLoginView_LoggedOutPreviews: PreviewProvider {
static var previews: some View {
AccountLoginView(
accountModel: .constant(AccountModel()),
isLoggedIn: .constant(false),
isLoggingIn: .constant(false)
)
}
}
struct AccountLoginView_LoggingInPreviews: PreviewProvider {
static var previews: some View {
AccountLoginView(
accountModel: .constant(AccountModel()),
isLoggedIn: .constant(false),
isLoggingIn: .constant(true)
)
}
}

View File

@ -1,90 +1,19 @@
import SwiftUI
struct AccountView: View {
@State var accountModel = AccountModel()
@State private var username: String = ""
@State private var password: String = ""
@State private var server: String = ""
@State private var accountModel = AccountModel()
@State private var isLoggingIn: Bool = false
@State private var isLoggedIn: Bool = false
@State private var isShowingAlert: Bool = false
@State private var alertMessage: String = ""
var body: some View {
if isLoggedIn {
AccountLogoutView(accountModel: $accountModel, isLoggedIn: $isLoggedIn, isLoggingIn: $isLoggingIn)
} else {
VStack {
HStack {
Image(systemName: "person.circle")
.foregroundColor(.gray)
TextField("Username", text: $username)
}
HStack {
Image(systemName: "lock.circle")
.foregroundColor(.gray)
SecureField("Password", text: $password)
}
HStack {
Image(systemName: "link.circle")
.foregroundColor(.gray)
TextField("Server URL", text: $server)
}
Spacer()
if isLoggingIn {
ProgressView("Logging in...")
} else {
Button(action: {
accountModel.login(
to: server,
as: username, password: password,
completion: loginHandler
)
isLoggingIn = true
}, label: {
Text("Login")
}).disabled(isLoggedIn)
}
}
.alert(isPresented: $isShowingAlert) {
Alert(
title: Text("Error Logging In"),
message: Text(alertMessage),
dismissButton: .default(Text("OK"))
)
}
AccountLoginView(accountModel: $accountModel, isLoggedIn: $isLoggedIn, isLoggingIn: $isLoggingIn)
}
}
func loginHandler(result: Result<UUID, AccountError>) {
do {
_ = try result.get()
isLoggedIn = true
isLoggingIn = false
} catch AccountError.serverNotFound {
alertMessage = """
The server could not be found. Please check that you've entered the information correctly and try again.
"""
isLoggedIn = false
isLoggingIn = false
isShowingAlert = true
} catch AccountError.invalidCredentials {
alertMessage = """
Invalid username or password. Please check that you've entered the information correctly and try again.
"""
isLoggedIn = false
isLoggingIn = false
isShowingAlert = true
} catch {
alertMessage = "An unknown error occurred. Please try again."
isLoggedIn = false
isLoggingIn = false
isShowingAlert = true
}
}
}
struct AccountLogin_Previews: PreviewProvider {
static var previews: some View {
AccountView()

View File

@ -13,6 +13,8 @@
17120DA724E19D11002B9F6C /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17120DA424E19CBF002B9F6C /* SettingsView.swift */; };
17120DA924E1B2F5002B9F6C /* AccountLogoutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17120DA824E1B2F5002B9F6C /* AccountLogoutView.swift */; };
17120DAA24E1B2F5002B9F6C /* AccountLogoutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17120DA824E1B2F5002B9F6C /* AccountLogoutView.swift */; };
17120DAC24E1B99F002B9F6C /* AccountLoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17120DAB24E1B99F002B9F6C /* AccountLoginView.swift */; };
17120DAD24E1B99F002B9F6C /* AccountLoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17120DAB24E1B99F002B9F6C /* AccountLoginView.swift */; };
171BFDF724D49FD400888236 /* PostCollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 171BFDF624D49FD400888236 /* PostCollection.swift */; };
171BFDF824D49FD400888236 /* PostCollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 171BFDF624D49FD400888236 /* PostCollection.swift */; };
171BFDFA24D4AF8300888236 /* CollectionListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 171BFDF924D4AF8300888236 /* CollectionListView.swift */; };
@ -68,6 +70,7 @@
/* Begin PBXFileReference section */
17120DA424E19CBF002B9F6C /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
17120DA824E1B2F5002B9F6C /* AccountLogoutView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountLogoutView.swift; sourceTree = "<group>"; };
17120DAB24E1B99F002B9F6C /* AccountLoginView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountLoginView.swift; sourceTree = "<group>"; };
171BFDF624D49FD400888236 /* PostCollection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostCollection.swift; sourceTree = "<group>"; };
171BFDF924D4AF8300888236 /* CollectionListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionListView.swift; sourceTree = "<group>"; };
1756AE6A24CB1E4B00FD7257 /* Post.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Post.swift; sourceTree = "<group>"; };
@ -272,6 +275,7 @@
17A5388B24DDC83F00DEFF9A /* AccountModel.swift */,
17A5388D24DDEC7400DEFF9A /* AccountView.swift */,
17120DA824E1B2F5002B9F6C /* AccountLogoutView.swift */,
17120DAB24E1B99F002B9F6C /* AccountLoginView.swift */,
);
path = Account;
sourceTree = "<group>";
@ -505,6 +509,7 @@
files = (
17DF32AC24C87D3500BCE2E3 /* ContentView.swift in Sources */,
1756AE8124CB844500FD7257 /* View+Keyboard.swift in Sources */,
17120DAC24E1B99F002B9F6C /* AccountLoginView.swift in Sources */,
17120DA924E1B2F5002B9F6C /* AccountLogoutView.swift in Sources */,
171BFDFA24D4AF8300888236 /* CollectionListView.swift in Sources */,
1756AE7724CB2EDD00FD7257 /* PostEditor.swift in Sources */,
@ -533,6 +538,7 @@
1756AE7824CB2EDD00FD7257 /* PostEditor.swift in Sources */,
17120DAA24E1B2F5002B9F6C /* AccountLogoutView.swift in Sources */,
17DF32D624C8CA3400BCE2E3 /* PostStatusBadge.swift in Sources */,
17120DAD24E1B99F002B9F6C /* AccountLoginView.swift in Sources */,
1756AE7B24CB65DF00FD7257 /* PostList.swift in Sources */,
171BFDFB24D4AF8300888236 /* CollectionListView.swift in Sources */,
17DF32AB24C87D3500BCE2E3 /* WriteFreely_MultiPlatformApp.swift in Sources */,