Commit 94f7a1f3 authored by Kateryna Kostiuk's avatar Kateryna Kostiuk Committed by Andreas Traczyk

fix: subscriptions memory management

This patch ensures that objects used in closures were not
deallocated.

Change-Id: Ic8426db0dac3e75d4009587acece236c53ac9f4a
Reviewed-by: Andreas Traczyk's avatarAndreas Traczyk <andreas.traczyk@savoirfairelinux.com>
parent ea7a2a98
......@@ -160,7 +160,7 @@ class CallViewModel: Stateable, ViewModel {
lazy var showCallOptions: Observable<Bool> = {
return Observable.combineLatest(self.screenTapped.asObservable(),
isActiveVideoCall) { [unowned self] (tapped, shouldRespond) in
isActiveVideoCall) { (tapped, shouldRespond) in
if tapped && shouldRespond {
return true
}
......@@ -184,8 +184,8 @@ class CallViewModel: Stateable, ViewModel {
let onImage = UIImage(asset: Asset.videoRunning)
let offImage = UIImage(asset: Asset.videoMuted)
return self.videoMuted.map({ [unowned self] muted in
let audioOnly = self.call?.isAudioOnly ?? false
return self.videoMuted.map({ [weak self] muted in
let audioOnly = self?.call?.isAudioOnly ?? false
if audioOnly || muted {
return offImage
}
......@@ -194,8 +194,8 @@ class CallViewModel: Stateable, ViewModel {
}()
lazy var videoMuted: Observable<Bool> = {
return self.callService.currentCall.filter({ [unowned self] call in
call.callId == self.call?.callId &&
return self.callService.currentCall.filter({ [weak self] call in
call.callId == self?.call?.callId &&
call.state == .current
}).map({call in
return call.videoMuted
......@@ -237,8 +237,8 @@ class CallViewModel: Stateable, ViewModel {
}()
lazy var audioMuted: Observable<Bool> = {
return self.callService.currentCall.filter({ [unowned self] call in
call.callId == self.call?.callId &&
return self.callService.currentCall.filter({ [weak self] call in
call.callId == self?.call?.callId &&
call.state == .current
}).map({call in
return call.audioMuted
......@@ -258,8 +258,8 @@ class CallViewModel: Stateable, ViewModel {
}()
lazy var callPaused: Observable<Bool> = {
return self.callService.currentCall.filter({ [unowned self] call in
call.callId == self.call?.callId &&
return self.callService.currentCall.filter({ [weak self] call in
call.callId == self?.call?.callId &&
(call.state == .hold ||
call.state == .unhold ||
call.state == .current)
......@@ -326,8 +326,8 @@ class CallViewModel: Stateable, ViewModel {
toRingId: uri,
userName: userName,
isAudioOnly: isAudioOnly)
.subscribe(onSuccess: { [unowned self] callModel in
self.call = callModel
.subscribe(onSuccess: { [weak self] callModel in
self?.call = callModel
}).disposed(by: self.disposeBag)
}
......
......@@ -72,20 +72,20 @@ class ContactRequestsViewController: UIViewController, StoryboardBased, ViewMode
//Accept button
cell.acceptButton.backgroundColor = UIColor.clear
cell.acceptButton.rx.tap.subscribe(onNext: { [unowned self] in
self.acceptButtonTapped(withItem: item)
cell.acceptButton.rx.tap.subscribe(onNext: { [weak self] in
self?.acceptButtonTapped(withItem: item)
}).disposed(by: cell.disposeBag)
//Discard button
cell.discardButton.backgroundColor = UIColor.clear
cell.discardButton.rx.tap.subscribe(onNext: { [unowned self] in
self.discardButtonTapped(withItem: item)
cell.discardButton.rx.tap.subscribe(onNext: { [weak self] in
self?.discardButtonTapped(withItem: item)
}).disposed(by: cell.disposeBag)
//Ban button
cell.banButton.backgroundColor = UIColor.clear
cell.banButton.rx.tap.subscribe(onNext: { [unowned self] in
self.banButtonTapped(withItem: item)
cell.banButton.rx.tap.subscribe(onNext: { [weak self] in
self?.banButtonTapped(withItem: item)
}).disposed(by: cell.disposeBag)
}
.disposed(by: disposeBag)
......
......@@ -746,50 +746,50 @@ extension ConversationViewController: UITableViewDataSource {
cell.acceptButton?.setTitle(L10n.Datatransfer.readableStatusAccept, for: .normal)
item.lastTransferStatus = .unknown
changeTransferStatus(cell, nil, item.message.transferStatus, item, viewModel)
item.transferStatus.asObservable()
.observeOn(MainScheduler.instance)
.filter {
return $0 != DataTransferStatus.unknown && $0 != item.lastTransferStatus && $0 != item.initialTransferStatus }
.subscribe(onNext: { [unowned self] status in
guard let currentIndexPath = tableView.indexPath(for: cell) else { return }
.subscribe(onNext: { [weak self, weak tableView] status in
guard let currentIndexPath = tableView?.indexPath(for: cell) else { return }
guard let transferId = item.daemonId else { return }
self.log.info("Transfer status change from: \(item.lastTransferStatus.description) to: \(status.description) for transferId: \(transferId) cell row: \(currentIndexPath.row)")
guard let model = self?.viewModel else { return }
self?.log.info("Transfer status change from: \(item.lastTransferStatus.description) to: \(status.description) for transferId: \(transferId) cell row: \(currentIndexPath.row)")
if item.bubblePosition() == .sent && item.shouldDisplayTransferedImage {
cell.displayTransferedImage(message: item, conversationID: self.viewModel.conversation.value.conversationId)
cell.displayTransferedImage(message: item, conversationID: model.conversation.value.conversationId)
} else {
self.changeTransferStatus(cell, currentIndexPath, status, item, self.viewModel)
self?.changeTransferStatus(cell, currentIndexPath, status, item, model)
cell.stopProgressMonitor()
}
item.lastTransferStatus = status
item.initialTransferStatus = status
tableView.reloadData()
tableView?.reloadData()
})
.disposed(by: cell.disposeBag)
cell.cancelButton.rx.tap
.subscribe(onNext: { [unowned self] _ in
.subscribe(onNext: { [weak self, weak tableView] _ in
guard let transferId = item.daemonId else { return }
self.log.info("canceling transferId \(transferId)")
_ = self.viewModel.cancelTransfer(transferId: transferId)
self?.log.info("canceling transferId \(transferId)")
_ = self?.viewModel.cancelTransfer(transferId: transferId)
item.initialTransferStatus = .canceled
item.message.transferStatus = .canceled
cell.stopProgressMonitor()
tableView.reloadData()
tableView?.reloadData()
})
.disposed(by: cell.disposeBag)
if item.bubblePosition() == .received {
cell.acceptButton?.rx.tap
.subscribe(onNext: { [unowned self] _ in
.subscribe(onNext: { [weak self, weak tableView] _ in
guard let transferId = item.daemonId else { return }
self.log.info("accepting transferId \(transferId)")
if self.viewModel.acceptTransfer(transferId: transferId, interactionID: item.messageId, messageContent: &item.message.content) != .success {
_ = self.viewModel.cancelTransfer(transferId: transferId)
self?.log.info("accepting transferId \(transferId)")
if self?.viewModel.acceptTransfer(transferId: transferId, interactionID: item.messageId, messageContent: &item.message.content) != .success {
_ = self?.viewModel.cancelTransfer(transferId: transferId)
item.initialTransferStatus = .canceled
item.message.transferStatus = .canceled
cell.stopProgressMonitor()
tableView.reloadData()
tableView?.reloadData()
}
})
.disposed(by: cell.disposeBag)
......
......@@ -63,8 +63,9 @@ class BlockListViewController: UIViewController, StoryboardBased, ViewModelBased
.bind(to: tableView.rx.items(cellIdentifier: cellIdentifier, cellType: BannedContactCell.self)) { [unowned self] _, item, cell in
cell.configureFromItem(item)
cell.unblockButton.rx.tap
.subscribe(onNext: { [unowned self] in
self.unbanContactTapped(withItem: item)
.subscribe(onNext: { [weak self, weak item] in
if item == nil {return}
self?.unbanContactTapped(withItem: item!)
}).disposed(by: cell.disposeBag)
}
.disposed(by: disposeBag)
......
......@@ -163,11 +163,11 @@ class MeViewController: EditProfileViewController, StoryboardBased, ViewModelBas
case .linkNew:
let cell = tableView.dequeueReusableCell(for: indexPath, cellType: LinkNewDeviceCell.self)
cell.addDeviceButton.rx.tap.subscribe(onNext: { [unowned self] in
self.viewModel.linkDevice()
cell.addDeviceButton.rx.tap.subscribe(onNext: { [weak self] in
self?.viewModel.linkDevice()
}).disposed(by: cell.disposeBag)
cell.addDeviceTitle.rx.tap.subscribe(onNext: { [unowned self] in
self.viewModel.linkDevice()
cell.addDeviceTitle.rx.tap.subscribe(onNext: { [weak self] in
self?.viewModel.linkDevice()
}).disposed(by: cell.disposeBag)
cell.selectionStyle = .none
return cell
......@@ -183,8 +183,8 @@ class MeViewController: EditProfileViewController, StoryboardBased, ViewModelBas
.disposed(by: cell.disposeBag)
cell.switchProxy.rx.isOn
.observeOn(MainScheduler.instance)
.subscribe(onNext: { [unowned self] (enable) in
self.viewModel.enableProxy(enable: enable)
.subscribe(onNext: { [weak self] (enable) in
self?.viewModel.enableProxy(enable: enable)
}).disposed(by: cell.disposeBag)
cell.selectionStyle = .none
return cell
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment