From 32eac56aa45d9bbf7b0619abad1a3384e7e44c46 Mon Sep 17 00:00:00 2001 From: Rayhan Nabi Date: Mon, 28 Oct 2019 16:21:32 +0600 Subject: [PATCH 1/8] Project Restructuring --- RNAlertController.xcodeproj/project.pbxproj | 48 +++++++++-- Source/Extensions/UIButton+Action.swift | 15 ---- Source/Helpers/AlertActionWrapper.swift | 24 ++++++ Source/Helpers/AlertButtonType.swift | 20 +++++ Source/Helpers/AlertLabelType.swift | 14 ++++ Source/Helpers/AlertPickerRow.swift | 24 ++++++ Source/Helpers/AlertURL.swift | 14 ++++ Source/Helpers/Button.swift | 23 ++++++ Source/Helpers/Typealiases.swift | 18 +++++ Source/RNAlertController+API.swift | 2 +- Source/RNAlertController.swift | 2 +- Source/Views/AlertActionButton.swift | 76 ------------------ Source/Views/AlertButton.swift | 89 +++++++++++++++------ Source/Views/AlertButtonStackView.swift | 6 +- Source/Views/AlertDatePicker.swift | 3 - Source/Views/AlertLabel.swift | 5 -- Source/Views/AlertPickerView.swift | 18 ----- Source/Views/AlertURLButton.swift | 5 -- 18 files changed, 246 insertions(+), 160 deletions(-) create mode 100644 Source/Helpers/AlertActionWrapper.swift create mode 100644 Source/Helpers/AlertButtonType.swift create mode 100644 Source/Helpers/AlertLabelType.swift create mode 100644 Source/Helpers/AlertPickerRow.swift create mode 100644 Source/Helpers/AlertURL.swift create mode 100644 Source/Helpers/Button.swift create mode 100644 Source/Helpers/Typealiases.swift delete mode 100644 Source/Views/AlertActionButton.swift diff --git a/RNAlertController.xcodeproj/project.pbxproj b/RNAlertController.xcodeproj/project.pbxproj index 2007b0b..9355485 100644 --- a/RNAlertController.xcodeproj/project.pbxproj +++ b/RNAlertController.xcodeproj/project.pbxproj @@ -7,6 +7,12 @@ objects = { /* Begin PBXBuildFile section */ + E82A05362366F50B00DF9E74 /* AlertActionWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = E82A05352366F50B00DF9E74 /* AlertActionWrapper.swift */; }; + E82A05382366F57900DF9E74 /* AlertButtonType.swift in Sources */ = {isa = PBXBuildFile; fileRef = E82A05372366F57900DF9E74 /* AlertButtonType.swift */; }; + E82A053A2366F5AD00DF9E74 /* AlertLabelType.swift in Sources */ = {isa = PBXBuildFile; fileRef = E82A05392366F5AD00DF9E74 /* AlertLabelType.swift */; }; + E82A053C2366F5E900DF9E74 /* AlertPickerRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = E82A053B2366F5E900DF9E74 /* AlertPickerRow.swift */; }; + E82A053E2366F62A00DF9E74 /* AlertURL.swift in Sources */ = {isa = PBXBuildFile; fileRef = E82A053D2366F62A00DF9E74 /* AlertURL.swift */; }; + E82A05402366F68F00DF9E74 /* Typealiases.swift in Sources */ = {isa = PBXBuildFile; fileRef = E82A053F2366F68F00DF9E74 /* Typealiases.swift */; }; E8508678231E5F2100F0D6B3 /* RNAlertControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E8508677231E5F2100F0D6B3 /* RNAlertControllerTests.swift */; }; E850867A231E5F2100F0D6B3 /* RNAlertController.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E8ECFACB22772ED000D4BA3F /* RNAlertController.framework */; }; E85086BB231E649800F0D6B3 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E85086B6231E649700F0D6B3 /* AppDelegate.swift */; }; @@ -29,11 +35,11 @@ E8ECFAE122772EFC00D4BA3F /* UIButton+Action.swift in Sources */ = {isa = PBXBuildFile; fileRef = E8ECFAD922772EFB00D4BA3F /* UIButton+Action.swift */; }; E8ECFAE222772EFC00D4BA3F /* UIFont+Extra.swift in Sources */ = {isa = PBXBuildFile; fileRef = E8ECFADA22772EFB00D4BA3F /* UIFont+Extra.swift */; }; E8ECFAE322772EFC00D4BA3F /* AlertLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = E8ECFADB22772EFB00D4BA3F /* AlertLabel.swift */; }; - E8ECFAE422772EFC00D4BA3F /* AlertButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = E8ECFADC22772EFB00D4BA3F /* AlertButton.swift */; }; + E8ECFAE422772EFC00D4BA3F /* Button.swift in Sources */ = {isa = PBXBuildFile; fileRef = E8ECFADC22772EFB00D4BA3F /* Button.swift */; }; E8ECFAE622772EFC00D4BA3F /* AlertButtonStackView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E8ECFADE22772EFB00D4BA3F /* AlertButtonStackView.swift */; }; E8ECFAE722772EFC00D4BA3F /* RNAlertController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E8ECFADF22772EFB00D4BA3F /* RNAlertController.swift */; }; E8F6B2E9233E32E0006A03F4 /* UIView+Appearance.swift in Sources */ = {isa = PBXBuildFile; fileRef = E8F6B2E8233E32E0006A03F4 /* UIView+Appearance.swift */; }; - E8FEEE242310409200A155E2 /* AlertActionButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = E8FEEE232310409200A155E2 /* AlertActionButton.swift */; }; + E8FEEE242310409200A155E2 /* AlertButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = E8FEEE232310409200A155E2 /* AlertButton.swift */; }; E8FEEE2623104A9F00A155E2 /* UIColor+Extra.swift in Sources */ = {isa = PBXBuildFile; fileRef = E8FEEE2523104A9F00A155E2 /* UIColor+Extra.swift */; }; E8FF167F2282B48C00CC8807 /* RNAlertController+API.swift in Sources */ = {isa = PBXBuildFile; fileRef = E8FF167E2282B48C00CC8807 /* RNAlertController+API.swift */; }; /* End PBXBuildFile section */ @@ -77,6 +83,12 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + E82A05352366F50B00DF9E74 /* AlertActionWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertActionWrapper.swift; sourceTree = ""; }; + E82A05372366F57900DF9E74 /* AlertButtonType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertButtonType.swift; sourceTree = ""; }; + E82A05392366F5AD00DF9E74 /* AlertLabelType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertLabelType.swift; sourceTree = ""; }; + E82A053B2366F5E900DF9E74 /* AlertPickerRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertPickerRow.swift; sourceTree = ""; }; + E82A053D2366F62A00DF9E74 /* AlertURL.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertURL.swift; sourceTree = ""; }; + E82A053F2366F68F00DF9E74 /* Typealiases.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Typealiases.swift; sourceTree = ""; }; E8508675231E5F2100F0D6B3 /* RNAlertControllerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RNAlertControllerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; E8508677231E5F2100F0D6B3 /* RNAlertControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RNAlertControllerTests.swift; sourceTree = ""; }; E8508679231E5F2100F0D6B3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -104,11 +116,11 @@ E8ECFAD922772EFB00D4BA3F /* UIButton+Action.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIButton+Action.swift"; sourceTree = ""; }; E8ECFADA22772EFB00D4BA3F /* UIFont+Extra.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIFont+Extra.swift"; sourceTree = ""; }; E8ECFADB22772EFB00D4BA3F /* AlertLabel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AlertLabel.swift; sourceTree = ""; }; - E8ECFADC22772EFB00D4BA3F /* AlertButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AlertButton.swift; sourceTree = ""; }; + E8ECFADC22772EFB00D4BA3F /* Button.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Button.swift; sourceTree = ""; }; E8ECFADE22772EFB00D4BA3F /* AlertButtonStackView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AlertButtonStackView.swift; sourceTree = ""; }; E8ECFADF22772EFB00D4BA3F /* RNAlertController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RNAlertController.swift; sourceTree = ""; }; E8F6B2E8233E32E0006A03F4 /* UIView+Appearance.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+Appearance.swift"; sourceTree = ""; }; - E8FEEE232310409200A155E2 /* AlertActionButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertActionButton.swift; sourceTree = ""; }; + E8FEEE232310409200A155E2 /* AlertButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertButton.swift; sourceTree = ""; }; E8FEEE2523104A9F00A155E2 /* UIColor+Extra.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIColor+Extra.swift"; sourceTree = ""; }; E8FF167E2282B48C00CC8807 /* RNAlertController+API.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RNAlertController+API.swift"; sourceTree = ""; }; /* End PBXFileReference section */ @@ -154,8 +166,7 @@ E813210D231E7343007BD8F7 /* Views */ = { isa = PBXGroup; children = ( - E8FEEE232310409200A155E2 /* AlertActionButton.swift */, - E8ECFADC22772EFB00D4BA3F /* AlertButton.swift */, + E8FEEE232310409200A155E2 /* AlertButton.swift */, E8ECFADE22772EFB00D4BA3F /* AlertButtonStackView.swift */, E8CE5292227B0FD400466CAF /* AlertContainerView.swift */, E8722542234B14E10054A937 /* AlertDatePicker.swift */, @@ -168,6 +179,20 @@ path = Views; sourceTree = ""; }; + E82A05342366F4E300DF9E74 /* Helpers */ = { + isa = PBXGroup; + children = ( + E82A05352366F50B00DF9E74 /* AlertActionWrapper.swift */, + E82A05372366F57900DF9E74 /* AlertButtonType.swift */, + E82A05392366F5AD00DF9E74 /* AlertLabelType.swift */, + E82A053B2366F5E900DF9E74 /* AlertPickerRow.swift */, + E82A053D2366F62A00DF9E74 /* AlertURL.swift */, + E8ECFADC22772EFB00D4BA3F /* Button.swift */, + E82A053F2366F68F00DF9E74 /* Typealiases.swift */, + ); + path = Helpers; + sourceTree = ""; + }; E8508676231E5F2100F0D6B3 /* RNAlertControllerTests */ = { isa = PBXGroup; children = ( @@ -255,6 +280,7 @@ E8ECFADF22772EFB00D4BA3F /* RNAlertController.swift */, E8FF167E2282B48C00CC8807 /* RNAlertController+API.swift */, E813210C231E7313007BD8F7 /* Extensions */, + E82A05342366F4E300DF9E74 /* Helpers */, E813210D231E7343007BD8F7 /* Views */, ); path = Source; @@ -427,8 +453,11 @@ buildActionMask = 2147483647; files = ( E8ECFAE722772EFC00D4BA3F /* RNAlertController.swift in Sources */, - E8ECFAE422772EFC00D4BA3F /* AlertButton.swift in Sources */, + E82A053A2366F5AD00DF9E74 /* AlertLabelType.swift in Sources */, + E8ECFAE422772EFC00D4BA3F /* Button.swift in Sources */, + E82A05362366F50B00DF9E74 /* AlertActionWrapper.swift in Sources */, E8ECFAE122772EFC00D4BA3F /* UIButton+Action.swift in Sources */, + E82A053C2366F5E900DF9E74 /* AlertPickerRow.swift in Sources */, E85EC3DE2293F6FA00C7B417 /* AlertURLButton.swift in Sources */, E8722543234B14E10054A937 /* AlertDatePicker.swift in Sources */, E8FEEE2623104A9F00A155E2 /* UIColor+Extra.swift in Sources */, @@ -437,11 +466,14 @@ E8CE5295227C506000466CAF /* AlertPickerView.swift in Sources */, E8ECFAE622772EFC00D4BA3F /* AlertButtonStackView.swift in Sources */, E8FF167F2282B48C00CC8807 /* RNAlertController+API.swift in Sources */, + E82A05382366F57900DF9E74 /* AlertButtonType.swift in Sources */, E8CE528F227B07D200466CAF /* AlertImageView.swift in Sources */, - E8FEEE242310409200A155E2 /* AlertActionButton.swift in Sources */, + E8FEEE242310409200A155E2 /* AlertButton.swift in Sources */, E8CE5291227B092400466CAF /* AlertStackView.swift in Sources */, + E82A05402366F68F00DF9E74 /* Typealiases.swift in Sources */, E8F6B2E9233E32E0006A03F4 /* UIView+Appearance.swift in Sources */, E8ECFAE322772EFC00D4BA3F /* AlertLabel.swift in Sources */, + E82A053E2366F62A00DF9E74 /* AlertURL.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Source/Extensions/UIButton+Action.swift b/Source/Extensions/UIButton+Action.swift index 15bfa5d..0b221a4 100644 --- a/Source/Extensions/UIButton+Action.swift +++ b/Source/Extensions/UIButton+Action.swift @@ -8,21 +8,6 @@ import UIKit -@objc class AlertActionWrapper: NSObject { - - let alertAction: AlertAction - - init(_ action: @escaping AlertAction) { - alertAction = action - super.init() - } - - @objc func invoke() { - alertAction() - } - -} - extension UIButton { func addAction(for controlState: UIControl.Event, action: @escaping AlertAction) { diff --git a/Source/Helpers/AlertActionWrapper.swift b/Source/Helpers/AlertActionWrapper.swift new file mode 100644 index 0000000..437bc9f --- /dev/null +++ b/Source/Helpers/AlertActionWrapper.swift @@ -0,0 +1,24 @@ +// +// AlertActionWrapper.swift +// RNAlertController +// +// Created by Rayhan on 28/10/19. +// Copyright © 2019 Rayhan Nabi. All rights reserved. +// + +import Foundation + +@objc class AlertActionWrapper: NSObject { + + let alertAction: AlertAction + + init(_ action: @escaping AlertAction) { + alertAction = action + super.init() + } + + @objc func invoke() { + alertAction() + } + +} diff --git a/Source/Helpers/AlertButtonType.swift b/Source/Helpers/AlertButtonType.swift new file mode 100644 index 0000000..9bf9609 --- /dev/null +++ b/Source/Helpers/AlertButtonType.swift @@ -0,0 +1,20 @@ +// +// AlertButtonType.swift +// RNAlertController +// +// Created by Rayhan on 28/10/19. +// Copyright © 2019 Rayhan Nabi. All rights reserved. +// + +import Foundation + +/// Describes the appearances of the alert buttons. +@objc public enum AlertButtonType: Int { + + /// Apply the default style to the button. + case `default` = 0 + /// Apply a style that indicates the action cancels the operation and leaves things unchanged. + case cancel + /// Apply a style that indicates the action might change or delete data. + case destructive +} diff --git a/Source/Helpers/AlertLabelType.swift b/Source/Helpers/AlertLabelType.swift new file mode 100644 index 0000000..3cc452f --- /dev/null +++ b/Source/Helpers/AlertLabelType.swift @@ -0,0 +1,14 @@ +// +// AlertLabelType.swift +// RNAlertController +// +// Created by Rayhan on 28/10/19. +// Copyright © 2019 Rayhan Nabi. All rights reserved. +// + +import Foundation + +enum AlertLabelType { + case title + case message +} diff --git a/Source/Helpers/AlertPickerRow.swift b/Source/Helpers/AlertPickerRow.swift new file mode 100644 index 0000000..847e0d6 --- /dev/null +++ b/Source/Helpers/AlertPickerRow.swift @@ -0,0 +1,24 @@ +// +// AlertPickerRow.swift +// RNAlertController +// +// Created by Rayhan on 28/10/19. +// Copyright © 2019 Rayhan Nabi. All rights reserved. +// + +import Foundation + +/// An object which identifies the row of a picker view. +public class AlertPickerRow: NSObject { + + /// Selected index starting from 0. + public let index: Int + + /// Title for the row. + public let title: String + + init(index: Int, title: String) { + self.index = index + self.title = title + } +} diff --git a/Source/Helpers/AlertURL.swift b/Source/Helpers/AlertURL.swift new file mode 100644 index 0000000..e1c736f --- /dev/null +++ b/Source/Helpers/AlertURL.swift @@ -0,0 +1,14 @@ +// +// AlertType.swift +// RNAlertController +// +// Created by Rayhan on 28/10/19. +// Copyright © 2019 Rayhan Nabi. All rights reserved. +// + +import Foundation + +struct AlertURL { + let url : URL? + let text: String? +} diff --git a/Source/Helpers/Button.swift b/Source/Helpers/Button.swift new file mode 100644 index 0000000..96b8e72 --- /dev/null +++ b/Source/Helpers/Button.swift @@ -0,0 +1,23 @@ +// +// Button.swift +// RNAlertController +// +// Created by Rayhan Nabi on 4/24/19. +// Copyright © 2019 Rayhan. All rights reserved. +// + +import UIKit + +class Button: NSObject { + + let text : String + let action : AlertAction + let type : AlertButtonType + + init(text: String, type: AlertButtonType, action: @escaping AlertAction) { + self.text = text + self.action = action + self.type = type + } + +} diff --git a/Source/Helpers/Typealiases.swift b/Source/Helpers/Typealiases.swift new file mode 100644 index 0000000..196269d --- /dev/null +++ b/Source/Helpers/Typealiases.swift @@ -0,0 +1,18 @@ +// +// Typealiases.swift +// RNAlertController +// +// Created by Rayhan on 28/10/19. +// Copyright © 2019 Rayhan Nabi. All rights reserved. +// + +import Foundation + +/// Action block to perform when a button is pressed. +public typealias AlertAction = () -> Void + +/// Action block when a picker item is selected. +public typealias AlertPickerAction = (AlertPickerRow) -> Void + +/// Action block to perform when a date is selected. +public typealias AlertDatePickerAction = (Date) -> Void diff --git a/Source/RNAlertController+API.swift b/Source/RNAlertController+API.swift index 9f4059d..01b0aaa 100644 --- a/Source/RNAlertController+API.swift +++ b/Source/RNAlertController+API.swift @@ -37,7 +37,7 @@ public extension RNAlertController { let action = { self.dismiss(animated: true, completion: action) } - let button = AlertButton(text: title, type: type, action: action) + let button = Button(text: title, type: type, action: action) buttons?.append(button) return self diff --git a/Source/RNAlertController.swift b/Source/RNAlertController.swift index b27f0d1..ebabba5 100644 --- a/Source/RNAlertController.swift +++ b/Source/RNAlertController.swift @@ -13,7 +13,7 @@ import UIKit var message : String? var attributedMessage : NSAttributedString? - var buttons : [AlertButton]? + var buttons : [Button]? var image : UIImage? var pickerData : [String]? var pickerAction : AlertPickerAction? diff --git a/Source/Views/AlertActionButton.swift b/Source/Views/AlertActionButton.swift deleted file mode 100644 index 68d9819..0000000 --- a/Source/Views/AlertActionButton.swift +++ /dev/null @@ -1,76 +0,0 @@ -// -// AlertActionButton.swift -// RNAlertController -// -// Created by Rayhan Nabi on 8/23/19. -// Copyright © 2019 Rayhan Nabi. All rights reserved. -// - -import Foundation - -class AlertActionButton: UIButton { - - override open var isHighlighted: Bool { - didSet { - setHighlightedColor() - } - } - - convenience init() { - self.init(type: .custom) - setBackgroundColor() - setConstraint() - } - - func setTitle(_ title: String, for type: AlertButtonType) { - let textAttributes = createAttributes(for: type) - let attributedTitle = NSAttributedString(string: title, attributes: textAttributes) - setAttributedTitle(attributedTitle, for: .normal) - } - - override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { - super.traitCollectionDidChange(previousTraitCollection) - setBackgroundColor() - setHighlightedColor() - } - - private func createAttributes(for type: AlertButtonType) -> [NSAttributedString.Key: Any] { - var attributes = [NSAttributedString.Key: Any]() - switch type { - case .cancel: - attributes[.font] = UIFont.alertButtonFontBold - attributes[.foregroundColor] = UIColor.alertButtonTextRegular - case .destructive: - attributes[.font] = UIFont.alertButtonFontRegular - attributes[.foregroundColor] = UIColor.alertButtonTextDestructive - case .default: - attributes[.font] = UIFont.alertButtonFontRegular - attributes[.foregroundColor] = UIColor.alertButtonTextRegular - } - return attributes - } - - private func setConstraint() { - NSLayoutConstraint.activate([ - heightAnchor.constraint(equalToConstant: 44) - ] - ) - } - - private func setBackgroundColor() { - backgroundColor = isDarkInterfaceStyle ? UIColor.defaultDarkBackground : UIColor.defaultLightBackground - } - - private func setHighlightedColor() { - if isHighlighted { - backgroundColor = isDarkInterfaceStyle ? - UIColor.highlightedDarkBackground : - UIColor.highlightedLightBackground - } else { - backgroundColor = isDarkInterfaceStyle ? - UIColor.defaultDarkBackground : - UIColor.defaultLightBackground - } - } - -} diff --git a/Source/Views/AlertButton.swift b/Source/Views/AlertButton.swift index 91c06fe..075464b 100644 --- a/Source/Views/AlertButton.swift +++ b/Source/Views/AlertButton.swift @@ -2,36 +2,75 @@ // AlertButton.swift // RNAlertController // -// Created by Rayhan Nabi on 4/24/19. -// Copyright © 2019 Rayhan. All rights reserved. +// Created by Rayhan Nabi on 8/23/19. +// Copyright © 2019 Rayhan Nabi. All rights reserved. // -import UIKit +import Foundation -/// Describes the appearances of the alert buttons. -@objc public enum AlertButtonType: Int { - - /// Apply the default style to the button. - case `default` = 0 - /// Apply a style that indicates the action cancels the operation and leaves things unchanged. - case cancel - /// Apply a style that indicates the action might change or delete data. - case destructive -} - -/// Action block to perform when a button is pressed. -public typealias AlertAction = () -> Void - -class AlertButton: NSObject { +class AlertButton: UIButton { + + override open var isHighlighted: Bool { + didSet { + setHighlightedColor() + } + } + + convenience init() { + self.init(type: .custom) + setBackgroundColor() + setConstraint() + } + + func setTitle(_ title: String, for type: AlertButtonType) { + let textAttributes = createAttributes(for: type) + let attributedTitle = NSAttributedString(string: title, attributes: textAttributes) + setAttributedTitle(attributedTitle, for: .normal) + } + + override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { + super.traitCollectionDidChange(previousTraitCollection) + setBackgroundColor() + setHighlightedColor() + } + + private func createAttributes(for type: AlertButtonType) -> [NSAttributedString.Key: Any] { + var attributes = [NSAttributedString.Key: Any]() + switch type { + case .cancel: + attributes[.font] = UIFont.alertButtonFontBold + attributes[.foregroundColor] = UIColor.alertButtonTextRegular + case .destructive: + attributes[.font] = UIFont.alertButtonFontRegular + attributes[.foregroundColor] = UIColor.alertButtonTextDestructive + case .default: + attributes[.font] = UIFont.alertButtonFontRegular + attributes[.foregroundColor] = UIColor.alertButtonTextRegular + } + return attributes + } + + private func setConstraint() { + NSLayoutConstraint.activate([ + heightAnchor.constraint(equalToConstant: 44) + ] + ) + } - let text : String - let action : AlertAction - let type : AlertButtonType + private func setBackgroundColor() { + backgroundColor = isDarkInterfaceStyle ? UIColor.defaultDarkBackground : UIColor.defaultLightBackground + } - init(text: String, type: AlertButtonType, action: @escaping AlertAction) { - self.text = text - self.action = action - self.type = type + private func setHighlightedColor() { + if isHighlighted { + backgroundColor = isDarkInterfaceStyle ? + UIColor.highlightedDarkBackground : + UIColor.highlightedLightBackground + } else { + backgroundColor = isDarkInterfaceStyle ? + UIColor.defaultDarkBackground : + UIColor.defaultLightBackground + } } } diff --git a/Source/Views/AlertButtonStackView.swift b/Source/Views/AlertButtonStackView.swift index 528624f..eba4eb2 100644 --- a/Source/Views/AlertButtonStackView.swift +++ b/Source/Views/AlertButtonStackView.swift @@ -23,7 +23,7 @@ final class AlertButtonStackView: UIStackView { super.init(coder: coder) } - convenience init(alertButtons: [AlertButton]) { + convenience init(alertButtons: [Button]) { self.init(frame: .zero) let stackItems = createButtonCollection(from: alertButtons) let shouldUseVerticalAxis = adjustForOversizedText(buttonCollection: stackItems) @@ -33,11 +33,11 @@ final class AlertButtonStackView: UIStackView { } } - fileprivate func createButtonCollection(from alertButtons: [AlertButton]) -> [UIView] { + fileprivate func createButtonCollection(from alertButtons: [Button]) -> [UIView] { var stackItems = [UIView]() for index in 0.. Void - class AlertDatePicker: UIDatePicker { var action: AlertDatePickerAction? diff --git a/Source/Views/AlertLabel.swift b/Source/Views/AlertLabel.swift index 6e9f1e7..73a8b85 100644 --- a/Source/Views/AlertLabel.swift +++ b/Source/Views/AlertLabel.swift @@ -8,11 +8,6 @@ import UIKit -enum AlertLabelType { - case title - case message -} - class AlertLabel: UILabel { override init(frame: CGRect) { diff --git a/Source/Views/AlertPickerView.swift b/Source/Views/AlertPickerView.swift index 16b2572..a8c1473 100644 --- a/Source/Views/AlertPickerView.swift +++ b/Source/Views/AlertPickerView.swift @@ -8,24 +8,6 @@ import UIKit -/// An object which identifies the row of a picker view. -public class AlertPickerRow: NSObject { - - /// Selected index starting from 0. - public let index: Int - - /// Title for the row. - public let title: String - - init(index: Int, title: String) { - self.index = index - self.title = title - } -} - -/// Action block when a picker item is selected. -public typealias AlertPickerAction = (AlertPickerRow) -> Void - class AlertPickerView: UIPickerView { override init(frame: CGRect) { diff --git a/Source/Views/AlertURLButton.swift b/Source/Views/AlertURLButton.swift index 5a0780e..85dcc08 100644 --- a/Source/Views/AlertURLButton.swift +++ b/Source/Views/AlertURLButton.swift @@ -8,11 +8,6 @@ import UIKit -struct AlertURL { - let url : URL? - let text: String? -} - class AlertURLButton: UIButton { convenience init(_ alertURL: AlertURL) { From 399193382a706edc5b962a30949cc7d1c421d90e Mon Sep 17 00:00:00 2001 From: Rayhan Nabi Date: Mon, 28 Oct 2019 16:24:17 +0600 Subject: [PATCH 2/8] Add AlertButtonPosition --- RNAlertController.xcodeproj/project.pbxproj | 4 ++++ Source/Helpers/AlertBannerPosition.swift | 14 ++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 Source/Helpers/AlertBannerPosition.swift diff --git a/RNAlertController.xcodeproj/project.pbxproj b/RNAlertController.xcodeproj/project.pbxproj index 9355485..766e9d9 100644 --- a/RNAlertController.xcodeproj/project.pbxproj +++ b/RNAlertController.xcodeproj/project.pbxproj @@ -27,6 +27,7 @@ E85086C7231E670200F0D6B3 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E85086B7231E649700F0D6B3 /* Assets.xcassets */; }; E85EC3DE2293F6FA00C7B417 /* AlertURLButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = E85EC3DD2293F6FA00C7B417 /* AlertURLButton.swift */; }; E8722543234B14E10054A937 /* AlertDatePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = E8722542234B14E10054A937 /* AlertDatePicker.swift */; }; + E88841062366F90E00CFF2AA /* AlertBannerPosition.swift in Sources */ = {isa = PBXBuildFile; fileRef = E88841052366F90E00CFF2AA /* AlertBannerPosition.swift */; }; E8CE528F227B07D200466CAF /* AlertImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E8CE528E227B07D200466CAF /* AlertImageView.swift */; }; E8CE5291227B092400466CAF /* AlertStackView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E8CE5290227B092400466CAF /* AlertStackView.swift */; }; E8CE5293227B0FD400466CAF /* AlertContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E8CE5292227B0FD400466CAF /* AlertContainerView.swift */; }; @@ -106,6 +107,7 @@ E85086CC231E6BBA00F0D6B3 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; E85EC3DD2293F6FA00C7B417 /* AlertURLButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertURLButton.swift; sourceTree = ""; }; E8722542234B14E10054A937 /* AlertDatePicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertDatePicker.swift; sourceTree = ""; }; + E88841052366F90E00CFF2AA /* AlertBannerPosition.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertBannerPosition.swift; sourceTree = ""; }; E8CE528E227B07D200466CAF /* AlertImageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertImageView.swift; sourceTree = ""; }; E8CE5290227B092400466CAF /* AlertStackView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertStackView.swift; sourceTree = ""; }; E8CE5292227B0FD400466CAF /* AlertContainerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertContainerView.swift; sourceTree = ""; }; @@ -183,6 +185,7 @@ isa = PBXGroup; children = ( E82A05352366F50B00DF9E74 /* AlertActionWrapper.swift */, + E88841052366F90E00CFF2AA /* AlertBannerPosition.swift */, E82A05372366F57900DF9E74 /* AlertButtonType.swift */, E82A05392366F5AD00DF9E74 /* AlertLabelType.swift */, E82A053B2366F5E900DF9E74 /* AlertPickerRow.swift */, @@ -460,6 +463,7 @@ E82A053C2366F5E900DF9E74 /* AlertPickerRow.swift in Sources */, E85EC3DE2293F6FA00C7B417 /* AlertURLButton.swift in Sources */, E8722543234B14E10054A937 /* AlertDatePicker.swift in Sources */, + E88841062366F90E00CFF2AA /* AlertBannerPosition.swift in Sources */, E8FEEE2623104A9F00A155E2 /* UIColor+Extra.swift in Sources */, E8CE5293227B0FD400466CAF /* AlertContainerView.swift in Sources */, E8ECFAE222772EFC00D4BA3F /* UIFont+Extra.swift in Sources */, diff --git a/Source/Helpers/AlertBannerPosition.swift b/Source/Helpers/AlertBannerPosition.swift new file mode 100644 index 0000000..9d5ad81 --- /dev/null +++ b/Source/Helpers/AlertBannerPosition.swift @@ -0,0 +1,14 @@ +// +// AlertBannerPosition.swift +// RNAlertController +// +// Created by Rayhan on 28/10/19. +// Copyright © 2019 Rayhan Nabi. All rights reserved. +// + +import Foundation + +enum AlertBannerPosition { + case afterBody + case beforeBody +} From 9f3bda5e94392e11eca3c5c9a8ed56fcf7eb1e85 Mon Sep 17 00:00:00 2001 From: Rayhan Nabi Date: Mon, 28 Oct 2019 16:37:36 +0600 Subject: [PATCH 3/8] Update API --- Source/Helpers/AlertBannerPosition.swift | 12 +++++++++++- Source/RNAlertController+API.swift | 10 ++++++---- Source/RNAlertController.swift | 1 + 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Source/Helpers/AlertBannerPosition.swift b/Source/Helpers/AlertBannerPosition.swift index 9d5ad81..df36d0e 100644 --- a/Source/Helpers/AlertBannerPosition.swift +++ b/Source/Helpers/AlertBannerPosition.swift @@ -8,7 +8,17 @@ import Foundation -enum AlertBannerPosition { + +/// Describes banner image positions. +@objc public enum AlertBannerPosition: Int { + + /// Banner image is displayed after title and message. + /// + /// - Note: Image contentMode is set to `scaleAspectFit`. case afterBody + + /// Banner image is displayed before the title. + /// + /// - Note: Image contentMode is set to `scaleAspectFill`. case beforeBody } diff --git a/Source/RNAlertController+API.swift b/Source/RNAlertController+API.swift index 01b0aaa..178c49d 100644 --- a/Source/RNAlertController+API.swift +++ b/Source/RNAlertController+API.swift @@ -72,13 +72,15 @@ public extension RNAlertController { /// Sets the banner image for the alert. /// - /// Banner image is displayed under the message body. Consequent calls of this method will result - /// in replacement of previously set image. + /// Banner image is displayed under the message body. + /// Consequent calls of this method will result in replacement of previously set image. /// - Parameter image: image to use in the alert. - /// - Returns: `RNAlertController` instance. + /// - Parameter position: determines the position of the image. + /// - Returns: RNAlertController` instance. @discardableResult - func setBannerImage(_ image: UIImage) -> RNAlertController { + func setBannerImage(_ image: UIImage, position: AlertBannerPosition = .afterBody) -> RNAlertController { self.image = image + self.imagePosition = position return self } diff --git a/Source/RNAlertController.swift b/Source/RNAlertController.swift index ebabba5..8d2559b 100644 --- a/Source/RNAlertController.swift +++ b/Source/RNAlertController.swift @@ -15,6 +15,7 @@ import UIKit var attributedMessage : NSAttributedString? var buttons : [Button]? var image : UIImage? + var imagePosition : AlertBannerPosition? var pickerData : [String]? var pickerAction : AlertPickerAction? var selectedPickerRow : Int? From d43a0e7430164351d21b11c86357df0e9f734121 Mon Sep 17 00:00:00 2001 From: Rayhan Nabi Date: Mon, 28 Oct 2019 17:04:52 +0600 Subject: [PATCH 4/8] Update imageview position handling --- .../Controllers/OtherViewController.swift | 2 +- Source/RNAlertController+API.swift | 8 +++++--- Source/RNAlertController.swift | 14 +++++++++++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/RNAlertControllerExample/Controllers/OtherViewController.swift b/RNAlertControllerExample/Controllers/OtherViewController.swift index d94ff23..80c8686 100644 --- a/RNAlertControllerExample/Controllers/OtherViewController.swift +++ b/RNAlertControllerExample/Controllers/OtherViewController.swift @@ -29,7 +29,7 @@ class OtherViewController: UIViewController { RNAlertController(title: "Kashmir Valley", message: nil) .setAttributedTextForMessage(attText) - .setBannerImage(UIImage(named: "crop")!) + .setBannerImage(UIImage(named: "crop")!, position: .beforeBody) .addOkButton() .present() } diff --git a/Source/RNAlertController+API.swift b/Source/RNAlertController+API.swift index 178c49d..d70bd0e 100644 --- a/Source/RNAlertController+API.swift +++ b/Source/RNAlertController+API.swift @@ -47,7 +47,8 @@ public extension RNAlertController { /// /// When set, message text will use attributed text instead of regular text. /// - Parameter attributedText: Attributed string to set. - @discardableResult func setAttributedTextForMessage(_ attributedText: NSAttributedString?) -> RNAlertController { + @discardableResult + func setAttributedTextForMessage(_ attributedText: NSAttributedString?) -> RNAlertController { self.attributedMessage = attributedText return self } @@ -74,8 +75,9 @@ public extension RNAlertController { /// /// Banner image is displayed under the message body. /// Consequent calls of this method will result in replacement of previously set image. - /// - Parameter image: image to use in the alert. - /// - Parameter position: determines the position of the image. + /// - Parameters: + /// - image: image to use in the alert. + /// - position: determines the position of the image. /// - Returns: RNAlertController` instance. @discardableResult func setBannerImage(_ image: UIImage, position: AlertBannerPosition = .afterBody) -> RNAlertController { diff --git a/Source/RNAlertController.swift b/Source/RNAlertController.swift index 8d2559b..77c0b91 100644 --- a/Source/RNAlertController.swift +++ b/Source/RNAlertController.swift @@ -148,7 +148,10 @@ private extension RNAlertController { func createImageView() -> AlertImageView? { guard let image = image else { return nil } + guard let position = imagePosition else { return nil } let imageView = AlertImageView(image: image) + imageView.contentMode = position == .beforeBody ? .scaleAspectFill: .scaleAspectFit + imageView.clipsToBounds = true return imageView } @@ -175,7 +178,9 @@ private extension RNAlertController { let pickerView = createPickerView() let urlView = createURLView() if imageView != nil { - extraStackItems.append(imageView!) + if let position = imagePosition, position == .afterBody { + extraStackItems.append(imageView!) + } } if pickerView != nil { extraStackItems.append(pickerView!) @@ -246,6 +251,13 @@ private extension RNAlertController { ] ) + if let bannerPosition = imagePosition, image != nil, bannerPosition == .beforeBody { + let imageView = createImageView() + if imageView != nil { + alertBody.addArrangedSubview(imageView!) + } + } + alertBody.addArrangedSubview(alertBodyBackground) if let buttons = buttons, buttons.count > 0 { let buttonStack = AlertButtonStackView(alertButtons: buttons) From b1038e9a30fa09c22ba204d3b98969a268e29bd8 Mon Sep 17 00:00:00 2001 From: Rayhan Nabi Date: Sat, 16 Nov 2019 21:51:40 +0600 Subject: [PATCH 5/8] Update intrinsic button size calculation --- Source/Views/AlertButtonStackView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Views/AlertButtonStackView.swift b/Source/Views/AlertButtonStackView.swift index eba4eb2..b8d94a1 100644 --- a/Source/Views/AlertButtonStackView.swift +++ b/Source/Views/AlertButtonStackView.swift @@ -51,7 +51,7 @@ final class AlertButtonStackView: UIStackView { if buttonCollection.count >= 2 { for view in buttonCollection { if let textIntrinsicSize = (view as? UIButton)?.titleLabel?.intrinsicContentSize { - if textIntrinsicSize.width > widthFactor { + if textIntrinsicSize.width > widthFactor - 8 { shouldUseVerticalAxis = true } } From 146148ca5d3e2d7af4ec2c64a800790c0998566d Mon Sep 17 00:00:00 2001 From: Rayhan Nabi Date: Sat, 16 Nov 2019 22:02:55 +0600 Subject: [PATCH 6/8] Version Bump --- RNAlertController.xcodeproj/project.pbxproj | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/RNAlertController.xcodeproj/project.pbxproj b/RNAlertController.xcodeproj/project.pbxproj index 766e9d9..fdef453 100644 --- a/RNAlertController.xcodeproj/project.pbxproj +++ b/RNAlertController.xcodeproj/project.pbxproj @@ -569,7 +569,7 @@ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 682; + CURRENT_PROJECT_VERSION = 670; DEVELOPMENT_TEAM = PCH85ZHVFN; INFOPLIST_FILE = "$(SRCROOT)/RNAlertControllerExample/Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 12.4; @@ -577,7 +577,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 0.6.7; + MARKETING_VERSION = 0.6.8; PRODUCT_BUNDLE_IDENTIFIER = com.rayhan.ios.RNAlertControllerExample; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 5.0; @@ -591,7 +591,7 @@ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 682; + CURRENT_PROJECT_VERSION = 670; DEVELOPMENT_TEAM = PCH85ZHVFN; INFOPLIST_FILE = "$(SRCROOT)/RNAlertControllerExample/Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 12.4; @@ -599,7 +599,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 0.6.7; + MARKETING_VERSION = 0.6.8; PRODUCT_BUNDLE_IDENTIFIER = com.rayhan.ios.RNAlertControllerExample; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 5.0; @@ -738,7 +738,7 @@ CLANG_ENABLE_MODULES = YES; CODE_SIGN_IDENTITY = ""; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 1006; + CURRENT_PROJECT_VERSION = 1010; DEFINES_MODULE = YES; DEVELOPMENT_TEAM = PCH85ZHVFN; DYLIB_COMPATIBILITY_VERSION = 1; @@ -752,7 +752,7 @@ "@executable_path/Frameworks", "@loader_path/Frameworks", ); - MARKETING_VERSION = 0.6.7; + MARKETING_VERSION = 0.6.8; PRODUCT_BUNDLE_IDENTIFIER = com.rayhan.ios.RNAlertController; PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; PROVISIONING_PROFILE_SPECIFIER = ""; @@ -770,7 +770,7 @@ CLANG_ENABLE_MODULES = YES; CODE_SIGN_IDENTITY = ""; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 1006; + CURRENT_PROJECT_VERSION = 1010; DEFINES_MODULE = YES; DEVELOPMENT_TEAM = PCH85ZHVFN; DYLIB_COMPATIBILITY_VERSION = 1; @@ -784,7 +784,7 @@ "@executable_path/Frameworks", "@loader_path/Frameworks", ); - MARKETING_VERSION = 0.6.7; + MARKETING_VERSION = 0.6.8; PRODUCT_BUNDLE_IDENTIFIER = com.rayhan.ios.RNAlertController; PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; PROVISIONING_PROFILE_SPECIFIER = ""; From d4fbe41098f55c7c3710cddf7daac9087c86dab7 Mon Sep 17 00:00:00 2001 From: Rayhan Nabi Date: Sat, 16 Nov 2019 22:03:41 +0600 Subject: [PATCH 7/8] Podspec Version Bump --- RNAlertController.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RNAlertController.podspec b/RNAlertController.podspec index 5ed674b..2a77cfc 100644 --- a/RNAlertController.podspec +++ b/RNAlertController.podspec @@ -5,7 +5,7 @@ Pod::Spec.new do |spec| spec.name = "RNAlertController" - spec.version = "0.6.7" + spec.version = "0.6.8" spec.summary = "Custom Alert Framework for iOS" spec.description = <<-DESC From 60df34ff72c9bf94c77332ada5a3ddda4226dc41 Mon Sep 17 00:00:00 2001 From: Rayhan Nabi Date: Sat, 16 Nov 2019 22:09:56 +0600 Subject: [PATCH 8/8] Update documentation --- docs/Classes.html | 29 +-- docs/Classes/AlertPickerRow.html | 3 + docs/Classes/RNAlertController.html | 29 ++- docs/Enums.html | 36 ++++ docs/Enums/AlertBannerPosition.html | 170 ++++++++++++++++++ docs/Enums/AlertButtonType.html | 3 + docs/Extensions.html | 3 + docs/Extensions/UIFont.html | 3 + docs/Typealiases.html | 31 ++-- .../Contents/Resources/Documents/Classes.html | 29 +-- .../Documents/Classes/AlertPickerRow.html | 3 + .../Documents/Classes/RNAlertController.html | 29 ++- .../Contents/Resources/Documents/Enums.html | 36 ++++ .../Documents/Enums/AlertBannerPosition.html | 170 ++++++++++++++++++ .../Documents/Enums/AlertButtonType.html | 3 + .../Resources/Documents/Extensions.html | 3 + .../Documents/Extensions/UIFont.html | 3 + .../Resources/Documents/Typealiases.html | 31 ++-- .../Contents/Resources/Documents/index.html | 3 + .../Contents/Resources/Documents/search.json | 2 +- .../Contents/Resources/docSet.dsidx | Bin 28672 -> 28672 bytes docs/docsets/RNAlertController.tgz | Bin 49578 -> 50276 bytes docs/index.html | 3 + docs/search.json | 2 +- docs/undocumented.json | 10 +- 25 files changed, 551 insertions(+), 83 deletions(-) create mode 100644 docs/Enums/AlertBannerPosition.html create mode 100644 docs/docsets/RNAlertController.docset/Contents/Resources/Documents/Enums/AlertBannerPosition.html diff --git a/docs/Classes.html b/docs/Classes.html index 1e2b587..de6f4ff 100644 --- a/docs/Classes.html +++ b/docs/Classes.html @@ -42,6 +42,9 @@