From 2c6f18fa6ebc5d31bde889dfcc50ee204d36f542 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 4 May 2018 13:57:29 -0400 Subject: [PATCH] Clean up ahead of PR. --- Signal.xcodeproj/project.pbxproj | 4 ++ .../ContactViewController.swift | 48 ++------------ .../ApproveContactShareViewController.swift | 44 ++----------- .../attachments/ContactFieldView.swift | 63 +++++++++++++++++++ .../EditContactShareNameViewController.swift | 42 +------------ 5 files changed, 80 insertions(+), 121 deletions(-) create mode 100644 SignalMessaging/attachments/ContactFieldView.swift diff --git a/Signal.xcodeproj/project.pbxproj b/Signal.xcodeproj/project.pbxproj index 6aad516aa..624e46442 100644 --- a/Signal.xcodeproj/project.pbxproj +++ b/Signal.xcodeproj/project.pbxproj @@ -163,6 +163,7 @@ 3478506C1FD9B78A007B8332 /* NoopNotificationsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 347850681FD9B78A007B8332 /* NoopNotificationsManager.swift */; }; 347850711FDAEB17007B8332 /* OWSUserProfile.m in Sources */ = {isa = PBXBuildFile; fileRef = 3478506F1FDAEB16007B8332 /* OWSUserProfile.m */; }; 347850721FDAEB17007B8332 /* OWSUserProfile.h in Headers */ = {isa = PBXBuildFile; fileRef = 347850701FDAEB16007B8332 /* OWSUserProfile.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 348BB254209CD4B80047AEC2 /* ContactFieldView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 348BB253209CD4B80047AEC2 /* ContactFieldView.swift */; }; 3496744D2076768700080B5F /* OWSMessageBubbleView.m in Sources */ = {isa = PBXBuildFile; fileRef = 3496744C2076768700080B5F /* OWSMessageBubbleView.m */; }; 3496744F2076ACD000080B5F /* LongTextViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3496744E2076ACCE00080B5F /* LongTextViewController.swift */; }; 34A55F3720485465002CC6DE /* OWS2FARegistrationViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 34A55F3520485464002CC6DE /* OWS2FARegistrationViewController.m */; }; @@ -759,6 +760,7 @@ 347850681FD9B78A007B8332 /* NoopNotificationsManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NoopNotificationsManager.swift; sourceTree = ""; }; 3478506F1FDAEB16007B8332 /* OWSUserProfile.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OWSUserProfile.m; sourceTree = ""; }; 347850701FDAEB16007B8332 /* OWSUserProfile.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OWSUserProfile.h; sourceTree = ""; }; + 348BB253209CD4B80047AEC2 /* ContactFieldView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ContactFieldView.swift; path = SignalMessaging/attachments/ContactFieldView.swift; sourceTree = SOURCE_ROOT; }; 348F2EAD1F0D21BC00D4ECE0 /* DeviceSleepManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeviceSleepManager.swift; sourceTree = ""; }; 3495BC911F1426B800B478F5 /* ar */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ar; path = translations/ar.lproj/Localizable.strings; sourceTree = ""; }; 3496744B2076768600080B5F /* OWSMessageBubbleView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OWSMessageBubbleView.h; sourceTree = ""; }; @@ -1602,6 +1604,7 @@ children = ( 34382265209A4E3F0094FEB7 /* ApproveContactShareViewController.swift */, 344F2498200FD03200CFB4F4 /* AttachmentApprovalViewController.swift */, + 348BB253209CD4B80047AEC2 /* ContactFieldView.swift */, 344F248220069E9B00CFB4F4 /* CountryCodeViewController.h */, 344F248320069E9B00CFB4F4 /* CountryCodeViewController.m */, 34382269209B63500094FEB7 /* EditContactShareNameViewController.swift */, @@ -3057,6 +3060,7 @@ 346129E61FD5C0C600532771 /* OWSDatabaseMigrationRunner.m in Sources */, 346129AB1FD1F0EE00532771 /* OWSFormat.m in Sources */, 344F249C200FD03300CFB4F4 /* AttachmentApprovalViewController.swift in Sources */, + 348BB254209CD4B80047AEC2 /* ContactFieldView.swift in Sources */, 451F8A461FD715BA005CB9DA /* OWSGroupAvatarBuilder.m in Sources */, 347850591FD9972E007B8332 /* SwiftSingletons.swift in Sources */, 344F248720069ECB00CFB4F4 /* ModalActivityIndicatorViewController.swift in Sources */, diff --git a/Signal/src/ViewControllers/ContactViewController.swift b/Signal/src/ViewControllers/ContactViewController.swift index e0c1e6ee5..8c03decff 100644 --- a/Signal/src/ViewControllers/ContactViewController.swift +++ b/Signal/src/ViewControllers/ContactViewController.swift @@ -367,48 +367,14 @@ class ContactViewController: OWSViewController, CNContactViewControllerDelegate private func createFieldsView() -> UIView { SwiftAssertIsOnMainThread(#function) - let fieldsView = UIView.container() - fieldsView.layoutMargins = .zero - fieldsView.preservesSuperviewLayoutMargins = false - - var lastRow: UIView? - - let addSpacerRow = { - guard let prevRow = lastRow else { - owsFail("\(self.logTag) missing last row") - return - } - let row = UIView() - row.backgroundColor = UIColor(rgbHex: 0xdedee1) - fieldsView.addSubview(row) - row.autoSetDimension(.height, toSize: 1) - row.autoPinLeadingToSuperviewMargin(withInset: self.hMargin) - row.autoPinTrailingToSuperviewMargin() - row.autoPinEdge(.top, to: .bottom, of: prevRow) - lastRow = row - } - - let addRow: ((UIView) -> Void) = { (row) in - if lastRow != nil { - addSpacerRow() - } - fieldsView.addSubview(row) - row.autoPinLeadingToSuperviewMargin() - row.autoPinTrailingToSuperviewMargin() - if let lastRow = lastRow { - row.autoPinEdge(.top, to: .bottom, of: lastRow) - } else { - row.autoPinEdge(toSuperviewEdge: .top) - } - lastRow = row - } + var rows = [UIView]() if viewMode == .nonSystemContact { - addRow(createActionRow(labelText: NSLocalizedString("CONVERSATION_SETTINGS_NEW_CONTACT", + rows.append(createActionRow(labelText: NSLocalizedString("CONVERSATION_SETTINGS_NEW_CONTACT", comment: "Label for 'new contact' button in conversation settings view."), action: #selector(didPressCreateNewContact))) - addRow(createActionRow(labelText: NSLocalizedString("CONVERSATION_SETTINGS_ADD_TO_EXISTING_CONTACT", + rows.append(createActionRow(labelText: NSLocalizedString("CONVERSATION_SETTINGS_ADD_TO_EXISTING_CONTACT", comment: "Label for 'new contact' button in conversation settings view."), action: #selector(didPressAddToExistingContact))) } @@ -424,7 +390,7 @@ class ContactViewController: OWSViewController, CNContactViewControllerDelegate for phoneNumber in contact.phoneNumbers { let formattedPhoneNumber = PhoneNumber.bestEffortLocalizedPhoneNumber(withE164: phoneNumber.phoneNumber) - addRow(createNameValueRow(name: phoneNumber.localizedLabel(), + rows.append(createNameValueRow(name: phoneNumber.localizedLabel(), value: formattedPhoneNumber, actionBlock: { guard let url = NSURL(string: "tel:\(phoneNumber.phoneNumber)") else { @@ -436,7 +402,7 @@ class ContactViewController: OWSViewController, CNContactViewControllerDelegate } for email in contact.emails { - addRow(createNameValueRow(name: email.localizedLabel(), + rows.append(createNameValueRow(name: email.localizedLabel(), value: email.email, actionBlock: { guard let url = NSURL(string: "mailto:\(email.email)") else { @@ -449,9 +415,7 @@ class ContactViewController: OWSViewController, CNContactViewControllerDelegate // TODO: Should we present addresses here too? How? - lastRow?.autoPinEdge(toSuperviewEdge: .bottom) - - return fieldsView + return ContactFieldView(rows: rows, hMargin: hMargin) } private let hMargin = CGFloat(16) diff --git a/SignalMessaging/attachments/ApproveContactShareViewController.swift b/SignalMessaging/attachments/ApproveContactShareViewController.swift index 571259750..b99c396d8 100644 --- a/SignalMessaging/attachments/ApproveContactShareViewController.swift +++ b/SignalMessaging/attachments/ApproveContactShareViewController.swift @@ -349,51 +349,15 @@ public class ApproveContactShareViewController: OWSViewController, EditContactSh private func createFieldsView() -> UIView { SwiftAssertIsOnMainThread(#function) - let fieldsView = UIView.container() - fieldsView.layoutMargins = .zero - fieldsView.preservesSuperviewLayoutMargins = false + var rows = [UIView]() - var lastRow: UIView? - - let addSpacerRow = { - guard let prevRow = lastRow else { - owsFail("\(self.logTag) missing last row") - return - } - let row = UIView() - row.backgroundColor = UIColor(rgbHex: 0xdedee1) - fieldsView.addSubview(row) - row.autoSetDimension(.height, toSize: 1) - row.autoPinLeadingToSuperviewMargin(withInset: self.hMargin) - row.autoPinTrailingToSuperviewMargin() - row.autoPinEdge(.top, to: .bottom, of: prevRow, withOffset: 0) - lastRow = row - } - - let addRow: ((UIView) -> Void) = { (row) in - if lastRow != nil { - addSpacerRow() - } - fieldsView.addSubview(row) - row.autoPinLeadingToSuperviewMargin(withInset: self.hMargin) - row.autoPinTrailingToSuperviewMargin(withInset: self.hMargin) - if let lastRow = lastRow { - row.autoPinEdge(.top, to: .bottom, of: lastRow, withOffset: 0) - } else { - row.autoPinEdge(toSuperviewEdge: .top, withInset: 0) - } - lastRow = row - } - - addRow(createNameRow()) + rows.append(createNameRow()) for fieldView in fieldViews { - addRow(fieldView) + rows.append(fieldView) } - lastRow?.autoPinEdge(toSuperviewEdge: .bottom, withInset: 0) - - return fieldsView + return ContactFieldView(rows: rows, hMargin: hMargin) } private let hMargin = CGFloat(16) diff --git a/SignalMessaging/attachments/ContactFieldView.swift b/SignalMessaging/attachments/ContactFieldView.swift new file mode 100644 index 000000000..ac4ef71f1 --- /dev/null +++ b/SignalMessaging/attachments/ContactFieldView.swift @@ -0,0 +1,63 @@ +// +// Copyright (c) 2018 Open Whisper Systems. All rights reserved. +// + +import Foundation + +public class ContactFieldView: UIView { + + @available(*, unavailable, message: "use other constructor instead.") + public required init?(coder aDecoder: NSCoder) { + fatalError("Unimplemented") + } + + public required init(rows: [UIView], hMargin: CGFloat) { + super.init(frame: CGRect.zero) + + self.layoutMargins = .zero + self.preservesSuperviewLayoutMargins = false + + addRows(rows: rows, hMargin: hMargin) + } + + private func addRows(rows: [UIView], hMargin: CGFloat) { + + var lastRow: UIView? + + let addSpacerRow = { + guard let prevRow = lastRow else { + owsFail("\(self.logTag) missing last row") + return + } + let row = UIView() + row.backgroundColor = UIColor(rgbHex: 0xdedee1) + self.addSubview(row) + row.autoSetDimension(.height, toSize: 1) + row.autoPinLeadingToSuperviewMargin(withInset: hMargin) + row.autoPinTrailingToSuperviewMargin() + row.autoPinEdge(.top, to: .bottom, of: prevRow, withOffset: 0) + lastRow = row + } + + let addRow: ((UIView) -> Void) = { (row) in + if lastRow != nil { + addSpacerRow() + } + self.addSubview(row) + row.autoPinLeadingToSuperviewMargin(withInset: hMargin) + row.autoPinTrailingToSuperviewMargin(withInset: hMargin) + if let lastRow = lastRow { + row.autoPinEdge(.top, to: .bottom, of: lastRow, withOffset: 0) + } else { + row.autoPinEdge(toSuperviewEdge: .top, withInset: 0) + } + lastRow = row + } + + for row in rows { + addRow(row) + } + + lastRow?.autoPinEdge(toSuperviewEdge: .bottom, withInset: 0) + } +} diff --git a/SignalMessaging/attachments/EditContactShareNameViewController.swift b/SignalMessaging/attachments/EditContactShareNameViewController.swift index 496efc4ff..14111331b 100644 --- a/SignalMessaging/attachments/EditContactShareNameViewController.swift +++ b/SignalMessaging/attachments/EditContactShareNameViewController.swift @@ -265,49 +265,13 @@ public class EditContactShareNameViewController: OWSViewController, ContactNameF private func createFieldsView() -> UIView { SwiftAssertIsOnMainThread(#function) - let fieldsView = UIView.container() - fieldsView.layoutMargins = .zero - fieldsView.preservesSuperviewLayoutMargins = false - - var lastRow: UIView? - - let addSpacerRow = { - guard let prevRow = lastRow else { - owsFail("\(self.logTag) missing last row") - return - } - let row = UIView() - row.backgroundColor = UIColor(rgbHex: 0xdedee1) - fieldsView.addSubview(row) - row.autoSetDimension(.height, toSize: 1) - row.autoPinLeadingToSuperviewMargin(withInset: self.hMargin) - row.autoPinTrailingToSuperviewMargin() - row.autoPinEdge(.top, to: .bottom, of: prevRow, withOffset: 0) - lastRow = row - } - - let addRow: ((UIView) -> Void) = { (row) in - if lastRow != nil { - addSpacerRow() - } - fieldsView.addSubview(row) - row.autoPinLeadingToSuperviewMargin(withInset: self.hMargin) - row.autoPinTrailingToSuperviewMargin(withInset: self.hMargin) - if let lastRow = lastRow { - row.autoPinEdge(.top, to: .bottom, of: lastRow, withOffset: 0) - } else { - row.autoPinEdge(toSuperviewEdge: .top, withInset: 0) - } - lastRow = row - } + var rows = [UIView]() for fieldView in fieldViews { - addRow(fieldView) + rows.append(fieldView) } - lastRow?.autoPinEdge(toSuperviewEdge: .bottom, withInset: 0) - - return fieldsView + return ContactFieldView(rows: rows, hMargin: hMargin) } private let hMargin = CGFloat(16)