Jakub Syty
05/08/2023, 8:30 AMDaniel
05/09/2023, 8:19 AM"You can also start it from outside of Ktor, but you won't be compatible with autoreload feature"
. I am not using the autoreload feature and would like to instanciate koin earlier (I have some command line parsing and setup logic before I launch Ktor itself). What function should I use so that the Koin features are still available later in the ktor app?Marco Gomiero
05/10/2023, 1:30 PMval myModule = module {
scope(named(scopeName)) {
scoped { Foo() }
}
viewModel {
ViewModel(
regularDeps = get(),
foo = getScope(scopeName).get(),
)
}
}
Lucas
05/12/2023, 4:28 PMfun ScreenOne(){
ScreenOne()
ScreenTwo()
}
fun SubScreenOne(){
val viewModel : MyViewModel by inject() //same instance
}
fun SubScreenTwo(){
val viewModel : MyViewModel by inject() // same instance
}
modules{
factory{ MyViewModel() }
}
I walso wouldnt want to pass it through parameters, if possibleDavid Glasser
05/12/2023, 6:49 PMnamed("whatever")
as a top-level variable from a file, but it would be nice if i could like, add one to a module so that a user of a module could pull it off, like
startKoin {
modules(listOf(
someModule,
module {
single(someModule.thatParticularThing) { MakeThing() }
}))
}
André Tessmer
05/16/2023, 6:25 PMBaseFragment
implementation. Let me know if you have any ideas on how to work around these issues, I would really appreciate itspechard
05/17/2023, 4:15 PMVlad Makarenko
05/18/2023, 10:26 AMparametersOf
function, it crashes on startup. The crash displays the following message:
Caused by: org.koin.core.error.DefinitionParameterException: No value found for type 'com.example.example.example.SamplePojo'.
Pedro Francisco de Sousa Neto
05/18/2023, 2:23 PMLucas
05/19/2023, 8:40 PM@Composable
fun Composable(
) {
val coroutineScope = rememberCoroutineScope()
val viewModel: ViewModel = koinInject { parametersOf(coroutineScope) }
}
koinInject has a remember so i dont understand why it doesnt work
@Composable
inline fun <reified T> koinInject(
qualifier: Qualifier? = null,
scope: Scope = LocalKoinScope.current,
noinline parameters: ParametersDefinition? = null,
): T = rememberKoinInject(qualifier, scope, parameters)
/**
* alias of koinInject()
*
* @see koinInject
*
* @author Arnaud Giuliani
*/
@Composable
inline fun <reified T> rememberKoinInject(
qualifier: Qualifier? = null,
scope: Scope = LocalKoinScope.current,
noinline parameters: ParametersDefinition? = null,
): T = remember(qualifier, scope, parameters) {
scope.get(qualifier, parameters)
}
Slack ConversationNish Patel
05/27/2023, 4:10 AMoverride fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
startKoin {
androidContext(this@MainActivity)
modules(appModule)
}
setContent {
Navigation()
}
}
}
val appModule = module {
single { AppwriteClient(androidContext()) }
single<SharedPreferences> {
androidContext().getSharedPreferences("my_app_prefs", Context.MODE_PRIVATE)
}
viewModel { AuthViewModel(get(), get()) }
}
Jerry Preissler
05/28/2023, 10:08 AMclass DokumentMemoryPersistenceAdapter: LoadDokumentPort, SaveDokumentPort {
....
}
a Koin module
val persistenceKoinModule = module(createdAtStart = true) {
singleOf(::DokumentMemoryPersistenceAdapter) {
bind<LoadDokumentPort>()
bind<SaveDokumentPort>()
}
}
and a test
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class DokumentPersistenceAdapterTest : KoinTest {
private val saveDokumentPort by inject<SaveDokumentPort>()
private val loadDokumentPort by inject<LoadDokumentPort>()
@Test
fun createAndLoadOkTest() {
println("*****************************************")
println(saveDokumentPort)
println(loadDokumentPort)
/*.....
/*save a new Dokument via saveDokumentPort, reload via loadDokumentPort
/*run assertions
}
}
If I run only the test createAndLoadOkTest, everything works fine and I can see in stdout that both ports are provided by the same instance:
org.codeshards.aktenordner.adapter.outgoing.persistence.DokumentMemoryPersistenceAdapter@25b2cfcb
org.codeshards.aktenordner.adapter.outgoing.persistence.DokumentMemoryPersistenceAdapter@25b2cfcb
But if I run alll tests within the class, the tests fails and I can see that the ports are provided by two instances instead:
org.codeshards.aktenordner.adapter.outgoing.persistence.DokumentMemoryPersistenceAdapter@60dce7ea
org.codeshards.aktenordner.adapter.outgoing.persistence.DokumentMemoryPersistenceAdapter@662f5666
I'm not really familiar with Koin yet, but my understanding is that the declaration _singleOf_(::DokumentMemoryPersistenceAdapter)
ensures that the component is a singleton. Is this wrong?Jerry Preissler
05/30/2023, 6:56 PMimport org.koin.dsl.module
class Presenter(val a : String, val b : String)
val presenterModule = module {
single { params -> Presenter(a = params.get(), b = params.get()) }
}
and
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.extension.RegisterExtension
import org.koin.core.component.inject
import org.koin.core.parameter.parametersOf
import org.koin.test.KoinTest
import org.koin.test.junit5.KoinTestExtension
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class KoinExploration : KoinTest {
@JvmField
@RegisterExtension
@Suppress("unused")
val koinTestExtension = KoinTestExtension.create {
modules(
presenterModule
)
}
@Test
fun parameterTest() {
val a = "bar"
val b = "foo"
val presenter : Presenter by inject { parametersOf(a, b) }
println("Result: ${presenter.a} ${presenter.b}")
}
}
I expected to get "Result: bar foo" but instead I got "Result: bar bar".
Can anyone shed some light here?Merseyside
05/31/2023, 4:07 AMyoussef
05/31/2023, 10:09 AMAhmad Hassan
05/31/2023, 4:55 PMactual fun Scope.createDriver(): SqlDriver {
val databaseName = "test.db"
val fileManager = NSFileManager.defaultManager
val documentsDirectory = fileManager.URLsForDirectory(NSDocumentDirectory, inDomains = NSUserDomainMask).first() as NSURL
val destinationURL = documentsDirectory.URLByAppendingPathComponent(databaseName)
val bundlePath = MR.assets.balagh_ul_quran.url
if (!fileManager.fileExistsAtPath(destinationURL?.path.toString())) {
val response = try {
if (destinationURL != null) {
fileManager.copyItemAtURL(bundlePath, toURL = destinationURL, error = null)
} else {
}
} catch (e: Exception) {
showLog("catch:${e.message}")
}
println("myLog: $response")
}
return NativeSqliteDriver(NoteDatabase.Schema, destinationURL?.path.toString())
}
But the app is crashing.
Logs are inside the thread:Jerry Preissler
06/01/2023, 7:54 PMclass DokumentMemoryPersistenceAdapter
(entries: List<Dokument>) : LoadDokumentPort, SaveDokumentPort
and the module
val persistenceKoinTestModule = module(createdAtStart = true) {
single {
(entries: List<Dokument>) ->
DokumentMemoryPersistenceAdapter(entries = get { parametersOf(entries) })
} withOptions {
bind<LoadDokumentPort>()
bind<SaveDokumentPort>()
}
}
In my test I do the following
@BeforeEach
fun beforeEach() {
println("BeforeEach")
val entries = emptyList<Dokument>()
val persistenceAdapter: DokumentMemoryPersistenceAdapter by inject { parametersOf(entries) }
}
@Test
fun loadDokumentOkTest() {
val loadDokumentPort by inject<LoadDokumentPort>()
but as a result I get a message "could not create instance for DokumentMemoryPersistenceAdapter" with a root cause
Caused by: org.koin.core.error.NoParameterFoundException: Can't get injected parameter #0 from DefinitionParameters[] for type 'java.util.List'
at app//org.koin.core.parameter.ParametersHolder.elementAt(ParametersHolder.kt:41)
at app//org.codeshards.aktenordner.adapter.outgoing.persistence.PersistenceKoinTestModuleKt$persistenceKoinTestModule$1$1.invoke(PersistenceKoinTestModule.kt:22)
at app//org.codeshards.aktenordner.adapter.outgoing.persistence.PersistenceKoinTestModuleKt$persistenceKoinTestModule$1$1.invoke(PersistenceKoinTestModule.kt:14)
at app//org.koin.core.instance.InstanceFactory.create(InstanceFactory.kt:51)
... 81 more
Lines 14 - 22 of my module happen to be the singleton declaration.
I also tried to supply the parameter for the injected interfaces along the following sample
val saveDokumentPort: SaveDokumentPort by inject { parametersOf(entries) }
and got the same results.
A similar test without a bind option works
class ListPresenter(val a: List<Dokument>)
val presenterModule = module {
single {
(a: List<Dokument>) -> ListPresenter(a = get { parametersOf(a) })
}
}
...
@Test
fun listTest() {
val a = emptyList<Dokument>()
val listPresenter: ListPresenter by inject { parametersOf(a) }
so I guess the bind option is the problem.
Any feedback would be appreciated.AmrJyniat
06/02/2023, 12:46 PMscope<Activity>{}
in android, but I couldn’t use it from the shared android codeNorbi
06/05/2023, 8:38 PMsingle {}
declarations with the same bound type will result in an override, and when will be both available for getAll()
? Thanks.Eugen Martynov
06/06/2023, 12:08 PMEugen Martynov
06/06/2023, 12:08 PMEugen Martynov
06/06/2023, 12:10 PMChristopher Mederos
06/08/2023, 2:54 AMfun provideKtorClient() = HttpClient(OkHttp) {
install(ContentNegotiation) {
json()
}
}
val ktorModule = module {
single { provideKtorClient() }
}
A simple test fails with the error -
@Test
fun checkKtorModule() {
ktorModule.verify()
}
org.koin.test.verify.MissingKoinDefinitionException: Missing type 'io.ktor.client.engine.HttpClientEngine' for class 'io.ktor.client.HttpClient' in definition '[Singleton:'io.ktor.client.HttpClient']'Austin
06/12/2023, 1:54 AMTobias Preuss
06/13/2023, 7:21 AMkoin-android:3.3.0
:
singleOf<AndroidPreferences> { AndroidPreferencesImpl(GlobalScope) }
When I update to koin-android:3.4.1
this becomes a compilation error. What is the recommended migration path here?
AndroidPreferences
is an interface
, AndroidPreferencesImpl
is a class
Merseyside
06/14/2023, 1:15 PMTran An
06/16/2023, 5:06 AMMerseyside
06/26/2023, 12:19 PMMartin Sloup
06/27/2023, 10:17 AMnull
parameter value can't be retrieved with get()
. I created issue for that.Christopher Mederos
06/30/2023, 2:38 AMnavigation-compose
?
In this pattern the navigation graph is defined in a NavHost
composable that directs to other compostables (which are each a top-level screen). Accordingly, I have a view model for each screen. However, I would like to access some view model functions like "save" from the top app bar. The top app bar is defined in a scaffold, so it doesn't have access to the view model that I'm injecting as a parameter in the screen-level composable.
I'm thinking that I should define a custom scope that achieves something similar to koin's built in _by_ koinNavGraphViewModel()
?