浏览代码

added initials badge with the intention to use this also in contact cell - in groupChatDetail update name now works

Bastian van de Wetering 6 年之前
父节点
当前提交
bae3c42465

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

@@ -105,6 +105,7 @@
 		AEE56D762253431E007DC082 /* AccountSetupController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEE56D752253431E007DC082 /* AccountSetupController.swift */; };
 		AEE56D7D2253ADB4007DC082 /* HudHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEE56D7C2253ADB4007DC082 /* HudHandler.swift */; };
 		AEE56D80225504DB007DC082 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEE56D7F225504DB007DC082 /* Extensions.swift */; };
+		AEE6EC382281AF2D00EDC689 /* InitialsBadge.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEE6EC372281AF2D00EDC689 /* InitialsBadge.swift */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -299,6 +300,7 @@
 		AEE56D752253431E007DC082 /* AccountSetupController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountSetupController.swift; sourceTree = "<group>"; };
 		AEE56D7C2253ADB4007DC082 /* HudHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HudHandler.swift; sourceTree = "<group>"; };
 		AEE56D7F225504DB007DC082 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
+		AEE6EC372281AF2D00EDC689 /* InitialsBadge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InitialsBadge.swift; sourceTree = "<group>"; };
 		FECB35E2B04CD5F5D02C157A /* Pods-deltachat-iosTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-deltachat-iosTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-deltachat-iosTests/Pods-deltachat-iosTests.release.xcconfig"; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
@@ -636,6 +638,7 @@
 				AE38B31722672DFC00EC37A1 /* ActionCell.swift */,
 				AE851ACD227CA54300ED86F0 /* InitialsLabel.swift */,
 				AE25F08F22807AD800CDEA66 /* GroupNameCell.swift */,
+				AEE6EC372281AF2D00EDC689 /* InitialsBadge.swift */,
 			);
 			path = View;
 			sourceTree = "<group>";
@@ -997,6 +1000,7 @@
 				7A451DBE1FB4AD0700177250 /* Wrapper.swift in Sources */,
 				AE851ACE227CA54400ED86F0 /* InitialsLabel.swift in Sources */,
 				7070FB6420FF345F000DC258 /* dc_dehtml.c in Sources */,
+				AEE6EC382281AF2D00EDC689 /* InitialsBadge.swift in Sources */,
 				70B8882E2091B8550074812E /* ContactCell.swift in Sources */,
 				7070FB8D20FF4118000DC258 /* dc_sqlite3.c in Sources */,
 				7A451D941FB1B1DB00177250 /* wrapper.c in Sources */,

+ 41 - 13
deltachat-ios/Controller/SingleChatDetailViewController.swift

@@ -11,7 +11,7 @@ import UIKit
 class ChatDetailViewController: UIViewController {
 	weak var coordinator: ChatDetailCoordinator?
 
-	fileprivate let chat: MRChat
+	fileprivate var chat: MRChat
 	var chatDetailTable: UITableView = {
 		let table = UITableView(frame: .zero, style: .grouped)
 		table.bounces = false
@@ -152,10 +152,16 @@ extension SingleChatDetailViewController: UITableViewDelegate, UITableViewDataSo
 	}
 }
 
-
 class GroupChatDetailViewController: ChatDetailViewController {
 
 //	var currentUserChatId:
+	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
@@ -167,6 +173,19 @@ class GroupChatDetailViewController: ChatDetailViewController {
 		title = "Group Info"
 		chatDetailTable.delegate = self
 		chatDetailTable.dataSource = self
+		navigationItem.rightBarButtonItem = editBarButtonItem
+	}
+
+	@objc override func editButtonPressed() {
+		if editingGroupName {
+			let newName = editGroupCell.getGroupName()
+			dc_set_chat_name(mailboxPointer, UInt32(chat.id), newName)
+			self.chat = MRChat(id: chat.id) // reload
+		}
+
+		editingGroupName = !editingGroupName
+		editBarButtonItem.title = editingGroupName ? "Save" : "Edit"
+		chatDetailTable.reloadData()
 	}
 }
 
