Skip to content

Instantly share code, notes, and snippets.

View Uhucream's full-sized avatar
:octocat:

Takashi Uhucream

:octocat:
View GitHub Profile

202207041909 AppStorage extension for strongly typed keys

#appstorage #swiftui

The @AppStorage property wrapper for observable access to UserDefaults keys is stringified.

To get a strongly typed key access, I oriented myself on the @Environment(\.keyPath) wrapper and my experience with SwiftyUserDefaults and its subscript-based access.

// Use defaultValue from the key:
@AppStorage(\.showOnboarding) var showOnboarding
@lukebrandonfarrell
lukebrandonfarrell / ExifData.swift
Created June 8, 2022 16:10
A Swift class for extracting exif data from URL, UIImage or Data types 🔭
//
// ExifData.swift
// Qeepsake
//
// Created by Luke Farrell on 26/05/2022.
//
import Foundation
import ImageIO
@ztrehagem
ztrehagem / AttributedString.swift
Last active June 7, 2023 02:50
正規表現でStringやAttributedStringをマッチするやつ
import Foundation
extension AttributedStringProtocol {
func match(_ pattern: String, options: String.CompareOptions? = nil) -> Range<AttributedString.Index>? {
let options = options?.union(.regularExpression) ?? .regularExpression
return self.range(of: pattern, options: options)
}
func matchAll(_ pattern: String, options: String.CompareOptions? = nil) -> [Range<AttributedString.Index>] {
guard let range = match(pattern, options: options) else {
@wata
wata / EventStoreNotificationInfo.swift
Last active March 27, 2024 05:27
Parse EKEventStoreChanged notification user info
struct EventStoreNotificationInfo {
let changeType: Int
let calendarDataChanged: Bool
let remindersDataChanged: Bool
let changedObjectIDs: [NSObject]
let modifiedObjectIdentifiers: Set<String>? // Not included in iOS 16?
struct UserInfoKeys {
static let changeType = "EKEventStoreChangeTypeUserInfoKey"
static let calendarDataChanged = "EKEventStoreCalendarDataChangedUserInfoKey"
@globulus
globulus / HyperlinkText.swift
Created August 26, 2021 14:28
SwiftUI Text with NSAttributedString, HTML or Markdown with tappable Hyperlinks
// full recipe at https://swiftuirecipes.com/blog/hyperlinks-in-swiftui-text
import SwiftUI
import SwiftUIFlowLayout
import MarkdownKit
struct HyperlinkTest: View {
var body: some View {
VStack {
HyperlinkText(html: "To <b>learn more</b>, <i>please</i> feel free to visit <a href=\"https://swiftuirecipes.com\">SwiftUIRecipes</a> for details, or check the <code>source code</code> at <a href=\"https://github.com/globulus\">Github page</a>.")
HyperlinkText(markdown: "To **learn more**, *please* feel free visit [SwiftUIRecipes](https://swiftuirecipes.com) for details, or check the `source code` at [Github page](https://github.com/globulus).")
@IanKeen
IanKeen / FocusState.swift
Last active June 30, 2023 17:00
SwiftUI: FocusedState shim for < iOS15
import Combine
import SwiftUI
extension View {
public func focused<T>(file: StaticString = #file, _ state: FocusState<T>, equals value: T) -> some View {
modifier(FocusedModifier(state: state, id: value, file: file))
}
}
@propertyWrapper
@AviTsadok
AviTsadok / LazyVStack-reordering.swift
Created July 10, 2021 12:14
SwiftUI LazyVStack reordering
import SwiftUI
import UniformTypeIdentifiers
@available(iOS 13.0.0, *)
struct ContentView: View {
@State var items = ["1", "2", "3"]
@State var draggedItem : String?
var body: some View {
@globulus
globulus / HTMLText.swift
Created May 27, 2021 07:08
SwiftUI Text with HTML via NSAttributedString
// full recipe at https://swiftuirecipes.com/blog/swiftui-text-with-html-via-nsattributedstring
extension Text {
init(html htmlString: String,
raw: Bool = false,
size: CGFloat? = nil,
fontFamily: String = "-apple-system") {
let fullHTML: String
if raw {
fullHTML = htmlString
} else {
@kshivang
kshivang / CombineFirebaseAuth+Publisher.swift
Last active November 18, 2023 19:51
Switch/Refresh publisher on Firebase Auth new login simple one-liner for rever-ai/CombineFirebase
func onLoginRefresher<T>(_ publisherGetter: @escaping () -> AnyPublisher<T, Never> ) -> AnyPublisher<T, Never> {
Auth.auth().stateDidChangePublisher
.filter { $0 != nil } // User LoggedIn
.map { _ in publisherGetter() }
.switchToLatest()
.eraseToAnyPublisher()
}
// e.g.: turn userDataPublisher to onLoginRefresher { userDataPublisher() } and done
//