Skip to content

Instantly share code, notes, and snippets.

@alexshr
alexshr / DataBindingKTX
Last active April 18, 2020 19:58
fragment binding delegate
allprojects {
repositories {
.......
maven { url "https://jitpack.io" }
}
--------------------
//ext lib
implementation 'com.github.wada811:DataBinding-ktx:2.0.2'
-------------------------------------
@alexshr
alexshr / LiveEventNavigation.kt
Last active August 12, 2019 01:21
Navigation using LiveEvent subscription
sample:
https://github.com/alexshr/android-kotlin-fundamentals-apps/tree/master/MarsRealEstateFinal
---------------------------------------------
//https://github.com/hadilq/LiveEvent
implementation "com.github.hadilq.liveevent:liveevent:1.0.1"
-----------------------------
ViewModel:
/**
@alexshr
alexshr / GetFragmentViewModel.kt
Last active August 11, 2019 01:53
Get ViewModel scoped to this Fragment (fragment-ktx)
//my sample
//https://github.com/alexshr/android-kotlin-fundamentals-apps/tree/master/MarsRealEstateFinal
//doc
//https://developer.android.com/reference/kotlin/androidx/fragment/app/package-summary#viewmodels
version_fragment = '1.2.0-alpha01'
implementation "androidx.fragment:fragment-ktx:${version_fragment}"
--------------------------------------------------------------------------------------
@alexshr
alexshr / SnackBarLiveEvent.kt
Last active August 7, 2019 18:00
LiveEvent sample (for snackbar)
/**
* Snackbar LiveEvent
* (see https://github.com/hadilq/LiveEvent)
* This is private because we don't want to expose setting this value to the Fragment.
*/
private val snackBarEvent = LiveEvent<String>()
val showSnackBarEvent: LiveData<String> = snackBarEvent
//https://github.com/hadilq/LiveEvent
@alexshr
alexshr / SnackbarAnko.kt
Last active August 7, 2019 17:57
show snackbar (using anko view extension)
// Observe snackbar event
sleepTrackerViewModel.showSnackBarEvent.observe(this, Observer {
binding.clearButton.snackbar(it)
})
//Anko
implementation "org.jetbrains.anko:anko-commons:$version_anko"
implementation "org.jetbrains.anko:anko-design:$version_anko" // For SnackBars
@alexshr
alexshr / GetFragArgsFromNavActionKtx.kt
Last active August 7, 2019 15:55
Get fragment args from navigation action (lazy delegate from navigation-ktx)
//using ktx for args https://developer.android.com/kotlin/ktx#navigation
val arguments by navArgs<SleepQualityFragmentArgs>()
@alexshr
alexshr / kotlin_supress_expiremental_api
Created April 30, 2019 20:38
kotlin coroutines @file:Suppress("EXPERIMENTAL_API_USAGE")
@file:Suppress("EXPERIMENTAL_API_USAGE")
@alexshr
alexshr / EspressoExecutor.java
Last active March 4, 2019 13:50
The utility repeats runnable or callable executing until it pass without errors or throws throwable after timeout. It works perfectly for Espresso tests.
/**
* Created by alexshr on 02.05.2017.
*/
package com.skb.goodsapp;
import android.os.SystemClock;
import android.util.Log;
import java.util.Date;
@alexshr
alexshr / isServiceRunning.java
Created February 22, 2019 01:56
isServiceRunning
private boolean isServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
boolean isRunning = false;
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (LocationService.class.getName().equals(service.service.getClassName())) {
isRunning = true;
}
}
Timber.d("isRunning=%b", isRunning);
return isRunning;
@alexshr
alexshr / drawableToBitmap.java
Created February 12, 2019 01:05
drawable to bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
int width = drawable.getIntrinsicWidth();
width = width > 0 ? width : 1;
int height = drawable.getIntrinsicHeight();
height = height > 0 ? height : 1;