Skip to content

Instantly share code, notes, and snippets.

@mkodekar
Created August 25, 2017 00:10
Show Gist options
  • Save mkodekar/bdfdfe38098728e90098461ee3913a30 to your computer and use it in GitHub Desktop.
Save mkodekar/bdfdfe38098728e90098461ee3913a30 to your computer and use it in GitHub Desktop.
a crossline function at the end
package co.kodparadise.khabarsamachar.application
import android.content.Context
import co.kodparadise.khabarsamachar.annotations.AppScope
import co.kodparadise.khabarsamachar.dependencies.ContextModule
import com.github.ajalt.timberkt.Timber
import dagger.Module
import dagger.Provides
import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import java.io.File
/**
* Created by rkodekar on 8/25/17.
*/
@Module(includes = arrayOf(ContextModule::class))
class NetworkModule {
@Provides
@AppScope
fun provideLoggingInterceptor() = {
val loggingInterceptor = HttpLoggingInterceptor{
Timber.i { it }
}
loggingInterceptor.level = HttpLoggingInterceptor.Level.BASIC
loggingInterceptor
}
@Provides
@AppScope
fun provideCache(cacheFile: File) = Cache(cacheFile, 10.toLong() into 1000 into 1000)
@Provides
@AppScope
fun provideCacheFile(context: Context) = File(context.cacheDir, "okhttp_cache")
@Provides
@AppScope
fun provideOkhttpClient(loggingInterceptor: HttpLoggingInterceptor, cache: Cache) = makeclient {
addInterceptor(loggingInterceptor)
cache(cache)
}
infix fun Long.into(nextValue: Long) = this * nextValue
inline fun makeclient(crossinline func: OkHttpClient.Builder.() -> Unit) = {
val builder = OkHttpClient.Builder()
builder.func()
builder.build()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment