-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
took the opportunity to modernize the screen a bit background color of yellow, loading indicator and twaek the version shown in dark mode
- Loading branch information
Showing
3 changed files
with
172 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Part of BeeSwift. Copyright Beeminder | ||
|
||
import SwiftUI | ||
import BeeKit | ||
|
||
struct SignInView: View { | ||
@StateObject private var viewModel = SignInViewModel() | ||
@State private var showingFailedSignInAlert = false | ||
@State private var showingMissingDataAlert = false | ||
@Environment(\.dismiss) private var dismiss | ||
|
||
var body: some View { | ||
ZStack { | ||
Color.yellow | ||
.opacity(0.8) | ||
.ignoresSafeArea() | ||
|
||
HStack(alignment: .center) { | ||
|
||
VStack(alignment: .center, spacing: 15) { | ||
Spacer() | ||
|
||
Image("website_logo_mid") | ||
.resizable() | ||
.scaledToFit() | ||
.frame(maxWidth: 200) | ||
.padding(.bottom) | ||
|
||
VStack(spacing: 15) { | ||
TextField("Email or username", text: $viewModel.email) | ||
.textFieldStyle(RoundedBorderTextFieldStyle()) | ||
.textInputAutocapitalization(.never) | ||
.autocorrectionDisabled() | ||
.keyboardType(.emailAddress) | ||
.textContentType(.emailAddress) | ||
.submitLabel(.next) | ||
|
||
SecureField("Password", text: $viewModel.password) | ||
.textFieldStyle(RoundedBorderTextFieldStyle()) | ||
.textInputAutocapitalization(.never) | ||
.submitLabel(.done) | ||
|
||
Button(action: signIn) { | ||
Text("Sign In") | ||
.font(.system(size: 20)) | ||
.foregroundColor(.white) | ||
.frame(maxWidth: .infinity) | ||
.frame(height: 44) | ||
} | ||
.background(Color("BeeminderGray")) | ||
.cornerRadius(8) | ||
.disabled(viewModel.isLoading) | ||
} | ||
.frame(maxWidth: .infinity) | ||
.padding(.horizontal, 40) | ||
|
||
Spacer() | ||
Spacer() | ||
Spacer() | ||
} | ||
.padding() | ||
|
||
} | ||
.alert("Could not sign in", isPresented: $showingFailedSignInAlert) { | ||
Button("OK", role: .cancel) { } | ||
} message: { | ||
Text("Invalid credentials") | ||
} | ||
.alert("Incomplete Account Details", isPresented: $showingMissingDataAlert) { | ||
Button("OK", role: .cancel) { } | ||
} message: { | ||
Text("Username and Password are required") | ||
} | ||
.onReceive(NotificationCenter.default.publisher(for: Notification.Name(CurrentUserManager.failedSignInNotificationName))) { _ in | ||
viewModel.isLoading = false | ||
showingFailedSignInAlert = true | ||
} | ||
.onReceive(NotificationCenter.default.publisher(for: Notification.Name(CurrentUserManager.signedInNotificationName))) { _ in | ||
viewModel.isLoading = false | ||
dismiss() | ||
} | ||
} | ||
.overlay { | ||
if viewModel.isLoading { | ||
ProgressView() | ||
.scaleEffect(1.5) | ||
.frame(maxWidth: .infinity, maxHeight: .infinity) | ||
.background(Color.black.opacity(0.2)) | ||
} | ||
} | ||
} | ||
|
||
private func signIn() { | ||
guard !viewModel.email.trimmingCharacters(in: .whitespaces).isEmpty, | ||
!viewModel.password.isEmpty else { | ||
showingMissingDataAlert = true | ||
return | ||
} | ||
|
||
Task { | ||
await viewModel.signIn() | ||
} | ||
} | ||
} | ||
|
||
@MainActor | ||
class SignInViewModel: ObservableObject { | ||
@Published var email = "" | ||
@Published var password = "" | ||
@Published var isLoading = false | ||
|
||
func signIn() async { | ||
isLoading = true | ||
|
||
// Delay the task by 3 seconds: | ||
do { | ||
try await Task.sleep(nanoseconds: 3_000_000_000) | ||
} catch { | ||
print("canceled early") | ||
} | ||
|
||
await ServiceLocator.currentUserManager.signInWithEmail( | ||
email.trimmingCharacters(in: .whitespaces), | ||
password: password | ||
) | ||
} | ||
} | ||
|
||
#Preview { | ||
SignInView() | ||
} |