From 1716bd29293ed905cace7159e29cc6a74d871ff7 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Wed, 8 Jul 2020 14:49:18 +1000 Subject: [PATCH] Show activity indicator until group creation is fully done --- .../View Controllers/NewClosedGroupVC.swift | 26 ++++++++++++------- .../translations/en.lproj/Localizable.strings | 2 ++ ...ModalActivityIndicatorViewController.swift | 1 + .../Closed Groups/ClosedGroupsProtocol.swift | 9 ++++--- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/Signal/src/Loki/View Controllers/NewClosedGroupVC.swift b/Signal/src/Loki/View Controllers/NewClosedGroupVC.swift index 77026a5dd..5ddff657d 100644 --- a/Signal/src/Loki/View Controllers/NewClosedGroupVC.swift +++ b/Signal/src/Loki/View Controllers/NewClosedGroupVC.swift @@ -1,3 +1,4 @@ +import PromiseKit final class NewClosedGroupVC : BaseVC, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate, UIScrollViewDelegate { private var selectedContacts: Set = [] @@ -173,20 +174,27 @@ final class NewClosedGroupVC : BaseVC, UITableViewDataSource, UITableViewDelegat guard selectedContacts.count >= 2 else { return showError(title: NSLocalizedString("Please pick at least 2 group members", comment: "")) } - guard selectedContacts.count <= 20 else { + guard selectedContacts.count < 20 else { // Minus one because we're going to include self later return showError(title: NSLocalizedString("A closed group cannot have more than 20 members", comment: "")) } let selectedContacts = self.selectedContacts ModalActivityIndicatorViewController.present(fromViewController: navigationController!, canCancel: false) { [weak self] _ in - let _ = FileServerAPI.getDeviceLinks(associatedWith: selectedContacts).ensure2 { - var thread: TSGroupThread! + FileServerAPI.getDeviceLinks(associatedWith: selectedContacts).then2 { _ -> Promise in + var promise: Promise! try! Storage.writeSync { transaction in - thread = ClosedGroupsProtocol.createClosedGroup(name: name, members: selectedContacts, transaction: transaction) - } - DispatchQueue.main.async { - self?.presentingViewController?.dismiss(animated: true, completion: nil) - SignalApp.shared().presentConversation(for: thread, action: .compose, animated: false) + promise = ClosedGroupsProtocol.createClosedGroup(name: name, members: selectedContacts, transaction: transaction) } + return promise + }.done(on: DispatchQueue.main) { thread in + self?.presentingViewController?.dismiss(animated: true, completion: nil) + SignalApp.shared().presentConversation(for: thread, action: .compose, animated: false) + }.catch(on: DispatchQueue.main) { _ in + self?.dismiss(animated: true, completion: nil) // Dismiss the modal + let title = NSLocalizedString("Couldn't Create Group", comment: "") + let message = NSLocalizedString("Please check your internet connection and try again.", comment: "") + let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) + alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default, handler: nil)) + self?.presentAlert(alert) } } } @@ -206,7 +214,7 @@ final class NewClosedGroupVC : BaseVC, UITableViewDataSource, UITableViewDelegat guard selectedContacts.count >= 2 else { return showError(title: NSLocalizedString("Please pick at least 2 group members", comment: "")) } - guard selectedContacts.count <= 10 else { + guard selectedContacts.count < 10 else { // Minus one because we're going to include self later return showError(title: NSLocalizedString("A closed group cannot have more than 10 members", comment: "")) } let userPublicKey = getUserHexEncodedPublicKey() diff --git a/Signal/translations/en.lproj/Localizable.strings b/Signal/translations/en.lproj/Localizable.strings index b7ff78262..4eb218912 100644 --- a/Signal/translations/en.lproj/Localizable.strings +++ b/Signal/translations/en.lproj/Localizable.strings @@ -2845,3 +2845,5 @@ "Please ask the open group operator to add you to the group." = "Please ask the open group operator to add you to the group."; "Unauthorized" = "Unauthorized"; "Closed group created" = "Closed group created"; +"Couldn't Create Group" = "Couldn't Create Group"; +"Please check your internet connection and try again." = "Please check your internet connection and try again."; diff --git a/SignalMessaging/ViewControllers/ModalActivityIndicatorViewController.swift b/SignalMessaging/ViewControllers/ModalActivityIndicatorViewController.swift index 54156723d..b78fa553b 100644 --- a/SignalMessaging/ViewControllers/ModalActivityIndicatorViewController.swift +++ b/SignalMessaging/ViewControllers/ModalActivityIndicatorViewController.swift @@ -42,6 +42,7 @@ public class ModalActivityIndicatorViewController: OWSViewController { let view = ModalActivityIndicatorViewController(canCancel: canCancel) // Present this modal _over_ the current view contents. view.modalPresentationStyle = .overFullScreen + view.modalTransitionStyle = .crossDissolve fromViewController.present(view, animated: false) { DispatchQueue.global().async { diff --git a/SignalServiceKit/src/Loki/Protocol/Closed Groups/ClosedGroupsProtocol.swift b/SignalServiceKit/src/Loki/Protocol/Closed Groups/ClosedGroupsProtocol.swift index 371265229..1c981c7f2 100644 --- a/SignalServiceKit/src/Loki/Protocol/Closed Groups/ClosedGroupsProtocol.swift +++ b/SignalServiceKit/src/Loki/Protocol/Closed Groups/ClosedGroupsProtocol.swift @@ -12,11 +12,11 @@ import PromiseKit /// See [the documentation](https://github.com/loki-project/session-protocol-docs/wiki/Medium-Size-Groups) for more information. @objc(LKClosedGroupsProtocol) public final class ClosedGroupsProtocol : NSObject { - public static let isSharedSenderKeysEnabled = false + public static let isSharedSenderKeysEnabled = true /// - Note: It's recommended to batch fetch the device links for the given set of members before invoking this, to avoid the message sending pipeline /// making a request for each member. - public static func createClosedGroup(name: String, members: Set, transaction: YapDatabaseReadWriteTransaction) -> TSGroupThread { + public static func createClosedGroup(name: String, members: Set, transaction: YapDatabaseReadWriteTransaction) -> Promise { // Prepare var members = members let messageSenderJobQueue = SSKEnvironment.shared.messageSenderJobQueue @@ -48,13 +48,14 @@ public final class ClosedGroupsProtocol : NSObject { // Establish sessions if needed establishSessionsIfNeeded(with: [String](members), using: transaction) // Not `membersAndLinkedDevices` as this internally takes care of multi device already // Send a closed group update message to all members (and their linked devices) using established channels + var promises: [Promise] = [] for member in members { // Not `membersAndLinkedDevices` as this internally takes care of multi device already let thread = TSContactThread.getOrCreateThread(withContactId: member, transaction: transaction) thread.save(with: transaction) let closedGroupUpdateMessageKind = ClosedGroupUpdateMessage.Kind.new(groupPublicKey: Data(hex: groupPublicKey), name: name, groupPrivateKey: groupKeyPair.privateKey, senderKeys: senderKeys, members: [String](members), admins: admins) let closedGroupUpdateMessage = ClosedGroupUpdateMessage(thread: thread, kind: closedGroupUpdateMessageKind) - messageSenderJobQueue.add(message: closedGroupUpdateMessage, transaction: transaction) + promises.append(SSKEnvironment.shared.messageSender.sendPromise(message: closedGroupUpdateMessage)) } // Add the group to the user's set of public keys to poll for Storage.setClosedGroupPrivateKey(groupKeyPair.privateKey.toHexString(), for: groupPublicKey, using: transaction) @@ -62,7 +63,7 @@ public final class ClosedGroupsProtocol : NSObject { let infoMessage = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: thread, messageType: .typeGroupUpdate) infoMessage.save(with: transaction) // Return - return thread + return when(fulfilled: promises).map2 { thread } } public static func addMembers(_ newMembers: Set, to groupPublicKey: String, using transaction: YapDatabaseReadWriteTransaction) {