Skip to content

Commit

Permalink
Merge pull request #1961 from InsertKoinIO/retained_scope_id_fix
Browse files Browse the repository at this point in the history
Retained scope id fix
  • Loading branch information
arnaudgiuliani committed Aug 30, 2024
2 parents 7df9e16 + c0a19a0 commit 46ab29b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.koin.sample.sandbox.mvvm.MVVMFragment
import org.koin.sample.sandbox.navigation.NavViewModel
import org.koin.sample.sandbox.navigation.NavViewModel2
import org.koin.sample.sandbox.scope.ScopedActivityA
import org.koin.sample.sandbox.scope.ScopedFragment
import org.koin.sample.sandbox.workmanager.SimpleWorker
import org.koin.sample.sandbox.workmanager.SimpleWorkerService

Expand Down Expand Up @@ -106,9 +107,13 @@ val scopeModule = lazyModule {

val scopeModuleActivityA = lazyModule {
scope<ScopedActivityA> {
fragmentOf(::ScopedFragment)
scopedOf(::Session)
scopedOf(::SessionActivity)
}
scope<ScopedFragment> {

}
}

val workerServiceModule = lazyModule {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import android.widget.Button
import org.koin.android.ext.android.get
import org.koin.android.ext.android.getKoin
import org.koin.android.ext.android.inject
import org.koin.androidx.fragment.android.replace
import org.koin.androidx.scope.RetainedScopeActivity
import org.koin.core.qualifier.named
import org.koin.sample.sandbox.R
import org.koin.sample.sandbox.components.*
import org.koin.sample.sandbox.components.scope.Session
import org.koin.sample.sandbox.mvvm.MVVMFragment
import org.koin.sample.sandbox.utils.navigateTo

class ScopedActivityA : RetainedScopeActivity(R.layout.scoped_activity_a) {
Expand All @@ -20,6 +22,10 @@ class ScopedActivityA : RetainedScopeActivity(R.layout.scoped_activity_a) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

supportFragmentManager.beginTransaction()
.replace<ScopedFragment>(R.id.mvvm_frame)
.commit()

assert(currentSession == get<Session>())
if (SESSION_ID_VAR.isEmpty()) {
println("Create ID for session: $SESSION_ID_VAR")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.koin.sample.sandbox.scope

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import org.koin.android.scope.AndroidScopeComponent
import org.koin.androidx.scope.fragmentScope
import org.koin.androidx.scope.getRetainedScopeOrNull
import org.koin.core.scope.Scope
import org.koin.sample.sandbox.R
import org.koin.sample.sandbox.components.scope.Session

class ScopedFragment : Fragment(R.layout.mvvm_fragment), AndroidScopeComponent {

override val scope: Scope by fragmentScope()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

checks()
}

private fun checks() {
assert(
scope.get<Session>() == requireActivity().getRetainedScopeOrNull()?.get<Session>()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@
android:layout_height="wrap_content"
android:text="Next" />

<FrameLayout
android:id="@+id/mvvm_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.koin.core.component.getScopeId
import org.koin.core.component.getScopeName
import org.koin.core.scope.Scope
import org.koin.core.scope.ScopeCallback
import org.koin.ext.getFullName

/**
* Provide Koin Scope tied to ComponentActivity
Expand Down Expand Up @@ -84,13 +85,14 @@ fun ComponentActivity.createActivityRetainedScope(): Scope {
if (this !is AndroidScopeComponent) {
error("Activity should implement AndroidScopeComponent")
}
// if (this.scope != null) {
// error("Activity Scope is already created")
// }

val scopeViewModel = viewModels<ScopeHandlerViewModel>().value
if (scopeViewModel.scope == null) {
val scope = getKoin().createScope(getScopeId(), getScopeName())
val scope = getKoin().createScope(retainedScopeId(), getScopeName())
scopeViewModel.scope = scope
}
return scopeViewModel.scope!!
}
}

fun ComponentActivity.retainedScopeId() : String = this::class.getFullName()
fun ComponentActivity.getRetainedScopeOrNull(): Scope? = getKoin().getScopeOrNull(retainedScopeId())
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import androidx.fragment.app.Fragment
import org.koin.android.ext.android.getKoin
import org.koin.android.scope.AndroidScopeComponent
import org.koin.core.component.getScopeId
import org.koin.core.component.getScopeName
import org.koin.core.scope.Scope

/**
Expand All @@ -33,11 +32,11 @@ fun Fragment.createFragmentScope(useParentActivityScope : Boolean = true): Scope
}
val scope = getKoin().getScopeOrNull(getScopeId()) ?: createScopeForCurrentLifecycle(this)
if (useParentActivityScope){
val activityScope = requireActivity().getScopeOrNull()
val activityScope = requireActivity().getScopeOrNull() ?: requireActivity().getRetainedScopeOrNull()
if (activityScope != null) {
scope.linkTo(activityScope)
} else {
scope.logger.debug("Fragment '$this' can't be linked to parent activity scope")
scope.logger.debug("Fragment '$this' can't be linked to parent activity scope. No Parent Scope found.")
}
}
return scope
Expand Down

0 comments on commit 46ab29b

Please sign in to comment.