@@ -183,19 +202,28 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
 		if section == 0 {
 			let bg = UIColor(red: 248 / 255, green: 248 / 255, blue: 255 / 255, alpha: 1.0)
 
-			let contactCell = ContactCell()
-			contactCell.backgroundColor = bg
-			contactCell.nameLabel.text = chat.name
-			contactCell.emailLabel.text = chat.subtitle
-			contactCell.darkMode = false
-			contactCell.selectionStyle = .none
-			if let img = chat.profileImage {
-				contactCell.setImage(img)
+			if editingGroupName {
+				editGroupCell.groupBadge.setColor(chat.color)
+				editGroupCell.backgroundColor = bg
+				editGroupCell.inputField.text = chat.name
+				editGroupCell.groupBadge.setText(chat.name)
+				return editGroupCell
 			} else {
-				contactCell.setBackupImage(name: chat.name, color: chat.color)
+
+				let contactCell = ContactCell()
+				contactCell.backgroundColor = bg
+				contactCell.nameLabel.text = chat.name
+				contactCell.emailLabel.text = chat.subtitle
+				contactCell.darkMode = false
+				contactCell.selectionStyle = .none
+				if let img = chat.profileImage {
+					contactCell.setImage(img)
+				} else {
+					contactCell.setBackupImage(name: chat.name, color: chat.color)
+				}
+				contactCell.setVerified(isVerified: chat.isVerified)
+				return contactCell
 			}
-			contactCell.setVerified(isVerified: chat.isVerified)
-			return contactCell
 		} else {
 			return nil
 		}

+ 1 - 1
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -229,7 +229,7 @@ class NewChatCoordinator: Coordinator {
 
 class ChatDetailCoordinator: Coordinator {
 	let navigationController: UINavigationController
-	
+
 	private var childCoordinators: [Coordinator] = []
 
 	init(navigationController: UINavigationController) {

+ 3 - 1
deltachat-ios/View/ContactCell.swift

@@ -8,6 +8,8 @@
 
 import UIKit
 
+// TODO: integrate InitialsBadge in here
+
 class ContactCell: UITableViewCell {
   private let initialsLabelSize: CGFloat = 54
   private let imgSize: CGFloat = 25
@@ -61,7 +63,7 @@ class ContactCell: UITableViewCell {
   }
 
   private func setupSubviews() {
-    let margin: CGFloat = 15
+    let margin: CGFloat = 10
 
     initialsLabel.translatesAutoresizingMaskIntoConstraints = false
     avatar.translatesAutoresizingMaskIntoConstraints = false

+ 12 - 5
deltachat-ios/View/GroupNameCell.swift

@@ -9,12 +9,15 @@
 import UIKit
 
 class GroupLabelCell: UITableViewCell {
-  private let groupBadgeSize: CGFloat = 60
+	var groupBadgeSize: CGFloat = 54
+
   var groupNameUpdated: ((String) -> Void)? // use this callback to update editButton in navigationController
 
-  lazy var groupBadge: InitialsLabel = {
-    let badge = InitialsLabel(size: groupBadgeSize)
-    badge.set(color: UIColor.lightGray)
+  lazy var groupBadge: InitialsBadge = {
+    let badge = InitialsBadge(frame: .zero)
+		badge.layer.cornerRadius = groupBadgeSize / 2
+		badge.clipsToBounds = true
+    badge.setColor(UIColor.lightGray)
     return badge
   }()
 
@@ -58,7 +61,11 @@ class GroupLabelCell: UITableViewCell {
 
   @objc func nameFieldChanged() {
     let groupName = inputField.text ?? ""
-    groupBadge.set(name: groupName)
+    groupBadge.setText(groupName)
     groupNameUpdated?(groupName)
   }
+
+	func getGroupName() -> String {
+		return inputField.text ?? ""
+	}
 }

+ 54 - 0
deltachat-ios/View/InitialsBadge.swift

@@ -0,0 +1,54 @@
+//
+//  IntitialsBadge.swift
+//  deltachat-ios
+//
+//  Created by Bastian van de Wetering on 07.05.19.
+//  Copyright © 2019 Jonas Reinsch. All rights reserved.
+//
+
+import UIKit
+
+
+// shall be used for contactCell/ groups
+
+class InitialsBadge: UIView {
+
+	private lazy var label: UILabel = {
+		let label = UILabel()
+		label.textAlignment = NSTextAlignment.center
+		label.textColor = UIColor.white
+		label.font = UIFont.systemFont(ofSize: 22)
+		// label.adjustsFontSizeToFitWidth = true
+		return label
+	}()
+
+	override init(frame: CGRect) {
+		super.init(frame: frame)
+		setupSubviews()
+
+		layer.cornerRadius = frame.width / 2
+		clipsToBounds = true
+	}
+	
+	required init?(coder aDecoder: NSCoder) {
+		fatalError("init(coder:) has not been implemented")
+	}
+
+	private func setupSubviews() {
+		addSubview(label)
+		label.translatesAutoresizingMaskIntoConstraints = false
+		label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 2).isActive = true
+		label.topAnchor.constraint(equalTo: topAnchor, constant: 2).isActive = true
+		label.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -2).isActive = true
+		label.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -2).isActive = true
+	}
+
+	func setColor(_ color: UIColor) {
+		backgroundColor = color
+	}
+
+	func setText(_ text: String) {
+		let initials = Utils.getInitials(inputName: text)
+		label.text = initials
+	}
+}