Skip to content

Instantly share code, notes, and snippets.

View timothycosta's full-sized avatar

Timothy Costa timothycosta

View GitHub Profile
@timothycosta
timothycosta / SwiftUIKeyboardAnimation.swift
Last active March 12, 2024 21:10
Create a SwiftUI Animation with the correct curve and duration from UIKit keyboard notifications
func animation(from notification: Notification) -> Animation? {
guard
let info = notification.userInfo,
let duration = info[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double,
let curveValue = info[UIResponder.keyboardAnimationCurveUserInfoKey] as? Int,
let uiKitCurve = UIView.AnimationCurve(rawValue: curveValue)
else {
return nil
}
@timothycosta
timothycosta / TrackedValue.swift
Created April 10, 2020 04:43
Replacement for didSet when using SwiftUI
/// TrackedValue is a property wrapper that allows you to work around the
/// lack of didSet when working with SwiftUI.
/// Explanation and example usage at: https://stackoverflow.com/a/60145397/467209
@propertyWrapper
struct TrackedValue<Tracked, Value>: DynamicProperty {
var trackedHolder: State<ValueHolder<Tracked>>
var valueHolder: State<ValueHolder<Value>>
init(wrappedValue value: Value, tracked: Tracked) {
self.trackedHolder = State(initialValue: ValueHolder(tracked))
@timothycosta
timothycosta / ViewPlayground.swift
Created August 9, 2019 08:33
Resetting @State when other state changes
struct StateUpdatedPreferenceKey: PreferenceKey {
static var defaultValue: Int = Int.min
static func reduce(value: inout Int, nextValue: () -> Int) {
value = nextValue()
}
}
struct StateUpdated: View {
@timothycosta
timothycosta / GeometryFinder.swift
Created July 11, 2019 03:41
Find the geometry of a SwiftUI View
//
// GeometryFinder.swift
//
// Created by Timothy Costa on 2019/06/24.
// Copyright © 2019 timothycosta.com. All rights reserved.
//
import SwiftUI
struct RectPreferenceKey: PreferenceKey {
@timothycosta
timothycosta / CADisplayLinkBinding.swift
Last active April 3, 2020 00:43
A CADisplayLink in a BindableObject
//
// CADisplayLinkBinding.swift
// lingq-5
//
// Created by Timothy Costa on 2019/07/11.
// Copyright © 2019 timothycosta.com. All rights reserved.
//
import Combine
import SwiftUI
@timothycosta
timothycosta / UIScrollViewWrapper.swift
Created July 5, 2019 03:25
UIScrollView wrapped for SwiftUI
//
// UIScrollViewWrapper.swift
// lingq-5
//
// Created by Timothy Costa on 2019/07/05.
// Copyright © 2019 timothycosta.com. All rights reserved.
//
import SwiftUI
@timothycosta
timothycosta / UIViewController+SwiftUI.swift
Last active April 28, 2023 07:00
Using UIViewController via the SwiftUI Environment
struct ViewControllerHolder {
weak var value: UIViewController?
init(_ value: UIViewController?) {
self.value = value
}
}
struct ViewControllerKey: EnvironmentKey {
static var defaultValue: ViewControllerHolder { return ViewControllerHolder(UIApplication.shared.windows.first?.rootViewController ) }
@timothycosta
timothycosta / UIViewWrapper.swift
Last active February 18, 2020 21:08
Get the position of a child View by wrapping it in a UIView
//
// UIViewWrapper.swift
// lingq-5
//
// Created by Timothy Costa on 2019/06/14.
// Copyright © 2019 timothycosta.com. All rights reserved.
//
import SwiftUI
import UIKit
@timothycosta
timothycosta / gist:112f2eb66a2539f2d7b583f83a711edb
Created January 2, 2017 05:20
Object has been deleted or invalidated
Fatal Exception: RLMException
0 CoreFoundation 0x1841badb0 __exceptionPreprocess
1 libobjc.A.dylib 0x18381ff80 objc_exception_throw
2 Realm 0x100b6e300 RLMGetArray(RLMObjectBase*, unsigned long) (RLMAccessor.mm:212)
3 LingQ 0x1000bdaf0 -[GCCellFactory configureCell:forLesson:locked:feed:] (GCCellFactory.m:173)
4 LingQ 0x1000f0188 -[LQTableViewController configureCell:section:row:] (LQTableViewController.m:353)
5 LingQ 0x1000effec -[LQTableViewController configuredCellForTableView:row:indexPath:] (LQTableViewController.m:342)
6 LingQ 0x1000f086c -[LQTableViewController tableView:cellForRowAtIndexPath:] (LQTableViewController.m:421)
7 UIKit 0x18966f030 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:]
8 UIKit 0x18966f198 -[UITableView _createPreparedCellForGlobalRow:willDispla
@timothycosta
timothycosta / gist:856afecaf4fef5dc74f323e98e8892c3
Created January 2, 2017 05:18
Object has been deleted or invalidated.
Fatal Exception: RLMException
0 CoreFoundation 0x1821821c0 __exceptionPreprocess
1 libobjc.A.dylib 0x180bbc55c objc_exception_throw
2 Realm 0x1009f6954 ___ZL17RLMAccessorGetterP11RLMProperty15RLMAccessorCode_block_invoke_2 (RLMAccessor.mm:308)
3 iLingQ 0x1001b149c __52-[FeedSearchViewController importDetailsForLessons:]_block_invoke (FeedSearchViewController.m:241)
4 Objective_Shorthand 0x1009aa198 __67-[NSArray(FunctionalMethods) arrayByTransformingObjectsUsingBlock:]_block_invoke (NSArray+FunctionalMethods.m:34)
5 CoreFoundation 0x18206f1a4 __53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke
6 CoreFoundation 0x18206f010 -[__NSArrayM enumerateObjectsWithOptions:usingBlock:]
7 Objective_Shorthand 0x1009aa134 -[NSArray(FunctionalMethods) arrayByTransformingObjectsUsingBlock:] (NSArray+FunctionalMethods.m:40)
8 iLingQ 0x1001b12c