Prechádzať zdrojové kódy

added qrCodeView and hintLabel to contentView

nayooti 5 rokov pred
rodič
commit
693ec9c986
1 zmenil súbory, kde vykonal 61 pridanie a 61 odobranie
  1. 61 61
      deltachat-ios/Controller/QrViewController.swift

+ 61 - 61
deltachat-ios/Controller/QrViewController.swift

@@ -21,7 +21,8 @@ class QrViewController: UIViewController {
     }()
 
     private lazy var qrContentView: QrViewContentView = {
-        let view = QrViewContentView()
+        let qrCode = dcContext.getSecurejoinQr(chatId: 0)
+        let view = QrViewContentView(contact: contact, qrCode: qrCode)
         view.translatesAutoresizingMaskIntoConstraints = false
         return view
     }()
@@ -44,12 +45,12 @@ class QrViewController: UIViewController {
 
     override func viewDidLayoutSubviews() {
         super.viewDidLayoutSubviews()
-        qrContentView.minContainerHeight = view.frame.height
+        qrContentView.minContainerHeight = view.frame.height - (view.safeAreaInsets.top + view.safeAreaInsets.bottom)
     }
 
     override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
         super.viewWillTransition(to: size, with: coordinator)
-        qrContentView.minContainerHeight = size.height
+        qrContentView.minContainerHeight = size.height - (view.safeAreaInsets.top + view.safeAreaInsets.bottom)
         scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
     }
 
@@ -63,9 +64,9 @@ class QrViewController: UIViewController {
         let contentGuide = scrollView.contentLayoutGuide
 
         frameGuide.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
-        frameGuide.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
+        frameGuide.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
         frameGuide.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
-        frameGuide.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
+        frameGuide.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
 
         contentGuide.leadingAnchor.constraint(equalTo: qrContentView.leadingAnchor).isActive = true
         contentGuide.topAnchor.constraint(equalTo: qrContentView.topAnchor).isActive = true
@@ -89,6 +90,21 @@ class QrViewController: UIViewController {
 // MARK: - QrViewContentView
 class QrViewContentView: UIView {
 
+    private var qrCodeView: QRCodeView = {
+        let view = QRCodeView(frame: .zero)
+        return view
+    }()
+
+    private var hintLabel: UILabel = {
+        let label = UILabel.init()
+        label.lineBreakMode = .byWordWrapping
+        label.numberOfLines = 0
+        label.textAlignment = .center
+        label.font = UIFont.systemFont(ofSize: 14)
+        label.makeBorder()
+        return label
+    }()
+
     private let container = UIView()
 
     var minContainerHeight: CGFloat = 0 {
@@ -101,15 +117,28 @@ class QrViewContentView: UIView {
         return container.heightAnchor.constraint(greaterThanOrEqualToConstant: 0)
     }()
 
-    init() {
+    init(contact: DcContact?, qrCode: String?) {
         super.init(frame: .zero)
+        if let contact = contact {
+            hintLabel.text = String.localizedStringWithFormat(
+                String.localized("qrshow_join_contact_hint"),
+                contact.email
+            )
+        }
+        if let qrCode = qrCode {
+            qrCodeView.generateCode(
+                qrCode,
+                foregroundColor: .darkText,
+                backgroundColor: .white
+            )
+        }
         setupSubviews()
     }
-    
+
     required init?(coder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }
-    
+
     private func setupSubviews() {
         addSubview(container)
         container.translatesAutoresizingMaskIntoConstraints = false
@@ -121,58 +150,29 @@ class QrViewContentView: UIView {
 
         containerMinHeightConstraint.isActive = true
 
+        let stackView = UIStackView(arrangedSubviews: [qrCodeView, hintLabel])
+        stackView.axis = .vertical
+        stackView.spacing = 10
+        stackView.translatesAutoresizingMaskIntoConstraints = false
+        container.addSubview(stackView)
+        stackView.centerXAnchor.constraint(equalTo: container.centerXAnchor).isActive = true
+        stackView.centerYAnchor.constraint(equalTo: container.centerYAnchor).isActive = true
+
+        qrCodeView.translatesAutoresizingMaskIntoConstraints = false
+        let qrCodeMinWidth = stackView.widthAnchor.constraint(lessThanOrEqualToConstant: 260)
+        qrCodeMinWidth.priority = UILayoutPriority(rawValue: 1000)
+        qrCodeMinWidth.isActive = true
+        let qrCodeDefaultWidth = stackView.widthAnchor.constraint(equalTo: container.widthAnchor, multiplier: 1)
+        qrCodeDefaultWidth.priority = UILayoutPriority(500)
+        qrCodeDefaultWidth.isActive = true
+        qrCodeView.heightAnchor.constraint(equalTo: qrCodeView.widthAnchor, multiplier: 1).isActive = true
+
+        let stackTopAnchor = stackView.topAnchor.constraint(equalTo: container.layoutMarginsGuide.topAnchor)
+        stackTopAnchor.priority = .defaultLow
+        stackTopAnchor.isActive = true
+
+        let stackBottomAnchor = stackView.bottomAnchor.constraint(equalTo: container.layoutMarginsGuide.bottomAnchor)
+        stackBottomAnchor.priority = .defaultLow
+        stackBottomAnchor.isActive = true
     }
-
 }
-
-/*
- private func createQRCodeCell() -> UITableViewCell {
-     let cell = UITableViewCell(style: .default, reuseIdentifier: "qrCodeCell")
-     let qrCode = createQRCodeView()
-     let infolabel = createInfoLabel()
-
-     cell.contentView.addSubview(qrCode)
-     cell.contentView.addSubview(infolabel)
-     cell.selectionStyle = .none
-
-     let qrCodeConstraints = [qrCode.constraintAlignTopTo(cell.contentView, paddingTop: 25),
-                              qrCode.constraintCenterXTo(cell.contentView)]
-     let infoLabelConstraints = [infolabel.constraintToBottomOf(qrCode, paddingTop: 25),
-                                 infolabel.constraintAlignLeadingTo(cell.contentView, paddingLeading: 8),
-                                 infolabel.constraintAlignTrailingTo(cell.contentView, paddingTrailing: 8)]
-     cell.contentView.addConstraints(qrCodeConstraints)
-     cell.contentView.addConstraints(infoLabelConstraints)
-     return cell
- }
-
- private func createInfoLabel() -> UIView {
-     let label = UILabel.init()
-     label.translatesAutoresizingMaskIntoConstraints = false
-     if let contact = contact {
-         label.text = String.localizedStringWithFormat(String.localized("qrshow_join_contact_hint"), contact.email)
-     }
-     label.lineBreakMode = .byWordWrapping
-     label.numberOfLines = 0
-     label.textAlignment = .center
-     label.font = UIFont.systemFont(ofSize: 14)
-     return label
- }
-
- private func createQRCodeView() -> UIView {
-     let width: CGFloat = 230
-     let frame = CGRect(origin: .zero, size: .init(width: width, height: width))
-     let imageView = QRCodeView(frame: frame)
-     if let qrCode = dcContext.getSecurejoinQr(chatId: 0) {
-         imageView.generateCode(
-             qrCode,
-             foregroundColor: .darkText,
-             backgroundColor: .white
-         )
-     }
-     imageView.translatesAutoresizingMaskIntoConstraints = false
-     imageView.widthAnchor.constraint(equalToConstant: width).isActive = true
-     imageView.heightAnchor.constraint(equalToConstant: width).isActive = true
-     return imageView
- }
-
- */