This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import Foundation | |
func generateQR(from string: String) -> Data? { | |
let data = string.data(using: String.Encoding.utf8) | |
guard let qrFilter = CIFilter(name: "CIQRCodeGenerator") else { return nil } | |
qrFilter.setValue(data, forKey: "inputMessage") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From this | |
didFinishLoading = { [weak self] in | |
guard let self = self else { return } | |
self.tableView.reloadData() | |
} | |
// To this | |
didFinishLoading = weaker(self) { s in | |
// No more boilerplate code :) | |
s.tableView.reloadData() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Handle UIControl event closure-base instead of (target/action) | |
Example: | |
```swift | |
// Button | |
button.on(.touchUpInside) { btn in | |
print("Button clicked") | |
} | |
// TextField | |
textField.on(.editingChanged) { tf in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Flexible and faster way to style your View | |
Example: | |
```swift | |
let label = UILabel() | |
label.styling(UIColor.blue.style, UIFont.boldSystemFont(ofSize: 18).style) | |
``` | |
*/ | |
protocol UIComponentStyler: AnyObject { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Return a new function that will be called only once after `delay` time passed between invocation | |
func debounce(delay: TimeInterval, queue: DispatchQueue = .main, function: @escaping () -> Void) -> () -> Void { | |
var currentWorkItem: DispatchWorkItem? | |
return { | |
currentWorkItem?.cancel() | |
currentWorkItem = DispatchWorkItem { function() } | |
queue.asyncAfter(deadline: .now() + delay, execute: currentWorkItem!) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Builder Pattern way to write AutoLayout code | |
Example: | |
```swift | |
AutoLayoutBuilder(button) | |
.bottomTo(view.layoutMarginsGuide.bottomAnchor) | |
.centerXTo(view.centerXAnchor) | |
.width(value: button.width) | |
.height(value: button.height) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func dPrint(_ item: Any) { | |
#if DEBUG | |
Swift.print(item) | |
#endif | |
} | |
final class Log { | |
private enum LogType: String { | |
case error = "[⛔️]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Reachability | |
final class NetworkListener { | |
private let notification = Notification.Name("NetworkListenerStatusDidChanged") | |
private let reachability: Reachability! | |
/// Return true if connected to Internet | |
private(set) var isConnected: Bool! | |
init() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIAlertController { | |
// MARK: - Typealias | |
typealias Alert = UIAlertController | |
typealias Action = UIAlertAction | |
typealias Block = ((Alert) -> Void)? | |
// MARK: - Static function | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Optional where Wrapped == String { | |
var orEmpty: Wrapped { | |
return self ?? "" | |
} | |
var isNullOrEmpty: Bool { | |
return self?.isEmpty ?? true | |
} |
OlderNewer