Skip to content

Instantly share code, notes, and snippets.

View codeactual's full-sized avatar

David Smith codeactual

  • Found Apparatus
  • Portland, OR
View GitHub Profile
@codeactual
codeactual / PomodoroPicker.swift
Created January 11, 2022 14:11 — forked from dmr121/PomodoroPicker.swift
SwiftUI - Snapping horizontal scrolling pomodoro picker
//
// PomodoroPicker.swift
// pomodoro
//
// Created by David Rozmajzl on 1/1/22.
//
import SwiftUI
struct PomodoroPicker<Content, Item: Hashable>: View where Content: View {
@codeactual
codeactual / StreamReader.swift
Created January 4, 2022 19:11 — forked from klgraham/StreamReader.swift
A Swift class to read a text file line by line, without loading the entire file into memory
// Usage:
/*
if let aStreamReader = StreamReader(path: "/path/to/file") {
defer {
aStreamReader.close()
}
while let line = aStreamReader.nextLine() {
print(line)
}
}
@codeactual
codeactual / CoreDataViewModelArchitecture.swift
Created December 19, 2021 18:40 — forked from ramzesenok/CoreDataViewModelArchitecture.swift
My attempt to build a reusable ViewModel that uses CoreData as its persistence store
import Combine
import SwiftUI
import CoreData
class CoreDataViewModel: NSObject, ObservableObject, NSFetchedResultsControllerDelegate {
let context: NSManagedObjectContext
private var controllerUpdates = [NSObject: CurrentValueSubject<[NSFetchRequestResult], Never>]()
init(context: NSManagedObjectContext) {
self.context = context
import SwiftUI
struct ContentView: View {
@State var horizontal: Bool = true
@Namespace var namespace
var body: some View {
VStack(spacing: 40) {
if horizontal {
HStack { items }
@codeactual
codeactual / PreviewDevice+Devices.swift
Created November 28, 2021 13:07 — forked from BrentMifsud/PreviewDevice+Devices.swift
Extension on PreviewDevice that includes all available devices
import SwiftUI
/// Static properties for all preview devices.
///
/// Usage:
///
/// ```swift
/// struct TestView_Previews: PreviewProvider {
/// static var previews: some View {
/// Group {
@codeactual
codeactual / HasRootNavigationController.swift
Created November 20, 2021 16:13 — forked from michaelhenry/HasRootNavigationController.swift
UINavigationController in swiftUI.
import SwiftUI
import UIKit
protocol HasRootNavigationController {
var rootVC:UINavigationController? { get }
func push<Content:View>(view: Content, animated:Bool)
func setRootNavigation<Content:View>(views:[Content], animated:Bool)
func pop(animated: Bool)
func popToRoot(animated: Bool)
@codeactual
codeactual / PHImageManager-requestImage-async.swift
Created November 3, 2021 16:01 — forked from sindresorhus/PHImageManager-requestImage-async.swift
How to use `PHImageManager#requestImage` with async/await in Swift.
import Photos
struct UnexpectedNilError: Error {}
extension PHImageManager {
func requestImage(
for asset: PHAsset,
targetSize: CGSize,
contentMode: PHImageContentMode,
options: PHImageRequestOptions?
@codeactual
codeactual / WithPopover.swift
Created October 25, 2021 18:02 — forked from wassupdoc/WithPopover.swift
Custom size popover in iOS SwiftUI
// -- Usage
struct Content: View {
@State var open = false
@State var popoverSize = CGSize(width: 300, height: 300)
var body: some View {
WithPopover(
showPopover: $open,
popoverSize: popoverSize,
// Swift's untyped errors are a goddam PiTA. Here's the pattern I use to try to work around this.
// The goal is basically to try to guarantee that every throwing function in the app throws an
// ApplicationError instead of some unknown error type. We can't actually enforce this statically
// But by following this convention we can simplify error handling
enum ApplicationError: Error, CustomStringConvertible {
// These are application-specific errors that may need special treatment
case specificError1
case specificError2(SomeType)
//
// ContentView.swift
// SwiftUIDemo
//
// Created by Damir Stuhec on 07/10/2021.
//
import SwiftUI
// MARK: - CustomButtonStyle