-
-
Notifications
You must be signed in to change notification settings - Fork 729
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add lazyViewModelForClass to get ViewModel lazily in custom classes
- Loading branch information
Showing
5 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...droidx-samples/src/main/java/org/koin/sample/sandbox/components/main/SimpleServiceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package org.koin.sample.sandbox.components.main | ||
|
||
class SimpleServiceImpl() : SimpleService { | ||
class SimpleServiceImpl : SimpleService { | ||
override val id: String = SERVICE_IMPL | ||
} | ||
const val SERVICE_IMPL = "DefaultService" |
22 changes: 22 additions & 0 deletions
22
examples/androidx-samples/src/main/java/org/koin/sample/sandbox/main/MainCustomView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.koin.sample.sandbox.main | ||
|
||
import android.content.Context | ||
import android.util.AttributeSet | ||
import androidx.appcompat.widget.AppCompatTextView | ||
import androidx.lifecycle.findViewTreeViewModelStoreOwner | ||
import org.koin.androidx.viewmodel.ext.android.lazyViewModelForClass | ||
import org.koin.sample.sandbox.components.mvvm.SimpleViewModel | ||
|
||
class MainCustomView( | ||
context: Context, | ||
attrs: AttributeSet? = null, | ||
) : AppCompatTextView(context, attrs) { | ||
|
||
private val viewModel: SimpleViewModel by lazyViewModelForClass(viewModelStoreOwnerLazy = { findViewTreeViewModelStoreOwner()!! }) | ||
|
||
override fun onAttachedToWindow() { | ||
super.onAttachedToWindow() | ||
|
||
text = viewModel.id | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...in-android/src/test/java/org/koin/test/android/viewmodel/ext/android/ViewModelLazyTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.koin.test.android.viewmodel.ext.android | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelStoreOwner | ||
import io.mockk.mockk | ||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Test | ||
import org.koin.androidx.viewmodel.dsl.viewModelOf | ||
import org.koin.androidx.viewmodel.ext.android.lazyViewModelForClass | ||
import org.koin.core.context.startKoin | ||
import org.koin.dsl.module | ||
|
||
class ViewModelLazyTest { | ||
@Test | ||
fun `lazyViewModelForClass shouldn't resolve ViewModelStoreOwner immediately`() { | ||
// Given | ||
startKoin { | ||
modules( | ||
module { | ||
viewModelOf(::FooViewModel) | ||
} | ||
) | ||
} | ||
|
||
var resolved = false | ||
val viewModelStoreOwnerResolver: () -> ViewModelStoreOwner = { | ||
resolved = true | ||
mockk(relaxed = true) | ||
} | ||
|
||
// When | ||
val lazy: Lazy<FooViewModel> = lazyViewModelForClass(viewModelStoreOwnerLazy = viewModelStoreOwnerResolver) | ||
|
||
// Then | ||
assertFalse(resolved) | ||
lazy.value | ||
assertTrue(resolved) | ||
} | ||
|
||
private class FooViewModel : ViewModel() | ||
} |