Skip to content

Instantly share code, notes, and snippets.

View jimmythai's full-sized avatar

Atsushi Yamamoto jimmythai

View GitHub Profile
@jimmythai
jimmythai / ToogleBluetooth.scpt
Created December 28, 2019 05:43
Open System Preferences -> Bluetooth -> Toggle the Bluetooth on/off switch
tell application "System Preferences"
reveal pane "com.apple.preferences.Bluetooth"
end tell
clickBluetoothSwitch()
delay 2
clickBluetoothSwitch()
extension Collection where Indices.Iterator.Element == Index {
public subscript(safe index: Index) -> Iterator.Element? {
return (startIndex <= index && index < endIndex) ? self[index] : nil
}
}
// Cell
final class CircleCell: UICollectionViewCell {
@IBOutlet private weak var circleView: UIView!
func setCircleView() {
layoutIfNeeded() // Without this, it doesn't draw a circle correctly.
circleView.layer.cornerRadius = circleView.frame.width / 2
}
}
let scale = 10
let numberStrings = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
print(numberStrings.prefix(scale)) // ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
print(numberStrings.suffix(scale - scale)) // []
protocol StructToDictionary: Encodable {
var dictionary: [String: Any]? { get }
}
extension StructToDictionary {
var dictionary: [String: Any]? {
let encoder = JSONEncoder()
encoder.keyEncodingStrategy = .convertToSnakeCase
guard let data = try? encoder.encode(self) else {
extension UIView {
enum BorderSide {
case left
case right
case top
case bottom
}
func addBorder(toSide side: BorderSide, withColor color: UIColor, withThickness thickness: CGFloat) {
let border = CALayer()
class Throttler {
private let delay: DispatchTimeInterval
private let queue: DispatchQueue
private var previousFiredTime: DispatchTime = .now()
init(delay: DispatchTimeInterval, queue: DispatchQueue = .main) {
self.delay = delay
self.queue = queue
}
class Debouncer {
private let delay: DispatchTimeInterval
private let queue: DispatchQueue
private var previousFiredTime: DispatchTime = .now()
init(delay: DispatchTimeInterval, queue: DispatchQueue = .main) {
self.delay = delay
self.queue = queue
}
@jimmythai
jimmythai / Debounce.swift
Last active April 21, 2019 12:40
Use case: incremental search
extension DispatchQueue {
func debounce(delay: DispatchTimeInterval) -> (_ block: @escaping () -> Void) -> Void {
var lastFireTime: DispatchTime = .now()
return { [weak self, delay] block in
let deadline: DispatchTime = .now() + delay
lastFireTime = .now()
self?.asyncAfter(deadline: deadline) { [delay] in
let now: DispatchTime = .now()
@jimmythai
jimmythai / Throttle.swift
Last active April 21, 2019 12:12
Use case: To prevent button from being tapped multiple times
extension DispatchQueue {
func throttle(delay: DispatchTimeInterval) -> (_ block: @escaping () -> Void) -> Void {
var lastFireTime: DispatchTime = .now()
return { [weak self, delay] block in
let deadline: DispatchTime = .now() + delay
self?.asyncAfter(deadline: deadline) { [delay] in
let now: DispatchTime = .now()
let when: DispatchTime = lastFireTime + delay