Skip to content

Commit

Permalink
Fix Activity Retained Scope id + attached fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudgiuliani committed Nov 28, 2024
1 parent d009593 commit df15529
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ 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()) {
if (SESSION_ID_VAR.isEmpty() || SESSION_ID_VAR.first() == "") {
if (SESSION_ID_VAR.isNotEmpty()){
SESSION_ID_VAR.removeFirst()
}
println("Create ID for session: $SESSION_ID_VAR")
SESSION_ID_VAR = currentSession.id
SESSION_ID_VAR.addFirst(currentSession.id)
}
assert(SESSION_ID_VAR == currentSession.id)
assert(SESSION_ID_VAR.first() == currentSession.id)

// Conpare different scope instances
val scopeSession1 = getKoin().createScope(SESSION_1, named(SCOPE_ID))
Expand All @@ -52,6 +51,11 @@ class ScopedActivityA : RetainedScopeActivity(R.layout.scoped_activity_a) {

title = "Scope Activity A"

findViewById<Button>(R.id.scoped_a_restart_button).setOnClickListener {
SESSION_ID_VAR.addFirst("")
navigateTo<ScopedActivityA>(isRoot = false)
}

findViewById<Button>(R.id.scoped_a_button).setOnClickListener {
navigateTo<ScopedActivityB>(isRoot = true)
}
Expand All @@ -60,7 +64,13 @@ class ScopedActivityA : RetainedScopeActivity(R.layout.scoped_activity_a) {
scopeSession2.close()
}

override fun onBackPressed() {
super.onBackPressed()

SESSION_ID_VAR.removeFirst()
}

companion object {
var SESSION_ID_VAR = ""
var SESSION_ID_VAR = ArrayDeque<String>()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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
Expand All @@ -22,7 +21,7 @@ class ScopedFragment : Fragment(R.layout.mvvm_fragment), AndroidScopeComponent {

private fun checks() {
assert(
scope.get<Session>() == requireActivity().getRetainedScopeOrNull()?.get<Session>()
scope.get<Session>() == (requireActivity() as AndroidScopeComponent).scope.get<Session>()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
android:gravity="center"
android:text="Scoped A completed!" />

<Button
android:id="@+id/scoped_a_restart_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Restart" />

<Button
android:id="@+id/scoped_a_button"
android:layout_width="wrap_content"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,8 @@ fun ComponentActivity.createActivityRetainedScope(): Scope {

val scopeViewModel = viewModels<ScopeHandlerViewModel>().value
if (scopeViewModel.scope == null) {
val scope = getKoin().createScope(retainedScopeId(), getScopeName())
val scope = getKoin().createScope(getScopeId(), 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 @@ -32,11 +32,11 @@ fun Fragment.createFragmentScope(useParentActivityScope : Boolean = true): Scope
}
val scope = getKoin().getScopeOrNull(getScopeId()) ?: createScopeForCurrentLifecycle(this)
if (useParentActivityScope){
val activityScope = requireActivity().getScopeOrNull() ?: requireActivity().getRetainedScopeOrNull()
val activityScope: Scope? = (activity as? AndroidScopeComponent)?.scope
if (activityScope != null) {
scope.linkTo(activityScope)
} else {
scope.logger.debug("Fragment '$this' can't be linked to parent activity scope. No Parent Scope found.")
scope.logger.debug("Fragment '$this' can't be linked to parent activity scope. No Parent Activity Scope found.")
}
}
return scope
Expand Down

0 comments on commit df15529

Please sign in to comment.