-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
wing
committed
Nov 23, 2016
1 parent
d95dac7
commit ad92e2b
Showing
11 changed files
with
99 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/com/wingsofts/gankclient/di/component/ApiComponent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
51
app/src/main/java/com/wingsofts/gankclient/di/module/ApiModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
app/src/main/java/com/wingsofts/gankclient/di/module/AppModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
5 changes: 4 additions & 1 deletion
5
.../com/wingsofts/gankclient/MainActivity.kt → ...ts/gankclient/ui/activity/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters