Skip to content

Commit

Permalink
Merge pull request #10 from lm2343635/fix/cancel
Browse files Browse the repository at this point in the history
Add cancel to alert config
  • Loading branch information
lm2343635 authored Oct 29, 2020
2 parents b59b971 + 3abedd6 commit dcef763
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 56 deletions.
14 changes: 7 additions & 7 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
PODS:
- Kingfisher (5.14.0):
- Kingfisher/Core (= 5.14.0)
- Kingfisher/Core (5.14.0)
- RxAlertViewable (0.9.2):
- Kingfisher (5.15.6):
- Kingfisher/Core (= 5.15.6)
- Kingfisher/Core (5.15.6)
- RxAlertViewable (0.9.3):
- RxCocoa (~> 5)
- RxSwift (~> 5)
- RxCocoa (5.1.1):
Expand Down Expand Up @@ -33,13 +33,13 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
Kingfisher: 7b64389a43139c903ec434788344c288217c792d
RxAlertViewable: ea5dcaf4234b24bc4d889fa6f263bb1282cb89f9
Kingfisher: b3554e7bf6106115b44e8795300bad580ef2fdc7
RxAlertViewable: 8bb45319682b7b8ee4c1dc0764ae4f7bb9212c72
RxCocoa: 32065309a38d29b5b0db858819b5bf9ef038b601
RxRelay: d77f7d771495f43c556cbc43eebd1bb54d01e8e9
RxSwift: 81470a2074fa8780320ea5fe4102807cb7118178
SnapKit: 97b92857e3df3a0c71833cce143274bf6ef8e5eb

PODFILE CHECKSUM: b4605b4abcc2e96e810853dd0c68eb98e91ba151

COCOAPODS: 1.9.3
COCOAPODS: 1.10.0
3 changes: 2 additions & 1 deletion Example/RxAlertViewable/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
RxAlert.config = RxAlertConfig(
RxAlertConfig.current = RxAlertConfig(
tip: "My Tip",
confirm: "My Confirm",
warning: "My Warning",
error: "My Error",
yes: "My Yes",
no: "My No",
ok: "My OK",
cancel: "My Cancel",
tintColor: .blue
)

Expand Down
2 changes: 1 addition & 1 deletion RxAlertViewable.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

Pod::Spec.new do |s|
s.name = 'RxAlertViewable'
s.version = '0.9.2'
s.version = '0.9.3'
s.summary = 'A simple alert library with RxSwift supported.'

s.description = <<-DESC
Expand Down
12 changes: 12 additions & 0 deletions RxAlertViewable/Classes/ObserverType+RxAlert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ extension ObserverType where Element == RxAlert {
) {
onNext(.customConfirm(title: title, message: message, item: item, onConfirm: onConfirm, onDeny: onDeny))
}

public func onNextCustomConfirm(
title: String,
message: String,
confirmTitle: String,
denyTitle: String? = nil,
onConfirm: RxAlertCompletion = nil,
onDeny: RxAlertCompletion = nil
) {
let item = UIAlertItem(confirmTitle: confirmTitle, denyTitle: denyTitle)
onNext(.customConfirm(title: title, message: message, item: item, onConfirm: onConfirm, onDeny: onDeny))
}

}

