Skip to content

Commit

Permalink
alert μˆ˜μ •
Browse files Browse the repository at this point in the history
  • Loading branch information
pkh0225 committed Nov 20, 2024
1 parent ad0c058 commit f5d335c
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 93 deletions.
13 changes: 6 additions & 7 deletions Example-iOS/TestSwiftHelper.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/* Begin PBXBuildFile section */
200019CB2C9D0B9D00E2094A /* MemoryViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 200019CA2C9D0B9D00E2094A /* MemoryViewController.swift */; };
200019CF2C9D2CAC00E2094A /* ScopeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 200019CE2C9D2CAC00E2094A /* ScopeViewController.swift */; };
2092293C2CEAFCF300E4B140 /* SwiftHelper in Frameworks */ = {isa = PBXBuildFile; productRef = 2092293B2CEAFCF300E4B140 /* SwiftHelper */; };
200EB4BD2CEDC0BD0011BD7B /* SwiftHelper in Frameworks */ = {isa = PBXBuildFile; productRef = 200EB4BC2CEDC0BD0011BD7B /* SwiftHelper */; };
520F389721229A890081B956 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 520F389621229A890081B956 /* AppDelegate.swift */; };
520F389921229A890081B956 /* TestViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 520F389821229A890081B956 /* TestViewController.swift */; };
520F389C21229A890081B956 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 520F389A21229A890081B956 /* Main.storyboard */; };
Expand Down Expand Up @@ -38,7 +38,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
2092293C2CEAFCF300E4B140 /* SwiftHelper in Frameworks */,
200EB4BD2CEDC0BD0011BD7B /* SwiftHelper in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -111,7 +111,7 @@
);
name = TestSwiftHelper;
packageProductDependencies = (
2092293B2CEAFCF300E4B140 /* SwiftHelper */,
200EB4BC2CEDC0BD0011BD7B /* SwiftHelper */,
);
productName = Test;
productReference = 520F389321229A890081B956 /* TestSwiftHelper.app */;
Expand Down Expand Up @@ -143,7 +143,7 @@
);
mainGroup = 520F388A21229A890081B956;
packageReferences = (
20DE3CFD2CEAFC5700B5F86A /* XCLocalSwiftPackageReference "../../SwiftHelper" */,
200EB4BB2CEDC0BD0011BD7B /* XCLocalSwiftPackageReference "../../SwiftHelper" */,
);
productRefGroup = 520F389421229A890081B956 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -394,16 +394,15 @@
/* End XCConfigurationList section */

