Skip to content

Instantly share code, notes, and snippets.

View bapspatil's full-sized avatar
🪄

Bapusaheb Patil bapspatil

🪄
View GitHub Profile
@bapspatil
bapspatil / CoroutineLifecycleObserver.kt
Created September 7, 2018 07:45 — forked from chrisbanes/CoroutineLifecycleObserver.kt
LifecycleObserver which allows easy cancelling of coroutines
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@bapspatil
bapspatil / RetrofitAPI.kt
Created September 7, 2018 06:59
Final code snippet for cache-enabled Retrofit
val cacheSize = (5 x 1024 x 1024).toLong()
val myCache = Cache(context.cacheDir, cacheSize)
val okHttpClient = OkHttpClient.Builder()
.cache(myCache)
.addInterceptor { chain ->
var request = chain.request()
request = if (hasNetwork(context)!!)
request.newBuilder().header("Cache-Control", "public, max-age=" + 5).build()
else
@bapspatil
bapspatil / RetrofitAPI.kt
Created September 7, 2018 06:50
Retrofit instance (Cache-enabled)
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
// Adding our OkHttpClient
.client(okHttpClient)
.build()
@bapspatil
bapspatil / MyOkHttpClient.kt
Last active January 4, 2021 19:39
OkHttpClient (SHORT) for caching
val okHttpClient = OkHttpClient.Builder()
// Specify the cache we created earlier.
.cache(myCache)
// Add an Interceptor to the OkHttpClient.
.addInterceptor { chain ->
// Get the request from the chain.
var request = chain.request()
/*
@bapspatil
bapspatil / NetworkCheck.kt
Last active September 7, 2018 08:06
A method that checks if the app is connected to the Internet or not.
fun hasNetwork(context: Context): Boolean? {
var isConnected: Boolean? = false // Initial Value
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork: NetworkInfo? = connectivityManager.activeNetworkInfo
if (activeNetwork != null && activeNetwork.isConnected)
isConnected = true
return isConnected
}
@bapspatil
bapspatil / CacheEnabledOkHttpClient.kt
Last active September 7, 2018 06:57
OkHttpClient (Cache-enabled)
val cacheSize = (5 x 1024 x 1024).toLong()
val myCache = Cache(context.cacheDir, cacheSize)
val okHttpClient = OkHttpClient.Builder()
.cache(myCache)
.addInterceptor { chain ->
var request = chain.request()
request = if (hasNetwork(context)!!)
request.newBuilder().header("Cache-Control", "public, max-age=" + 5).build()
else
@bapspatil
bapspatil / RetrofitAPI.kt
Created September 7, 2018 05:45
Retrofit instance without caching
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
@bapspatil
bapspatil / MainActivity.java in app module
Last active August 22, 2018 06:39
MainActivity.java in app module for Android App Bundle
// Global instance of SplitInstallManager, needed to send the request to download the required module(s)
private SplitInstallManager splitInstallManager = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate an instance of SplitInstallManager for the dynamic feature module
splitInstallManager =
@bapspatil
bapspatil / build.gradle for feature1
Created August 22, 2018 05:12
feature1 module's build.gradle file for Android App Bundle
apply plugin: 'com.android.dynamic-feature'
android {
...
}
dependencies {
...
// Add the app module as a dependency in the dynamic feature modules.
implementation project(':app')
@bapspatil
bapspatil / build.gradle for app
Last active August 22, 2018 12:54
Modified build.gradle for app for Android App Bundle
apply plugin: 'com.android.application'
android {
...
// This specifies the dynamic features.
dynamicFeatures = [":feature1", ":feature2"]
}