Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudgiuliani committed Aug 30, 2024
1 parent 975dba0 commit c0a19a0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 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>

0 comments on commit c0a19a0

Please sign in to comment.