Expand Down
4 changes: 2 additions & 2 deletions RxAlertViewable/Classes/RxActionSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public struct RxActionSheetConfig {
var tintColor: UIColor?

public init(cancel: String? = nil, tintColor: UIColor? = nil) {
self.cancel = cancel ?? "Cancel"
self.cancel = cancel ?? RxAlertConfig.current.cancel
self.tintColor = tintColor
}

Expand Down Expand Up @@ -72,7 +72,7 @@ public struct RxActionSheet {
}.forEach {
alertController.addAction($0)
}
if let tintColor = RxAlert.config.tintColor {
if let tintColor = RxAlertConfig.current.tintColor {
alertController.view.tintColor = tintColor
}
if let view = sourceView {
Expand Down
51 changes: 8 additions & 43 deletions RxAlertViewable/Classes/RxAlert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

public struct RxAlertConfig {

var tip: String
var confirm: String
var warning: String
var error: String
var yes: String
var no: String
var ok: String
var tintColor: UIColor?

public init(
tip: String? = nil,
confirm: String? = nil,
warning: String? = nil,
error: String? = nil,
yes: String? = nil,
no: String? = nil,
ok: String? = nil,
tintColor: UIColor? = nil
) {
self.tip = tip ?? "Tip"
self.confirm = confirm ?? "Confirm"
self.warning = warning ?? "Warning"
self.error = error ?? "Error"
self.yes = yes ?? "Yes"
self.no = no ?? "No"
self.ok = ok ?? "OK"
self.tintColor = tintColor
}

}

public typealias RxAlertCompletion = (() -> ())?

public enum RxAlertCategory {
Expand All @@ -66,8 +33,6 @@ public enum RxAlertCategory {

public struct RxAlert {

public static var config = RxAlertConfig()

private var title: String
private var message: String
private var item: RxAlertItem?
Expand All @@ -90,7 +55,7 @@ public struct RxAlert {

let alertController = controllerType.create(title: title, message: message)
alertController.setAction(for: category, item: item)
if let tintColor = RxAlert.config.tintColor {
if let tintColor = RxAlertConfig.current.tintColor {
alertController.view.tintColor = tintColor
}
return alertController
Expand All @@ -102,27 +67,27 @@ extension RxAlert {

public static func tip(_ message: String, onConfirm: RxAlertCompletion = nil) -> RxAlert {
self.init(
title: config.tip,
title: RxAlertConfig.current.tip,
message: message,
item: UIAlertItem(confirmTitle: config.ok),
item: UIAlertItem(confirmTitle: RxAlertConfig.current.ok),
category: .single(onConfirm: onConfirm)
)
}

public static func warning(_ message: String, onConfirm: RxAlertCompletion = nil) -> RxAlert {
self.init(
title: config.warning,
title: RxAlertConfig.current.warning,
message: message,
item: UIAlertItem(confirmTitle: config.ok),
item: UIAlertItem(confirmTitle: RxAlertConfig.current.ok),
category: .single(onConfirm: onConfirm)
)
}

public static func error(_ message: String, onConfirm: RxAlertCompletion = nil) -> RxAlert {
self.init(
title: config.error,
title: RxAlertConfig.current.error,
message: message,
item: UIAlertItem(confirmTitle: config.ok),
item: UIAlertItem(confirmTitle: RxAlertConfig.current.ok),
category: .single(onConfirm: onConfirm)
)
}
Expand All @@ -133,7 +98,7 @@ extension RxAlert {
onDeny: RxAlertCompletion = nil
) -> RxAlert {
self.init(
title: config.confirm,
title: RxAlertConfig.current.confirm,
message: message,
category: .double(onConfirm: onConfirm, onDeny: onDeny)
)
Expand Down
61 changes: 61 additions & 0 deletions RxAlertViewable/Classes/RxAlertConfig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// RxAlertConfig.swift
// RxAlertViewable
//
// Created by Meng Li on 2020/10/29.
// Copyright © 2018 MuShare. All rights reserved.
//

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

public struct RxAlertConfig {
var tip: String
var confirm: String
var warning: String
var error: String
var yes: String
var no: String
var ok: String
var cancel: String
var tintColor: UIColor?

public init(
tip: String? = nil,
confirm: String? = nil,
warning: String? = nil,
error: String? = nil,
yes: String? = nil,
no: String? = nil,
ok: String? = nil,
cancel: String? = nil,
tintColor: UIColor? = nil
) {
self.tip = tip ?? "Tip"
self.confirm = confirm ?? "Confirm"
self.warning = warning ?? "Warning"
self.error = error ?? "Error"
self.yes = yes ?? "Yes"
self.no = no ?? "No"
self.ok = ok ?? "OK"
self.cancel = cancel ?? "Cancel"
self.tintColor = tintColor
}

public static var current = RxAlertConfig()
}
4 changes: 2 additions & 2 deletions RxAlertViewable/Classes/RxAlertController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ extension UIAlertController: RxAlertController {
}

public func setAction(for category: RxAlertCategory, item: RxAlertItem?) {
var confirmTitle = RxAlert.config.yes
var denyTitle = RxAlert.config.no
var confirmTitle = RxAlertConfig.current.yes
var denyTitle = RxAlertConfig.current.no
if let alertItem = item as? UIAlertItem {
confirmTitle = alertItem.confirmTitle
if let deny = alertItem.denyTitle {
Expand Down

0 comments on commit dcef763

Please sign in to comment.