-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite the Change Password view in SwiftUI
Use the Handler and MLPromise frameworks And only display the old password if it was autogenerated
- Loading branch information
1 parent
7f985a0
commit 60f8015
Showing
10 changed files
with
182 additions
and
269 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// | ||
// ChangePassword.swift | ||
// Monal | ||
// | ||
// Created by lissine on 2/8/2024. | ||
// Copyright © 2024 monal-im.org. All rights reserved. | ||
// | ||
|
||
struct ChangePassword: View { | ||
@Environment(\.dismiss) private var dismiss | ||
|
||
@State private var oldPass = "" | ||
@State private var newPass = "" | ||
|
||
@State private var showAlert = false | ||
@State private var alertPrompt = AlertPrompt(dismissLabel: Text("Close")) | ||
|
||
@StateObject private var overlay = LoadingOverlayState() | ||
|
||
let accountID: NSNumber | ||
|
||
private func errorAlert(title: Text, message: Text = Text("")) { | ||
alertPrompt.title = title | ||
alertPrompt.message = message | ||
showAlert = true | ||
alertPrompt.dismissCallback = nil | ||
} | ||
private func successAlert(title: Text, message: Text) { | ||
alertPrompt.title = title | ||
alertPrompt.message = message | ||
showAlert = true | ||
alertPrompt.dismissCallback = { | ||
dismiss() | ||
} | ||
} | ||
private func passwordChangeProcessing() { | ||
guard let account = MLXMPPManager.sharedInstance().getEnabledAccount(forID: accountID) else { | ||
errorAlert( | ||
title: Text("Account Disabled"), | ||
message: Text("Please enable your account before changing its password.") | ||
) | ||
return | ||
} | ||
|
||
guard MLXMPPManager.sharedInstance().isValidPassword(oldPass, forAccount: accountID) else { | ||
errorAlert( | ||
title: Text("Wrong Password!"), | ||
message: Text("The current password is not correct.") | ||
) | ||
return | ||
} | ||
|
||
showPromisingLoadingOverlay(overlay, headlineView: Text("Changing Password"), descriptionView: Text("")) { | ||
account.changePassword(newPass) | ||
} | ||
.done { _ in | ||
successAlert(title: Text("Success"), message: Text("The password has been changed")) | ||
} | ||
.catch { error in | ||
errorAlert(title: Text("Error"), message: Text(error.localizedDescription)) | ||
} | ||
|
||
} | ||
|
||
var body: some View { | ||
Form { | ||
Section(header: Text("Enter your new password. Passwords may not be empty. They may also be governed by server or company policies.")) { | ||
#if IS_QUICKSY | ||
if HelperTools.defaultsDB().bool(forKey: "autogeneratedPassword") { | ||
TextField(NSLocalizedString("Current Password", comment: ""), text: $oldPass) | ||
.textInputAutocapitalization(.never) | ||
.autocorrectionDisabled() | ||
.onAppear { | ||
oldPass = MLXMPPManager.sharedInstance().getPasswordForAccount(accountID) | ||
} | ||
} else { | ||
SecureField(NSLocalizedString("Current Password", comment: ""), text: $oldPass) | ||
} | ||
#else | ||
SecureField(NSLocalizedString("Current Password", comment: ""), text: $oldPass) | ||
#endif | ||
SecureField(NSLocalizedString("New Password", comment: ""), text: $newPass) | ||
} | ||
|
||
Section { | ||
Button(action: passwordChangeProcessing) { | ||
Text("Change Password") | ||
.frame(maxWidth: .infinity, alignment: .center) | ||
} | ||
.disabled(oldPass.isEmpty || newPass.isEmpty) | ||
} | ||
|
||
} | ||
.alert( | ||
alertPrompt.title, | ||
isPresented: $showAlert, | ||
actions: { Button(NSLocalizedString("Close", comment: ""), action: alertPrompt.dismissCallback ?? {}) }, | ||
message: { alertPrompt.message } | ||
) | ||
.navigationTitle(Text("Change Password")) | ||
.navigationBarTitleDisplayMode(NavigationBarItem.TitleDisplayMode.inline) | ||
.addLoadingOverlay(overlay) | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.