Skip to content

Instantly share code, notes, and snippets.

View vibrazy's full-sized avatar

Daniel Hollis Tavares vibrazy

View GitHub Profile
@vibrazy
vibrazy / UIStoryboardSegue+DestinationController.swift
Last active April 15, 2016 07:57
Generic UIStoryboardSegue Extension for Loading destinationViewController
extension UIStoryboardSegue {
/*
Generic way of loading destinationViewController
Usage:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let controller = segue.destinationController("SegueName", type: CustomController.self) {
print(controller)
}
}
*/
@vibrazy
vibrazy / NSUserDefaults+Extensions.swift
Created April 17, 2016 15:18
NSUserDefaults Save Generic Values
extension NSUserDefaults {
static func getSavedValue<T>(key: String) -> T? {
return NSUserDefaults.standardUserDefaults().objectForKey(key) as? T
}
static func setSavedValue<T>(value: T, key: String) {
NSUserDefaults.standardUserDefaults().setObject(value as? AnyObject, forKey: key)
}
}
/*
@vibrazy
vibrazy / Optional+Extensions.swift
Created June 28, 2016 22:28
Convenience extension on Optionals to return true or false if the collection is not empty and has elements
var userPreferences: [String]? = [""]
/// Type safe method to check if collection is nil and if not if it has values
func nilOrEmptyCollection<Collection where Collection: CollectionType>(collection: Collection?) -> Bool {
guard let collection = collection else { return true }
return collection.isEmpty
}
/// Extension on Options with constraint to collection type
extension Optional where Wrapped: CollectionType {
//
// Created by Dan Tavares on 10/07/2020.
//
import SwiftUI
struct WindowKey: EnvironmentKey {
static let defaultValue: UIWindow? = {
guard
let firstScene = UIApplication.shared.connectedScenes.first,
@vibrazy
vibrazy / TilignTabView.swift
Created July 12, 2020 16:01
iOS 14, `ScrollViewReader` + `TabView`, rough around the edges but you get the idea.
//
// TilingTabView.swift
//
//
// Created by Dan Tavares on 12/07/2020.
//
import SwiftUI
class State: ObservableObject {
@vibrazy
vibrazy / ColorPickerStorageExample.swift
Last active January 28, 2023 18:19
SwiftUI `ColorPicker` load and save values to disk using `AppStorage`
//
// ContentView.swift
// Shared
//
// Created by Dan Tavares on 01/07/2022.
// More Info: https://nilcoalescing.com/blog/EncodeAndDecodeSwiftUIColor/
import SwiftUI
#if os(iOS)
@vibrazy
vibrazy / OptionSetSwiftUI.swift
Last active May 31, 2023 09:06
Using OptionSet to simplify your view state. Unify your states into 1
//
// Created by Daniel Tavares on 07/05/2021.
//
import SwiftUI
// MARK: - OptionsSet
// Blog post https://dev.to/vibrazy/easy-swiftui-view-bindings-using-optionset-and-sourcery-4edb
protocol OptionsBinding {}
@vibrazy
vibrazy / Toggle.swift
Created May 10, 2021 10:56
Toggled - Simpler way to deal with hardcoded ViewModifers values in SwiftUI
struct ToggledExampleView_Previews: PreviewProvider {
static var previews: some View {
DebuggingToggledExampleView()
}
}
// MARK: - Toggled Source
protocol ToggleInterface {
associatedtype ValueType
@vibrazy
vibrazy / PreviewDeviceExporter.swift
Created May 17, 2021 08:33
Generate strongly typed PreviewDevice for SwiftUI
//
// main.swift
// PreviewDeviceExporter
//
// Created by Dan Tavares on 13/05/2021.
// https://github.com/JohnSundell/Files
// https://github.com/JohnSundell/ShellOut.git
// Packages used
@vibrazy
vibrazy / AnimatedState
Created June 1, 2021 09:38
Making it easier to deal with animations in SwiftUI
@propertyWrapper
struct AnimatedState<Value> : DynamicProperty {
let storage: State<Value>
let animation: Animation?
init(
value: Value,
animation: Animation? = nil
) {
self.storage = State<Value>(initialValue: value)