增加离线地图下载流程
This commit is contained in:
94
app/src/main/java/com/navinfo/omqs/hilt/GlobalModule.kt
Normal file
94
app/src/main/java/com/navinfo/omqs/hilt/GlobalModule.kt
Normal file
@@ -0,0 +1,94 @@
|
||||
package com.navinfo.omqs.hilt
|
||||
|
||||
import android.app.Application
|
||||
import android.util.Log
|
||||
import com.google.gson.Gson
|
||||
import com.navinfo.omqs.Constant
|
||||
import com.navinfo.omqs.OMQSApplication
|
||||
import com.navinfo.omqs.http.RetrofitNetworkServiceAPI
|
||||
import dagger.Lazy
|
||||
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 retrofit2.converter.gson.GsonConverterFactory
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* 全局单例 注入对象
|
||||
*/
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
class GlobalModule {
|
||||
|
||||
@Singleton
|
||||
@Provides
|
||||
fun provideApplication(application: Application): OMQSApplication {
|
||||
return application as OMQSApplication
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入 网络OKHttp 对象
|
||||
*/
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideOkHttpClient(interceptor: HttpLoggingInterceptor): OkHttpClient {
|
||||
return OkHttpClient.Builder().addInterceptor(interceptor)
|
||||
.connectTimeout(60, TimeUnit.SECONDS).writeTimeout(5, TimeUnit.MINUTES)
|
||||
.readTimeout(5, TimeUnit.MINUTES).build()
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入 OHHttp log
|
||||
*/
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideLoggingInterceptor(): HttpLoggingInterceptor {
|
||||
return HttpLoggingInterceptor {
|
||||
if (Constant.DEBUG) {
|
||||
Log.e("jingo", it)
|
||||
}
|
||||
}.apply {
|
||||
level = if (Constant.DEBUG) {
|
||||
HttpLoggingInterceptor.Level.BODY
|
||||
} else {
|
||||
HttpLoggingInterceptor.Level.NONE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideRetrofit(
|
||||
client: Lazy<OkHttpClient>,
|
||||
converterFactory: GsonConverterFactory,
|
||||
): Retrofit {
|
||||
val retrofitBuilder = Retrofit.Builder()
|
||||
.baseUrl(Constant.SERVER_ADDRESS)
|
||||
.client(client.get())
|
||||
.addConverterFactory(converterFactory)
|
||||
|
||||
return retrofitBuilder.build()
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideGson(): Gson = Gson()
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideGsonConverterFactory(gson: Gson): GsonConverterFactory {
|
||||
return GsonConverterFactory.create(gson)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideNetworkService(retrofit: Retrofit): RetrofitNetworkServiceAPI {
|
||||
return retrofit.create(RetrofitNetworkServiceAPI::class.java)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.navinfo.omqs.hilt
|
||||
|
||||
import android.util.Log
|
||||
import com.navinfo.collect.library.map.NIMapController
|
||||
import com.navinfo.omqs.ui.activity.map.MainViewModel
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.components.ActivityRetainedComponent
|
||||
import dagger.hilt.android.scopes.ActivityRetainedScoped
|
||||
|
||||
@InstallIn(ActivityRetainedComponent::class)
|
||||
@Module
|
||||
class MainActivityModule {
|
||||
|
||||
@ActivityRetainedScoped
|
||||
@Provides
|
||||
fun providesMapController(): NIMapController = NIMapController()
|
||||
|
||||
/**
|
||||
* 实验失败,这样创建,viewmodel不会在activity销毁的时候同时销毁
|
||||
*/
|
||||
// @ActivityRetainedScoped
|
||||
// @Provides
|
||||
// fun providesMainViewModel(mapController: NIMapController): MainViewModel {
|
||||
// Log.e("jingo", "MainViewModel 被创建")
|
||||
// return MainViewModel(mapController)
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.navinfo.omqs.hilt
|
||||
|
||||
import com.navinfo.omqs.http.NetworkService
|
||||
import com.navinfo.omqs.http.NetworkServiceImpl
|
||||
import dagger.Binds
|
||||
import dagger.Module
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
|
||||
@InstallIn(SingletonComponent::class)
|
||||
@Module
|
||||
abstract class NetworkServiceModule {
|
||||
|
||||
@Binds
|
||||
abstract fun bindNetworkService(networkServiceImpl: NetworkServiceImpl): NetworkService
|
||||
}
|
||||
Reference in New Issue
Block a user