Howdy! :wave: I'm working to create user sessions...
# koin
s
Howdy! 👋 I'm working to create user sessions with Koin scopes! My simple proof-of-concept project has a few issues that I'm seeking help with. Thank you for taking a look! 🧵
I'm looking to scope various resources under the idea of a
UserSession
. When a session is closed, all of those resources should be cleaned up. When one is opened, they should be recreated. I believe this is a good way to create the scopes for this purpose:
Copy code
val sessionModule = module {
    single<SessionManager> {
        SessionManager()
    }

    scope<AuthenticatedSession> {
        scopedOf(::AuthenticatedSession)
    }
}
The scopes can be applied to viewmodels and resources like so:
Copy code
val androidModule = module {
    scope<AuthenticatedSession> {
        viewModel<AuthenticatedViewModel> {
            AuthenticatedViewModel(session = get())
        }
    }

    viewModel<UnauthenticatedViewModel> {
        UnauthenticatedViewModel(sessionManager = get())
    }
}
And finally, the session classes themselves:
SessionManager.kt
Copy code
class SessionManager {
    var session: AuthenticatedSession? = null

    fun login() {
        session?.logout()
        session = AuthenticatedSession()
    }

    fun logout() {
        session?.logout()
        session = null
    }
}
AuthenticatedSession.kt
Copy code
class AuthenticatedSession : KoinScopeComponent {
    override val scope: Scope by lazy { createScope(this) }

    val userId = Random.nextInt().toString()
    val logger = getKoin().logger

    init {
        <http://logger.info|logger.info>("AuthenticatedSession created: $userId")
    }

    fun logout() {
        <http://logger.info|logger.info>("AuthenticatedSession close: $userId")
        scope.close()
    }
}
Issue #1 When I call
SessionManager.login()
from a viewmodel, two sessions with different ID's appear to be created.
Copy code
2024-09-13 12:33:20.677  9004-9004  [Koin]                  dev.seri.kointests.android           I  AuthenticatedSession close: -1128270103
2024-09-13 12:33:20.679  9004-9004  [Koin]                  dev.seri.kointests.android           I  AuthenticatedSession created: 1187421452
Issue #2 I can only create a ViewModel from an instance of a Scope. This API appears to be missing a
Scope.koinViewModel
method, similar to the plain
koinViewModel
. When I inject this way, I appear to be recreating several instances of the ViewModel, rather than reusing the same one.
Copy code
authenticatedViewModel: AuthenticatedViewModel = session.get<AuthenticatedViewModel>()
Figured it out! Solution #1 • The duplicate session was being initialized from the
scopedOf(::AuthenticatedSession)
in my sessionModule. This definition is unnecessary, and ended up created a new scope-of-a-scope field. Solution #2 • When using
koinViewModel()
to create a scoped viewModel, simply pass in the scope to resolve from.
I've published my prototype project for managing User Session scopes here! https://github.com/Serisium/KoinSession