Skip to content

Instantly share code, notes, and snippets.

humblehacker /
Last active Mar 17, 2022
Injecting view models into Composable functions with kotlin-inject

According to the docs, the non-DI way to associate an view model with an @Composable is via the viewModel() function from androidx.lifecycle.viewmodel.compose.viewModel, like this:

fun ProfileScreen(val viewModel: ProfileViewModel = viewModel() {

But if your view model requires dependencies, you have to pass a subclass of ViewModelProvider.Factory that holds the dependency and knows how to construct the view model for you. Like this:

humblehacker / LaunchVSMac.applescript
Last active Jun 26, 2020
Given an absolute path, line number, and column number, launch Visual Studio for Mac and go there.
View LaunchVSMac.applescript
on run {filepath, linenumber, columnnumber}
tell application "Visual Studio" to activate
tell application "System Events"
-- ⌘O File → Open...
keystroke "o" using command down
delay 1
humblehacker / swiftui-prefs.swift
Created Jan 2, 2020
Demonstrates setting a preference based on an action
View swiftui-prefs.swift
import SwiftUI
struct ContentView: View {
var body: some View {
WrapperView {
VStack {
humblehacker / KeychainItem.swift
Last active Sep 28, 2019
Chris Eidhof's KeychainItem property wrapper made generic
View KeychainItem.swift
// KeychainItem.swift
// Created by David Whetstone on 9/28/19.
// Original code by Chris Eidhof
// As yet unlicensed
humblehacker /
Last active Jul 16, 2022
Don't expose MutableLiveData
humblehacker / Sequence_associateBy.swift
Created Jun 27, 2017
Swift 3 implementation of Kotlin's associateBy for transforming a Sequence to a Dictionary
View Sequence_associateBy.swift
extension Sequence
/// Returns a Dictionary using the result of `keySelector` as the key, and the result of `valueTransform` as the value
public func associateBy<T, K: Hashable, V>(_ keySelector: (T) -> K, _ valueTransform: (T) -> V) -> [K:V] where T == Iterator.Element
var dict: [K:V] = [:]
for element in self {
dict[keySelector(element)] = valueTransform(element)
View OptionalInterpolation.swift
// Based on
import Foundation
protocol Unwrappable
func unwrap() -> Any?
humblehacker / CVPixelBufferDeepCopy.swift
Last active Feb 13, 2022
Creates a deep copy of a CVPixelBuffer. Compatible with Swift 2.3.
View CVPixelBufferDeepCopy.swift
extension CVPixelBuffer
/// Deep copy a CVPixelBuffer:
func copy() -> CVPixelBuffer
precondition(CFGetTypeID(self) == CVPixelBufferGetTypeID(), "copy() cannot be called on a non-CVPixelBuffer")
var _copy: CVPixelBuffer?
humblehacker / wish.m
Created May 24, 2016
Variable declaration of concrete class and protocol conformance
View wish.m
UIViewController<SomeProtocol> *vc = ...;
/** I wish I could do this in Swift.
let vc: UIViewController, SomeProtocol = ...