From eb6cfba9aac0a79b2d70433fd63e11f1050ce0bf Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 14 Dec 2019 11:59:31 -0500 Subject: [PATCH] Fix tablel view cells being re-selected on aborted nav swipe back --- Tusker.xcodeproj/project.pbxproj | 4 ++ .../ConversationTableViewController.swift | 5 +-- .../NotificationsTableViewController.swift | 1 + .../InstanceSelectorTableViewController.swift | 32 ++++++++------- .../Profile/ProfileTableViewController.swift | 2 + .../EnhancedTableViewController.swift | 7 ++++ .../Account Cell/AccountTableViewCell.swift | 10 ++--- .../Hashtag Cell/HashtagTableViewCell.swift | 14 +++---- .../Instance Cell/InstanceTableViewCell.swift | 19 --------- ...ActionNotificationGroupTableViewCell.swift | 39 +++++++++---------- ...FollowNotificationGroupTableViewCell.swift | 28 +++++++------ Tusker/Views/SelectableTableViewCell.swift | 13 +++++++ .../Status/TimelineStatusTableViewCell.swift | 14 +++---- 13 files changed, 93 insertions(+), 95 deletions(-) create mode 100644 Tusker/Views/SelectableTableViewCell.swift diff --git a/Tusker.xcodeproj/project.pbxproj b/Tusker.xcodeproj/project.pbxproj index 851bd5fa..1de1358a 100644 --- a/Tusker.xcodeproj/project.pbxproj +++ b/Tusker.xcodeproj/project.pbxproj @@ -76,6 +76,7 @@ D61AC1D5232E9FA600C54D2D /* InstanceSelectorTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D61AC1D4232E9FA600C54D2D /* InstanceSelectorTableViewController.swift */; }; D61AC1D8232EA42D00C54D2D /* InstanceTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D61AC1D6232EA42D00C54D2D /* InstanceTableViewCell.swift */; }; D61AC1D9232EA42D00C54D2D /* InstanceTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = D61AC1D7232EA42D00C54D2D /* InstanceTableViewCell.xib */; }; + D627943223A5466600D38C68 /* SelectableTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D627943123A5466600D38C68 /* SelectableTableViewCell.swift */; }; D627FF76217E923E00CC0648 /* DraftsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D627FF75217E923E00CC0648 /* DraftsManager.swift */; }; D627FF79217E950100CC0648 /* DraftsTableViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = D627FF78217E950100CC0648 /* DraftsTableViewController.xib */; }; D627FF7B217E951500CC0648 /* DraftsTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D627FF7A217E951500CC0648 /* DraftsTableViewController.swift */; }; @@ -339,6 +340,7 @@ D61AC1D4232E9FA600C54D2D /* InstanceSelectorTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstanceSelectorTableViewController.swift; sourceTree = ""; }; D61AC1D6232EA42D00C54D2D /* InstanceTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstanceTableViewCell.swift; sourceTree = ""; }; D61AC1D7232EA42D00C54D2D /* InstanceTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = InstanceTableViewCell.xib; sourceTree = ""; }; + D627943123A5466600D38C68 /* SelectableTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectableTableViewCell.swift; sourceTree = ""; }; D627FF75217E923E00CC0648 /* DraftsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DraftsManager.swift; sourceTree = ""; }; D627FF78217E950100CC0648 /* DraftsTableViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = DraftsTableViewController.xib; sourceTree = ""; }; D627FF7A217E951500CC0648 /* DraftsTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DraftsTableViewController.swift; sourceTree = ""; }; @@ -1074,6 +1076,7 @@ D6C693F82162E4DB007D6A6D /* StatusContentLabel.swift */, D641C77E213DC78A004B4513 /* InlineTextAttachment.swift */, 04ED00B021481ED800567C53 /* SteppedProgressView.swift */, + D627943123A5466600D38C68 /* SelectableTableViewCell.swift */, D67C57A721E2649B00C3118B /* Account Detail */, D67C57B021E28F9400C3118B /* Compose Status Reply */, D60C07E221E817560057FAA8 /* Compose Media */, @@ -1603,6 +1606,7 @@ D6AEBB4A23216F0400E5038B /* UnfollowAccountActivity.swift in Sources */, D663626421360D2300C9CBA2 /* AvatarStyle.swift in Sources */, D679C09F215850EF00DA27FE /* XCBActions.swift in Sources */, + D627943223A5466600D38C68 /* SelectableTableViewCell.swift in Sources */, D6DD353F22F502EC00A9563A /* Preferences+Notification.swift in Sources */, D63661C02381C144004B9E16 /* PreferencesNavigationController.swift in Sources */, D6AEBB4523216AF800E5038B /* FollowAccountActivity.swift in Sources */, diff --git a/Tusker/Screens/Conversation/ConversationTableViewController.swift b/Tusker/Screens/Conversation/ConversationTableViewController.swift index c14311f9..1807dcf2 100644 --- a/Tusker/Screens/Conversation/ConversationTableViewController.swift +++ b/Tusker/Screens/Conversation/ConversationTableViewController.swift @@ -113,10 +113,7 @@ class ConversationTableViewController: EnhancedTableViewController { } } - override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { - let statusID = statuses[indexPath.row].id - return statusID == mainStatusID ? nil : indexPath - } + // MARK: - Table view delegate override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return true diff --git a/Tusker/Screens/Notifications/NotificationsTableViewController.swift b/Tusker/Screens/Notifications/NotificationsTableViewController.swift index 7d420425..e9e8b866 100644 --- a/Tusker/Screens/Notifications/NotificationsTableViewController.swift +++ b/Tusker/Screens/Notifications/NotificationsTableViewController.swift @@ -109,6 +109,7 @@ class NotificationsTableViewController: EnhancedTableViewController { } } + // MARK: - Table view delegate override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { if indexPath.row == groups.count - 1 { diff --git a/Tusker/Screens/Onboarding/InstanceSelectorTableViewController.swift b/Tusker/Screens/Onboarding/InstanceSelectorTableViewController.swift index ea03e883..fc174c8e 100644 --- a/Tusker/Screens/Onboarding/InstanceSelectorTableViewController.swift +++ b/Tusker/Screens/Onboarding/InstanceSelectorTableViewController.swift @@ -51,12 +51,10 @@ class InstanceSelectorTableViewController: UITableViewController { case let .selected(instance): let cell = tableView.dequeueReusableCell(withIdentifier: instanceCell, for: indexPath) as! InstanceTableViewCell cell.updateUI(instance: instance) - cell.delegate = self return cell case let .recommended(instance): let cell = tableView.dequeueReusableCell(withIdentifier: instanceCell, for: indexPath) as! InstanceTableViewCell cell.updateUI(instance: instance) - cell.delegate = self return cell } }) @@ -135,6 +133,23 @@ class InstanceSelectorTableViewController: UITableViewController { self.dataSource.apply(snapshot) } } + + // MARK: - Table view delegate + override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + guard let delegate = delegate, let item = dataSource.itemIdentifier(for: indexPath) else { + return + } + switch item { + case let .selected(instance): + delegate.didSelectInstance(url: URL(string: instance.uri)!) + case let .recommended(instance): + var components = URLComponents() + components.scheme = "https" + components.host = instance.domain + components.path = "/" + delegate.didSelectInstance(url: components.url!) + } + } } extension InstanceSelectorTableViewController { @@ -179,16 +194,3 @@ extension InstanceSelectorTableViewController: UISearchResultsUpdating { urlCheckerSubject.send(currentQuery) } } - -extension InstanceSelectorTableViewController: InstanceTableViewCellDelegate { - func didSelectInstance(_ instance: Instance) { - delegate?.didSelectInstance(url: URL(string: instance.uri)!) - } - func didSelectInstance(_ instance: InstanceSelector.Instance) { - var components = URLComponents() - components.scheme = "https" - components.host = instance.domain - components.path = "/" - delegate?.didSelectInstance(url: components.url!) - } -} diff --git a/Tusker/Screens/Profile/ProfileTableViewController.swift b/Tusker/Screens/Profile/ProfileTableViewController.swift index 1d08c17c..326ddbfd 100644 --- a/Tusker/Screens/Profile/ProfileTableViewController.swift +++ b/Tusker/Screens/Profile/ProfileTableViewController.swift @@ -180,6 +180,8 @@ class ProfileTableViewController: EnhancedTableViewController { } } + // MARK: - Table view delegate + override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { if timelineSegments.count > 0 && indexPath.section - 1 == timelineSegments.count && indexPath.row == timelineSegments[indexPath.section - 2].count - 1 { guard let older = older else { return } diff --git a/Tusker/Screens/Utilities/EnhancedTableViewController.swift b/Tusker/Screens/Utilities/EnhancedTableViewController.swift index a8f1681a..56f54a9c 100644 --- a/Tusker/Screens/Utilities/EnhancedTableViewController.swift +++ b/Tusker/Screens/Utilities/EnhancedTableViewController.swift @@ -35,6 +35,13 @@ class EnhancedTableViewController: UITableViewController { override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { prevScrollToTopOffset = nil } + + + override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + if let cell = tableView.cellForRow(at: indexPath) as? SelectableTableViewCell { + cell.didSelectCell() + } + } } diff --git a/Tusker/Views/Account Cell/AccountTableViewCell.swift b/Tusker/Views/Account Cell/AccountTableViewCell.swift index c97cc49e..bbb04a21 100644 --- a/Tusker/Views/Account Cell/AccountTableViewCell.swift +++ b/Tusker/Views/Account Cell/AccountTableViewCell.swift @@ -56,14 +56,12 @@ class AccountTableViewCell: UITableViewCell { updateUIForPrefrences() } - override func setSelected(_ selected: Bool, animated: Bool) { - super.setSelected(selected, animated: animated) +} - if selected { - delegate?.selected(account: accountID) - } +extension AccountTableViewCell: SelectableTableViewCell { + func didSelectCell() { + delegate?.selected(account: accountID) } - } extension AccountTableViewCell: MenuPreviewProvider { diff --git a/Tusker/Views/Hashtag Cell/HashtagTableViewCell.swift b/Tusker/Views/Hashtag Cell/HashtagTableViewCell.swift index 7f434f03..387c7652 100644 --- a/Tusker/Views/Hashtag Cell/HashtagTableViewCell.swift +++ b/Tusker/Views/Hashtag Cell/HashtagTableViewCell.swift @@ -23,12 +23,10 @@ class HashtagTableViewCell: UITableViewCell { hashtagLabel.text = "#\(hashtag.name)" } - override func setSelected(_ selected: Bool, animated: Bool) { - super.setSelected(selected, animated: animated) - - if selected { - delegate?.selected(tag: hashtag) - } - } - +} + +extension HashtagTableViewCell: SelectableTableViewCell { + func didSelectCell() { + delegate?.selected(tag: hashtag) + } } diff --git a/Tusker/Views/Instance Cell/InstanceTableViewCell.swift b/Tusker/Views/Instance Cell/InstanceTableViewCell.swift index 0ebcd05f..624caf15 100644 --- a/Tusker/Views/Instance Cell/InstanceTableViewCell.swift +++ b/Tusker/Views/Instance Cell/InstanceTableViewCell.swift @@ -9,15 +9,8 @@ import UIKit import Pachyderm -protocol InstanceTableViewCellDelegate { - func didSelectInstance(_ instance: Instance) - func didSelectInstance(_ instance: InstanceSelector.Instance) -} - class InstanceTableViewCell: UITableViewCell { - var delegate: InstanceTableViewCellDelegate? - @IBOutlet weak var thumbnailImageView: UIImageView! @IBOutlet weak var domainLabel: UILabel! @IBOutlet weak var adultLabel: UILabel! @@ -74,16 +67,4 @@ class InstanceTableViewCell: UITableViewCell { } } - override func setSelected(_ selected: Bool, animated: Bool) { - super.setSelected(selected, animated: animated) - - if selected, let delegate = delegate { - if let instance = instance { - delegate.didSelectInstance(instance) - } else if let instance = selectorInstance { - delegate.didSelectInstance(instance) - } - } - } - } diff --git a/Tusker/Views/Notifications/ActionNotificationGroupTableViewCell.swift b/Tusker/Views/Notifications/ActionNotificationGroupTableViewCell.swift index f6bff913..bddfa518 100644 --- a/Tusker/Views/Notifications/ActionNotificationGroupTableViewCell.swift +++ b/Tusker/Views/Notifications/ActionNotificationGroupTableViewCell.swift @@ -149,29 +149,28 @@ class ActionNotificationGroupTableViewCell: UITableViewCell { updateTimestampWorkItem?.cancel() updateTimestampWorkItem = nil } - - override func setSelected(_ selected: Bool, animated: Bool) { - super.setSelected(selected, animated: animated) - - if selected, let delegate = delegate { - let notifications = group.notificationIDs.compactMap(MastodonCache.notification(for:)) - let accountIDs = notifications.map { $0.account.id } - let action: StatusActionAccountListTableViewController.ActionType - switch notifications.first!.kind { - case .favourite: - action = .favorite - case .reblog: - action = .reblog - default: - fatalError() - } - let vc = delegate.statusActionAccountList(action: action, statusID: statusID, statusState: .unknown, accountIDs: accountIDs) - delegate.show(vc) - } - } } +extension ActionNotificationGroupTableViewCell: SelectableTableViewCell { + func didSelectCell() { + guard let delegate = delegate else { return } + let notifications = group.notificationIDs.compactMap(MastodonCache.notification(for:)) + let accountIDs = notifications.map { $0.account.id } + let action: StatusActionAccountListTableViewController.ActionType + switch notifications.first!.kind { + case .favourite: + action = .favorite + case .reblog: + action = .reblog + default: + fatalError() + } + let vc = delegate.statusActionAccountList(action: action, statusID: statusID, statusState: .unknown, accountIDs: accountIDs) + delegate.show(vc) + } +} + extension ActionNotificationGroupTableViewCell: MenuPreviewProvider { func getPreviewProviders(for location: CGPoint, sourceViewController: UIViewController) -> PreviewProviders? { diff --git a/Tusker/Views/Notifications/FollowNotificationGroupTableViewCell.swift b/Tusker/Views/Notifications/FollowNotificationGroupTableViewCell.swift index db8f4f97..84849595 100644 --- a/Tusker/Views/Notifications/FollowNotificationGroupTableViewCell.swift +++ b/Tusker/Views/Notifications/FollowNotificationGroupTableViewCell.swift @@ -112,23 +112,21 @@ class FollowNotificationGroupTableViewCell: UITableViewCell { updateTimestampWorkItem?.cancel() updateTimestampWorkItem = nil } - - override func setSelected(_ selected: Bool, animated: Bool) { - super.setSelected(selected, animated: animated) - - if selected { - let people = group.notificationIDs.compactMap(MastodonCache.notification(for:)).map { $0.account.id } - switch people.count { - case 0: - return - case 1: - delegate?.selected(account: people.first!) - default: - delegate?.showFollowedByList(accountIDs: people) - } + +} + +extension FollowNotificationGroupTableViewCell: SelectableTableViewCell { + func didSelectCell() { + let people = group.notificationIDs.compactMap(MastodonCache.notification(for:)).map { $0.account.id } + switch people.count { + case 0: + return + case 1: + delegate?.selected(account: people.first!) + default: + delegate?.showFollowedByList(accountIDs: people) } } - } extension FollowNotificationGroupTableViewCell: MenuPreviewProvider { diff --git a/Tusker/Views/SelectableTableViewCell.swift b/Tusker/Views/SelectableTableViewCell.swift new file mode 100644 index 00000000..7d27ce54 --- /dev/null +++ b/Tusker/Views/SelectableTableViewCell.swift @@ -0,0 +1,13 @@ +// +// SelectableTableViewCell.swift +// Tusker +// +// Created by Shadowfacts on 12/14/19. +// Copyright © 2019 Shadowfacts. All rights reserved. +// + +import UIKit + +protocol SelectableTableViewCell { + func didSelectCell() +} diff --git a/Tusker/Views/Status/TimelineStatusTableViewCell.swift b/Tusker/Views/Status/TimelineStatusTableViewCell.swift index 7425cd46..15be357b 100644 --- a/Tusker/Views/Status/TimelineStatusTableViewCell.swift +++ b/Tusker/Views/Status/TimelineStatusTableViewCell.swift @@ -119,14 +119,6 @@ class TimelineStatusTableViewCell: BaseStatusTableViewCell { showPinned = false } - override func setSelected(_ selected: Bool, animated: Bool) { - super.setSelected(selected, animated: animated) - - if selected { - delegate?.selected(status: statusID, state: statusState.copy()) - } - } - @objc func reblogLabelPressed() { guard let rebloggerID = rebloggerID else { return } delegate?.selected(account: rebloggerID) @@ -141,6 +133,12 @@ class TimelineStatusTableViewCell: BaseStatusTableViewCell { } +extension TimelineStatusTableViewCell: SelectableTableViewCell { + func didSelectCell() { + delegate?.selected(status: statusID, state: statusState.copy()) + } +} + extension TimelineStatusTableViewCell: TableViewSwipeActionProvider { func leadingSwipeActionsConfiguration() -> UISwipeActionsConfiguration? {