Skip to content

Commit

Permalink
inject retrofit
Browse files Browse the repository at this point in the history
  • Loading branch information
wing committed Nov 23, 2016
1 parent d95dac7 commit ad92e2b
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 5 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ dependencies {
kapt dependencies.daggerCompiler
provided dependencies.javaxAnnotation

compile dependencies.kotlinStdlib

// Kotlin
// compile dependencies.kotlinStdlib
testCompile 'junit:junit:4.12'
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".ui.activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/com/wingsofts/gankclient/App.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package com.wingsofts.gankclient

import android.app.Application
import com.wingsofts.gankclient.di.component.DaggerApiComponent
import com.wingsofts.gankclient.di.module.ApiModule
import com.wingsofts.gankclient.di.module.AppModule
import retrofit2.Retrofit
import javax.inject.Inject

/**
* Created by wing on 16-11-22.
*/
class App :Application(){
@Inject lateinit var mRetrofit :Retrofit
override fun onCreate() {
super.onCreate()

DaggerApiComponent.builder().apiModule(ApiModule()).appModule(AppModule(this)).build().inject(this)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.wingsofts.gankclient.di.component

import com.wingsofts.gankclient.App
import com.wingsofts.gankclient.di.module.ApiModule
import dagger.Component

/**
* Created by wing on 16-11-23.
*/
@Component(modules = arrayOf(ApiModule::class))
interface ApiComponent{

fun inject(app: App)

}

51 changes: 51 additions & 0 deletions app/src/main/java/com/wingsofts/gankclient/di/module/ApiModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.wingsofts.gankclient.di.module

import android.content.Context
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import dagger.Module
import dagger.Provides
import okhttp3.Cache
import okhttp3.HttpUrl
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import rx.schedulers.Schedulers
import java.io.File

/**
* Created by wing on 16-11-23.
*/
@Module(includes = arrayOf(AppModule::class))
class ApiModule {
@Provides fun provideRetrofit(baseUrl: HttpUrl, client: OkHttpClient, gson: Gson) =
Retrofit.Builder()
.client(client)
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()))
.build()

@Provides fun provideBaseUrl() = HttpUrl.parse("http://gank.io/api/")
@Provides fun provideOkhttp(context: Context,interceptor: HttpLoggingInterceptor): OkHttpClient {
val cacheSize = 1024 * 1024 * 10L
val cacheDir = File(context.cacheDir, "http")
val cache = Cache(cacheDir, cacheSize)
return OkHttpClient.Builder()
.cache(cache)
.addInterceptor(interceptor).build()
}
@Provides fun provideInterceptor() :HttpLoggingInterceptor{
val interceptor = HttpLoggingInterceptor{

}
interceptor.level = HttpLoggingInterceptor.Level.BODY
return interceptor
}

@Provides fun provideGson() = GsonBuilder().create()
}

13 changes: 13 additions & 0 deletions app/src/main/java/com/wingsofts/gankclient/di/module/AppModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.wingsofts.gankclient.di.module

import android.content.Context
import dagger.Module
import dagger.Provides

/**
* Created by wing on 16-11-23.
*/
@Module
class AppModule(private val context: Context){
@Provides fun provideContext() = context
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.wingsofts.gankclient
package com.wingsofts.gankclient.ui.activity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.wingsofts.gankclient.R
import okhttp3.OkHttpClient

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
OkHttpClient.Builder().build()
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.wingsofts.gankclient.MainActivity">
tools:context="com.wingsofts.gankclient.ui.activity.MainActivity">

<TextView
android:layout_width="wrap_content"
Expand Down
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.4"

classpath "org.jetbrains.kotlin:kotlin-android-extensions:1.0.4"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
3 changes: 3 additions & 0 deletions config/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,8 @@ ext {
supportCustomTabs : "com.android.support:customtabs:$supportLibVersion",
supportMultidex : 'com.android.support:multidex:1.0.1',

kotlinStdlib : "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion",
kotlinReflect : "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion",

]
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.1-all.zip

0 comments on commit ad92e2b

Please sign in to comment.