Преглед на файлове

editButton in GroupDetail disabled if user left group - deleting from groups - leave groups work now

Bastian van de Wetering преди 6 години
родител
ревизия
565f8b1184
променени са 3 файла, в които са добавени 79 реда и са изтрити 24 реда
  1. 68 9
      deltachat-ios/Controller/SingleChatDetailViewController.swift
  2. 6 9
      deltachat-ios/View/ActionCell.swift
  3. 5 6
      deltachat-ios/View/ContactCell.swift

+ 68 - 9
deltachat-ios/Controller/SingleChatDetailViewController.swift

@@ -12,12 +12,14 @@ class ChatDetailViewController: UIViewController {
 	weak var coordinator: ChatDetailCoordinator?
 
 	fileprivate var chat: MRChat
+
 	var chatDetailTable: UITableView = {
 		let table = UITableView(frame: .zero, style: .grouped)
 		table.bounces = false
 		table.register(UITableViewCell.self, forCellReuseIdentifier: "tableCell")
 		table.register(ActionCell.self, forCellReuseIdentifier: "actionCell")
 		table.register(ContactCell.self, forCellReuseIdentifier: "contactCell")
+
 		return table
 	}()
 
@@ -70,7 +72,6 @@ class SingleChatDetailViewController: ChatDetailViewController {
 	override func viewDidLoad() {
 		super.viewDidLoad()
 		title = "Info"
-		navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Edit", style: .plain, target: self, action: #selector(editButtonPressed))
 		chatDetailTable.delegate = self
 		chatDetailTable.dataSource = self
 
@@ -125,6 +126,7 @@ extension SingleChatDetailViewController: UITableViewDelegate, UITableViewDataSo
 		if section == 0 {
 			let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath)
 			cell.textLabel?.text = "Notifications"
+			cell.selectionStyle = .none
 			return cell
 		} else if section == 1 {
 			let cell = tableView.dequeueReusableCell(withIdentifier: "actionCell", for: indexPath) as! ActionCell
@@ -160,14 +162,12 @@ class GroupChatDetailViewController: ChatDetailViewController {
 	let editGroupCell = GroupLabelCell()
 
 	var editingGroupName: Bool = false
+
 	lazy var editBarButtonItem: UIBarButtonItem = {
 		UIBarButtonItem(title: editingGroupName ? "Done" : "Edit", style: .plain, target: self, action: #selector(editButtonPressed))
 	}()
 
-	var groupMembers: [MRContact] {
-		let ids = chat.contactIds
-		return ids.map({MRContact(id: $0)})
-	}
+	var groupMembers: [MRContact] = []
 
 	override func viewDidLoad() {
 		super.viewDidLoad()
@@ -177,6 +177,17 @@ class GroupChatDetailViewController: ChatDetailViewController {
 		navigationItem.rightBarButtonItem = editBarButtonItem
 	}
 
+	override func viewWillAppear(_ animated: Bool) {
+		updateGroupMembers()
+		editBarButtonItem.isEnabled = currentUser != nil
+	}
+
+	private func updateGroupMembers() {
+		let ids = chat.contactIds
+		groupMembers = ids.map({MRContact(id: $0)})
+		chatDetailTable.reloadData()
+	}
+
 	@objc override func editButtonPressed() {
 		if editingGroupName {
 			let newName = editGroupCell.getGroupName()
@@ -188,6 +199,15 @@ class GroupChatDetailViewController: ChatDetailViewController {
 		editBarButtonItem.title = editingGroupName ? "Save" : "Edit"
 		chatDetailTable.reloadData()
 	}
+
+	private func leaveGroup() {
+		if let userId = currentUser?.id {
+			dc_remove_contact_from_chat(mailboxPointer, UInt32(chat.id), UInt32(userId))
+			editBarButtonItem.isEnabled = false
+			updateGroupMembers()
+		}
+	}
+
 }
 
 extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSource {
@@ -231,6 +251,12 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
 	}
 
 	func numberOfSections(in tableView: UITableView) -> Int {
+		/*
+		section 0: config
+		section 1: members
+		section 2: leave group (optional - if user already left group this option will be hidden)
+		*/
+
 		if currentUser == nil {
 			return 2
 		}
@@ -256,6 +282,7 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
 		if section == 0 {
 			let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath)
 			cell.textLabel?.text = "Notifications"
+			cell.selectionStyle = .none
 			return cell
 		} else  if section == 1 {
 			let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath) as! ContactCell
@@ -275,6 +302,7 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
 		return UITableViewCell(frame: .zero)
 	}
 
+
 	func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
 		let section = indexPath.section
 		let row = indexPath.row
@@ -283,12 +311,43 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
 		} else if section == 1 {
 			// ignore for now - in Telegram tapping a contactCell leads into ContactDetail
 		} else if section == 2 {
-			// leave group
-			if let userId = currentUser?.id {
-				dc_remove_contact_from_chat(mailboxPointer, UInt32(chat.id), UInt32(userId))
-				tableView.reloadData()
+			leaveGroup()
+		}
+	}
+
+	func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
+		let section = indexPath.section
+		let row = indexPath.row
+
+		if let currentUser = currentUser {
+			if section == 1 && groupMembers[row].id != currentUser.id {
+				return true
 			}
+		}
+		return false
+	}
 
+	func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
+
+		let section = indexPath.section
+		let row = indexPath.row
+
+		// assigning swipe by delete to members (except for current user)
+		if section == 1 && groupMembers[row].id != currentUser?.id {
+			let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
+
+				let memberId = self.groupMembers[row].id
+				let success = dc_remove_contact_from_chat(mailboxPointer, UInt32(self.chat.id), UInt32(memberId))
+				if success == 1 {
+					self.groupMembers.remove(at: row)
+					tableView.deleteRows(at: [indexPath], with: .fade)
+					tableView.reloadData()
+				}
+			}
+			delete.backgroundColor = UIColor.red
+			return [delete]
+		} else {
+			return nil
 		}
 	}
 }

+ 6 - 9
deltachat-ios/View/ActionCell.swift

@@ -31,15 +31,16 @@ class ActionCell: UITableViewCell {
   }()
 
   // use this constructor if cell won't be reused
-  init(title: String) {
-    actionTitle = title
-    super.init(style: .default, reuseIdentifier: nil)
-    setupSubviews()
-  }
+  convenience init(title: String) {
+    self.init(style: .default, reuseIdentifier: nil)
+		actionTitle = title
+		selectionStyle = .none
+	}
 
   override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
     super.init(style: style, reuseIdentifier: reuseIdentifier)
     setupSubviews()
+		selectionStyle = .none
   }
 
   required init?(coder _: NSCoder) {
@@ -51,10 +52,6 @@ class ActionCell: UITableViewCell {
     // Initialization code
   }
 
-  override func setSelected(_: Bool, animated _: Bool) {
-    // no selection style ...
-  }
-
   private func setupSubviews() {
     contentView.addSubview(actionLabel)
     actionLabel.translatesAutoresizingMaskIntoConstraints = false

+ 5 - 6
deltachat-ios/View/ContactCell.swift

@@ -58,8 +58,11 @@ class ContactCell: UITableViewCell {
   }
 
   override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
-    super.init(style: style, reuseIdentifier: reuseIdentifier)
-    setupSubviews()
+
+		super.init(style: style, reuseIdentifier: reuseIdentifier)
+		selectionStyle = .none
+		setupSubviews()
+
   }
 
   private func setupSubviews() {
@@ -116,10 +119,6 @@ class ContactCell: UITableViewCell {
     imgView.center.y = avatar.center.y + (avatar.frame.height / 2) + imgSize - 5
   }
 
-	override func setSelected(_ selected: Bool, animated: Bool) {
-		// no animation please
-	}
-
   func setVerified(isVerified: Bool) {
     imgView.isHidden = !isVerified
   }