Skip to content

Instantly share code, notes, and snippets.

View HarshilShah's full-sized avatar
🏎️

Harshil Shah HarshilShah

🏎️
View GitHub Profile
@HarshilShah
HarshilShah / UserDefault.swift
Created October 18, 2019 15:23
An observable property wrapper for UserDefaults
import Foundation
import Combine
@propertyWrapper
final class UserDefault<Wrapped>: NSObject, ObservableObject {
typealias Validator = (Wrapped) -> (Wrapped)
private let suite: UserDefaults
private let key: String
@HarshilShah
HarshilShah / LabeledContainer.swift
Created March 5, 2020 14:27
A basic attempt at recreating the Form SwiftUI component on macOS
import SwiftUI
struct LabeledViewAlignment: AlignmentID {
static func defaultValue(in context: ViewDimensions) -> CGFloat {
context[.leading]
}
}
extension HorizontalAlignment {
static let labeledViewAlignment = HorizontalAlignment(LabeledViewAlignment.self)
@HarshilShah
HarshilShah / FetchResult.swift
Created July 15, 2020 13:32
A wrapper around PHFetchResult that conforms to various Swift sequence protocols
import Photos
final class FetchResult<Object: AnyObject> {
let phFetchResult: PHFetchResult<Object>
init(_ phFetchResult: PHFetchResult<Object>) {
self.phFetchResult = phFetchResult
}
}
@HarshilShah
HarshilShah / Property.swift
Created February 5, 2021 16:00
A ReactiveSwift.Property equivalent in Combine
import Combine
import Foundation
final class Property<Value> {
// MARK: Public properties
var value: Value { subject.value }
// MARK: Private properties
@HarshilShah
HarshilShah / TappableButton.swift
Created April 29, 2021 09:12
A SwiftUI view that allows you to create a button which can be styled inline, based on whether it is pressed
import SwiftUI
struct TappableButton<Content: View>: View {
private struct PressableButtonStyle: ButtonStyle {
@ViewBuilder var label: (Bool) -> Content
func makeBody(configuration: Configuration) -> some View {
label(configuration.isPressed)
}
}
@HarshilShah
HarshilShah / ContentView.swift
Last active January 4, 2024 14:07
Trying to backport the `safeAreaInset` modifier to iOS 14
struct ContentView: View {
@State private var text = ""
var body: some View {
ScrollView {
LazyVStack(alignment: .leading) {
ForEach(1 ..< 20) { number in
Text("Row number \(number)")
.padding()
}
@HarshilShah
HarshilShah / Effect+ResultTask.swift
Created January 27, 2022 17:19
A convenience initialiser for Effect that allows you to use Effect.task while still retaining strongly typed errors
extension Effect {
static func resultTask(
priority: TaskPriority? = nil,
operation: @escaping @Sendable () async -> Result<Output, Failure>
) -> Self {
Effect<Result<Output, Failure>, Never>
.task(priority: priority, operation: operation)
.flatMap { result -> Self in
switch result {
case .success(let value): return Effect(value: value)
@HarshilShah
HarshilShah / SymmetricHStack.swift
Created June 14, 2022 15:36
A SwiftUI view to render an HStack with centered, leading, and trailing contents
import SwiftUI
struct SymmetricHStack<Content: View, Leading: View, Trailing: View>: View {
@ViewBuilder var content: () -> Content
@ViewBuilder var leading: () -> Leading
@ViewBuilder var trailing: () -> Trailing
var body: some View {
HStack {
ZStack(alignment: .leading) {
@HarshilShah
HarshilShah / View+ScaledFrame.swift
Created July 30, 2022 04:51
A SwiftUI extension that scales a frame using a specified dynamic type style
struct ScalingFrameModifier: ViewModifier {
@ScaledMetric var width: CGFloat
@ScaledMetric var height: CGFloat
var hasWidthSpecified: Bool
var hasHeightSpecified: Bool
var alignment: Alignment = .center
init(
width: CGFloat? = nil,
height: CGFloat? = nil,
@HarshilShah
HarshilShah / PhotosStylePicker-Final.swift
Created December 21, 2022 10:23
A SwiftUI picker that tries to match the Years/Months/Days/All Photos palette in the Library tab of Photos for iOS
import SwiftUI
protocol TitleProvider {
var title: String { get }
}
extension ColorScheme {
var dual: ColorScheme {
switch self {
case .light: return .dark