refactor: 修改原有网络层代码为Hilt注入
This commit is contained in:
@ -2,6 +2,7 @@ package com.quyunshuo.androidbaseframemvvm.common.ui
|
||||
|
||||
import androidx.viewbinding.ViewBinding
|
||||
import com.quyunshuo.androidbaseframemvvm.base.mvvm.v.BaseFrameActivity
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.AndroidBugFixUtils
|
||||
|
||||
/**
|
||||
* Activity基类
|
||||
@ -9,4 +10,11 @@ import com.quyunshuo.androidbaseframemvvm.base.mvvm.v.BaseFrameActivity
|
||||
* @author Qu Yunshuo
|
||||
* @since 8/27/20
|
||||
*/
|
||||
abstract class BaseActivity<VB : ViewBinding> : BaseFrameActivity<VB>()
|
||||
abstract class BaseActivity<VB : ViewBinding> : BaseFrameActivity<VB>() {
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
// 解决某些特定机型会触发的Android本身的Bug
|
||||
AndroidBugFixUtils().fixSoftInputLeaks(this)
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,7 @@ package com.quyunshuo.androidbaseframemvvm.common.ui
|
||||
|
||||
import androidx.viewbinding.ViewBinding
|
||||
import com.quyunshuo.androidbaseframemvvm.base.mvvm.v.BaseFrameNotMVVMActivity
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.AndroidBugFixUtils
|
||||
|
||||
/**
|
||||
* 不是 MVVM 模式的基类
|
||||
@ -9,4 +10,11 @@ import com.quyunshuo.androidbaseframemvvm.base.mvvm.v.BaseFrameNotMVVMActivity
|
||||
* @author Qu Yunshuo
|
||||
* @since 9/10/20
|
||||
*/
|
||||
abstract class BaseNotMVVMActivity<VB : ViewBinding> : BaseFrameNotMVVMActivity<VB>()
|
||||
abstract class BaseNotMVVMActivity<VB : ViewBinding> : BaseFrameNotMVVMActivity<VB>() {
|
||||
|
||||
override fun onDestroy() {
|
||||
// 解决某些特定机型会触发的Android本身的Bug
|
||||
AndroidBugFixUtils().fixSoftInputLeaks(this)
|
||||
super.onDestroy()
|
||||
}
|
||||
}
|
||||
@ -6,7 +6,7 @@ package com.quyunshuo.androidbaseframemvvm.net
|
||||
* @author Qu Yunshuo
|
||||
* @since 4/17/21 3:27 PM
|
||||
*/
|
||||
internal object NetBaseUrl {
|
||||
internal object NetBaseUrlConstant {
|
||||
|
||||
const val BASE_URLl = ""
|
||||
const val MAIN_URL = ""
|
||||
}
|
||||
@ -1,62 +0,0 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.net
|
||||
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
/**
|
||||
* 网络客户端提供者
|
||||
*
|
||||
* @author Qu Yunshuo
|
||||
* @since 4/17/21 2:35 PM
|
||||
*/
|
||||
object NetClientProvider {
|
||||
|
||||
/**
|
||||
* 连接超时
|
||||
*/
|
||||
private const val CONNECT_TIME_OUT = 15L * 1000L
|
||||
|
||||
/**
|
||||
* 读取超时
|
||||
*/
|
||||
private const val READ_TIME_OUT = 20L * 1000L
|
||||
|
||||
/**
|
||||
* 日志拦截器
|
||||
*/
|
||||
private val mLogInterceptor: Interceptor by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
|
||||
val level = if (BuildConfig.DEBUG) {
|
||||
HttpLoggingInterceptor.Level.BODY
|
||||
} else {
|
||||
HttpLoggingInterceptor.Level.NONE
|
||||
}
|
||||
HttpLoggingInterceptor().setLevel(level)
|
||||
}
|
||||
|
||||
/**
|
||||
* OkHttpClient
|
||||
*/
|
||||
private val mOkHttpClient: OkHttpClient by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
|
||||
OkHttpClient.Builder()
|
||||
.connectTimeout(CONNECT_TIME_OUT, TimeUnit.MILLISECONDS) // 连接超时
|
||||
.readTimeout(READ_TIME_OUT, TimeUnit.MILLISECONDS) // 读取超时
|
||||
.addInterceptor(mLogInterceptor) // 日志拦截器
|
||||
.retryOnConnectionFailure(true) // 失败重连
|
||||
.build()
|
||||
}
|
||||
|
||||
/**
|
||||
* 项目服务器Retrofit
|
||||
*/
|
||||
val mRetrofit: Retrofit by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
|
||||
Retrofit.Builder()
|
||||
.baseUrl(NetBaseUrl.BASE_URLl)
|
||||
.addConverterFactory(GsonConverterFactory.create()) // Gson转换器
|
||||
.client(mOkHttpClient)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,63 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.net.di
|
||||
|
||||
import com.quyunshuo.androidbaseframemvvm.base.BuildConfig
|
||||
import com.quyunshuo.androidbaseframemvvm.net.NetBaseUrlConstant
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import retrofit2.Retrofit
|
||||
import java.util.concurrent.TimeUnit
|
||||
import okhttp3.logging.HttpLoggingInterceptor.Level.NONE
|
||||
import okhttp3.logging.HttpLoggingInterceptor.Level.BODY
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* 全局作用域的网络层的依赖注入模块
|
||||
*
|
||||
* @author Qu Yunshuo
|
||||
* @since 6/4/21 8:58 AM
|
||||
*/
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
class DINetworkModule {
|
||||
|
||||
/**
|
||||
* [OkHttpClient]依赖提供方法
|
||||
*
|
||||
* @return OkHttpClient
|
||||
*/
|
||||
@Singleton
|
||||
@Provides
|
||||
fun provideOkHttpClient(): OkHttpClient {
|
||||
// 日志拦截器部分
|
||||
val level = if (BuildConfig.DEBUG) BODY else NONE
|
||||
val logInterceptor = HttpLoggingInterceptor().setLevel(level)
|
||||
|
||||
return OkHttpClient.Builder()
|
||||
.connectTimeout(15L * 1000L, TimeUnit.MILLISECONDS)
|
||||
.readTimeout(20L * 1000L, TimeUnit.MILLISECONDS)
|
||||
.addInterceptor(logInterceptor)
|
||||
.retryOnConnectionFailure(true)
|
||||
.build()
|
||||
}
|
||||
|
||||
/**
|
||||
* 项目主要服务器地址的[Retrofit]依赖提供方法
|
||||
*
|
||||
* @param okHttpClient OkHttpClient OkHttp客户端
|
||||
* @return Retrofit
|
||||
*/
|
||||
@Singleton
|
||||
@Provides
|
||||
fun provideMainRetrofit(okHttpClient: OkHttpClient): Retrofit {
|
||||
return Retrofit.Builder()
|
||||
.baseUrl(NetBaseUrlConstant.MAIN_URL)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.client(okHttpClient)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
package com.quyunshuo.module.home
|
||||
|
||||
import com.quyunshuo.androidbaseframemvvm.base.mvvm.m.BaseRepository
|
||||
import com.quyunshuo.module.home.net.HomeApiService
|
||||
import kotlinx.coroutines.delay
|
||||
import javax.inject.Inject
|
||||
|
||||
@ -12,6 +13,9 @@ import javax.inject.Inject
|
||||
*/
|
||||
class HomeRepository @Inject constructor() : BaseRepository() {
|
||||
|
||||
@Inject
|
||||
lateinit var mApi: HomeApiService
|
||||
|
||||
/**
|
||||
* 模拟获取数据
|
||||
*/
|
||||
|
||||
@ -0,0 +1,32 @@
|
||||
package com.quyunshuo.module.home.di
|
||||
|
||||
import com.quyunshuo.module.home.net.HomeApiService
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import retrofit2.Retrofit
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* 全局作用域的Home组件网络接口代理依赖注入模块
|
||||
*
|
||||
* @author Qu Yunshuo
|
||||
* @since 6/4/21 5:51 PM
|
||||
*/
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
class DIHomeNetServiceModule {
|
||||
|
||||
/**
|
||||
* Home模块的[HomeApiService]依赖提供方法
|
||||
*
|
||||
* @param retrofit Retrofit
|
||||
* @return HomeApiService
|
||||
*/
|
||||
@Singleton
|
||||
@Provides
|
||||
fun provideHomeApiService(retrofit: Retrofit): HomeApiService {
|
||||
return retrofit.create(HomeApiService::class.java)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package com.quyunshuo.module.home.net
|
||||
|
||||
/**
|
||||
* Home模块的接口
|
||||
*
|
||||
* @author Qu Yunshuo
|
||||
* @since 6/4/21 5:51 PM
|
||||
*/
|
||||
interface HomeApiService
|
||||
Reference in New Issue
Block a user