Skip to content

Instantly share code, notes, and snippets.

View marlonjames71's full-sized avatar
💻
Learning Swift & SwiftUI

Marlon Raskin marlonjames71

💻
Learning Swift & SwiftUI
View GitHub Profile
import UIKit
import SwiftUI
// Hacky workaround, use at your own risk and all that
struct BottomSheetPresenter<Content>: UIViewRepresentable where Content: View{
let label: String
let content: Content
let detents: [UISheetPresentationController.Detent]
init(_ label: String, detents: [UISheetPresentationController.Detent], @ViewBuilder content: () -> Content) {
// A URLSession extension that fetches data from a URL and decodes to some Decodable type.
// Usage: let user = try await URLSession.shared.decode(UserData.self, from: someURL)
// Note: this requires Swift 5.5.
extension URLSession {
func decode<T: Decodable>(
_ type: T.Type = T.self,
from url: URL,
keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys,
dataDecodingStrategy: JSONDecoder.DataDecodingStrategy = .deferredToData,
dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate
@cassidoo
cassidoo / Igneous.css
Last active December 1, 2021 21:20
An Obsidian theme
/*
IMPORTANT: Change the fonts on lines 31-35 to make this work the way you want!
*/
:root {
--dark0: #2e3440;
--dark1: #3b4252;
--dark2: #434c5e;
@markmals
markmals / Wiggle.swift
Last active October 13, 2025 14:21
The iOS Home Screen wiggle animation, in SwiftUI
import SwiftUI
extension View {
func wiggling() -> some View {
modifier(WiggleModifier())
}
}
struct WiggleModifier: ViewModifier {
@State private var isWiggling = false
@JensAyton
JensAyton / Tristate.swift
Created February 25, 2021 09:02
A very useful type
enum Tristate: ExpressibleByNilLiteral, ExpressibleByBooleanLiteral {
case maybe
case no
case yes
init(nilLiteral: Void) {
self = .maybe
}
init(booleanLiteral value: Bool) {
@dabodamjan
dabodamjan / MailComposeViewController.swift
Last active April 24, 2024 07:31
Sending a contact us email on iOS 14 (can be also used with SwiftUI)
//
// Feel free to use this code in your project.
// Inspired by SO answer https://stackoverflow.com/a/65743126
// Uses DeviceKit as a dependency https://github.com/devicekit/DeviceKit
//
import Foundation
import MessageUI
import DeviceKit
@V8tr
V8tr / AutoLayoutDSL.swift
Last active March 23, 2025 19:10
Auto Layout DSL
import UIKit
/// Represents a single `NSLayoutConstraint`
enum LayoutAnchor {
case constant(attribute: NSLayoutConstraint.Attribute,
relation: NSLayoutConstraint.Relation,
constant: CGFloat)
case relative(attribute: NSLayoutConstraint.Attribute,
relation: NSLayoutConstraint.Relation,
@pennisi
pennisi / HUDView.swift
Created December 8, 2020 15:29
SwiftUI success animation/HUD
//To configure the contents of the HUD
struct HUDConfig {
var text: String
var icon: Image
var autohide = true
static func success(_ s: String) -> HUDConfig {
return HUDConfig(text: s, icon: Image(systemName: "checkmark.circle.fill"))
}
}
@davidsteppenbeck
davidsteppenbeck / PreviewProviderModifier.swift
Last active October 31, 2022 10:30
A SwiftUI view modifier for simple preview providers.
import SwiftUI
enum PreviewProviderMode: CaseIterable {
/// Use for a light appearance preview.
case lightMode
/// Use for a dark appearance preview.
case darkMode
/// iOS 13 version here: https://gist.github.com/aheze/bd4e1990dbeda66e60c00c6341451930
/// Note: iOS 13 version has a "bug" where adding a second finger freezes the touch event
/// More details here: https://developer.apple.com/forums/thread/660070
/// Thanks @brentbrinkley for discovering the bug!
/// if you're on iPad Swift Playgrounds and you put all of this code in a seperate file,
/// you need to make everything public so that the compiler detects it.
/// the possible states of the button
public enum ButtonState {