/* Begin XCLocalSwiftPackageReference section */
20DE3CFD2CEAFC5700B5F86A /* XCLocalSwiftPackageReference "../../SwiftHelper" */ = {
200EB4BB2CEDC0BD0011BD7B /* XCLocalSwiftPackageReference "../../SwiftHelper" */ = {
isa = XCLocalSwiftPackageReference;
relativePath = ../../SwiftHelper;
};
/* End XCLocalSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
2092293B2CEAFCF300E4B140 /* SwiftHelper */ = {
200EB4BC2CEDC0BD0011BD7B /* SwiftHelper */ = {
isa = XCSwiftPackageProductDependency;
package = 20DE3CFD2CEAFC5700B5F86A /* XCLocalSwiftPackageReference "../../SwiftHelper" */;
productName = SwiftHelper;
};
/* End XCSwiftPackageProductDependency section */
Expand Down
2 changes: 1 addition & 1 deletion Example-iOS/TestSwiftHelper/VC/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="TAb-HP-PfS">
<rect key="frame" x="119" y="316" width="136" height="35"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="mainQueue"/>
<state key="normal" title="PopupQueue"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="borderWidth">
<real key="value" value="2"/>
Expand Down
24 changes: 14 additions & 10 deletions Example-iOS/TestSwiftHelper/VC/ClosureQueueController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ class ClosureQueueController: UIViewController, RouterProtocol {
override func viewDidLoad() {
super.viewDidLoad()
ActionQueue.shared.addAction { _ in
UIAlertController.alert(title: "Test 111")
alert(title: "Test 111", message: nil)
}
ActionQueue.shared.addAction { _ in
UIAlertController.alert(title:"Test 222")
alert(title:"Test 222", message: nil)
}
ActionQueue.shared.addAction { _ in
UIAlertController.alert(title:"Test 333")
alert(title:"Test 333", message: nil)
}
}

Expand All @@ -30,27 +30,31 @@ class ClosureQueueController: UIViewController, RouterProtocol {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func onInQueue(_ sender: UIButton) {
sender.tag += 1
let count = sender.tag
ActionQueue.shared.addAction { _ in
UIAlertController.alert(title:"Test \(count)")
alert(title:"Test \(count)", message: nil)
}
}

@IBAction func onDequeue(_ sender: UIButton) {
ActionQueue.shared.nextRun()
}


@IBAction func onMainQueue(_ sender: UIButton) {
DispatchQueue.global(qos: .userInteractive).async {
for i in 0..<999 {
DispatchQueue.main.async {
print(i)
for i in 0..<5 {
alert(title:"Test __(\(i))", message: nil, cancelButtonTitle: "μ·¨μ†Œ", otherButtonTitles: ["확인"]) { alertVC,buttonIndex in
print(buttonIndex)
}
}
gcd_main_after(0.5) {
for i in 10..<15 {
alert(title:"Test __(\(i))", message: nil, cancelButtonTitle: "μ·¨μ†Œ", otherButtonTitles: ["확인"]) { alertVC,buttonIndex in
print(buttonIndex)
}
}
}

}
}
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.7
// swift-tools-version: 5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand Down
13 changes: 7 additions & 6 deletions Sources/SwiftHelper/Dispatc/DispatchQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,22 @@ public final class ActionQueue: Sendable {

public init() {}

@preconcurrency public func nextRun(_ value: [String: Any]? = nil) {
public func nextRun(_ value: [String: Any]? = nil) {
queue.sync {
guard !self.actions.isEmpty, let work = self.actions.first else { return }
let ucsw = UncheckedSendableWrappers(value1: work, value2: value)
let workWrapper = UncheckedSendableWrapper(work)
let valueWrapper = UncheckedSendableWrapper(value)
self.actions.removeFirst()
DispatchQueue.main.async {
ucsw.value1(ucsw.value2)
workWrapper.value(valueWrapper.value)
}
}
}

@preconcurrency public func addAction(_ work: @escaping @convention(block) (_ value: [String: Any]?) -> Void) {
let ucsw = UncheckedSendableWrapper(work)
public func addAction(_ work: @escaping @convention(block) (_ value: [String: Any]?) -> Void) {
let workWrapper = UncheckedSendableWrapper(work)
self.queue.async(flags: .barrier) {
self.actions.append(ucsw.value)
self.actions.append(workWrapper.value)
}
}
}
11 changes: 0 additions & 11 deletions Sources/SwiftHelper/Json/JSONSerializableExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,3 @@ extension JSONSerializable {
}
}
}

extension Optional: JSONSerializable {
public var JSONRepresentation: [String: Any] {
guard let x = self else { return [String: Any]() }
if let value: JSONSerializable = x as? JSONSerializable {
return value.JSONRepresentation
}
return [String: Any]()
}

}
96 changes: 66 additions & 30 deletions Sources/SwiftHelper/UI/UIAlertControllerExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,44 @@

import UIKit

public typealias UIAlertControllerClosure = (_ alertVC: UIAlertController, _ buttonIndex: Int) -> Void
@MainActor
class AlertQueue {
static var actions: [() -> Void] = []

public static func nextRun() {
DispatchQueue.main.async {
guard !self.actions.isEmpty, let work = self.actions.first else { return }
work()
self.actions.removeFirst()
}
}

public static func addAction(work: @escaping () -> Void) {
self.actions.append(work)
}
}

@MainActor
public func alert(title: String?,
message: String?,
cancelButtonTitle: String? = "확인",
otherButtonTitles: [String]? = nil,
closure: ((_ alertVC: UIAlertController, _ buttonIndex: Int) -> Void)? = nil) {
AlertQueue.addAction {
UIAlertController.alert(title: title,
message: message,
cancelButtonTitle: cancelButtonTitle,
otherButtonTitles: otherButtonTitles,
isAutoHide: false,
autoHideClosure: nil) { (alertVC, buttonIndex) in
closure?(alertVC, buttonIndex)
AlertQueue.nextRun()
}
}
if AlertQueue.actions.count == 1 {
AlertQueue.nextRun()
}
}

extension UIAlertController {
public func getActionIndex(_ action: UIAlertAction) -> Int {
Expand Down Expand Up @@ -68,50 +105,49 @@ extension UIAlertController {
}

public static func alert(title: String?,
message: String? = nil,
cancelButtonTitle: String? = "확인",
otherButtonTitles: String?...,
isAutoHide: Bool = false,
closure: UIAlertControllerClosure? = nil,
autoHideClosure: VoidClosure? = nil) {
let alert: UIAlertController = UIAlertController(title: title, message: message, preferredStyle: .alert)

if let cancelTitle = cancelButtonTitle {
let cancelAction: UIAlertAction = UIAlertAction(title: cancelTitle, style: .cancel, handler: { (action: UIAlertAction) in
if let callBack = closure {
callBack(alert, alert.getActionIndex(action))
}
})
message: String?,
cancelButtonTitle: String?,
otherButtonTitles: [String]? = nil,
isAutoHide: Bool = false,
autoHideClosure: VoidClosure? = nil,
closure: ((_ alertVC: UIAlertController, _ buttonIndex: Int) -> Void)? = nil) {
let alertVC: UIAlertController = UIAlertController(title: title, message: message, preferredStyle: .alert)

let cancelAction: UIAlertAction = UIAlertAction(title: cancelButtonTitle, style: .cancel, handler: { (action: UIAlertAction) in
if let closure {
closure(alertVC, alertVC.getActionIndex(action))
}
})

alert.addAction(cancelAction)
}
alertVC.addAction(cancelAction)

for title in otherButtonTitles {
guard let title else { continue }
let otherAction: UIAlertAction = UIAlertAction(title: title, style: .default, handler: { (action: UIAlertAction) in
if let callBack = closure {
callBack(alert, alert.getActionIndex(action))
}
})
if let otherButtonTitles {
for title in otherButtonTitles {
let otherAction: UIAlertAction = UIAlertAction(title: title, style: .default, handler: { (action: UIAlertAction) in
if let closure {
closure(alertVC, alertVC.getActionIndex(action))
}
})

alert.addAction(otherAction)
alertVC.addAction(otherAction)
}
}

alert.show()
alertVC.show()

if isAutoHide {
// auto close
let deadlineTime: DispatchTime = DispatchTime.now() + .seconds(2)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
alert.dismiss(animated: true, completion: autoHideClosure)
alertVC.dismiss(animated: true, completion: autoHideClosure)
}
}
}

public static func actionSheet(title: String? = nil,
message: String? = nil,
buttonTitles: [String],
closure: UIAlertControllerClosure? = nil) {
message: String? = nil,
buttonTitles: [String],
closure: ((_ alertVC: UIAlertController, _ buttonIndex: Int) -> Void)? = nil) {
guard buttonTitles.count > 0 else { return }

let alert: UIAlertController = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftHelper/UI/UIImageExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ extension UIImage {

defer {
if success == false {
UIAlertController.alert(title: "", message: "μ΅œλŒ€ \(Int(maxSize))MB κΉŒμ§€ 등둝 κ°€λŠ₯ν•©λ‹ˆλ‹€.")
alert(title: nil, message: "μ΅œλŒ€ \(Int(maxSize))MB κΉŒμ§€ 등둝 κ°€λŠ₯ν•©λ‹ˆλ‹€.")
}
}

Expand Down
3 changes: 2 additions & 1 deletion Sources/SwiftHelper/UI/UILabelExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ extension UILabel {
guard let owner else { return }
print("\n 🌸 Show Class Name : \(owner.className)\n")
UIPasteboard.general.string = owner.className
UIAlertController.alert(title: nil, message: "\(owner.className) λ³΅μ‚¬λ˜μ—ˆμŠ΅λ‹ˆλ‹€!")
alert(title: nil, message: "\(owner.className) λ³΅μ‚¬λ˜μ—ˆμŠ΅λ‹ˆλ‹€!")
// UIAlertController.alert(title: nil, message: "\(owner.className) λ³΅μ‚¬λ˜μ—ˆμŠ΅λ‹ˆλ‹€!")
}
label.addTapGesture { [weak label] recognizer in
guard let label else { return }
Expand Down
25 changes: 0 additions & 25 deletions Sources/SwiftHelper/UI/UITextFieldExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,6 @@ extension UITextField {
self.leftViewMode = UITextField.ViewMode.always
}

/// Ways to validate by comparison
public enum textFieldValidationOptions: Int {
case equalTo
case greaterThan
case greaterThanOrEqualTo
case lessThan
case lessThanOrEqualTo
}

/// Validation length of character counts in UITextField
public func validateLength(ofCount count: Int, option: UITextField.textFieldValidationOptions) -> Bool {
switch option {
case .equalTo:
return self.text!.count == count
case .greaterThan:
return self.text!.count > count
case .greaterThanOrEqualTo:
return self.text!.count >= count
case .lessThan:
return self.text!.count < count
case .lessThanOrEqualTo:
return self.text!.count <= count
}
}

/// Validation of email format based on https://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address and https://stackoverflow.com/questions/2049502/what-characters-are-allowed-in-an-email-address
// TODO match String.isEmail method
public func validateEmail() -> Bool {
Expand Down

0 comments on commit f5d335c

Please sign in to comment.