Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
sgr-ksmt committed Dec 13, 2017
2 parents 5435ae3 + e36c912 commit 25979c1
Show file tree
Hide file tree
Showing 15 changed files with 309 additions and 94 deletions.
2 changes: 1 addition & 1 deletion Alertift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Alertift"
s.version = "3.0"
s.version = "3.1"
s.summary = "UIAlertControlelr wrapper for Swift."
s.homepage = "https://github.com/sgr-ksmt/Alertift"
# s.screenshots = ""
Expand Down
15 changes: 15 additions & 0 deletions Demo/Demo/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
Expand Down Expand Up @@ -29,6 +39,11 @@
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ios-marketing",
"size" : "1024x1024",
"scale" : "1x"
}
],
"info" : {
Expand Down
6 changes: 6 additions & 0 deletions Demo/Demo/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
21 changes: 21 additions & 0 deletions Demo/Demo/Assets.xcassets/alertImage.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "img2.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions Demo/Demo/Assets.xcassets/icon.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "icon.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Binary file added Demo/Demo/Assets.xcassets/icon.imageset/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 15 additions & 20 deletions Demo/Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
Alertift.alert(title: "Alertift", message: "Alertift is swifty, modern, and awesome UIAlertController wrapper.")
.image(#imageLiteral(resourceName: "alertImage"))
.titleTextColor(.red)
.messageTextColor(.blue)
.action(.default("")) { (action, index, _) in
Expand All @@ -58,14 +59,16 @@ class ViewController: UIViewController {
.action(.default("")) { (action, index, _) in
print(action, index)
}
.finally { (action, index, _) in
.finally { [weak self] (action, index, _) in
print(action, index)
self?.showAlertWithActionImage()
}
.show(on: self)
}

private func showSimpleAlert() {
Alertift.alert(title: "Sample 1", message: "Simple alert!")
.image(#imageLiteral(resourceName: "alertImage"))
.action(.default("OK"))
.show()
}
Expand Down Expand Up @@ -108,11 +111,19 @@ class ViewController: UIViewController {
}
.show()
}

private func showAlertWithActionImage() {
Alertift.alert(message: "Can use image in alert action")
.action(.default("info"), image: #imageLiteral(resourceName: "icon"))
.show(on: self)
}

private func showActionSheet(anchorView: UIView) {
Alertift.actionSheet(message: "Which food do you like?", anchorView: anchorView)
.actions(["🍣", "🍎", "🍖", "🍅"]) { (action, index) in
print(action, index)
}
.action(.default("🍣"), image: #imageLiteral(resourceName: "icon"))
.action(.default("🍎"), image: #imageLiteral(resourceName: "icon"))
.action(.default("🍖"), image: #imageLiteral(resourceName: "icon"))
.action(.default("🍅"), image: #imageLiteral(resourceName: "icon"))
.action(.cancel("None of them"))
.finally { action, index in
if action.style == .cancel {
Expand All @@ -123,22 +134,6 @@ class ViewController: UIViewController {
.show()
}
.show()

// Alertift.actionSheet(message: "Which food do you like?", anchorView: anchorView)
// .action(.default("🍣"))
// .action(.default("🍎"))
// .action(.default("🍖"))
// .action(.default("🍅"))
// .action(.cancel("None of them"))
// .finally { action, index in
// if action.style == .cancel {
// return
// }
// Alertift.alert(message: "\(index). \(action.title!)")
// .action(.default("OK"))
// .show()
// }
// .show()
}
}

35 changes: 35 additions & 0 deletions Documents/how_to_use.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,41 @@ Alertift.actionSheet(message: "Which food do you like?")

![img5](img5.png)

### Display Image

```swift
let alertImage: UIImage = ...
Alertift.alert(title: "Alert with image", message: "You can add image simpley. call `alertift.image()`")
.image(alertImage)
.action(.default("OK"))
.show()
```

![img7](img7.png)

### Add imaget to UIAlertAction

```swift
let infoIconImage: UIImage = ...
Alertift.actionSheet(message: "Which food do you like?", anchorView: anchorView)
.action(.default("🍣"), image: infoIconImage)
.action(.default("🍎"), image: infoIconImage)
.action(.default("🍖"), image: infoIconImage)
.action(.default("🍅"), image: infoIconImage)
.action(.cancel("None of them"))
.finally { action, index in
if action.style == .cancel {
return
}
Alertift.alert(message: "\(index). \(action.title!)")
.action(.default("OK"))
.show()
}
.show()
```

![img8](img8.png)

### Change colors

```swift
Expand Down
Binary file added Documents/img7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Documents/img8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 3 additions & 64 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,74 +20,13 @@ Alertift.alert(title: "Alertift", message: "Alertift is swifty, modern, and awes
- Method chain.
- Can add multiple actions at once.
- UITextField support.
- Image support. (above v3.1)
- iPad support(Action Sheet, popover).
- Can change title/message/button text/ background color **without** using private APIs.
- Can change title/message's alignment **without** using private APIs.

## Examples

```swift
Alertift.alert(title: "Sample 1", message: "Simple alert!")
.action(.default("OK"))
.show(on: self) // show on specified view controller
```

![img2](Documents/img2.png)


```swift
Alertift.alert(title: "Confirm", message: "Delete this post?")
.action(.destructive("Delete")) { _ in
// delete post
}
.action(.cancel("Cancel"))
.show()
// Default presented view controller is `UIApplication.shared.keyWindow?.rootViewController`
```

![img1](Documents/img1.png)


```swift
Alertift.alert(title: "Sign in", message: "Input your ID and Password")
.textField { textField in
textField.placeholder = "ID"
}
.textField { textField in
textField.placeholder = "Password"
textField.isSecureTextEntry = true
}
.action(.cancel("Cancel"))
.action(.default("Sign in")) { _, _, textFields in
let id = textFields?.first?.text ?? ""
let password = textFields?.last?.text ?? ""
// sign in
}
.show()
```

![img3](Documents/img3.png)


```swift
Alertift.actionSheet(message: "Which food do you like?")
.actions(["🍣", "🍎", ,"🍖", "🍅"])
.action(.cancel("None of them"))
.finally { action, index in
if action.style == .cancel {
return
}
Alertift.alert(message: "\(index). \(action.title!)")
.action(.default("OK"))
.show()
}
.show()
```

![img4](Documents/img4.png)


**See more: [How to use](Documents/how_to_use.md)**
## How to use
👉👉👉 **[How to use](Documents/how_to_use.md)**

## Requirements
- iOS 9.0+
Expand Down
23 changes: 20 additions & 3 deletions Sources/ActionSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Foundation
extension Alertift {
/// ActionSheet
final public class ActionSheet: AlertType, _AlertType {

public typealias Handler = (UIAlertAction, Int) -> Void

var _alertController: InnerAlertController!
Expand All @@ -35,9 +36,20 @@ extension Alertift {

/// Add action to alertController
public func action(_ action: Alertift.Action, handler: Handler? = nil) -> Self {
_alertController.addAction(buildAlertAction(action, handler:
return self.action(action, image: nil, handler: handler)
}

/// Add action to alertController
public func action(_ action: Alertift.Action, image: UIImage?, renderingMode: UIImageRenderingMode = .automatic, handler: Handler? = nil) -> Self {
let alertAction = buildAlertAction(action, handler:
merge(_alertController.actionHandler, handler ?? { (_, _) in })
))
)

if let image = image {
alertAction.setValue(image.withRenderingMode(renderingMode), forKey: "image")
}

_alertController.addAction(alertAction)
return self
}

Expand Down Expand Up @@ -81,7 +93,12 @@ extension Alertift {
func convertFinallyHandler(_ handler: Any) -> InnerAlertController.FinallyHandler {
return { (action, index, _) in (handler as? Handler)?(action, index) }
}


public func image(_ image: UIImage?, imageTopMargin: Alertift.ImageTopMargin = .none) -> Self {
_alertController.setImage(image, imageTopMargin: imageTopMargin)
return self
}

deinit {
Debug.log()
}
Expand Down
51 changes: 45 additions & 6 deletions Sources/Alert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
import Foundation

extension Alertift {
public enum AlertImagePosition {
case top // Above title and message
case center // Between title and message
case bottom // Below title and message
}

/// Alert
final public class Alert: AlertType, _AlertType {
public typealias Handler = (UIAlertAction, Int, [UITextField]?) -> Void
Expand Down Expand Up @@ -51,13 +57,41 @@ extension Alertift {
/// - handler: The block to execute after this action performed.
/// - Returns: Myself
public func action(_ action: Alertift.Action, isPreferred: Bool, handler: Handler? = nil) -> Self {
addActionToAlertController(
buildAlertAction(
action,
handler: merge(_alertController.actionWithTextFieldsHandler, handler ?? { (_, _, _)in })
),
isPreferred: isPreferred
return self.action(action, image: nil, isPreferred: isPreferred, handler: handler)
}

/// Add action to Alert
///
/// - Parameters:
/// - action: Alert action.
/// - image: Image of action.
/// - renderMode: Render mode for alert action image. Default is `.automatic`
/// - handler: The block to execute after this action performed.
/// - Returns: Myself
public func action(_ action: Alertift.Action, image: UIImage?, renderingMode: UIImageRenderingMode = .automatic, handler: Handler? = nil) -> Self {
return self.action(action, image: image, renderingMode: renderingMode, isPreferred: false, handler: handler)
}

/// Add action to Alert
///
/// - Parameters:
/// - action: Alert action.
/// - image: Image of action
/// - renderMode: Render mode for alert action image. Default is `.automatic`
/// - isPreferred: If you want to change this action to preferredAction, set true. Default is false.
/// - handler: The block to execute after this action performed.
/// - Returns: Myself
public func action(_ action: Alertift.Action, image: UIImage?, renderingMode: UIImageRenderingMode = .automatic, isPreferred: Bool, handler: Handler? = nil) -> Self {
let alertAction = buildAlertAction(
action,
handler: merge(_alertController.actionWithTextFieldsHandler, handler ?? { (_, _, _)in })
)

if let image = image {
alertAction.setValue(image.withRenderingMode(renderingMode), forKey: "image")
}

addActionToAlertController(alertAction, isPreferred: isPreferred)
return self
}

Expand Down Expand Up @@ -100,6 +134,11 @@ extension Alertift {
func convertFinallyHandler(_ handler: Any) -> InnerAlertController.FinallyHandler {
return { (handler as? Handler)?($0, $1, $2) }
}

public func image(_ image: UIImage?, imageTopMargin: Alertift.ImageTopMargin = .none) -> Self {
_alertController.setImage(image, imageTopMargin: imageTopMargin)
return self
}

deinit {
Debug.log()
Expand Down
Loading

0 comments on commit 25979c1

Please sign in to comment.