@@ -81,7 +81,7 @@ open class OTRRoomOccupantsViewController: UIViewController {
8181 var notificationToken : NSObjectProtocol ? = nil
8282
8383 /// opens implicit db transaction
84- open var room : OTRXMPPRoom ? {
84+ private var room : OTRXMPPRoom ? {
8585 return connections? . ui. fetch { self . room ( $0) }
8686 }
8787
@@ -117,19 +117,17 @@ open class OTRRoomOccupantsViewController: UIViewController {
117117
118118 @objc public func setupViewHandler( databaseConnection: YapDatabaseConnection , roomKey: String ) {
119119 self . roomUniqueId = roomKey
120- guard let _ = self . room else {
120+ guard let room = self . room else {
121121 return
122122 }
123- self . fetchMembersList ( )
123+ self . fetchMembersList ( room : room )
124124 viewHandler = OTRYapViewHandler ( databaseConnection: databaseConnection)
125125 if let viewHandler = self . viewHandler {
126126 viewHandler. delegate = self
127127 viewHandler. setup ( DatabaseExtensionName . groupOccupantsViewName. name ( ) , groups: [ GroupName . header. rawValue, roomKey, GroupName . footer. rawValue] )
128128 }
129129
130- self . notificationToken = NotificationCenter . default. addObserver ( forName: NSNotification . Name. YapDatabaseModified, object: OTRDatabaseManager . shared. database, queue: OperationQueue . main) { [ weak self] ( notification) -> Void in
131- self ? . yapDatabaseModified ( notification)
132- }
130+
133131 }
134132
135133 deinit {
@@ -171,11 +169,16 @@ open class OTRRoomOccupantsViewController: UIViewController {
171169 self . tableView. register ( GenericHeaderCell . self, forCellReuseIdentifier: DynamicCellIdentifier . omemoConfig. rawValue)
172170 self . tableView. register ( GenericHeaderCell . self, forCellReuseIdentifier: DynamicCellIdentifier . omemoToggle. rawValue)
173171
172+ self . tableView. reloadData ( )
174173 self . updateUIBasedOnOwnRole ( )
174+
175+ self . notificationToken = NotificationCenter . default. addObserver ( forName: NSNotification . Name. YapDatabaseModified, object: OTRDatabaseManager . shared. database, queue: OperationQueue . main) { [ weak self] ( notification) -> Void in
176+ self ? . yapDatabaseModified ( notification)
177+ }
175178 }
176179
177180 func yapDatabaseModified( _ notification: Notification ) {
178- guard let connection = self . connection , let roomUniqueId = self . roomUniqueId else { return }
181+ guard let connection = self . connections ? . ui , let roomUniqueId = self . roomUniqueId else { return }
179182 if connection. hasChange ( forKey: roomUniqueId, inCollection: OTRXMPPRoom . collection, in: [ notification] ) {
180183 // Subject has changed, update cell
181184 refreshSubjectCell ( )
@@ -408,18 +411,20 @@ open class OTRRoomOccupantsViewController: UIViewController {
408411 default : break
409412 }
410413 }
411- @objc func didChangeNotificationSwitch( _ sender: UIControl ! ) {
412- guard let notificationSwitch = sender as? UISwitch ,
413- let room = self . room? . copyAsSelf ( ) else {
414- return
415- }
416- if notificationSwitch. isOn {
417- room. muteExpiration = Date . distantFuture
418- } else {
419- room. muteExpiration = nil
420- }
421- connection? . readWrite ( { ( transaction) in
414+
415+ @objc func didChangeNotificationSwitch( _ sender: UISwitch ) {
416+ let notificationSwitchIsOn = sender. isOn
417+ connections? . write. asyncReadWrite ( { [ weak self] ( transaction) in
418+ guard let room = self ? . room ( transaction) else { return }
419+ if notificationSwitchIsOn {
420+ room. muteExpiration = Date . distantPast
421+ } else {
422+ room. muteExpiration = Date . distantFuture
423+ }
422424 room. save ( with: transaction)
425+ } , completionBlock: { [ weak self] in
426+ guard let room = self ? . room else { return }
427+ self ? . refreshNotificationSwitch ( sender, room: room, animated: false )
423428 } )
424429 }
425430
@@ -510,9 +515,8 @@ extension OTRRoomOccupantsViewController {
510515 return xmpp? . roomManager
511516 }
512517
513- fileprivate func fetchMembersList( ) {
514- guard let room = self . room,
515- let xmppRoom = xmppRoom ( for: room) else {
518+ fileprivate func fetchMembersList( room: OTRXMPPRoom ) {
519+ guard let xmppRoom = xmppRoom ( for: room) else {
516520 return
517521 }
518522 ignoreChanges = true
0 commit comments