Selaa lähdekoodia

create a new ProfileView controller, that doesn't use a UITableView

cyberta 6 vuotta sitten
vanhempi
commit
21f14e5ada

+ 133 - 0
deltachat-ios/Controller/NewProfileViewController.swift

@@ -0,0 +1,133 @@
+//
+//  NewProfileViewController.swift
+//  deltachat-ios
+//
+
+import Foundation
+import UIKit
+
+
+class NewProfileViewController: UIViewController {
+	weak var coordinator: ProfileCoordinator?
+
+	var contact: DCContact? {
+		// This is nil if we do not have an account setup yet
+		if !DCConfig.configured {
+			return nil
+		}
+		return DCContact(id: Int(DC_CONTACT_ID_SELF))
+	}
+	
+	var fingerprint: String? {
+		if !DCConfig.configured {
+			return nil
+		}
+		
+		if let cString = dc_get_securejoin_qr(mailboxPointer, 0) {
+			return String(cString: cString)
+		}
+		
+		return nil
+	}
+
+	override func loadView() {
+		let view = UIView()
+		view.backgroundColor = UIColor.white
+		self.view = view
+	}
+	
+	override func viewDidLoad() {
+		super.viewDidLoad()
+		title = String.localized("my_profile")
+		self.edgesForExtendedLayout = []
+
+		let contactCell = createContactCell()
+		let qrCode = createQRCodeView()
+		let qrCodeScanner = createQRCodeScannerButton()
+	
+		self.view.addSubview(contactCell)
+		self.view.addSubview(qrCode)
+		self.view.addSubview(qrCodeScanner)
+
+		self.view.addConstraint(contactCell.constraintAlignTopTo(self.view))
+		self.view.addConstraint(contactCell.constraintAlignLeadingTo(self.view))
+		self.view.addConstraint(contactCell.constraintAlignTrailingTo(self.view))
+		self.view.addConstraint(qrCode.constraintCenterYTo(self.view))
+		self.view.addConstraint(qrCode.constraintCenterXTo(self.view))
+		self.view.addConstraint(qrCodeScanner.constraintToBottomOf(qrCode, paddingTop: 25))
+		self.view.addConstraint(qrCodeScanner.constraintCenterXTo(self.view))
+	}
+	
+	private func createQRCodeScannerButton() -> UIView {
+		let btn = UIButton.init(type: UIButton.ButtonType.system)
+		btn.translatesAutoresizingMaskIntoConstraints = false
+		btn.setTitle(String.localized("qrscan_title"), for: .normal)
+		btn.addTarget(self, action:#selector(self.openQRCodeScanner), for: .touchUpInside)
+		return btn
+	}
+
+	@objc func openQRCodeScanner() {
+		let qrCodeReaderController = QrCodeReaderController()
+		if let ctrl = navigationController {
+			ctrl.pushViewController(qrCodeReaderController, animated: true)
+		}
+	}
+	
+	private func createQRCodeView() -> UIView {
+		if let fingerprint = self.fingerprint {
+			let width: CGFloat = 130
+			
+			let frame = CGRect(origin: .zero, size: .init(width: width, height: width))
+			let imageView = QRCodeView(frame: frame)
+			imageView.generateCode(
+				fingerprint,
+				foregroundColor: .darkText,
+				backgroundColor: .white
+			)
+			imageView.translatesAutoresizingMaskIntoConstraints = false
+			imageView.widthAnchor.constraint(equalToConstant: width).isActive = true
+			imageView.heightAnchor.constraint(equalToConstant: width).isActive = true
+			imageView.translatesAutoresizingMaskIntoConstraints = false
+			return imageView
+		}
+		return UIImageView()
+	}
+	
+	private func createContactCell() -> UIView {
+		let bg = UIColor(red: 248 / 255, green: 248 / 255, blue: 255 / 255, alpha: 1.0)
+		
+		let profileView = ProfileView(frame: CGRect())
+		if let contact = self.contact {
+			let name = DCConfig.displayname ?? contact.name
+			profileView.setBackgroundColor(bg)
+			profileView.nameLabel.text = name
+			profileView.emailLabel.text = contact.email
+			profileView.darkMode = false
+			if let img = contact.profileImage {
+				profileView.setImage(img)
+			} else {
+				profileView.setBackupImage(name: name, color: contact.color)
+			}
+			profileView.setVerified(isVerified: contact.isVerified)
+		} else {
+			profileView.nameLabel.text = String.localized("no_account_setup")
+		}
+		
+		return profileView
+	}
+	
+	override func viewWillAppear(_: Bool) {
+		navigationController?.navigationBar.prefersLargeTitles = true
+	}
+	
+	func displayNewChat(contactId: Int) {
+		let chatId = dc_create_chat_by_contact_id(mailboxPointer, UInt32(contactId))
+		let chatVC = ChatViewController(chatId: Int(chatId))
+		
+		chatVC.hidesBottomBarWhenPushed = true
+		navigationController?.pushViewController(chatVC, animated: true)
+	}
+	
+	
+}
+

+ 192 - 0
deltachat-ios/View/ProfileView.swift

@@ -0,0 +1,192 @@
+//
+//  ProfileView.swift
+//  deltachat-ios
+//
+//  Created by Macci on 05.08.19.
+//  Copyright © 2019 Jonas Reinsch. All rights reserved.
+//
+
+import UIKit
+
+class ProfileView: UIView {
+
+	private let initialsLabelSize: CGFloat = 54
+	private let imgSize: CGFloat = 25
+	
+	override init(frame: CGRect) {
+		super.init(frame: frame)
+		translatesAutoresizingMaskIntoConstraints = false
+		setupSubviews()
+	}
+	
+	required init?(coder _: NSCoder) {
+		fatalError("init(coder:) has not been implemented")
+	}
+
+	let avatar: UIView = {
+		let avatar = UIView()
+		return avatar
+	}()
+	
+	lazy var imgView: UIImageView = {
+		let imgView = UIImageView()
+		let img = UIImage(named: "approval")!.withRenderingMode(.alwaysTemplate)
+		imgView.isHidden = true
+		imgView.image = img
+		imgView.bounds = CGRect(
+			x: 0,
+			y: 0,
+			width: imgSize, height: imgSize
+		)
+		return imgView
+	}()
+	
+	lazy var initialsLabel: UILabel = {
+		let initialsLabel = UILabel()
+		initialsLabel.textAlignment = NSTextAlignment.center
+		initialsLabel.textColor = UIColor.white
+		initialsLabel.font = UIFont.systemFont(ofSize: 22)
+		initialsLabel.backgroundColor = UIColor.green
+		let initialsLabelCornerRadius = (initialsLabelSize - 6) / 2
+		initialsLabel.layer.cornerRadius = initialsLabelCornerRadius
+		initialsLabel.clipsToBounds = true
+		return initialsLabel
+	}()
+	
+	let nameLabel: UILabel = {
+		let label = UILabel()
+		label.font = UIFont.systemFont(ofSize: 16, weight: .medium)
+		label.lineBreakMode = .byTruncatingTail
+		label.textColor = UIColor(hexString: "2f3944")
+		// label.makeBorder()
+		return label
+		
+	}()
+	
+	let emailLabel: UILabel = {
+		let label = UILabel()
+		label.font = UIFont.systemFont(ofSize: 14)
+		label.textColor = UIColor(hexString: "848ba7")
+		label.lineBreakMode = .byTruncatingTail
+		return label
+	}()
+	
+	/*private let timeLabel: UILabel = {
+		let label = UILabel()
+		label.font = UIFont.systemFont(ofSize: 14)
+		label.textColor = UIColor(hexString: "848ba7")
+		label.textAlignment = .right
+		// label.makeBorder()
+		return label
+	}()*/
+	
+	/*private let deliveryStatusIndicator: UIImageView = {
+		let view = UIImageView()
+		view.tintColor = UIColor.green
+		view.isHidden = true
+		return view
+	}()*/
+	
+	var darkMode: Bool = false {
+		didSet {
+			if darkMode {
+				self.backgroundColor = UIColor.darkGray
+				nameLabel.textColor = UIColor.white
+				emailLabel.textColor = UIColor.white
+			}
+		}
+	}
+	
+	
+	
+	private func setupSubviews() {
+		let margin: CGFloat = 10
+		
+		initialsLabel.translatesAutoresizingMaskIntoConstraints = false
+		avatar.translatesAutoresizingMaskIntoConstraints = false
+		initialsLabel.widthAnchor.constraint(equalToConstant: initialsLabelSize - 6).isActive = true
+		initialsLabel.heightAnchor.constraint(equalToConstant: initialsLabelSize - 6).isActive = true
+		// avatar.backgroundColor = .red
+		
+		avatar.widthAnchor.constraint(equalToConstant: initialsLabelSize).isActive = true
+		avatar.heightAnchor.constraint(equalToConstant: initialsLabelSize).isActive = true
+		
+		avatar.addSubview(initialsLabel)
+		self.addSubview(avatar)
+		
+		initialsLabel.topAnchor.constraint(equalTo: avatar.topAnchor, constant: 3).isActive = true
+		initialsLabel.leadingAnchor.constraint(equalTo: avatar.leadingAnchor, constant: 3).isActive = true
+		initialsLabel.trailingAnchor.constraint(equalTo: avatar.trailingAnchor, constant: -3).isActive = true
+		
+		avatar.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: margin).isActive = true
+		avatar.center.y = self.center.y
+		avatar.center.x += initialsLabelSize / 2
+		avatar.topAnchor.constraint(equalTo: self.topAnchor, constant: margin).isActive = true
+		avatar.bottomAnchor.constraint(lessThanOrEqualTo: self.bottomAnchor, constant: -margin).isActive = true
+		initialsLabel.center = avatar.center
+		
+		//deliveryStatusIndicator.translatesAutoresizingMaskIntoConstraints = false
+		//deliveryStatusIndicator.heightAnchor.constraint(equalToConstant: 25).isActive = true
+		//deliveryStatusIndicator.widthAnchor.constraint(equalToConstant: 25).isActive = true
+		
+		let myStackView = UIStackView()
+		myStackView.translatesAutoresizingMaskIntoConstraints = false
+		myStackView.clipsToBounds = true
+		
+		let toplineStackView = UIStackView()
+		toplineStackView.axis = .horizontal
+		
+		let bottomLineStackView = UIStackView()
+		bottomLineStackView.axis = .horizontal
+		
+		toplineStackView.addArrangedSubview(nameLabel)
+		//toplineStackView.addArrangedSubview(timeLabel)
+		
+		bottomLineStackView.addArrangedSubview(emailLabel)
+		//bottomLineStackView.addArrangedSubview(deliveryStatusIndicator)
+		
+		self.addSubview(myStackView)
+		myStackView.leadingAnchor.constraint(equalTo: avatar.trailingAnchor, constant: margin).isActive = true
+		myStackView.centerYAnchor.constraint(equalTo: avatar.centerYAnchor).isActive = true
+		myStackView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -margin).isActive = true
+		myStackView.axis = .vertical
+		myStackView.addArrangedSubview(toplineStackView)
+		myStackView.addArrangedSubview(bottomLineStackView)
+		
+		imgView.tintColor = DCColors.primary
+		
+		avatar.addSubview(imgView)
+		
+		imgView.center.x = avatar.center.x + (avatar.frame.width / 2) + imgSize - 5
+		imgView.center.y = avatar.center.y + (avatar.frame.height / 2) + imgSize - 5
+	}
+
+	func setBackgroundColor(_ color: UIColor) {
+		self.backgroundColor = color
+	}
+	
+	func setColor(_ color: UIColor) {
+		initialsLabel.backgroundColor = color
+	}
+	
+	func setVerified(isVerified: Bool) {
+		imgView.isHidden = !isVerified
+	}
+	
+	func setImage(_ img: UIImage) {
+		let attachment = NSTextAttachment()
+		attachment.image = img
+		initialsLabel.attributedText = NSAttributedString(attachment: attachment)
+	}
+	
+	func setBackupImage(name: String, color: UIColor) {
+		let text = Utils.getInitials(inputName: name)
+		
+		initialsLabel.textAlignment = .center
+		initialsLabel.text = text
+		
+		setColor(color)
+	}
+
+	
+}