Fix stale thread selection in chat list

See [here](https://github.com/signalapp/Signal-iOS/pull/5432).
This commit is contained in:
Michelle Linington 2022-10-18 12:14:49 -07:00 committed by Evan Hahn
parent 5fc3c033d8
commit 809a792aae
6 changed files with 38 additions and 6 deletions

View File

@ -22,7 +22,6 @@ extension ChatListViewController {
if let previousIndexPath = renderState.indexPath(beforeThread: currentThread),
let thread = self.thread(forIndexPath: previousIndexPath) {
self.present(thread, action: .compose, animated: true)
tableView.selectRow(at: previousIndexPath, animated: true, scrollPosition: .none)
}
}
@ -41,7 +40,6 @@ extension ChatListViewController {
if let nextIndexPath = renderState.indexPath(afterThread: currentThread),
let thread = self.thread(forIndexPath: nextIndexPath) {
self.present(thread, action: .compose, animated: true)
tableView.selectRow(at: nextIndexPath, animated: true, scrollPosition: .none)
}
}
}

View File

@ -146,8 +146,6 @@ public extension ChatListViewController {
owsFailDebug("Missing threadViewModel.")
return nil
}
self.lastViewedThread = threadViewModel.threadRecord
let vc = ConversationViewController(threadViewModel: threadViewModel,
action: .none,
focusMessageId: nil)

View File

@ -29,7 +29,9 @@ NS_ASSUME_NONNULL_BEGIN
- (void)unarchiveSelectedConversation;
@property (nonatomic, readonly) CLVViewState *viewState;
@property (nonatomic) TSThread *lastViewedThread;
/// Used to update the selected cell for split view and maintain scroll positions for reappearing collapsed views.
- (void)updateLastViewedThread:(TSThread *)thread animated:(BOOL)animated;
// For use by Swift extension.
- (void)updateBarButtonItems;

View File

@ -53,6 +53,8 @@ NSString *const kArchiveButtonPseudoGroup = @"kArchiveButtonPseudoGroup";
@property (nonatomic) BOOL hasEverPresentedExperienceUpgrade;
@property (nonatomic, nullable) TSThread *lastViewedThread;
@end
#pragma mark -
@ -696,6 +698,12 @@ NSString *const kArchiveButtonPseudoGroup = @"kArchiveButtonPseudoGroup";
[self.searchResultsController viewWillDisappear:animated];
}
- (void)updateLastViewedThread:(TSThread *)thread animated:(BOOL)animated
{
self.lastViewedThread = thread;
[self ensureSelectedThread:thread animated:animated];
}
#pragma mark -
- (void)pullToRefreshPerformed:(UIRefreshControl *)refreshControl

View File

@ -497,6 +497,32 @@ public extension ChatListViewController {
navigationController.setViewControllers(viewControllers, animated: false)
presentFormSheet(navigationController, animated: true, completion: completion)
}
/// Verifies that the currently selected cell matches the provided thread.
/// If it does or if the user's in multi-select: Do nothing.
/// If it doesn't: Select the first cell matching the provided thread, if one exists. Otherwise, deselect the current row.
@objc
func ensureSelectedThread(_ targetThread: TSThread, animated: Bool) {
// Ignore any updates if we're in multiselect mode. I don't think this can happen,
// but if it does let's avoid stepping over the user's manual selection.
let currentSelection = tableView.indexPathsForSelectedRows ?? []
guard viewState.multiSelectState.isActive == false, currentSelection.count < 2 else {
return
}
let currentlySelectedThread = currentSelection.first.flatMap {
self.tableDataSource.thread(forIndexPath: $0, expectsSuccess: false)
}
if currentlySelectedThread?.uniqueId != targetThread.uniqueId {
if let targetPath = tableDataSource.renderState.indexPath(forUniqueId: targetThread.uniqueId) {
tableView.selectRow(at: targetPath, animated: animated, scrollPosition: .none)
tableView.scrollToRow(at: targetPath, at: .none, animated: animated)
} else if let stalePath = currentSelection.first {
tableView.deselectRow(at: stalePath, animated: animated)
}
}
}
}
extension ChatListViewController: BadgeExpirationSheetDelegate {

View File

@ -184,7 +184,7 @@ class ConversationSplitViewController: UISplitViewController, ConversationSplit
// Update the last viewed thread on the conversation list so it
// can maintain its scroll position when navigating back.
homeVC.chatListViewController.lastViewedThread = thread
homeVC.chatListViewController.updateLastViewedThread(thread, animated: animated)
let threadViewModel = databaseStorage.read {
return ThreadViewModel(thread: thread,