Skip to content

Instantly share code, notes, and snippets.

View belinwu's full-sized avatar

吴上阿吉 belinwu

View GitHub Profile
@aartikov
aartikov / DecomposeUtils.kt
Created December 14, 2022 09:27
Creates CoroutineScope for Decompose component
fun ComponentContext.componentCoroutineScope(): CoroutineScope {
val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
if (lifecycle.state != Lifecycle.State.DESTROYED) {
lifecycle.doOnDestroy {
scope.cancel()
}
} else {
scope.cancel()
}
@surajsau
surajsau / ParallaxScreen.kt
Last active June 28, 2024 21:14
Parallax effect with Jetpack Compose
@Composable
fun ParallaxScreen(modifier: Modifier = Modifier) {
val context = LocalContext.current
val scope = rememberCoroutineScope()
var data by remember { mutableStateOf<SensorData?>(null) }
DisposableEffect(Unit) {
val dataManager = SensorDataManager(context)
dataManager.init()
@burnoo
burnoo / Jetpack Compose two-way data binding.kt
Last active October 13, 2022 07:08
Jetpack Compose two-way data binding
private class MutableStateAdapter<T>(
private val state: State<T>,
private val mutate: (T) -> Unit
) : MutableState<T> {
override var value: T
get() = state.value
set(value) {
mutate(value)
}
@wasabeef
wasabeef / 0-how-to-use-maven-central-publish.sh
Last active July 7, 2021 04:59
maven-central-v1.gradle
./gradlew clean buildRelease publish
@jevakallio
jevakallio / readme.md
Last active November 1, 2024 00:01
`adb pull` from app data directory without root access

TL;DR;

com.package.name is your app identifier, and the file path is relative to the app user's home directory, e.g. '/data/user/0/com.package.name.

adb shell run-as com.package.name cp relative/path/file.ext /sdcard
adb pull /sdcard/file.ext

See long explanation below.

@makiftutuncu
makiftutuncu / KotlinVarianceTrick.kt
Created May 26, 2020 12:34
Trick to Use Covariant (out) Type in Contravariant (in) Position in Kotlin
sealed class Option<out A>(open val value: A?) {
fun <B> fold(ifNone: () -> B, ifSome: (A) -> B): B =
when (this) {
is None -> ifNone()
is Some -> ifSome(this.value)
}
// This doesn't compile because `out` type A is in `in` position
fun getOrElseThatWontWork(default: () -> A): A =
when (this) {
@karangoel16
karangoel16 / colorUtils.kt
Last active October 15, 2022 02:57
ColorWithTransparency
fun String.toTransparentColor(num: Int): String {
return "#" + when (num) {
100 -> "FF"
99 -> "FC"
98 -> "FA"
97 -> "F7"
96 -> "F5"
95 -> "F2"
94 -> "F0"
@wilik16
wilik16 / build.gradle
Last active November 8, 2022 12:52
Archive/Copy debug or release APK / AAB (Android App Bundle) file and/or mapping.txt to a versioned folder
android {
applicationVariants.all { variant ->
variant.outputs.all {
def fileName = "app"
switch (variant.buildType.name) {
case "debug":
fileName = "${appNameDebug}-${variant.versionCode}"
break
case "release":
fileName = "${appNameRelease}-${variant.versionCode}"
@fay59
fay59 / Quirks of C.md
Last active September 4, 2024 23:07
Quirks of C

Here's a list of mildly interesting things about the C language that I learned mostly by consuming Clang's ASTs. Although surprises are getting sparser, I might continue to update this document over time.

There are many more mildly interesting features of C++, but the language is literally known for being weird, whereas C is usually considered smaller and simpler, so this is (almost) only about C.

1. Combined type and variable/field declaration, inside a struct scope [https://godbolt.org/g/Rh94Go]

struct foo {
   struct bar {
 int x;
@JoseAlcerreca
JoseAlcerreca / EventObserver.kt
Created April 26, 2018 12:14
An Observer for Events, simplifying the pattern of checking if the Event's content has already been handled.
/**
* An [Observer] for [Event]s, simplifying the pattern of checking if the [Event]'s content has
* already been handled.
*
* [onEventUnhandledContent] is *only* called if the [Event]'s contents has not been handled.
*/
class EventObserver<T>(private val onEventUnhandledContent: (T) -> Unit) : Observer<Event<T>> {
override fun onChanged(event: Event<T>?) {
event?.getContentIfNotHandled()?.let { value ->
onEventUnhandledContent(value)