-
-
Notifications
You must be signed in to change notification settings - Fork 694
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add viewmodel get key to koin, add add KoinViewModelLazy class
- Loading branch information
1 parent
c954e53
commit 887df2d
Showing
11 changed files
with
99 additions
and
39 deletions.
There are no files selected for viewing
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
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
37 changes: 37 additions & 0 deletions
37
android/koin-android/src/main/java/org/koin/androidx/viewmodel/KoinViewModelLazy.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,37 @@ | ||
package org.koin.androidx.viewmodel | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.ViewModelStore | ||
import kotlin.reflect.KClass | ||
|
||
class KoinViewModelLazy<VM : ViewModel> ( | ||
private val viewModelClass: KClass<VM>, | ||
private val viewModelKey: String? = null, | ||
private val storeProducer: () -> ViewModelStore, | ||
private val factoryProducer: () -> ViewModelProvider.Factory, | ||
) : Lazy<VM> { | ||
private var cached: VM? = null | ||
|
||
override val value: VM | ||
get() { | ||
val viewModel = cached | ||
return if (viewModel == null) { | ||
val factory = factoryProducer() | ||
val store = storeProducer() | ||
if (viewModelKey != null) { | ||
ViewModelProvider(store, factory)[viewModelKey, viewModelClass.java].also { | ||
cached = it | ||
} | ||
} else { | ||
ViewModelProvider(store, factory)[viewModelClass.java].also { | ||
cached = it | ||
} | ||
} | ||
} else { | ||
viewModel | ||
} | ||
} | ||
|
||
override fun isInitialized(): Boolean = cached != null | ||
} |
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
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
Oops, something went wrong.