Skip to content

Commit

Permalink
Rewrite the Change Password view in SwiftUI
Browse files Browse the repository at this point in the history
  • Loading branch information
lissine0 committed Sep 15, 2024
1 parent 4ff03a6 commit 7071d13
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 243 deletions.
98 changes: 98 additions & 0 deletions Monal/Classes/ChangePassword.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//
// ChangePassword.swift
// Monal
//
// Created by lissine on 2/8/2024.
// Copyright © 2024 monal-im.org. All rights reserved.
//

struct ChangePassword: View {
@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
}
private func successAlert(title: Text, message: Text) {
alertPrompt.title = title
alertPrompt.message = message
showAlert = true
}
private func passwordChangeProcessing() {
guard let account = MLXMPPManager.sharedInstance().getEnabledAccount(forID: accountID) else {
errorAlert(title: Text("Account Offline"), message: Text("Please make sure you are connected before changing your password."))
return
}

guard MLXMPPManager.sharedInstance().isValidPassword(oldPass, forAccount: accountID) else {
errorAlert(title: Text("Wrong Password!"), message: Text("The current password is not correct."))
return
}

showLoadingOverlay(overlay, headlineView: Text("Changing Password"), descriptionView: Text(""))

account.changePassword(newPass) { success, message in
DispatchQueue.main.async {
hideLoadingOverlay(overlay)
if success {
successAlert(title: Text("Success"), message: Text("The password has been changed"))
MLXMPPManager.sharedInstance().updatePassword(newPass, forAccount: accountID)
} else {
errorAlert(title: Text("Error"), message: Text(message ?? "Could not change the password"))
}

}
}

}

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
TextField("Current Password", text: $oldPass)
.textInputAutocapitalization(.never)
.disableAutocorrection(true)
.onAppear {
oldPass = MLXMPPManager.sharedInstance().getPasswordForAccount(accountID)
}
#else
SecureField("Current Password", text: $oldPass)
#endif
SecureField("New Password", text: $newPass)
}

Section {
Button(action: passwordChangeProcessing) {
Text("Change Password")
.frame(maxWidth: .infinity, alignment: .center)
}
.disabled(oldPass.isEmpty || newPass.isEmpty)
.alert(
alertPrompt.title,
isPresented: $showAlert
) {
Button("Close") {
}
} message: {
alertPrompt.message
}

}
}
.navigationTitle("Change Password")
.navigationBarTitleDisplayMode(NavigationBarItem.TitleDisplayMode.inline)
.addLoadingOverlay(overlay)

}
}
25 changes: 0 additions & 25 deletions Monal/Classes/MLPasswordChangeTableViewController.h

This file was deleted.

200 changes: 0 additions & 200 deletions Monal/Classes/MLPasswordChangeTableViewController.m

This file was deleted.

9 changes: 8 additions & 1 deletion Monal/Classes/SwiftuiHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,14 @@ class SwiftuiInterface : NSObject {
}
return host
}


@objc
func makeChangePasswordView(for accountID: NSNumber) -> UIViewController {
let host = UIHostingController(rootView:AnyView(EmptyView()))
host.rootView = AnyView(ChangePassword(accountID: accountID))
return host
}

@objc
func makeAccountRegistration(_ registerData: [String:AnyObject]?) -> UIViewController {
let delegate = SheetDismisserProtocol()
Expand Down
15 changes: 4 additions & 11 deletions Monal/Classes/XMPPEdit.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#import "MLBlockedUsersTableViewController.h"
#import "MLButtonCell.h"
#import "MLImageManager.h"
#import "MLPasswordChangeTableViewController.h"
#import "MLSwitchCell.h"
#import "MLOMEMO.h"
#import "MLNotificationQueue.h"
Expand Down Expand Up @@ -829,9 +828,11 @@ -(void) tableView:(UITableView*) tableView didSelectRowAtIndexPath:(NSIndexPath*
{
switch(newIndexPath.row)
{
case SettingsChangePasswordRow:
[self performSegueWithIdentifier:@"showPassChange" sender:self];
case SettingsChangePasswordRow: {
UIViewController* changePasswordView = [[SwiftuiInterface new] makeChangePasswordViewFor:self.accountID];
[self showDetailViewController:changePasswordView sender:self];
break;
}
case SettingsOmemoKeysRow: {
UIViewController* ownOmemoKeysView;
xmpp* xmppAccount = [[MLXMPPManager sharedInstance] getEnabledAccountForID:self.accountID];
Expand Down Expand Up @@ -900,14 +901,6 @@ -(void) prepareForSegue:(UIStoryboardSegue*) segue sender:(id) sender
MLBlockedUsersTableViewController* blockedUsers = (MLBlockedUsersTableViewController*)segue.destinationViewController;
blockedUsers.xmppAccount = xmppAccount;
}
else if([segue.identifier isEqualToString:@"showPassChange"])
{
if(self.jid && self.accountID)
{
MLPasswordChangeTableViewController* pwchange = (MLPasswordChangeTableViewController*)segue.destinationViewController;
pwchange.xmppAccount = [[MLXMPPManager sharedInstance] getEnabledAccountForID:self.accountID];
}
}
}

#pragma mark - text input fielddelegate
Expand Down
Loading

0 comments on commit 7071d13

Please sign in to comment.