Skip to content

Instantly share code, notes, and snippets.

View seifscape's full-sized avatar
:octocat:

Seif Kobrosly seifscape

:octocat:
View GitHub Profile
//
// Persisting.swift
//
// Created by James Sedlacek on 1/30/25.
//
import SwiftUI
/// A property wrapper that manages the persistence of Codable types using UserDefaults.
///
@JamesSedlacek
JamesSedlacek / View+OpenUrl.swift
Last active February 14, 2025 11:13
This file provides a safe way to open URLs in SwiftUI applications.
//
// View+OpenUrl.swift
//
// Created by James Sedlacek on 11/26/24.
//
/// This file provides a safe way to open URLs in SwiftUI applications.
/// It adds a view modifier that handles URL opening with user confirmation
/// and multiple opening options (browser, in-app, or copy to clipboard).
///
import Cocoa
final class AppDelegate: NSObject, NSApplicationDelegate {
private var statusItem: NSStatusItem!
func applicationDidFinishLaunching(_ aNotification: Notification) {
statusItem = NSStatusBar.system.statusItem(withLength: 450)
let contentView = ContentView()
contentView.translatesAutoresizingMaskIntoConstraints = false
if let button = statusItem.button {
@lucaswkuipers
lucaswkuipers / ProfileView.swift
Last active February 26, 2024 10:58
LinkedIn Profile View in SwiftUI
import SwiftUI
struct CustomButton {
let title: String
let url: URL
}
struct Profile {
let name: String
let headline: String
@lucaswkuipers
lucaswkuipers / ArrayCountPerformanceTests.swift
Created December 26, 2023 06:13
Swift Array<T> count performance test comparison
import XCTest
// # Setup
// Big array size: 1_000_000_000 (1 billion elements of 12345)
// Small array size: 2 [12345, 12345]
// Loop: 1_000_000 (1 million) times.
// Machine: Macbook Pro 2023 16" M2 Max 32gb
// # Results
// TLDR: Stored: 3% faster
import SwiftUI
private struct OnFirstAppear: ViewModifier {
let perform: () -> Void
@State private var firstTime = true
func body(content: Content) -> some View {
content.onAppear {
if firstTime {
@lucaswkuipers
lucaswkuipers / View+onLoad.swift
Last active November 29, 2023 17:45
View+onLoad
import SwiftUI
private struct OnLoad: ViewModifier {
let action: () -> Void
@State private var loaded = false
func body(content: Content) -> some View {
content.onAppear {
if !loaded {
loaded = true
extension StringProtocol {
subscript(_ offset: Int) -> String.Element {
if offset >= 0 {
self[index(startIndex, offsetBy: offset)]
} else {
self[index(endIndex, offsetBy: offset)]
}
}
@tfield
tfield / sdkman-vs-jenv.md
Last active March 25, 2025 01:36
SDKMAN vs JENV

sdkman vs jenv

Jenv is great, but the JDKs have to be manually installed. If you need more than just a couple of them, you'll want to be able to install them via command line, and sdkman provides this capability very cleanly for java. So... is it possible to work with both sdkman and jenv? Yes and no. Some tricks will allow you to tell jenv about the java versions that are managed by sdkman, but setting defaults (using jenv local 17.0 for instance) does not work very well. Time for a cleaner solution. Sdkman provides all of the same functionality, with more power and a slightly different (but equally simple) syntax. I've switched from jenv to sdkman. Here's my cheat sheet.

Setup

Download and install sdkman. Follow the required instructions after installing. Then, run the config command to enable auto_env support.

echo “Installing SDKMAN"
curl -s https://get.sdkman.io | bash
import SwiftUI
private struct OnFirstAppear: ViewModifier {
let perform: () -> Void
@State private var firstTime = true
func body(content: Content) -> some View {
content.onAppear {
if firstTime {