Skip to content

Instantly share code, notes, and snippets.

@PatilShreyas
Created Sep 16, 2021
Embed
What would you like to do?
@Singleton
interface SessionManager {
suspend fun getCurrentUser(): User?
suspend fun setUserSession(user: User)
suspend fun clear()
}
@Singleton
class DefaultSessionManager @Inject constructor(
// Provided by Dagger-Hilt injection module
@UserPreferences private val userPreferences: SharedPreferences
) : SessionManager {
override suspend fun setUserSession(user: User) = withContext(Dispatchers.IO) {
userPreferences.edit {
putInt(Keys.ID, user.id)
putString(Keys.NAME, user.name)
putString(Keys.EMAIL, user.email)
}
}
// ...
// OTHER FUNCTION IMPLEMENTATIONS
// ...
/**
* Object holding user preference key details
*/
object UserPreferencesDetails {
const val NAME = "user_pref"
object Keys {
const val ID = "user_id"
const val NAME = "user_name"
const val EMAIL = "user_email"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment