Android 改型与匕首2-机器人

Android 改型与匕首2-机器人,android,retrofit2,dagger-2,Android,Retrofit2,Dagger 2,我使用以下样本: 如何定义我的SQLite连接和改装等。。。在本课程中,AppModule以下是一个示例。希望这会有所帮助 @Module class AppModule(val app: Context) { @Provides @Singleton fun provideContext(): Context = app @Provides @Singleton fun provideDatabase(context: Context): D

我使用以下样本:


如何定义我的
SQLite连接
改装
等。。。在本课程中,
AppModule

以下是一个示例。希望这会有所帮助

@Module
class AppModule(val app: Context) {
    @Provides
    @Singleton
    fun provideContext(): Context = app

    @Provides
    @Singleton
    fun provideDatabase(context: Context): Database = Room.databaseBuilder(context, Database::class.java, "my_db").build()

    @Provides
    @Singleton
    fun provideUserDao(database: Database): UserDao = database.userDao()

    @Provides
    @Singleton
    fun providePostDao(database: Database): PostDao = database.postDao()

    @Provides
    @Singleton
    fun provideCommentsDao(database: Database): CommentDao = database.commentDao()

    @Provides
    @Singleton
    fun provideHttpLogging(): HttpLoggingInterceptor = HttpLoggingInterceptor().apply {
        level = HttpLoggingInterceptor.Level.BODY
    }

    @Provides
    @Singleton
    fun provideOkHttpClient(loggingInterceptor: HttpLoggingInterceptor): OkHttpClient =
        OkHttpClient.Builder().addInterceptor(loggingInterceptor).build()

    @Provides
    @Singleton
    fun provideJackson(): JacksonConverterFactory = JacksonConverterFactory.create()

    @Provides
    @Singleton
    fun provideRetrofit(okHttpClient: OkHttpClient, jackson: JacksonConverterFactory): Retrofit = Retrofit.Builder()
            .client(okHttpClient)
            .baseUrl(baseUrl)
            .addConverterFactory(jackson)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()

    @Provides
    @Singleton
    fun provideApiInterface(retrofit: Retrofit): ApiInterface = retrofit.create(
        ApiInterface::class.java
    )
}