Skip to content

Instantly share code, notes, and snippets.

@humblehacker
humblehacker / swiftui-prefs.swift
Created January 2, 2020 21:42
Demonstrates setting a preference based on an action
import SwiftUI
struct ContentView: View {
var body: some View {
WrapperView {
VStack {
PrefView()
PrefView()
}
}
@humblehacker
humblehacker / LaunchVSMac.applescript
Last active June 26, 2020 00:11
Given an absolute path, line number, and column number, launch Visual Studio for Mac and go there.
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
humblehacker / kotlin-inject-composable-viewmodel.md
Last active March 17, 2022 13:56
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:

@Composable
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
humblehacker / Kotlin generic type constraints are ignored when the inferred type is an interface.md
Last active December 18, 2022 17:37
Kotlin generic type constraints are ignored when the inferred type is an interface.md

tl;dr: I expect the following code to fail to compile as NotABlob fails the Blob type constraint:

open class Blob
interface NotABlob

fun <T: Blob> foo(): T {
   TODO()
}
@humblehacker
humblehacker / make-opencv2-xcframework.md
Last active January 22, 2024 07:07
Make opencv2.xcframework for iOS arm64, and iOSSimulator arm64 & x86_64

I had some trouble attempting to build an XCFramework of OpenCV, but I finally got a successful build with the following steps:

  1. Clone the OpenCV repo:
    git clone git@github.com:opencv/opencv.git
    cd opencv
    
  2. Disable building with libjpeg-turbo[^1] by applying a patch:
    git apply path/to/opencv-5-28-50.patch
@humblehacker
humblehacker / next-karabiner-profile.sh
Created January 14, 2024 18:05
Choose next Karabiner profile
#!/usr/bin/env zsh
alias kb="/Library/Application\ Support/org.pqrs/Karabiner-Elements/bin/karabiner_cli"
# Get the list of items
IFS=$'\n' items=($(kb --list-profile-names))
# Get the current item
current_item=$(kb --show-current-profile-name)