Skip to content

Instantly share code, notes, and snippets.

View raghunandankavi2010's full-sized avatar
🤵‍♂️
Android dev looking to learn new technologies and always open for a new job.

Raghunandan Kavi raghunandankavi2010

🤵‍♂️
Android dev looking to learn new technologies and always open for a new job.
View GitHub Profile
@raghunandankavi2010
raghunandankavi2010 / AsyncDiffUtil
Created March 17, 2020 17:06
AsyncDiffUtil using kotlin coroutnes
package me.raghu.mvpassignment.util
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListUpdateCallback
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel.Factory.CONFLATED
import kotlinx.coroutines.channels.actor
import java.util.*
@raghunandankavi2010
raghunandankavi2010 / BounceScrollView
Created February 15, 2020 11:43
A scrollview that is animated off screen if you touch, move is greater than 50% of screen height
import android.animation.Animator
import android.animation.ObjectAnimator
import android.animation.ValueAnimator.AnimatorUpdateListener
import android.content.Context
import android.content.res.Resources
import androidx.core.widget.NestedScrollView
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
@raghunandankavi2010
raghunandankavi2010 / ServiceGenerator
Created February 22, 2019 05:06
Service Generator
import android.text.TextUtils;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
@raghunandankavi2010
raghunandankavi2010 / auth.java
Created February 12, 2019 14:34
Okhttp authenticator
final OkHttpClient client = new OkHttpClient.Builder()
. authenticator(new Authenticator() {
@Override public Request authenticate(Route route, Response response) throws IOException {
if (response.request().header("Authorization") != null) {
return null; // Give up, we've already attempted to authenticate.
}
String credential = Credentials.basic("jesse", "password1");
return response.request().newBuilder()
.header("Authorization", credential)
.build();
@raghunandankavi2010
raghunandankavi2010 / BackPress
Created February 5, 2019 04:12
Fragment back stack back press handling
@Override
public void onBackPressed() {
if(mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mDrawerLayout.closeDrawers();
}
else if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
String fragmentTag = getSupportFragmentManager().getBackStackEntryAt(getSupportFragmentManager().getBackStackEntryCount() - 1).getName();
if (fragmentTag.equals("dashboard")) {
//Toast.makeText(this.getApplicationContext(),""+fragmentTag,Toast.LENGTH_SHORT).show();
finish();
class class RetrofitDependency : RetrofitInterface {
override fun provideRetrofit(): Retrofit {
val logging = HttpLoggingInterceptor()
logging.level = HttpLoggingInterceptor.Level.BASIC
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(logging)
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
interface RetrofitInterface {
fun provideRetrofit(): Retrofit
}
class ListRepositoryImpl(private val retrofitDependency: RetrofitDependency): ListRepository {
var data = MutableLiveData<List<Users>>()
override fun getUsers(): MutableLiveData<List<Users>>{
val service = retrofitDependency.provideRetrofit().create(Api::class.java)
service.users.enqueue(object : Callback<List<Users>> {
override fun onResponse(call: Call<List<Users>>, response: Response<List<Users>>) {
if (response.isSuccessful) {
class ListScreenViewModel(private val repo: ListRepositoryImpl) : ViewModel() {
fun getUsers() : LiveData<List<Users>> {
return repo.getUsers()
}
}
val applicationModule = module {
single<RetrofitInterface>("Retrofit") { RetrofitDependency() }
single<raghu.me.myapplication.di.ListRepository>("ListRepository"){ ListRepositoryImpl(get("Retrofit")) }
// ListScreenViewModel
viewModel { ListScreenViewModel(get("ListRepository")) }
}