Browse Source

show slection background only if tableView is in editing mode, introduce protocol SelectableCell for reuse

cyberta 3 years ago
parent
commit
3cb40c43be

+ 4 - 0
deltachat-ios.xcodeproj/project.pbxproj

@@ -72,6 +72,7 @@
 		30860EE926F49E64002651A6 /* DownloadOnDemandViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30860EE826F49E64002651A6 /* DownloadOnDemandViewController.swift */; };
 		3095A351237DD1F700AB07F7 /* MediaPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3095A350237DD1F700AB07F7 /* MediaPicker.swift */; };
 		30A4149724F6EFBE00EC91EB /* InfoMessageCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30A4149624F6EFBE00EC91EB /* InfoMessageCell.swift */; };
+		30AAD71B2762869600DE3DC1 /* SelectableCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30AAD71A2762869600DE3DC1 /* SelectableCell.swift */; };
 		30B0ACFA24AB5B99004D5E29 /* SettingsEphemeralMessageController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30B0ACF924AB5B99004D5E29 /* SettingsEphemeralMessageController.swift */; };
 		30C0D49D237C4908008E2A0E /* CertificateCheckController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30C0D49C237C4908008E2A0E /* CertificateCheckController.swift */; };
 		30C2BFFE27032375005505DA /* ChatSearchAccessoryBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30C2BFFD27032375005505DA /* ChatSearchAccessoryBar.swift */; };
@@ -324,6 +325,7 @@
 		30860EE826F49E64002651A6 /* DownloadOnDemandViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DownloadOnDemandViewController.swift; sourceTree = "<group>"; };
 		3095A350237DD1F700AB07F7 /* MediaPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MediaPicker.swift; sourceTree = "<group>"; };
 		30A4149624F6EFBE00EC91EB /* InfoMessageCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoMessageCell.swift; sourceTree = "<group>"; };
+		30AAD71A2762869600DE3DC1 /* SelectableCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectableCell.swift; sourceTree = "<group>"; };
 		30AC265E237F1807002A943F /* AvatarHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AvatarHelper.swift; sourceTree = "<group>"; };
 		30B0ACF924AB5B99004D5E29 /* SettingsEphemeralMessageController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsEphemeralMessageController.swift; sourceTree = "<group>"; };
 		30C0D49C237C4908008E2A0E /* CertificateCheckController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CertificateCheckController.swift; sourceTree = "<group>"; };
@@ -641,6 +643,7 @@
 				30A4149624F6EFBE00EC91EB /* InfoMessageCell.swift */,
 				3008CB7124F93EB900E6A617 /* AudioMessageCell.swift */,
 				3010968826838A040032CBA0 /* VideoInviteCell.swift */,
+				30AAD71A2762869600DE3DC1 /* SelectableCell.swift */,
 			);
 			path = Cells;
 			sourceTree = "<group>";
@@ -1283,6 +1286,7 @@
 				303492B32577E40700A523D0 /* DocumentPreview.swift in Sources */,
 				3008CB7424F9436C00E6A617 /* AudioPlayerView.swift in Sources */,
 				AED62BCE247687E6009E220D /* LocationStreamingIndicator.swift in Sources */,
+				30AAD71B2762869600DE3DC1 /* SelectableCell.swift in Sources */,
 				AE9DAF0F22C278C6004C9591 /* ChatTitleView.swift in Sources */,
 				AE4AEE3522B1030D000AA495 /* PreviewController.swift in Sources */,
 				7070FB9B2101ECBB000DC258 /* NewGroupController.swift in Sources */,

+ 12 - 0
deltachat-ios/Chat/ChatViewController.swift

@@ -715,6 +715,7 @@ class ChatViewController: UITableViewController {
         }
 
         cell.baseDelegate = self
+        cell.showSelectionBackground(tableView.isEditing)
         cell.update(dcContext: dcContext,
                     msg: message,
                     messageStyle: configureMessageStyle(for: message, at: indexPath),
@@ -831,6 +832,14 @@ class ChatViewController: UITableViewController {
         }
     }
 
+    override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
+        if let cell = tableView.cellForRow(at: indexPath) as? SelectableCell {
+            cell.showSelectionBackground(tableView.isEditing)
+            return indexPath
+        }
+        return nil
+    }
+
     override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
         if tableView.isEditing {
             handleEditingBar()
@@ -1643,6 +1652,9 @@ class ChatViewController: UITableViewController {
             if tableView.indexPathsForSelectedRows?.contains(indexPath) ?? false {
                 tableView.deselectRow(at: indexPath, animated: false)
             } else {
+                if let cell = tableView.cellForRow(at: indexPath) as? SelectableCell {
+                    cell.showSelectionBackground(true)
+                }
                 tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
             }
             handleEditingBar()

+ 12 - 2
deltachat-ios/Chat/Views/Cells/BaseMessageCell.swift

@@ -168,8 +168,11 @@ public class BaseMessageCell: UITableViewCell {
         container.isUserInteractionEnabled = true
         return container
     }()
+    
+    private var showSelectionBackground: Bool
 
     override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
+        showSelectionBackground = false
         super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
         clipsToBounds = false
         backgroundColor = .none
@@ -275,8 +278,8 @@ public class BaseMessageCell: UITableViewCell {
 
     public override func setSelected(_ selected: Bool, animated: Bool) {
          super.setSelected(selected, animated: animated)
-         if selected {
-             selectedBackgroundView?.backgroundColor = DcColors.chatBackgroundColor.withAlphaComponent(0.25)
+         if selected && showSelectionBackground {
+             selectedBackgroundView?.backgroundColor = DcColors.chatBackgroundColor.withAlphaComponent(0.5)
          } else {
              selectedBackgroundView?.backgroundColor = .clear
          }
@@ -443,6 +446,7 @@ public class BaseMessageCell: UITableViewCell {
         messageLabel.delegate = nil
         quoteView.prepareForReuse()
         actionButton.isEnabled = true
+        showSelectionBackground = false
     }
 
     // MARK: - Context menu
@@ -509,6 +513,12 @@ extension BaseMessageCell: MessageLabelDelegate {
     public func didSelectCustom(_ pattern: String, match: String?) {}
 }
 
+extension BaseMessageCell: SelectableCell {
+    public func showSelectionBackground(_ show: Bool) {
+        showSelectionBackground = show
+    }
+}
+
 // MARK: - BaseMessageCellDelegate
 // this delegate contains possible events from base cells or from derived cells
 public protocol BaseMessageCellDelegate: class {

+ 3 - 0
deltachat-ios/Chat/Views/Cells/SelectableCell.swift

@@ -0,0 +1,3 @@
+public protocol SelectableCell {
+    func showSelectionBackground(_ show: Bool)
+}