Skip to content

Instantly share code, notes, and snippets.

@gustavkarlsson
gustavkarlsson / Aurora.kt
Created April 12, 2021 07:57
Aurora Borealis effect made in compose for Android
import androidx.annotation.FloatRange
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
@Preview
@Composable
private fun ThemeSwapDemo() {
var dark by remember { mutableStateOf(false) }
Crossfade(targetState = dark) { isDark ->
MyMaterialTheme(darkTheme = isDark) {
Surface(color = MaterialTheme.myColors.background) {
Box(Modifier.fillMaxSize(), Alignment.Center) {
Button(
onClick = { dark = !dark },
val MaterialTheme.myColors: MyColors
@Composable
@ReadOnlyComposable
get() = LocalColors.current
private val LocalColors = staticCompositionLocalOf { LightColorPalette }
@Composable
fun MyMaterialTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
content: @Composable () -> Unit,
) {
val colors = if (darkTheme) {
DarkColorPalette
} else {
data class MyColors(
val material: Colors,
val warning: Color,
val onWarning: Color,
) {
val primary: Color get() = material.primary
val primaryVariant: Color get() = material.primaryVariant
val secondary: Color get() = material.secondary
val secondaryVariant: Color get() = material.secondaryVariant
val background: Color get() = material.background
@gustavkarlsson
gustavkarlsson / AndroidViewModelBinding.kt
Last active January 22, 2018 16:07
Android ViewModel binding
class MyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
/* ... */
bindData(MyViewModel())
}
private fun bindData(viewModel: MyViewModel) {
// From views
firstName.textChanges()
@gustavkarlsson
gustavkarlsson / .bash_prompt
Last active December 25, 2015 23:09
My git powered bash prompt. Shows current working directory, time, the prompt symbol and the current git branch The color the git branch is displayed in depends on the state of the repository.
# Shows current working directory, time, the prompt symbol and the current git branch
# The color the git branch is displayed in depends on the state of the repository:
#
# Red = has unstaged changes
# Yellow = index matches working tree but is uncommitted
# Blue = has untracked files
# Green = repository is clean
# Purple = has unmerged changes
#
#