Skip to content

Instantly share code, notes, and snippets.

@humblehacker
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 {
PrefView()
PrefView()
}
}
@humblehacker
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
humblehacker / kotlin-inject-composable-viewmodel.md
Last active Mar 17, 2022
Injecting view models into Composable functions with kotlin-inject
View kotlin-inject-composable-viewmodel.md

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: