Skip to content

Commit 716cf7f

Browse files
author
Anton Poltoratskyi
authored
Merge pull request #4 from AntonPoltoratskyi/feature/automatic-dismiss
Added `shouldDismissAutomatically` flag
2 parents 0a834b7 + 55df13c commit 716cf7f

File tree

5 files changed

+59
-15
lines changed

5 files changed

+59
-15
lines changed

Example/Podfile.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
PODS:
2-
- NativeUI (1.0.1):
3-
- NativeUI/Core (= 1.0.1)
4-
- NativeUI/Alert (1.0.1):
2+
- NativeUI (1.1.0):
3+
- NativeUI/Core (= 1.1.0)
4+
- NativeUI/Alert (1.1.0):
55
- NativeUI/Utils
6-
- NativeUI/Core (1.0.1):
6+
- NativeUI/Core (1.1.0):
77
- NativeUI/Alert
8-
- NativeUI/Utils (1.0.1)
8+
- NativeUI/Utils (1.1.0)
99

1010
DEPENDENCIES:
1111
- NativeUI (from `../`)
@@ -15,7 +15,7 @@ EXTERNAL SOURCES:
1515
:path: "../"
1616

1717
SPEC CHECKSUMS:
18-
NativeUI: 6de137806356923678e5b124502c00246ed8a14a
18+
NativeUI: d82fff2460d835a9fbd645902462e2b1d9573507
1919

2020
PODFILE CHECKSUM: bb46d7bf1ae3b119e00a9331a12cd0a4b5cac170
2121

NativeUI.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "NativeUI"
3-
s.version = "1.0.1"
3+
s.version = "1.1.0"
44
s.summary = "Library that includes customizable replacements for native UIKit components"
55

66
s.description = <<-DESC

NativeUI/Sources/Alert/Alert.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ public struct Alert {
5252

5353
public let disabledTintColor: UIColor?
5454

55-
public let actions: [Action]
55+
public private(set) var actions: [Action]
5656

5757
public init(title: Text?,
5858
message: Text?,
5959
contentView: UIView? = nil,
6060
tintColor: UIColor? = nil,
6161
disabledTintColor: UIColor? = nil,
62-
actions: [Action]) {
62+
actions: [Action] = []) {
6363
self.title = title
6464
self.message = message
6565
self.contentView = contentView
@@ -75,7 +75,7 @@ public struct Alert {
7575
contentView: UIView? = nil,
7676
tintColor: UIColor? = nil,
7777
disabledTintColor: UIColor? = nil,
78-
actions: [Action]) {
78+
actions: [Action] = []) {
7979
self.init(title: title.map { .string($0, titleFont) },
8080
message: message.map { .string($0, messageFont) },
8181
contentView: contentView,
@@ -89,12 +89,16 @@ public struct Alert {
8989
contentView: UIView? = nil,
9090
tintColor: UIColor? = nil,
9191
disabledTintColor: UIColor? = nil,
92-
actions: [Action]) {
92+
actions: [Action] = []) {
9393
self.init(title: title.map { .attributedString($0) },
9494
message: message.map { .attributedString($0) },
9595
contentView: contentView,
9696
tintColor: tintColor,
9797
disabledTintColor: disabledTintColor,
9898
actions: actions)
9999
}
100+
101+
mutating func addAction(_ action: Action) {
102+
actions.append(action)
103+
}
100104
}

NativeUI/Sources/Alert/AlertViewController.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import UIKit
1010

1111
public final class AlertViewController: UIViewController, AlertViewDelegate {
1212

13-
private let viewModel: Alert
13+
public var shouldDismissAutomatically: Bool = true
14+
15+
private var viewModel: Alert
1416

1517
// MARK: - Subviews
1618

@@ -43,6 +45,12 @@ public final class AlertViewController: UIViewController, AlertViewDelegate {
4345
setupViewModel()
4446
}
4547

48+
// MARK: - Public Interface
49+
50+
public func addAction(_ action: Alert.Action) {
51+
viewModel.addAction(action)
52+
}
53+
4654
// MARK: - UI Setup
4755

4856
private func setupAppearance() {
@@ -98,7 +106,9 @@ extension AlertViewController {
98106
let action = viewModel.actions[index]
99107
action.handler?(action)
100108
}
101-
dismiss(animated: true, completion: nil)
109+
if shouldDismissAutomatically {
110+
dismiss(animated: true, completion: nil)
111+
}
102112
}
103113
}
104114

README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616

1717
```ruby
1818
target 'MyApp' do
19-
pod 'NativeUI', '~> 1.0'
19+
pod 'NativeUI', '~> 1.1'
2020
end
2121
```
2222

2323
If you don't need to connect all UI components you may use subspecs like:
2424

2525
```ruby
2626
target 'MyApp' do
27-
pod 'NativeUI/Alert', '~> 1.0'
27+
pod 'NativeUI/Alert', '~> 1.1'
2828
end
2929
```
3030

@@ -101,6 +101,36 @@ present(alert, animated: true)
101101

102102
See [Alert.swift](https://github.com/AntonPoltoratskyi/NativeUI/blob/master/NativeUI/Sources/Alert/Alert.swift) for more details.
103103

104+
### Edge cases
105+
106+
When you need to present few alerts in a row you should set `shouldDismissAutomatically` flag to `false` and add action AFTER view controller initialization to be able to capture alert instance in action handler's closure.
107+
108+
```swift
109+
110+
let viewModel = Alert(
111+
title: "Your Title",
112+
message: "Your Message"
113+
)
114+
let alert = AlertViewController(viewModel: viewModel)
115+
alert.shouldDismissAutomatically = false
116+
117+
let cancelAction = Alert.Action(title: "Cancel", style: .primary) { [weak alert] _ in
118+
alert?.dismiss(animated: true) {
119+
// present something else
120+
}
121+
}
122+
alert.addAction(cancelAction)
123+
124+
let confirmAction = Alert.Action(title: "Confirm", style: .default) { [weak alert] _ in
125+
alert?.dismiss(animated: true) {
126+
// present something else
127+
}
128+
}
129+
alert.addAction(confirmAction)
130+
131+
present(alert, animated: true)
132+
```
133+
104134
## Author
105135

106136
Anton Poltoratskyi

0 commit comments

Comments
 (0)