Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Fragment extension functions #768

Merged
merged 3 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ _Core_
* `[FIXED]` - declare to use reified type
* `[FIXED]` - Qualifier type as pure string
* `[FIXED]` - docs contribution
* `[UPDATED]` - Kotlin 1.3.71


_Ktor_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import org.junit.Assert.*
import org.koin.android.ext.android.getKoin
import org.koin.androidx.scope.lifecycleScope
import org.koin.androidx.viewmodel.ext.android.sharedViewModel
import org.koin.androidx.viewmodel.ext.android.stateSharedViewModel
import org.koin.androidx.viewmodel.ext.android.stateViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.koin.core.parameter.parametersOf
import org.koin.sample.android.R
Expand All @@ -22,7 +24,9 @@ class MVVMFragment(val session: Session) : Fragment() {
val shared: SimpleViewModel by sharedViewModel { parametersOf(ID) }
val simpleViewModel: SimpleViewModel by viewModel { parametersOf(ID) }

val saved by stateViewModel<SavedStateViewModel> { parametersOf(ID) }
val sharedSaved: SavedStateViewModel by sharedViewModel { parametersOf(ID) }
val sharedSaved2 by stateSharedViewModel<SavedStateViewModel> { parametersOf(ID) }

override fun onCreateView(
inflater: LayoutInflater,
Expand All @@ -38,10 +42,15 @@ class MVVMFragment(val session: Session) : Fragment() {
assertNotNull(session)

assertNotEquals(shared, simpleViewModel)
assertNotEquals((requireActivity() as MVVMActivity).savedVm, saved)

assertEquals((activity as MVVMActivity).simpleViewModel, shared)
assertEquals((activity as MVVMActivity).savedVm, sharedSaved)
assertEquals((requireActivity() as MVVMActivity).simpleViewModel, shared)
assertEquals((requireActivity() as MVVMActivity).savedVm, sharedSaved)
assertEquals((requireActivity() as MVVMActivity).savedVm, sharedSaved)
assertEquals((requireActivity() as MVVMActivity).savedVm, sharedSaved)
assertEquals((requireActivity() as MVVMActivity).savedVm, sharedSaved2)
assertEquals(sharedSaved, sharedSaved2)

assertEquals(activity!!.lifecycleScope.get<Session>(), getKoin().getProperty("session"))
assertEquals(requireActivity().lifecycleScope.get<Session>(), getKoin().getProperty("session"))
}
}
2 changes: 1 addition & 1 deletion koin-projects/gradle/versions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ext {
koin_version = '2.1.5'

// Kotlin
kotlin_version = '1.3.70'
kotlin_version = '1.3.71'
coroutines_version = "1.3.3"

// Dokka
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
*/
package org.koin.androidx.viewmodel.ext.android

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import org.koin.android.ext.android.getKoin
import org.koin.androidx.viewmodel.koin.getStateViewModel
import org.koin.androidx.viewmodel.koin.getViewModel
import org.koin.core.parameter.ParametersDefinition
import org.koin.core.qualifier.Qualifier
Expand Down Expand Up @@ -60,4 +62,39 @@ fun <T : ViewModel> Fragment.getSharedViewModel(
qualifier,
parameters
)
}

fun <T : ViewModel> Fragment.stateSharedViewModel(
clazz: KClass<T>,
qualifier: Qualifier? = null,
bundle: Bundle? = null,
parameters: ParametersDefinition? = null
): Lazy<T> {
return lazy(LazyThreadSafetyMode.NONE) { getStateSharedViewModel(clazz, qualifier, bundle, parameters) }
}

inline fun <reified T : ViewModel> Fragment.stateSharedViewModel(
qualifier: Qualifier? = null,
bundle: Bundle? = null,
noinline parameters: ParametersDefinition? = null
): Lazy<T> {
return lazy(LazyThreadSafetyMode.NONE) { getStateSharedViewModel(T::class, qualifier, bundle, parameters) }
}

inline fun <reified T : ViewModel> Fragment.getStateSharedViewModel(
qualifier: Qualifier? = null,
bundle: Bundle? = null,
noinline parameters: ParametersDefinition? = null
): T {
return getStateViewModel(T::class, qualifier, bundle, parameters)
}

fun <T : ViewModel> Fragment.getStateSharedViewModel(
clazz: KClass<T>,
qualifier: Qualifier? = null,
bundle: Bundle? = null,
parameters: ParametersDefinition? = null
): T {
val bundleOrDefault: Bundle = bundle ?: Bundle()
return getKoin().getStateViewModel(requireActivity(), clazz, qualifier, bundleOrDefault, parameters)
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,5 @@ fun Application.koin(configuration: KoinAppDeclaration) = featureOrNull(Koin)?.a
*
* Gets or installs a [Koin] feature for the this [Application] and install a [block] modules on it
*/
@KoinExperimentalAPI
@ContextDsl
fun Application.modules(vararg block: Module) = koin { modules(block.asList()) }