Skip to content

Instantly share code, notes, and snippets.

View disc0infern0's full-sized avatar

Disco disc0infern0

View GitHub Profile
@disc0infern0
disc0infern0 / login.swift
Created October 30, 2021 10:29
Simple Login form to demonstrate change of focus in SwiftUI
struct Login: View {
@State private var username = ""
@State private var password = ""
enum Field: Hashable {
case username
case password
}
@FocusState private var focusedField: Field?
var body: some View {
@disc0infern0
disc0infern0 / sync.swift
Created October 30, 2021 10:41
View extension to mirror focus state between a view and view model
extension View {
/// Mirror changes between an @Published variable (typically in your View Model) and an @FocusedState variable in a view
func sync<T: Equatable>(_ field1: Binding<T>, _ field2: FocusState<T>.Binding ) -> some View {
self
.onChange(of: field1.wrappedValue) { field2.wrappedValue = $0 }
.onChange(of: field2.wrappedValue) { field1.wrappedValue = $0 }
}
}
@disc0infern0
disc0infern0 / LoginVM.swift
Created October 30, 2021 10:45
Login form demonstrating change of focus using a view model
class LoginViewModel: ObservableObject {
@Published var username = ""
@Published var password = ""
@Published var focusedField: Field?
enum Field: Hashable {
case username
case password
}
@disc0infern0
disc0infern0 / TaskList.swift
Last active October 30, 2021 13:08
List and FocusState - non functioning
struct Task: Identifiable, Codable {
var id = UUID().uuidString
var text: String = ""
static var examples = [ Task(text: "one"), Task(text: "two")]
}
struct Tasks: View {
enum Field : Hashable { case text }
@State private var tasks = Task.examples
@FocusState private var focusedField: Field?
@disc0infern0
disc0infern0 / TaskListFocus.swift
Created October 30, 2021 13:49
Working example demonstrating how to control focus to individual rows in a List #SwiftUI
import SwiftUI
struct TaskList: View {
@StateObject var vm = TaskListVM()
@FocusState private var focusedField: RowID?
var body: some View {
List() {
ForEach($vm.tasks) { $task in
TaskRow(task: $task)
.focused($focusedField, equals: .row(id: task.id))
@disc0infern0
disc0infern0 / ReminderList
Created November 4, 2021 12:17
a Reminder List with control of focus, adding new values with double click, auto deleting nil values, using Combine for delay
//
// ReminderList.swift
//
// Demonstrates:-
// list settings (style, colouring etc )
// autoscrolling
// control of focus in List,
// adding new values with enter, or double click,
// auto deleting nil values,
// using Combine for managed delay/UI updates
@disc0infern0
disc0infern0 / EasyFocusExample.swift
Created November 21, 2021 14:43
EasyFocus Example: Making it easier to set/get focus in your Lists.
//
// Easy Focus Example
//
// The package allows the code below to focus (🙂) on the data
// that is changing, and not on underlying id records as is required
// when using @FocusState.
// Code differences when using this package are highlighted
// below with /*👉*/
//
// Created by Andrew Cowley on 21/11/2021.
@disc0infern0
disc0infern0 / picker.swift
Last active December 4, 2021 19:27
Drill down picker for Caseiterable & RawRepresentable collections
// Created by Andrew Cowley on 01/12/2021.
//
import SwiftUI
enum Priority: String, Pickable {
case none, low, medium, high // any raw type can be used
}
struct ContentView: View {
@State var priority: Priority = .none
@disc0infern0
disc0infern0 / SSaver.swift
Created June 13, 2022 18:08
Screen Saver in SwiftUI - simple colour rotation
//
import SwiftUI
import ScreenSaver
struct Defaults {
/// Use bundleID for the Module name. It is found in the project properties, of target, General Tab.
let storage = ScreenSaverDefaults(forModuleWithName: "com.ajc.SSaver")!
let refreshRateKey = "refreshRate"
var refreshRate: Double { storage.double(forKey: refreshRateKey) }