Mohsen
11/13/2020, 4:42 PMJoost Klitsie
11/13/2020, 6:52 PMJoost Klitsie
11/13/2020, 6:53 PMJoost Klitsie
11/13/2020, 6:53 PMJoost Klitsie
11/13/2020, 6:53 PMromainbsl
11/13/2020, 8:56 PMForecastRepositoryImpl
in ForecastFragment
. do you use some custom context?Mohsen
11/13/2020, 10:39 PMclass ForecastApplication : Application(), KodeinAware {
override val kodein = Kodein.lazy {
import(androidXModule(this@ForecastApplication))
bind() from provider { LocationServices.getFusedLocationProviderClient(instance<Context>()) }
bind<Double>() with singleton { instance<ForecastRepositoryImpl>().latitude; instance<ForecastRepositoryImpl>().longitude }
bind() from singleton { ForecastDatabase(instance()) }
bind() from singleton { instance<ForecastDatabase>().forecastDao() }
bind<ConnectivityInterceptor>() with singleton { ConnectivityInterceptorImpl(instance()) }
bind() from singleton { ForecastApiService(instance()) }
bind<ForecastNetworkDataSource>() with singleton { ForecastNetworkDataSourceImpl(instance()) }
bind<ForecastRepository>() with singleton { ForecastRepositoryImpl(instance(), instance(),instance(), instance()) }
bind() from provider { ForecastViewModelFactory(instance()) }
}
override fun onCreate() {
super.onCreate()
AndroidThreeTen.init(this)
}
}
Mohsen
11/13/2020, 10:40 PMMohsen
11/13/2020, 10:47 PMviewModel = ViewModelProvider(this, viewModelFactory).get(ForecastViewModel::class.java)
This is ForecastViewModel
class:
class ForecastViewModel(
private val forecastRepository: ForecastRepository
) : ViewModel() {
val forecast by lazyDeferred {
forecastRepository.getForecast(10.0, 10.0)
}
}
The `ForecastRepository`:
interface ForecastRepository {
suspend fun getForecast(lat: Double, lon: Double): LiveData<out Forecast>
}
And finally `ForecastRepositoryImpl`:
class ForecastRepositoryImpl(
private val forecastDao: ForecastDao,
private val forecastNetworkDataSource: ForecastNetworkDataSource,
internal val latitude: Double,
internal val longitude: Double
) : ForecastRepository {
---
}
Joost Klitsie
11/14/2020, 9:50 AMbind<Double>() with singleton { instance<ForecastRepositoryImpl>().latitude; instance<ForecastRepositoryImpl>().longitude }
You should use instance<ForecastRepository>
instead of the Impl
one.
With bind<SomeClass>
in other places you can only find the SomeClass
and not the SomeClassImpl
that you were binding it to.Joost Klitsie
11/14/2020, 9:51 AMbind<Double>() with singleton { instance<ForecastRepositoryImpl>().latitude; instance<ForecastRepositoryImpl>().longitude }
Joost Klitsie
11/14/2020, 9:51 AMJoost Klitsie
11/14/2020, 9:51 AMinstance<ForecastRepositoryImpl>().longitude
(because whatever you put before and divided with the ';' is ignored here) Also after that you are trying to inject this double what you get from the ForecastRepositoryImpl into the constructor of the ForecastRepositoryImpl creating a loop. You never even pass a value to this double so how would kodein have to figure out what the latitude and longitude values are?Joost Klitsie
11/14/2020, 9:54 AMJoost Klitsie
11/14/2020, 9:58 AMJoost Klitsie
11/14/2020, 9:59 AMclass ForecastRepositoryImpl(
private val forecastDao: ForecastDao,
private val forecastNetworkDataSource: ForecastNetworkDataSource,
// --> internal val latitude: Double, // <-- not necessary
// --> internal val longitude: Double // <-- not necessary
) : ForecastRepository {
---
}
If you really need these, then either inject them with Named arguments or with a multitonJoost Klitsie
11/14/2020, 9:59 AMMohsen
11/14/2020, 11:56 AMbind<Double>
was because of this latest error:
org.kodein.di.Kodein$NotFoundException: No binding found for bind<Double>() with ? { ? }
,after this error I added that.Mohsen
11/14/2020, 11:59 AMinstance<ForecastRepository>
instead of the Impl
one, because of this syntax error:
Type inference failed. Expected type mismatch:
required:KodeinBinding<in Any?, in Unit, out Double>
found:Singleton<Any?, Nothing?, ForecastRepository>
Joost Klitsie
11/14/2020, 12:01 PMJoost Klitsie
11/14/2020, 12:01 PMJoost Klitsie
11/14/2020, 12:05 PMMohsen
11/14/2020, 12:11 PMMohsen
11/15/2020, 6:17 PMJoost Klitsie
11/15/2020, 6:28 PMvalue
is nullJoost Klitsie
11/15/2020, 6:30 PMfun loadAtCoordinates(latitude: Double, longitude: Double) {
// ...
}
I think in your interface:
interface ForecastRepository {
suspend fun getForecast(lat: Double, lon: Double): LiveData<out Forecast>
}
this is defined, so no need to also pass in the lat and lon into the constructor