Skip to content

Instantly share code, notes, and snippets.

View HarshilShah's full-sized avatar
🏎️

Harshil Shah HarshilShah

🏎️
View GitHub Profile
@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
@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 / 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) {