Skip to content

Commit

Permalink
Promisify changePassword:
Browse files Browse the repository at this point in the history
  • Loading branch information
lissine0 committed Nov 2, 2024
1 parent dea150f commit b54427d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
22 changes: 9 additions & 13 deletions Monal/Classes/ChangePassword.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,15 @@ struct ChangePassword: View {
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"))
}

}
showPromisingLoadingOverlay(overlay, headlineView: Text("Changing Password"), descriptionView: Text("")) {
account.changePassword(newPass)
}
.done { _ in
successAlert(title: Text("Success"), message: Text("The password has been changed"))
MLXMPPManager.sharedInstance().updatePassword(newPass, forAccount: accountID)
}
.catch { error in
errorAlert(title: Text("Error"), message: Text(error.localizedDescription))
}

}
Expand Down
2 changes: 1 addition & 1 deletion Monal/Classes/xmpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ typedef void (^monal_iq_handler_t)(XMPPIQ* _Nullable);

#pragma mark - account management

-(void) changePassword:(NSString*) newPass withCompletion:(xmppCompletion _Nullable) completion;
-(AnyPromise*) changePassword:(NSString*) newPass;

-(void) requestRegFormWithToken:(NSString* _Nullable) token andCompletion:(xmppDataCompletion) completion andErrorCompletion:(xmppCompletion) errorCompletion;
-(void) registerUser:(NSString*) username withPassword:(NSString*) password captcha:(NSString* _Nullable) captcha andHiddenFields:(NSDictionary* _Nullable) hiddenFields withCompletion:(xmppCompletion _Nullable) completion;
Expand Down
28 changes: 12 additions & 16 deletions Monal/Classes/xmpp.m
Original file line number Diff line number Diff line change
Expand Up @@ -4596,23 +4596,19 @@ -(void) createInvitationWithCompletion:(monal_id_block_t) completion
}];
}

-(void) changePassword:(NSString *) newPass withCompletion:(xmppCompletion) completion
-(AnyPromise*) changePassword:(NSString*) newPass
{
XMPPIQ* iq = [[XMPPIQ alloc] initWithType:kiqSetType];
[iq setiqTo:self.connectionProperties.identity.domain];
[iq changePasswordForUser:self.connectionProperties.identity.user newPassword:newPass];
[self sendIq:iq withResponseHandler:^(XMPPIQ* response __unused) {
//dispatch completion handler outside of the receiveQueue
if(completion)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
completion(YES, @"");
});
} andErrorHandler:^(XMPPIQ* error) {
//dispatch completion handler outside of the receiveQueue
if(completion)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
completion(NO, error ? [HelperTools extractXMPPError:error withDescription:NSLocalizedString(@"Could not change password", @"")] : NSLocalizedString(@"Could not change password: your account is currently not connected", @""));
});
return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
XMPPIQ* iq = [[XMPPIQ alloc] initWithType:kiqSetType];
[iq setiqTo:self.connectionProperties.identity.domain];
[iq changePasswordForUser:self.connectionProperties.identity.user newPassword:newPass];

[self sendIq:iq withResponseHandler:^(XMPPIQ* response) {
resolve(nil);
} andErrorHandler:^(XMPPIQ* error) {
NSString* errorMessage = error ? [HelperTools extractXMPPError:error withDescription:NSLocalizedString(@"Could not change password", @"")] : NSLocalizedString(@"Could not change password: your account is currently not connected", @"");
resolve([NSError errorWithDomain:@"Monal" code:0 userInfo:@{NSLocalizedDescriptionKey: errorMessage}]);
}];
}];
}

Expand Down

0 comments on commit b54427d

Please sign in to comment.