Skip to content

Commit

Permalink
Merge branch 'fix/MAILIOS-4187-app-crashes-when-swiping-to-next-messa…
Browse files Browse the repository at this point in the history
…ge' into 'release/4.12.1'

MAILIOS-4187 Fix app crash when swiping with trashed messages and embedded images

See merge request ProtonMail/protonmail-ios!3957
  • Loading branch information
jacekkra committed Feb 14, 2024
2 parents 58cb2a8 + 1955e67 commit a7a34e5
Showing 1 changed file with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,14 @@ class ConversationViewModel {
}
self?.perform(update: update, on: tableView)
if case .didUpdate = update {
self?.checkTrashedHintBanner()
self?.checkTrashedHintBanner(insertRowManually: true)
self?.reloadRowsIfNeeded()
self?.markMessagesReadIfNeeded()
}
} storedMessages: { [weak self] messages in
self?.updateMessageDataSource(messages: messages)
self?.markMessagesReadIfNeeded()
self?.checkTrashedHintBanner()
self?.checkTrashedHintBanner(insertRowManually: false)
self?.reloadTableView?()
self?.messagesAreLoaded = true
}
Expand Down Expand Up @@ -506,23 +506,29 @@ class ConversationViewModel {
}

/// Add trashed hint banner if the messages contain trashed message
private func checkTrashedHintBanner() {
private func checkTrashedHintBanner(insertRowManually: Bool) {
let hasMessages = !self.messagesDataSource.isEmpty
guard hasMessages else { return }
let trashed = self.messagesDataSource
.filter { $0.messageViewModel?.isTrashed ?? false }
let hasTrashed = !trashed.isEmpty
let isAllTrashed = trashed.count == self.messagesDataSource.count
if self.isTrashFolder {
self.checkTrashedHintBannerForTrashFolder(hasTrashed: hasTrashed,
isAllTrashed: isAllTrashed)
checkTrashedHintBannerForTrashFolder(
hasTrashed: hasTrashed,
isAllTrashed: isAllTrashed,
insertRowManually: insertRowManually
)
} else {
self.checkTrashedHintBannerForNonTrashFolder(hasTrashed: hasTrashed,
isAllTrashed: isAllTrashed)
checkTrashedHintBannerForNonTrashFolder(
hasTrashed: hasTrashed,
isAllTrashed: isAllTrashed,
insertRowManually: insertRowManually
)
}
}

private func checkTrashedHintBannerForTrashFolder(hasTrashed: Bool, isAllTrashed: Bool) {
private func checkTrashedHintBannerForTrashFolder(hasTrashed: Bool, isAllTrashed: Bool, insertRowManually: Bool) {
guard hasTrashed else {
self.removeTrashedHintBanner()
// In trash folder, without trashed message
Expand All @@ -539,11 +545,11 @@ class ConversationViewModel {
if isAllTrashed {
self.removeTrashedHintBanner()
} else {
self.showTrashedHintBanner()
self.showTrashedHintBanner(insertRowManually: insertRowManually)
}
}

private func checkTrashedHintBannerForNonTrashFolder(hasTrashed: Bool, isAllTrashed: Bool) {
private func checkTrashedHintBannerForNonTrashFolder(hasTrashed: Bool, isAllTrashed: Bool, insertRowManually: Bool) {
if isAllTrashed {
// In non trash folder, without trashed message
// Should show trashed messages
Expand All @@ -557,7 +563,7 @@ class ConversationViewModel {
}

if hasTrashed {
self.showTrashedHintBanner()
self.showTrashedHintBanner(insertRowManually: insertRowManually)
} else {
self.removeTrashedHintBanner()
}
Expand All @@ -570,13 +576,24 @@ class ConversationViewModel {
self.tableView?.reloadSections(headerIndexSet, with: .automatic)
}

private func showTrashedHintBanner() {
private func showTrashedHintBanner(insertRowManually: Bool) {
if self.headerSectionDataSource.contains(.trashedHint) { return }
self.headerSectionDataSource.append(.trashedHint)

guard insertRowManually else {
return
}

let visible = self.tableView?.visibleCells.count ?? 0
if visible > 0 {
let row = IndexPath(row: 0, section: 0)
self.tableView?.insertRows(at: [row], with: .automatic)
do {
try ObjC.catchException {
self.tableView?.insertRows(at: [row], with: .automatic)
}
} catch {
PMAssertionFailure(error)
}
}
}

Expand Down

0 comments on commit a7a34e5

Please sign in to comment.