Anvith
06/08/2022, 7:22 AMclass F1 : F{
val b = DepB()
val a = DepA(b)
val c = DepC(b)
}
class F2 : F{
val b = DepB()
val a = DepA(b)
val c = DepC(b)
}
Basically I want factory instances for all except within a particular F<x>
class(i.e F1 or F2) I wish that DepB instance needs to be shared. I want to be able to do this at a super class F
level instead of defining the same scope over and over again for each of the subclasses F1
and F2
.Bogdan Cordier
06/08/2022, 10:40 AMankushg
06/09/2022, 12:30 AMclass Outer(val middle: Middle?)
class Middle(val inner: Inner)
interface Inner
module {
factory { Outer(getOrNull()) }
factoryOf(::Middle)
}
Depending on certain conditions, I dynamically provide an implementation of Inner
into the Koin instance.
Current Behavior:
• when Inner
is dynamically loaded, Koin can inject both Middle
and Outer
.
• when Inner
is not dynamically loaded, Koin fails to inject both Middle
and Outer
Desired behavior:
• When Inner
is not dynamically loaded and Middle
cannot be injected (because Inner
is missing), I would still like to inject Outer
using null
as the parameter value
Does anyone know how to do this? I’m guessing it might be possible with a method that’s similar to getOrNull
?Ugurcan Yildirim
06/13/2022, 8:33 AMUgurcan Yildirim
06/13/2022, 8:56 AMUgurcan Yildirim
06/13/2022, 8:56 AM@Factory
class ****Adapter(private val imageLoader: ImageLoader) :
RecyclerView.Adapter<****Adapter.ItemViewHolder>()
Mini
06/13/2022, 10:26 AMkoinViewModel()
. Any suggestions on how to go about this?Renaud
06/13/2022, 4:37 PM:desktop:jvmMain: Could not resolve io.insert-koin:koin-core:3.2.0.
It looks like a dependency issue but all my other dependencies look fine.
For more context:
In :common:build.gradle.kts
sourceSets {
val commonMain by getting {
dependencies {
...
// Koin
api("io.insert-koin:koin-core:3.2.0")
api("io.insert-koin:koin-android:3.2.0")
}
}
My settings.gradle.kts
:
pluginManagement {
repositories {
google()
gradlePluginPortal()
mavenCentral()
maven(url = "<https://maven.pkg.jetbrains.space/public/p/compose/dev>")
maven(url = "<https://jitpack.io>")
}
plugins {
kotlin("multiplatform") version "1.6.10"
kotlin("android") version "1.6.10"
kotlin("jvm") version "1.6.10"
kotlin("plugin.serialization") version "1.6.20"
id("com.android.application") version "7.0.4"
id("com.android.library") version "7.0.4"
id("org.jetbrains.compose") version "1.1.0"
id("org.jetbrains.dokka") version "1.6.20"
id("com.github.johnrengelman.shadow") version "7.1.2"
}
}
and my root build.gradle.kts
:
allprojects {
repositories {
google()
gradlePluginPortal()
mavenCentral()
maven(url = "<https://maven.pkg.jetbrains.space/public/p/compose/dev>")
maven(url = "<https://jitpack.io>")
}
}
plugins {
kotlin("multiplatform") apply false
kotlin("android") apply false
kotlin("jvm") apply false
kotlin("plugin.serialization") apply false
id("com.android.application") apply false
id("com.android.library") apply false
id("org.jetbrains.compose") apply false
id("org.jetbrains.dokka") apply false
id("com.github.johnrengelman.shadow") apply false
}
I didn’t use buildscript
on purpose.Orhan Tozan
06/14/2022, 4:57 PMMark Malik
06/15/2022, 8:58 AM"Not allow logging in with incorrect api key" {
// given
val payload = JsonObject(
mapOf(
"apikey" to JsonPrimitive("wrong"),
"username" to JsonPrimitive("jamma"),
)
)
// when
testApplication {
application { Application::testModule }
environment {
config = ApplicationConfig("test-application.conf")
}
val client = createClient { install(ContentNegotiation) { json() } }
val response = <http://client.post|client.post>("api/login") {
contentType(ContentType.Application.Json)
setBody(payload)
}
// then
response shouldHaveStatus HttpStatusCode.Unauthorized
}
}
I’m using KoinExtensions to inject the dependencies.
override fun extensions(): List<Extension> =
listOf(KoinExtension(listOf(testDbModule, testHttpModule, RepositoryModule)))
jmfayard
06/20/2022, 2:15 PMLilly
06/22/2022, 2:11 PMgetKoin().declare(MyObject())
.
class MyObject(val source: Map<String, Any> = emptyMap())
Later I redeclare it, but with data: getKoin().declare(MyObject(mapOf("key" to value), allowOverride = true))
My API exposes this object like:
class Api : KoinComponent {
val myObject: MyObject by inject()
}
but when I access it, I get the old version of the object with empty map. Any ideas?
EDIT:
When I call getKoin().get<DeviceContext>()
at the position where I want to access the map, I get the correct object, while api.myObject
is another object. I'm wondering how this can be since by inject is lazy, so actually it should grab the recent oneMark Malik
06/23/2022, 5:44 AMKieran Wallbanks
06/23/2022, 12:02 PMsingle<Element>(createdAtStart = true)
and if this was loaded post-start (i.e. in a later call to loadKoinModules
) this would init the object immediately. However, using the new DSL with singleOf(::Element) { createdAtStart() }
this is no longer the case. What can I do instead?pitpit
06/23/2022, 2:13 PMAlejandro Rios
06/23/2022, 4:54 PM3.1.5
to 3.2.0
, I've changed some declarations from single { }
to singleOf()
and from viewModel { }
to viewModelOf()
but after clean and reun the app I'm getting this error:
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/koin/dsl/ModuleKt;
at com.metamap.sdk_components.di.AppModuleKt.<clinit>(AppModule.kt:26)
at com.metamap.sdk_components.di.AppModuleKt.getApplicationModule(AppModule.kt:26)
at com.metamap.metamap_sdk.SdkStartUpProvider.<init>(SdkStartUpProvider.kt:25)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateProvider(AppComponentFactory.java:121)
at androidx.core.app.CoreComponentFactory.instantiateProvider(CoreComponentFactory.java:67)
at android.app.ActivityThread.installProvider(ActivityThread.java:6380)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:5938)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5853)
at android.app.ActivityThread.access$1100(ActivityThread.java:199)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.koin.dsl.ModuleKt" on path:
any idea of what am I missing?Jay
06/23/2022, 9:30 PMMockProviderRule
but I cannot resolve it in version 3.1.6
. Any idea what might be wrong?Nick
06/23/2022, 10:47 PMjava.lang.IllegalStateException: Compose Runtime internal error. Unexpected or incorrect use of the Compose internal runtime API (Start/end imbalance
I see this ticket here, I was wondering if anyone has a solution for this?
...
Ninja edit: See googles response hereBao Le Duc
06/28/2022, 7:41 AMscope.close()
whenever we don’t need the scope instance? (I’m new to Koin)Slackbot
06/30/2022, 10:08 AMdeviant
06/30/2022, 11:09 AMscoped<DateResolver> { (r: CurrentReservation) -> DateResolverFactory(r) }
scoped<DateResolver> { (pd: ProductDetail) -> DateResolverFactory(pd) }
Partho Paul
06/30/2022, 11:11 AMclass A(b: B(), c: C())
class B(d: D())
class C()
I want to write test cases of A
and I did the following:
class ATest : KoinTest {
@JvmField
@RegisterExtension
val koinTestExtension = KoinTestExtension.create {
val serviceModules = module {
factory { B(get()) }
}
val daoModules = module {
factory { C() }
}
val httpClient = module {
factory { D() }
}
modules(httpClient, daoModules, serviceModules)
}
@JvmField
@RegisterExtension
val mockProvider = MockProviderExtension.create { clazz ->
mockkClass(clazz)
}
@Test
fun doTest(koin: Koin) = runBlocking {
declareMock<B> {
coEvery { something(any()) } returns Either.Right("response")
}
declareMock<C> {
coEvery { fetchDoc(any()) } returns Either.Right(GetItemResponse {
item = mapOf("key" to AttributeValue.S("value"))
})
}
val a = A(
b = koin.get<B>(),
c = koin.get<C>()
)
val resp = a.doSomething(request)
assertEquals(resp.isLeft(), false)
}
}
Is there a more elegant way of mocking `B`and C
and put them inside A
?miqbaldc
07/02/2022, 2:01 AMKoin
requires compatibility (needs same Kotlin
version) with Kotlin
versions to use?
or is it okay to this ways?
e.g: using Kotlin
1.6
, but Koin
already using 1.7
Marcin Wisniowski
07/04/2022, 9:24 PM@Single
classes are ignored by the code generation if they reference BuildConfig
, with no error or warning visible in the build output (other than the inevitable missing dependency when running the app).Lilly
07/07/2022, 4:28 PMscope(named("CONNECTED")) { }
. Every definition should live in this scope. But the definitions are spread across multiple gradle modules. Any guidelines for this kind of use case?Merseyside
07/15/2022, 9:43 AMMobile Dev.
07/16/2022, 11:50 PMfun appModule() = listOf(AppModule().module)
@Module
@ComponentScan("com.makswin.bifrost")
class AppModule
class DependencyInjection : KoinComponent {
val listTrainingsViewModel: ListTrainingsViewModel by inject()
val getLastTrainingViewModel: GetLastTrainingViewModel by inject()
val addFeedbackViewModel: AddFeedbackViewModel by inject()
val insuranceViewModel: InsuranceViewModel by inject()
val authViewModel: AuthViewModel by inject()
}
fun initKoin() {
startKoin {
modules(appModule())
}
}
My App Delegate on ios
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
AppModuleKt.doInitKoin()
return true
}
Here is my observable view model object
class ObservableListTrainingViewModel : ObservableObject {
@Published var state: ListTrainingsState?
var viewModel: ListTrainingsViewModel?
init(viewModel: ListTrainingsViewModel) {
self.viewModel = viewModel
asPublisher(viewModel.state)
.compactMap { $0 }
.receive(on: DispatchQueue.main)
.assign(to: &state)
}
}
Here is my sample swift ui view
struct ContentView : View {
@Environment(\.presentationMode) var presentationMode
@ObservedObject var viewModel: ObservableListTrainingViewModel
init() {
viewModel = ObservableListTrainingViewModel(viewModel: DependencyInjection().listTrainingsViewModel)
}
var body: some View {
VStack {
if let state = viewModel.state {
Button("Button") {
if state.trainings.isEmpty {
self.viewModel.viewModel?.getTrainings(type: .next) // api call
} else {
presentationMode.wrappedValue.dismiss() // close the page
}
}
if state.isLoading {
Text("LOADING")
}
if !state.trainings.isEmpty {
Text("\(state.trainings.first?.title ?? "NONE")")
}
}
}
}
}
and my Common View Model Class
@Single
class ListTrainingsViewModel(val listTrainingsUseCase: ListTrainingsUseCase) : ViewModel() {
private val _state: MutableStateFlow<ListTrainingsState> = MutableStateFlow(ListTrainingsState())
val state: CommonFlow<ListTrainingsState> = _state.asCommonFlow()
fun getTrainings(type: TrainingListTimeType) {
listTrainingsUseCase(type).collectCommon { result ->
when (result) {
is Resource.Error -> {
_state.value = ListTrainingsState(error = result.message ?: "")
}
is Resource.Loading -> {
_state.value = ListTrainingsState(isLoading = true)
}
is Resource.Success -> {
_state.value = ListTrainingsState(
trainings = result.data ?: emptyList(),
isLoading = false
)
}
}
}
}
}
fun <T> Flow<T>.collectCommon(
coroutineScope: CoroutineScope? = null,
callback: (T) -> Unit,
): Job {
return onEach {
callback(it)
}.launchIn(coroutineScope ?: CoroutineScope(Dispatchers.Main))
}
How can i solve this issue ?Vivek Modi
07/19/2022, 10:55 AMviewModelOf
package com.vivek.sportsresult.di
import org.koin.androidx.viewmodel.dsl.viewModelOf
import org.koin.dsl.module
val appModule = module {
viewModelOf(::MainActivityViewModel)
}
Can someone how can I fix this error.arnaud.giuliani
07/21/2022, 9:40 AMcarloxavier
07/21/2022, 10:51 AMVariant 'apiElements' capability io.insert-koin:koin-test-junit4:3.2.0 declares an API of a library, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm':
- Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8
- Variant 'runtimeElements' capability io.insert-koin:koin-test-junit4:3.2.0 declares a runtime of a library, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm':
- Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8
I didn't find any official info about this change of source compatibility, any hint about this would be greatly appreciated, thankscarloxavier
07/21/2022, 10:51 AMVariant 'apiElements' capability io.insert-koin:koin-test-junit4:3.2.0 declares an API of a library, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm':
- Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8
- Variant 'runtimeElements' capability io.insert-koin:koin-test-junit4:3.2.0 declares a runtime of a library, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm':
- Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8
I didn't find any official info about this change of source compatibility, any hint about this would be greatly appreciated, thanks