Skip to content

Instantly share code, notes, and snippets.

View trunghq3101's full-sized avatar

Miller Go Dev trunghq3101

View GitHub Profile
@trunghq3101
trunghq3101 / ErrorInterceptor.kt
Created July 27, 2019 00:50 — forked from yitz-grocerkey/ErrorInterceptor.kt
Retrofit RxJava global error handling in Kotlin
// for any errors that should be handled before being handed off to RxJava.
// In other words global error logic.
// An example might be 401 when not logging in
import okhttp3.Interceptor
import okhttp3.Response
class ErrorInterceptor: Interceptor {
override fun intercept(chain: Interceptor.Chain?): Response {
@trunghq3101
trunghq3101 / ApiModule.java
Created July 27, 2019 00:57 — forked from msangel/ApiModule.java
Retrofit 1 error handling behaviour in Retrofit 2.3.0
// Dagger 1 example
@Module(
complete = false,
library = true
)
public final class ApiModule {
@Provides
@Singleton
Retrofit provideRetrofit(Gson gson, Application app) {
return new Retrofit.Builder()
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import io.reactivex.Completable;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.annotations.NonNull;
package com.miller.futurechat
import android.content.Context
import android.util.Log
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.signature.ObjectKey
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
@trunghq3101
trunghq3101 / styles.xml
Created October 16, 2019 02:07
Custom drawer arrow style (Up arrow)
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="drawerArrowStyle">@style/AppTheme.DrawerArrowStyle</item>
<item name="android:textSize">14sp</item>
</style>
<style name="AppTheme.DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
@trunghq3101
trunghq3101 / nav_graph.xml
Created October 21, 2019 03:43
Pop this fragment from backstack when go to new destination
<fragment
android:id="@+id/blankFragment"
android:name="com.miller.futurechat.presentation.blank.BlankFragment"
android:label="Home"
tools:layout="@layout/fragment_blank">
<action
android:id="@+id/action_blank_to_conversations"
app:destination="@id/conversationsFragment"
app:popUpTo="@id/blankFragment"
app:popUpToInclusive="true" />
@trunghq3101
trunghq3101 / MessagingFragment.kt
Created October 21, 2019 04:34
Detech when soft keyboard shows over a RecyclerView
recyclerMessages.addOnLayoutChangeListener { v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
if (bottom < oldBottom) {
recyclerMessages.smoothScrollToPosition(0)
}
}
@trunghq3101
trunghq3101 / BaseRecyclerAdapter.kt
Created November 28, 2019 15:18
example RecyclerView Adapter using view type
abstract class BaseRecyclerAdapter<Item, ViewBinding : ViewDataBinding>(
callBack: DiffUtil.ItemCallback<Item>
) : ListAdapter<Item, BaseViewHolder<ViewBinding>>(
AsyncDifferConfig.Builder<Item>(callBack)
.setBackgroundThreadExecutor(Executors.newSingleThreadExecutor())
.build()
) {
override fun submitList(list: List<Item>?) {
super.submitList(ArrayList<Item>(list ?: listOf()))
@trunghq3101
trunghq3101 / CoroutineUnderstanding.kt
Created April 25, 2020 06:52
Kotlin Coroutine Understanding
/**
* You can edit, run, and share this code.
* play.kotlinlang.org
*/
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlin.system.*
fun test1() {
var time = 0L
@trunghq3101
trunghq3101 / AuthenticatorInterceptor.kt
Created May 28, 2020 08:56
Refresh token interceptor
class AuthenticateInterceptor(
private val dataStorage: DataStorage,
private val authService: AuthService
) : Interceptor {
override fun intercept(chain: Chain): Response {
val newRequest = buildAuthorizedRequest(chain)
val response = chain.proceed(newRequest)
return if (response.code == CODE_TOKEN_EXPIRED) {