Skip to content

Instantly share code, notes, and snippets.

@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:

View boilerplate.swift
// Run any SwiftUI view as a Mac app.
import Cocoa
import SwiftUI
NSApplication.shared.run {
VStack {
Text("Hello, World")
.padding()
.background(Capsule().fill(Color.blue))
@fitomad
fitomad / KeyboardResponder.swift
Created Nov 22, 2019
Manage iOS keyboard presentation and dismiss with your SwiftUI views.
View KeyboardResponder.swift
//
// KeyboardResponder.swift
// MoveMAD
//
// Created by Adolfo Vera Blasco on 19/11/2019.
// Copyright © 2019 desappstre {eStudio}. All rights reserved.
//
import SwiftUI
@jamesmacfie
jamesmacfie / README.md
Created Oct 22, 2019
iTerm 2 - script to change theme depending on Mac OS dark mode
View README.md

How to use

In iTerm2, in the menu bar go to Scripts > Manage > New Python Script

Select Basic. Select Long-Running Daemon

Give the script a decent name (I chose auto_dark_mode.py)

Save and open the script in your editor of choice.

View LazyView.swift
struct LazyView<Content: View>: View {
let build: () -> Content
init(_ build: @autoclosure @escaping () -> Content) {
self.build = build
}
var body: Content {
build()
}
}
@simonliotier
simonliotier / SwiftUI+CustomFonts+DynamicType.swift
Last active Dec 11, 2019
Example of how to get Dynamic Type with custom fonts in SwiftUI
View SwiftUI+CustomFonts+DynamicType.swift
import SwiftUI
/// Example of how to get Dynamic Type with custom fonts in SwiftUI.
struct ContentView: View {
var body: some View {
VStack(spacing: 20) {
Text("A large title").customFont(.largeTitle) // "Optima-ExtraBlack", 28
Text("A body").customFont(.body) // "Kailasa", 16
Text("A caption").customFont(.caption2) // "IowanOldStyle-Italic", 11
}
@humblehacker
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
public
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)
}
@JonDouglas
JonDouglas / xamarinandroidbindings.md
Last active May 7, 2022
Xamarin Android Bindings Troubleshooting
View xamarinandroidbindings.md

Approaching a Xamarin.Android Bindings Case

1. Investigation

One of the best ways to investigate a problematic Xamarin.Android Binding is to first ensure you have the proper tooling available:

@gonzalezreal
gonzalezreal / ios-cell-registration-swift.md
Last active Feb 14, 2022
iOS Cell Registration & Reusing with Swift Protocol Extensions and Generics
View ios-cell-registration-swift.md

iOS Cell Registration & Reusing with Swift Protocol Extensions and Generics

A common task when developing iOS apps is to register custom cell subclasses for both UITableView and UICollectionView. Well, that is if you don’t use Storyboards, of course.

Both UITableView and UICollectionView offer a similar API to register custom cell classes:

public func registerClass(cellClass: AnyClass?, forCellWithReuseIdentifier identifier: String)
public func registerNib(nib: UINib?, forCellWithReuseIdentifier identifier: String)
@robb
robb / Example.m
Last active Jan 19, 2021
A macro to convert nullable references to nonnull references while triggering an assert if the expression is actually true. Think of this as unsafe unwrap for Objective-C.
View Example.m
NS_ASSUME_NONNULL_BEGIN
void Log(NSString *foo) {
NSLog(@"%@", foo);
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSDictionary *stuff = @{
@"a": @"Test"