Koin Определение класса не найдено, не удалось создать экземпляр - PullRequest
0 голосов
/ 06 августа 2020

У меня ошибка cra sh, похоже, что Koin не может создать экземпляр из-за чего-то пропущенного, но я понятия не имею, что это может быть. Я пытался найти решение в inte rnet, но безрезультатно. Вы можете помочь мне? Ниже того, что мне удалось написать до сих пор.

Приложение

class App : Application() {

    override fun onCreate() {
        super.onCreate()

        startKoin{
            androidContext(this@App)
            modules(moduleList)
        }
    }
    private val moduleList = listOf(dataModule )+ apiModule +  appModule
}

Что я хочу ввести:


 - class WeatherViewModel(api: WeatherRepository) : BaseViewModel(api)

Другие необходимые объявления:


 - abstract class BaseViewModel(val api: WeatherRepository) : ViewModel()

 - class ApplicationSchedulerProvider : SchedulerProvider

 - class WeatherRepositoryImpl( private val apiService: ApiService,
private val schedulersProvider: SchedulerProvider): WeatherRepository

 - interface WeatherRepository

 - class DataManager

Модули, которые я написал:

val dataModule = module{
    single { DataManager() }
}

val viewModelModule = module {
    viewModel {
        WeatherViewModel(get())
    }
}

val rxModule = module {
     single{ApplicationSchedulerProvider() as SchedulerProvider}
}

val appModule = listOf(viewModelModule, rxModule)

val apiServiceModule = module{
    single{  createWebService<ApiService>(get(), BuildConfig.BASE_URL) }
}
val okHttpModule = module{
    single { createOkHttpClient(get()) }
}

fun createOkHttpClient(dataManager: DataManager): OkHttpClient {
    val httpLoggingInterceptor = HttpLoggingInterceptor()
    httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BASIC
    return OkHttpClient.Builder()
        .addInterceptor(AuthInterceptor(dataManager))
        .connectTimeout(60L, TimeUnit.SECONDS)
        .readTimeout(60L, TimeUnit.SECONDS)
        .addInterceptor(httpLoggingInterceptor).build()
}
val apiModule= listOf(apiServiceModule, okHttpModule)


internal class AuthInterceptor(val dataManager: DataManager) : Interceptor {

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request().newBuilder().addHeader("apiid", BuildConfig.API_KEY).build()
        return chain.proceed(request)
    }
}

inline fun <reified T> createWebService(okHttpClient: OkHttpClient, url: String): T {
    val retrofit = Retrofit.Builder()
        .baseUrl(url)
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create()).build()
    return retrofit.create(T::class.java)
}
...