Skip to content

Instantly share code, notes, and snippets.

View jenzz's full-sized avatar

Jens Driller jenzz

View GitHub Profile
@wesalvaro
wesalvaro / SavingFlow.kt
Last active June 1, 2022 15:00
Creating a `MutableStateFlow` from a Jetpack `ViewModel`'s `SavedStateHandle`.
import androidx.lifecycle.SavedStateHandle
import kotlinx.coroutines.flow.MutableStateFlow
private class SavingFlow<T> private constructor(
private val save: (T) -> Unit,
private val msf: MutableStateFlow<T>
) :
MutableStateFlow<T> by msf {
constructor(
@objcode
objcode / Permissions.kt
Last active February 21, 2023 23:01
Quick demo of compose permissions using activity result API. Uses Flow instead of State to make it reusable outside of Compose.
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@objcode
objcode / ConcurrencyHelpers.kt
Last active January 15, 2024 05:17
Helpers to control concurrency for one shot requests using Kotlin coroutines.
/* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@drd
drd / a-tangled-miasma.md
Last active April 15, 2019 07:57
A tangled miasma of hacks that somehow manage to launch stetho

So I got annoyed at always having to reconnect to Stetho, and I figured there would be some better way to do this in Android Studio ... and maybe there is, but this is the best I've gotten so far. Putting this together in a gist was inspired by a question on reddit: https://www.reddit.com/r/androiddev/comments/7hz3xy/stetho_anyone_know_of_a_convenient_way_to/

How does this work?

  • Use Android Studio custom Run configuration to call a new gradle task :app:launchStetho before the app launches
  • The gradle task uses launchctl (docs) to start an out-of-band AppleScript automation open-stetho.applescript
  • The AppleScript waits a few seconds, then controls Chrome to open chrome://inspect and loops until the "Inspect" action is available, and then executes a click() against that DOM element

Setup

  1. Choose a location for the applescript and ensure that it's executable `chmo
@chrisbanes
chrisbanes / KotterKnife.kt
Last active February 7, 2021 15:25
LifecycleAware KotterKnife
package kotterknife
import android.app.Activity
import android.app.Dialog
import android.app.DialogFragment
import android.app.Fragment
import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.LifecycleObserver
import android.arch.lifecycle.LifecycleOwner
import android.arch.lifecycle.OnLifecycleEvent
@yanngx
yanngx / FragmentArgumentDelegate.kt
Last active January 19, 2023 09:26
Fragment arguments without hassle !
package be.brol
import android.os.Binder
import android.os.Bundle
import android.support.v4.app.BundleCompat
import android.support.v4.app.Fragment
/**
* Eases the Fragment.newInstance ceremony by marking the fragment's args with this delegate
* Just write the property in newInstance and read it like any other property after the fragment has been created
@amake
amake / android-7-localization.org
Last active July 25, 2023 22:34
Correct localization on Android 7

Correct localization on Android 7

Prior to Android 7, the system had a single preferred locale, and fallback behavior was quite rudimentary. Starting with Android 7, the user can now specify a priority list of locales, and fallback behavior is improved.

However, in many cases it is still surprisingly difficult to make full use of locale fallback, and there are some hidden gotchas when trying to fully support both Android 7 and earlier versions.

@koral--
koral-- / build.gradle
Last active December 12, 2017 01:31
testApt to testAnnotationProcessor migration, workaround for issue: https://code.google.com/p/android/issues/detail?id=224272
android.applicationVariants.all {
def aptOutputDir = new File(buildDir, "generated/source/apt/${it.unitTestVariant.dirName}")
it.unitTestVariant.addJavaSourceFoldersToModel(aptOutputDir)
}
@nickbutcher
nickbutcher / avd_bundle.xml
Last active December 31, 2021 01:03
An example of the Android xml bundle format for creating an AnimatedVectorDrawable. See https://plus.google.com/+NickButcher/posts/A8KKxnJdg4r
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2016 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
@Takhion
Takhion / RetainedObservableActivity.java
Last active September 30, 2016 16:05
Simplest way to retain an active Observable during configuration changes
package me.eugeniomarletti.example;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.Toast;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;