Skip to content

Instantly share code, notes, and snippets.

View rudrankriyam's full-sized avatar
🎵
Living.

Rudrank Riyam rudrankriyam

🎵
Living.
View GitHub Profile
@tshortli
tshortli / healthkit-async-await-workouts.swift
Last active January 31, 2022 08:30
A Swift async function that gets the most recent workouts from HealthKit
func mostRecentWorkouts(for healthStore: HKHealthStore, limit: Int) async throws -> [HKWorkout] {
let query = HKSampleQueryDescriptor(
predicates: [.workout()],
sortDescriptors: [SortDescriptor(\.endDate, order: .reverse)],
limit: limit
)
return try await query.result(for: healthStore)
}
@d4rkd3v1l
d4rkd3v1l / ExpansionHandler.swift
Last active January 31, 2024 02:24
SwiftUI: DisclosureGroup "expansion handler" to only have one group expanded at a time, and automatically hiding the last one, when expanding a new one.
class ExpansionHandler<T: Equatable>: ObservableObject {
@Published private (set) var expandedItem: T?
func isExpanded(_ item: T) -> Binding<Bool> {
return Binding(
get: { item == self.expandedItem },
set: { self.expandedItem = $0 == true ? item : nil }
)
}
@Priva28
Priva28 / GradientEffect.swift
Last active December 1, 2023 04:43
Gradient effect to emulate Apple Music Lyrics/Now Playing screen.
//
// ContentView.swift
// GradientEffect
//
// Created by Christian Privitelli on 18/7/20.
//
import SwiftUI
struct ContentView: View {
@SatoTakeshiX
SatoTakeshiX / convert.swift
Last active June 6, 2024 12:25
Take capture a view by SwiftUI
//
// ContentView.swift
// TryGeometryReader
//
// Created by satoutakeshi on 2019/12/07.
// Copyright © 2019 satoutakeshi. Licensed under MIT.
//
import SwiftUI
@surakamy
surakamy / swift_alert_in_enum.swift
Last active July 17, 2021 20:06
SwiftUI Alert example using enum
import SwiftUI
enum Message {
/// A message and OK button
case information(body: String)
/// A message and OK button
case warning(body: String)
/// A question with YES and NO buttons
case confirmation(body: String, action: () -> Void)
/// A question about destractive action with `action` and CANCEL buttons
@wilsoncusack
wilsoncusack / ReusableExample.swift
Last active June 30, 2023 23:24
SwiftUI Reusable View Using Generic Types
struct HorizontalList<Item, Card, Detail>: View where Item: NSManagedObject, Card: View, Detail: View{
var items: FetchedResults<Item>
var card: (Item) -> Card
var detail: (Item) -> Detail
// init(items: FetchedResults<Item>, @ViewBuilder card: @escaping (Item) -> Card, @ViewBuilder detail: @escaping (Item) -> Detail) {
// self.items = items
// self.card = card
// self.detail = detail
// }
@Amzd
Amzd / UIKitTabView.swift
Last active September 29, 2024 12:32
UIKitTabView. SwiftUI tab bar view that respects navigation stacks when tabs are switched (unlike the TabView implementation)
/// An iOS style TabView that doesn't reset it's childrens navigation stacks when tabs are switched.
public struct UIKitTabView: View {
private var viewControllers: [UIHostingController<AnyView>]
private var selectedIndex: Binding<Int>?
@State private var fallbackSelectedIndex: Int = 0
public init(selectedIndex: Binding<Int>? = nil, @TabBuilder _ views: () -> [Tab]) {
self.viewControllers = views().map {
let host = UIHostingController(rootView: $0.view)
host.tabBarItem = $0.barItem
@unnamedd
unnamedd / MacEditorTextView.swift
Last active May 26, 2024 17:49
[SwiftUI] MacEditorTextView - A simple and small NSTextView wrapped by SwiftUI.
/**
* MacEditorTextView
* Copyright (c) Thiago Holanda 2020-2021
* https://twitter.com/tholanda
*
* MIT license
*/
import Combine
import SwiftUI
@ulitiy
ulitiy / ContentView.swift
Last active October 3, 2022 01:56
Use PencilKit PKCanvasView in SwiftUI example
import SwiftUI
struct ContentView : View {
var body: some View {
PKCanvasRepresentation()
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
@simonliotier
simonliotier / SwiftUI+CustomFonts+DynamicType.swift
Last active December 11, 2019 14:27
Example of how to get Dynamic Type with custom fonts in SwiftUI
import SwiftUI
/// Example of how to get Dynamic Type with custom fonts in SwiftUI.
struct ContentView: View {
var body: some View {
VStack(spacing: 20) {
Text("A large title").customFont(.largeTitle) // "Optima-ExtraBlack", 28
Text("A body").customFont(.body) // "Kailasa", 16
Text("A caption").customFont(.caption2) // "IowanOldStyle-Italic", 11
}