Skip to content

Instantly share code, notes, and snippets.

View truedem's full-sized avatar

Pavel Kuznetsov truedem

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / rxjava_throttle_notfirst.java
Created May 27, 2017 10:03
Throttle not first item in RxJava Observable
// https://github.com/ReactiveX/RxJava/issues/1323#issuecomment-45149897
// thank you, akarnokd !
public class ThrottleNotFirst {
public static void main(String[] args) throws Exception {
for (int firstWait = 0; firstWait < 2; firstWait++) {
PublishSubject<String> source = PublishSubject.create();
PublishSubject<Observable<String>> immediates = PublishSubject.create();
@truedem
truedem / gist:7781529841209ada6bae351390328a2a
Last active November 1, 2016 04:46
Delayed AdMob interstitial request to prevent ANR
content.postDelayed(new Runnable() {
@Override
public void run() {
if ((Build.VERSION.SDK_INT >= 17 && LWQSettingsActivity.this.isDestroyed()) || LWQSettingsActivity.this.isFinishing()) {
return;
}
requestNewInterstitial();
}
}, 1000);