Skip to content

Instantly share code, notes, and snippets.

View HarshilShah's full-sized avatar
🏎️

Harshil Shah HarshilShah

🏎️
View GitHub Profile
@HarshilShah
HarshilShah / ShapeStyle+SmoothShadow.swift
Created October 15, 2024 07:52
A ShapeStyle extension to generate smooth shadows
import SwiftUI
extension ShapeStyle {
// Based on https://gist.github.com/sophiateutschler/54c0e6ca490fee658e303a63487dc382
func smoothShadow(
color: Color = Color(white: 0.0, opacity: 1.0 / 3.0),
layers: Int = 3,
curve: UnitCurve = .easeInOut,
radius baseRadius: CGFloat,
x baseX: CGFloat = 0.0,
@HarshilShah
HarshilShah / Task.swift
Created August 12, 2024 05:04
A SwiftUI Task modifier that gives you the current and previous ID values in the body
public extension View {
func task<T: Equatable>(
id: T,
priority: TaskPriority = .userInitiated,
@_inheritActorContext _ action: @Sendable @escaping (T, T) async -> Void
) -> some View {
self.modifier(TaskViewModifier(id: id, priority: priority, action: action))
}
}
@HarshilShah
HarshilShah / CustomMenuPicker.swift
Created December 15, 2023 08:20
Reimplementing SwiftUI's PickerStyle.menu to be able to use any custom ButtonStyle
import SwiftUI
public struct CustomMenuPicker<Item: Hashable, Content: View>: View {
var label: LocalizedStringKey
var items: [Item]
@Binding var selection: Item
var content: (Item) -> Content
public init(
_ label: LocalizedStringKey,
@HarshilShah
HarshilShah / EdgeInsets.swift
Created October 31, 2023 10:40
A SwiftUI modifier that allows you to apply only selected edges from some insets as padding
import SwiftUI
public extension EdgeInsets {
init(_ edges: Edge.Set, _ insets: EdgeInsets) {
func inset(for edge: Edge) -> CGFloat {
return edges.contains(Edge.Set(edge)) ? insets[edge] : 0
}
self.init(
top: inset(for: .top),
import SwiftUI
enum MarketingBump: String, CaseIterable, Hashable {
case none
case patch
case minor
case major
}
enum ASCGroup: String, CaseIterable, Hashable {
@HarshilShah
HarshilShah / Collection+Prefixes.swift
Created May 4, 2023 01:50
Collection extensions to get all possible prefixes in decreasing or increasing order of length
public extension Collection {
func contractingPrefixes() -> some Collection<SubSequence> {
self.indices.reversed().lazy.map { index in
self[startIndex ... index]
}
}
func expandingPrefixes() -> some Collection<SubSequence> {
self.indices.lazy.map { index in
self[startIndex ... index]
@HarshilShah
HarshilShah / CycledAnimationTimelineView.swift
Created April 29, 2023 05:17
A SwiftUI view that performs a looping animation in whole cycles. When the animation is disabled, it drives the current loop to completion, and pauses only then
struct CycledAnimationTimelineView<Content: View>: View {
var duration: Double
var isAnimating: Bool
@ViewBuilder var content: (Double) -> Content
@State private var isActuallyAnimating = false
@State private var startDate: Date?
var body: some View {
TimelineView(.animation(paused: !isActuallyAnimating)) { context in
@HarshilShah
HarshilShah / Squircle.swift
Last active April 28, 2023 04:05
A more concise way to create a continuously rounded rectangle in SwiftUI
import SwiftUI
public struct Squircle: InsettableShape {
// MARK: Properties
var cornerSize: CGSize
var inset = 0.0
// MARK: Initializers
@HarshilShah
HarshilShah / LinearGradient.swift
Created April 2, 2023 09:16
Convenient initialisers for SwiftUI's LinearGradient view
import SwiftUI
public extension UnitPoint {
var diametricallyOpposite: UnitPoint {
UnitPoint(x: 1 - x, y: 1 - y)
}
}
public extension LinearGradient {
init(colors: [Color], towards end: UnitPoint) {
@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