Skip to content

Instantly share code, notes, and snippets.

View JH108's full-sized avatar
🕵️
Android, Kotlin, Jetpack Compose, Go, always learning!

Jesse Hill JH108

🕵️
Android, Kotlin, Jetpack Compose, Go, always learning!
View GitHub Profile
@JH108
JH108 / question.md
Created September 29, 2022 20:01
Compose Navigation Question

I’ve got a question on compose navigation. I know that navigation is supposed to be event based, user presses X button to go to Y screen, etc...

This is all well and good until I got to implementing more complex state with login status, whether they've onboarded, etc...

My first approach was to separate the main flows of the app into separate NavGraphs and then dynamically set the start destination of the navigation host based on state. So if the state showed the user was unauthenticated then the LoginGraph is set as the start destination, if they are authenticated but not finished onboarding then they get the OnboardingGraph.

This appeared to be working but what I hadn't realized is that when the app was first initialized the navigation host would touch at least two or three graphs before the state was fully hydrated.

I'm wondering how I would handle the scenario of needing to place the user on the right path based on some state without actually making the navgation based on state instead of events.

@JH108
JH108 / example.kt
Last active June 17, 2022 15:59
Exploring the plus and minus operators for immutable lists in Kotlin
// TLDR: If you use list - [element] then it will remove the first occurrance of that element.
// If you use list - listOf([element]) then it will remove all occrrances of that element.
// If you use list + [element] then it will append regardless if that element already matches one in the list.
fun main() {
var t = listOf<Boolean>()
t = t + true
println("t + true: $t") // t + true: [true]
t = t + false
println("t + false: $t") // t + false: [true, false]
@JH108
JH108 / Database.kt
Last active May 26, 2022 20:03
Orthogonality and DRY with Room Database
data class DB(val entities: Map<String, Entity>, val daos: List<Dao>)
typealias ForeignKey = Pair<String, String>
class PersonDao(val db: DB) {
fun getAll(): List<PersonEntity> {
val peopleTable = db.entities["people"]
return peopleTable ?: listOf()
}
@JH108
JH108 / AutoScroll.kt
Created December 4, 2021 01:32
AutoScroll TextField Compose
package com.me.app
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.shape.ZeroCornerSize
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.onFocusChanged
@JH108
JH108 / more-observables.kt
Last active September 29, 2021 18:12
Random Kotlin
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlin.properties.Delegates
fun separator(title: String?) {
println()
if (title != null) {
val truncatedTitle = if (title.length > 80) title.substring(0, 78) else title
val beforeAfter = (80 - truncatedTitle.length) / 2
@JH108
JH108 / ChipStack.kt
Created June 2, 2021 16:08
Chip Stack for Compose
@Composable
fun ChipStack(modifier: Modifier = Modifier) {
val size = 80.dp
val sizeModifier = Modifier.size(size)
val colors = listOf(Color.Green, Color.Yellow, Color.Cyan, Color.Magenta, Color.LightGray)
val width = (size / 2) * (colors.size + 1)
Box(modifier = modifier.size(width, size)
.graphicsLayer {
alpha = 0.99f // slight alpha to force compositing layer
},
@JH108
JH108 / Random.kt
Last active February 3, 2021 15:00
Random Kotlin Code
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.selects.*
import java.text.*
import java.util.*
// Define variables
val names = arrayOf("What", "Even", "is", "this🤯")
val longName = "Some random function name"
@JH108
JH108 / xorWithKey.js
Created July 14, 2020 13:31
XOR A String using a key
// This doesn't seem like it would be secure since whatever "key" was being stored on the device
// would have to have either been generated by the device or requested by the device
export const encryptXOR = (text, key) => {
const bytes = utf8.encode(text);
const output = [];
for (let i = 0; i < bytes.length; i++) {
output[i] = String.fromCharCode(
bytes[i].charCodeAt(0) ^ key[i % key.length].charCodeAt(0)
);
@JH108
JH108 / random.js
Created January 17, 2020 14:11
Random stuff that is valid JS
var a = null;
function b(c) { var d = ['abc']; return d.find(e => e === c)}
if (Boolean(a)) console.log('True'); // True
if (!Boolean(a)) console.log('True'); // nothing
b('h') // undefined
@JH108
JH108 / example.sh
Created January 16, 2020 13:14
Using Cloc to count lines of javascript in a project
cloc . --exclude-dir node_modules,android,ios,lib | grep Script | sed 's/\n/\n/g' | sed 's/.*\ //g'