Skip to content

Instantly share code, notes, and snippets.

View jasdev's full-sized avatar

Jasdev Singh jasdev

View GitHub Profile
@jasdev
jasdev / ScrimLoader.swift
Last active December 18, 2023 02:35
Rough sketch of Arc’s scrim loading view.
import SwiftUI
/**
### Exercises for the viewer
- Phase interrupt handling.
- Use Swift concurrency.
- Color scheme awareness.
- Rework animations to be more spring-like à la what shipped in `0.90.0`.
@jasdev
jasdev / srt_parser.swift
Last active November 15, 2022 20:50
Sketch of an SRT file parser.
import Parsing
import Foundation
// [SubRip file format spec.](https://en.wikipedia.org/wiki/SubRip#File_format)
let sampleSRTString =
"""
1
00:00:00,540 --> 00:00:00,960
Yo-yo
@jasdev
jasdev / geometryPreference.swift
Last active April 24, 2020 19:57
objc.io’s geometryPreference modifier and Preference view for propagating up proxied geometries and reading them.
import SwiftUI
struct Preference<PreferenceKey: SwiftUI.PreferenceKey, Content: View>: View
where PreferenceKey.Value: Equatable {
private let content: (PreferenceKey.Value) -> Content
@State private var value: PreferenceKey.Value = PreferenceKey.defaultValue
init(
_ key: PreferenceKey.Type,
@jasdev
jasdev / final_cancellation_handling.swift
Last active April 20, 2020 16:00
Final `ReplaySubject` and `.Subscription` cancellation handling.
final class ReplaySubject<Output, Failure: Error>: Subject {
typealias Output = Output
typealias Failure = Failure
private let bufferSize: Int
private var completion: Subscribers.Completion<Failure>?
private var isActive: Bool { /* … */ }
@jasdev
jasdev / postfix_type_erasure.swift
Last active March 25, 2020 21:22
Postfix type erasure in Combine.
import Combine
postfix operator ^
postfix func ^ <Publisher: Combine.Publisher>(_ publisher: Publisher)
-> AnyPublisher<Publisher.Output, Publisher.Failure> {
publisher.eraseToAnyPublisher()
}
@jasdev
jasdev / FoundationErrorWrapper.swift
Created July 14, 2016 02:05
An attempt at a protocol that can imply the present of specific cases
import Foundation
public protocol FoundationErrorWrapper: ErrorProtocol {
static func wrappingError(_ error: NSError) -> Self
}
enum SomeError: ErrorProtocol, FoundationErrorWrapper {
case a
case wrappingError(NSError)
}
@jasdev
jasdev / CharacterSet+StringLiteralConvertible.swift
Created June 30, 2016 01:33
CharacterSet StringLiteralConvertible
extension CharacterSet: StringLiteralConvertible, ExtendedGraphemeClusterLiteralConvertible {
public init(stringLiteral value: String) {
self.init(charactersIn: value)
}
public init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterType) {
self.init(charactersIn: value)
}
public init(unicodeScalarLiteral value: UnicodeScalarType) {
@jasdev
jasdev / AppDefault.swift
Last active June 14, 2019 10:20
Swift nonmutating Example
import Foundation
enum AppDefault {
case APIServer, runMode, homeDirectory, reactHost
var stringValue: String? {
get {
return NSUserDefaults.standardUserDefaults().stringForKey(String(self))
}
final class SampleViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .System)
// ...
button.addTarget(self, action: #selector(SampleViewController.buttonTapped(_:)), forControlEvents: .TouchUpInside)
final class SampleViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .System)
// ...
button.addTarget(self, action: #selector(SampleViewController.buttonTapped(_:)), forControlEvents: .TouchUpInside)