Show activity indicator until group creation is fully done

pull/223/head
nielsandriesse 5 years ago
parent 7149deb15f
commit 1716bd2929

@ -1,3 +1,4 @@
import PromiseKit
final class NewClosedGroupVC : BaseVC, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate, UIScrollViewDelegate { final class NewClosedGroupVC : BaseVC, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate, UIScrollViewDelegate {
private var selectedContacts: Set<String> = [] private var selectedContacts: Set<String> = []
@ -173,20 +174,27 @@ final class NewClosedGroupVC : BaseVC, UITableViewDataSource, UITableViewDelegat
guard selectedContacts.count >= 2 else { guard selectedContacts.count >= 2 else {
return showError(title: NSLocalizedString("Please pick at least 2 group members", comment: "")) 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: "")) return showError(title: NSLocalizedString("A closed group cannot have more than 20 members", comment: ""))
} }
let selectedContacts = self.selectedContacts let selectedContacts = self.selectedContacts
ModalActivityIndicatorViewController.present(fromViewController: navigationController!, canCancel: false) { [weak self] _ in ModalActivityIndicatorViewController.present(fromViewController: navigationController!, canCancel: false) { [weak self] _ in
let _ = FileServerAPI.getDeviceLinks(associatedWith: selectedContacts).ensure2 { FileServerAPI.getDeviceLinks(associatedWith: selectedContacts).then2 { _ -> Promise<TSGroupThread> in
var thread: TSGroupThread! var promise: Promise<TSGroupThread>!
try! Storage.writeSync { transaction in try! Storage.writeSync { transaction in
thread = ClosedGroupsProtocol.createClosedGroup(name: name, members: selectedContacts, transaction: transaction) promise = ClosedGroupsProtocol.createClosedGroup(name: name, members: selectedContacts, transaction: transaction)
} }
DispatchQueue.main.async { return promise
}.done(on: DispatchQueue.main) { thread in
self?.presentingViewController?.dismiss(animated: true, completion: nil) self?.presentingViewController?.dismiss(animated: true, completion: nil)
SignalApp.shared().presentConversation(for: thread, action: .compose, animated: false) 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 { guard selectedContacts.count >= 2 else {
return showError(title: NSLocalizedString("Please pick at least 2 group members", comment: "")) 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: "")) return showError(title: NSLocalizedString("A closed group cannot have more than 10 members", comment: ""))
} }
let userPublicKey = getUserHexEncodedPublicKey() let userPublicKey = getUserHexEncodedPublicKey()

@ -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."; "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"; "Unauthorized" = "Unauthorized";
"Closed group created" = "Closed group created"; "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.";

@ -42,6 +42,7 @@ public class ModalActivityIndicatorViewController: OWSViewController {
let view = ModalActivityIndicatorViewController(canCancel: canCancel) let view = ModalActivityIndicatorViewController(canCancel: canCancel)
// Present this modal _over_ the current view contents. // Present this modal _over_ the current view contents.
view.modalPresentationStyle = .overFullScreen view.modalPresentationStyle = .overFullScreen
view.modalTransitionStyle = .crossDissolve
fromViewController.present(view, fromViewController.present(view,
animated: false) { animated: false) {
DispatchQueue.global().async { DispatchQueue.global().async {

@ -12,11 +12,11 @@ import PromiseKit
/// See [the documentation](https://github.com/loki-project/session-protocol-docs/wiki/Medium-Size-Groups) for more information. /// See [the documentation](https://github.com/loki-project/session-protocol-docs/wiki/Medium-Size-Groups) for more information.
@objc(LKClosedGroupsProtocol) @objc(LKClosedGroupsProtocol)
public final class ClosedGroupsProtocol : NSObject { 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 /// - 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. /// making a request for each member.
public static func createClosedGroup(name: String, members: Set<String>, transaction: YapDatabaseReadWriteTransaction) -> TSGroupThread { public static func createClosedGroup(name: String, members: Set<String>, transaction: YapDatabaseReadWriteTransaction) -> Promise<TSGroupThread> {
// Prepare // Prepare
var members = members var members = members
let messageSenderJobQueue = SSKEnvironment.shared.messageSenderJobQueue let messageSenderJobQueue = SSKEnvironment.shared.messageSenderJobQueue
@ -48,13 +48,14 @@ public final class ClosedGroupsProtocol : NSObject {
// Establish sessions if needed // Establish sessions if needed
establishSessionsIfNeeded(with: [String](members), using: transaction) // Not `membersAndLinkedDevices` as this internally takes care of multi device already 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 // Send a closed group update message to all members (and their linked devices) using established channels
var promises: [Promise<Void>] = []
for member in members { // Not `membersAndLinkedDevices` as this internally takes care of multi device already for member in members { // Not `membersAndLinkedDevices` as this internally takes care of multi device already
let thread = TSContactThread.getOrCreateThread(withContactId: member, transaction: transaction) let thread = TSContactThread.getOrCreateThread(withContactId: member, transaction: transaction)
thread.save(with: transaction) thread.save(with: transaction)
let closedGroupUpdateMessageKind = ClosedGroupUpdateMessage.Kind.new(groupPublicKey: Data(hex: groupPublicKey), name: name, let closedGroupUpdateMessageKind = ClosedGroupUpdateMessage.Kind.new(groupPublicKey: Data(hex: groupPublicKey), name: name,
groupPrivateKey: groupKeyPair.privateKey, senderKeys: senderKeys, members: [String](members), admins: admins) groupPrivateKey: groupKeyPair.privateKey, senderKeys: senderKeys, members: [String](members), admins: admins)
let closedGroupUpdateMessage = ClosedGroupUpdateMessage(thread: thread, kind: closedGroupUpdateMessageKind) 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 // Add the group to the user's set of public keys to poll for
Storage.setClosedGroupPrivateKey(groupKeyPair.privateKey.toHexString(), for: groupPublicKey, using: transaction) 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) let infoMessage = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: thread, messageType: .typeGroupUpdate)
infoMessage.save(with: transaction) infoMessage.save(with: transaction)
// Return // Return
return thread return when(fulfilled: promises).map2 { thread }
} }
public static func addMembers(_ newMembers: Set<String>, to groupPublicKey: String, using transaction: YapDatabaseReadWriteTransaction) { public static func addMembers(_ newMembers: Set<String>, to groupPublicKey: String, using transaction: YapDatabaseReadWriteTransaction) {

Loading…
Cancel
Save