Skip to content

Instantly share code, notes, and snippets.

View mreichelt's full-sized avatar
👨‍💻
Coding is awesome 🎉

Marc Reichelt mreichelt

👨‍💻
Coding is awesome 🎉
View GitHub Profile
@mreichelt
mreichelt / RunBlockingTest.kt
Created March 31, 2022 11:58
Kotlin Coroutines 1.6: run blocking test, but cancel scope at exit without caring about endless-running jobs
fun TestCoroutineScope.runBlockingTest(
testBody: suspend TestCoroutineScope.() -> Unit,
) {
val regularEndOfTest = CancellationException()
try {
this.runBlockingTestKotlin { // import kotlinx.coroutines.test.runBlockingTest as runBlockingTestKotlin
testBody()
cancel(regularEndOfTest)
}
} catch (e: CancellationException) {
@mreichelt
mreichelt / KotlinMapIntersection.kt
Created August 13, 2018 11:25
Kotlin: Intersect maps with different value types on their keys
/**
* Intersect two maps with same keys, but different data types. Preserves the order of the first map.
* Returns a list of the combined type provided by the transformation.
*/
inline fun <Key, Value1, Value2, T> Map<Key, Value1>.intersectWith(
other: Map<Key, Value2>,
transformationFunction: (Value1, Value2) -> T): List<T> {
return flatMap { oldEntry ->
other.filterKeys { it == oldEntry.key }
.map { transformationFunction(oldEntry.value, it.value) }
@mreichelt
mreichelt / build.gradle
Last active January 22, 2019 12:47
Example of build.gradle where the signing config comes from system environment variables
android {
signingConfigs {
release {
// export these as environment variables like ORG_GRADLE_PROJECT_MYAPP_RELEASE_STORE_FILE
// (prefix 'ORG_GRADLE_PROJECT_' is needed for Gradle project properties)
storeFile rootProject.file('app/' + project.findProperty('MYAPP_RELEASE_STORE_FILE'))
storePassword project.findProperty('MYAPP_RELEASE_STORE_PASSWORD')
keyAlias project.findProperty('MYAPP_RELEASE_KEY_ALIAS')
keyPassword project.findProperty('MYAPP_RELEASE_KEY_PASSWORD')
}
@mreichelt
mreichelt / firefox_docker.sh
Last active June 26, 2023 01:53
Run Firefox in Docker image on Mac with X11 forwarding to XQuartz
#!/bin/bash
# allow access from localhost
xhost + 127.0.0.1
# run firefox with X11 forwarding and keep running until it quits
docker run -e DISPLAY=host.docker.internal:0 jess/firefox
@mreichelt
mreichelt / constraint_layout_grow_until_guideline.xml
Last active April 25, 2023 09:00
Example of using ConstraintLayout: The growing view will be only as large as it needs to be - unless it grows too large. Then it will only be as large as the guideline allows.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"

Keybase proof

I hereby claim:

  • I am mreichelt on github.
  • I am mreichelt (https://keybase.io/mreichelt) on keybase.
  • I have a public key ASDe0J_nGJJs5XI-gKOUObaNPS447BwR4QI0z4KYmaxZ9go

To claim this, I am signing this object:

public class TestApp extends OurApp {
@Override
protected void createComponents() {
OurTestComponent component = mock(OurTestComponent.class);
// fixed the memory leak by using a plain stub (without invocation tracking)
OurActivityLifecycleCallbacks callbacks =
mock(OurActivityLifecycleCallbacks.class, withSettings().stubOnly());
when(component.activityLifecycleCallbacks()).thenReturn(mock(callbacks));
OurApp.setComponent(component);
public class TestApp extends OurApp {
/** overrides method in OurApp that initializes our actual Dagger2 component */
@Override
protected void createComponents() {
OurTestComponent component = mock(OurTestComponent.class);
// required so our App init call works properly, which registers these callbacks
// WOAH, this is responsible for the memory leak!! But why?
when(component.activityLifecycleCallbacks())
.thenReturn(mock(activityLifecycleCallbacks));
@mreichelt
mreichelt / trackapksize.sh
Created May 25, 2016 21:55
Script to output CSV file of APK size
OUTFILE=flinc/app/build/outputs/apksize.csv
echo filesize > $OUTFILE
# yep, that's for Mac. Use "stat -c %s" instead on Linux
stat -f%z flinc/app/build/outputs/apk/app-release.apk >> $OUTFILE
@mreichelt
mreichelt / build.gradle
Created May 25, 2016 21:20
dexcount: app/build.gradle
android {
// your android block goes here
}
apply plugin: 'com.getkeepsafe.dexcount'