Skip to content

Commit

Permalink
Merge pull request #1101 from ZuiRenA/master
Browse files Browse the repository at this point in the history
Fix for reuse fragment scope
  • Loading branch information
arnaudgiuliani authored Jun 10, 2021
2 parents 6c63f8d + 943b9dd commit 96a45b0
Showing 1 changed file with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import kotlin.reflect.KProperty

class LifecycleScopeDelegate(
val lifecycleOwner: LifecycleOwner,
koinContext: KoinContext = GlobalContext,
createScope: (Koin) -> Scope = { koin: Koin -> koin.createScope(lifecycleOwner.getScopeId(), lifecycleOwner.getScopeName()) },
private val koinContext: KoinContext = GlobalContext,
private val createScope: (Koin) -> Scope = { koin: Koin -> koin.createScope(lifecycleOwner.getScopeId(), lifecycleOwner.getScopeName()) },
) : ReadOnlyProperty<LifecycleOwner, Scope> {

private var _scope: Scope? = null
Expand All @@ -43,6 +43,16 @@ class LifecycleScopeDelegate(
}

override fun getValue(thisRef: LifecycleOwner, property: KProperty<*>): Scope {
return _scope ?: error("can't get Scope for $lifecycleOwner")
if (_scope != null) return _scope!!

val ownerState = thisRef.lifecycle.currentState
val ownerIsActive = ownerState.isAtLeast(Lifecycle.State.CREATED)
if (!ownerIsActive) {
error("can't get Scope for $lifecycleOwner")
}

val koin = koinContext.get()
_scope = koin.getScopeOrNull(thisRef.getScopeId()) ?: createScope(koin)
return _scope!!
}
}

0 comments on commit 96a45b0

Please sign in to comment.