Skip to content

Commit

Permalink
Merge pull request #4 from AntonPoltoratskyi/feature/automatic-dismiss
Browse files Browse the repository at this point in the history
Added `shouldDismissAutomatically` flag
  • Loading branch information
devpolant authored Aug 15, 2020
2 parents 0a834b7 + 55df13c commit 716cf7f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 15 deletions.
12 changes: 6 additions & 6 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
PODS:
- NativeUI (1.0.1):
- NativeUI/Core (= 1.0.1)
- NativeUI/Alert (1.0.1):
- NativeUI (1.1.0):
- NativeUI/Core (= 1.1.0)
- NativeUI/Alert (1.1.0):
- NativeUI/Utils
- NativeUI/Core (1.0.1):
- NativeUI/Core (1.1.0):
- NativeUI/Alert
- NativeUI/Utils (1.0.1)
- NativeUI/Utils (1.1.0)

DEPENDENCIES:
- NativeUI (from `../`)
Expand All @@ -15,7 +15,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
NativeUI: 6de137806356923678e5b124502c00246ed8a14a
NativeUI: d82fff2460d835a9fbd645902462e2b1d9573507

PODFILE CHECKSUM: bb46d7bf1ae3b119e00a9331a12cd0a4b5cac170

Expand Down
2 changes: 1 addition & 1 deletion NativeUI.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "NativeUI"
s.version = "1.0.1"
s.version = "1.1.0"
s.summary = "Library that includes customizable replacements for native UIKit components"

s.description = <<-DESC
Expand Down
12 changes: 8 additions & 4 deletions NativeUI/Sources/Alert/Alert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ public struct Alert {

public let disabledTintColor: UIColor?

public let actions: [Action]
public private(set) var actions: [Action]

public init(title: Text?,
message: Text?,
contentView: UIView? = nil,
tintColor: UIColor? = nil,
disabledTintColor: UIColor? = nil,
actions: [Action]) {
actions: [Action] = []) {
self.title = title
self.message = message
self.contentView = contentView
Expand All @@ -75,7 +75,7 @@ public struct Alert {
contentView: UIView? = nil,
tintColor: UIColor? = nil,
disabledTintColor: UIColor? = nil,
actions: [Action]) {
actions: [Action] = []) {
self.init(title: title.map { .string($0, titleFont) },
message: message.map { .string($0, messageFont) },
contentView: contentView,
Expand All @@ -89,12 +89,16 @@ public struct Alert {
contentView: UIView? = nil,
tintColor: UIColor? = nil,
disabledTintColor: UIColor? = nil,
actions: [Action]) {
actions: [Action] = []) {
self.init(title: title.map { .attributedString($0) },
message: message.map { .attributedString($0) },
contentView: contentView,
tintColor: tintColor,
disabledTintColor: disabledTintColor,
actions: actions)
}

mutating func addAction(_ action: Action) {
actions.append(action)
}
}
14 changes: 12 additions & 2 deletions NativeUI/Sources/Alert/AlertViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import UIKit

public final class AlertViewController: UIViewController, AlertViewDelegate {

private let viewModel: Alert
public var shouldDismissAutomatically: Bool = true

private var viewModel: Alert

// MARK: - Subviews

Expand Down Expand Up @@ -43,6 +45,12 @@ public final class AlertViewController: UIViewController, AlertViewDelegate {
setupViewModel()
}

// MARK: - Public Interface

public func addAction(_ action: Alert.Action) {
viewModel.addAction(action)
}

// MARK: - UI Setup

private func setupAppearance() {
Expand Down Expand Up @@ -98,7 +106,9 @@ extension AlertViewController {
let action = viewModel.actions[index]
action.handler?(action)
}
dismiss(animated: true, completion: nil)
if shouldDismissAutomatically {
dismiss(animated: true, completion: nil)
}
}
}

Expand Down
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

```ruby
target 'MyApp' do
pod 'NativeUI', '~> 1.0'
pod 'NativeUI', '~> 1.1'
end
```

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

```ruby
target 'MyApp' do
pod 'NativeUI/Alert', '~> 1.0'
pod 'NativeUI/Alert', '~> 1.1'
end
```

Expand Down Expand Up @@ -101,6 +101,36 @@ present(alert, animated: true)

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

### Edge cases

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.

```swift

let viewModel = Alert(
title: "Your Title",
message: "Your Message"
)
let alert = AlertViewController(viewModel: viewModel)
alert.shouldDismissAutomatically = false

let cancelAction = Alert.Action(title: "Cancel", style: .primary) { [weak alert] _ in
alert?.dismiss(animated: true) {
// present something else
}
}
alert.addAction(cancelAction)

let confirmAction = Alert.Action(title: "Confirm", style: .default) { [weak alert] _ in
alert?.dismiss(animated: true) {
// present something else
}
}
alert.addAction(confirmAction)

present(alert, animated: true)
```

## Author

Anton Poltoratskyi
Expand Down

0 comments on commit 716cf7f

Please sign in to comment.