Skip to content

Instantly share code, notes, and snippets.

View truedem's full-sized avatar

Pavel Kuznetsov truedem

View GitHub Profile
@truedem
truedem / Example @Test
Created September 11, 2017 19:28
Espresso IdlingResource for RxJava tests
@After
public void release() {
Espresso.unregisterIdlingResources();
}
@Test
public void buttonShouldUpdateText() {
// may put following 3 lines into @BeforeEach annotaded method as well
rxEspressoScheduleHandler = new RxEspressoScheduleHandler();
RxJavaPlugins.setScheduleHandler(rxEspressoScheduleHandler);
@truedem
truedem / TextViewOutline.java
Created March 4, 2018 15:46
Android class to create an outlined text with thick stroke (+ shadow if needed)
public class TextViewOutline extends AppCompatTextView {
/*
Add to attr.xml:
<declare-styleable name="TextViewOutline">
<attr name="outlineSize" format="dimension"/>
<attr name="outlineColor" format="color|reference"/>
<attr name="android:shadowRadius"/>
<attr name="android:shadowDx"/>
<attr name="android:shadowDy"/>
@truedem
truedem / SafelySetMutableLiveData.kt
Created October 24, 2018 00:59
Safely set value for MutableLiveData
// to avoid this situation: https://kinnrot.github.io/live-data-pitfall-you-should-be-aware-of/
fun <T> MutableLiveData<T>.safeSetValue(newValue: T) { if(Thread.currentThread() == Looper.getMainLooper().thread) value = newValue else postValue(newValue) }
@truedem
truedem / RxJavaReturns.java
Last active October 24, 2018 22:43
Types to use in RxJava for Retrofit
// One shot operation that returns a value (ie GET, shared prefs)? Single.
// One shot operation that returns nothing? (POST) Completable.
// One shot operation that might return null (ie fetch from a DB)? Maybe.
// An operation that returns multiple objects over time (ie websockets or a list)? Observable.
// use Kotlin coroutines? Deferred<T>
@truedem
truedem / whenNotNull.kt
Created November 5, 2018 03:28
Descriptive replacement for variable?.let{}
// oroginal:
// variable?.let { // doStuff }
// replacement:
inline fun <T:Any, R> whenNotNull(input: T?, callback: (T)->R): R? {
return input?.let(callback)
}
@truedem
truedem / InOut.txt
Created November 13, 2018 21:53
in-out Kotlin = super-extends Java
List<out T> is like List<? extends T> in Java
List<in T> is like List<? super T> in Java
@truedem
truedem / AndroidFragmentFactoryDI.kt
Last active January 9, 2020 22:26
Android: Inject ViewModel in Fragment/Activity which has run-time dependencies
// https://stackoverflow.com/questions/57752301/dagger2-10-inject-viewmodel-in-fragment-activity-which-has-run-time-dependenci
class ProductViewModel(
val product: Product,
val resourceProvider: ResourceProvider,
val appUtils: AppUtils,
val alertManager: AlertManager
) {
// ...
}
@truedem
truedem / ClosestDistance.kt
Last active January 21, 2020 22:14
How close is the geo-location to the path?
// https://www.reddit.com/r/androiddev/comments/ert88w/asking_if_this_is_possible_to_do_with_maps_api/
fun closestDistance(point:LatLng, route:Polyline) : Double {
var minDist = Double.MAX_VALUE
for (i in 0 until (route.points?.size ?: 0) - 1) {
val dist=lineDistance(point,route.points[i],route.points[i+1])
if (dist<minDist){
minDist=dist
}
@truedem
truedem / RecyclerViewExample.kt
Last active September 1, 2021 06:55
Save / restore recyclerview state for Android (after screen rotation) - example
private val ARGS_SCROLL_STATE = "recyclerState"
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putParcelable(ARGS_SCROLL_STATE, recyclerView.getLayoutManager()?.onSaveInstanceState())
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
@truedem
truedem / DayNightModeOn.kt
Created June 26, 2020 20:39
Kotlin one-liner to check if Android device is in day or night mode?
// usage in fragment: val color = if(requireContext().isDarkThemeOn()) Color.DKGRAY else Color.WHITE
// source: https://stackoverflow.com/questions/55787035/is-there-an-api-to-detect-which-theme-the-os-is-using-dark-or-light-or-other
fun Context.isDarkThemeOn() = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES