Android – Source architecture – How to create Shared Preference repository?

Source structure

app\
—— java\
————- [package name]\
———————- AppApplication.kt
———————- repository\
——————————- UserPreferenceRepository.kt
——————————- RepositoryModule.kt

Create “RepositoryModule.kt”

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import android.content.Context
import org.koin.android.ext.koin.androidApplication
import org.koin.dsl.module.module
val repositoryModule = module {
single { UserPreferenceRepository() }
single("userPreferences") { androidApplication().getSharedPreferences("userPreferences", Context.MODE_PRIVATE) }
}
import android.content.Context import org.koin.android.ext.koin.androidApplication import org.koin.dsl.module.module val repositoryModule = module { single { UserPreferenceRepository() } single("userPreferences") { androidApplication().getSharedPreferences("userPreferences", Context.MODE_PRIVATE) } }
import android.content.Context
import org.koin.android.ext.koin.androidApplication
import org.koin.dsl.module.module

val repositoryModule = module {
    single { UserPreferenceRepository() }

    single("userPreferences") { androidApplication().getSharedPreferences("userPreferences", Context.MODE_PRIVATE) }
}

Create “UserPreferenceRepository.kt”

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import android.content.SharedPreferences
import org.koin.standalone.KoinComponent
import org.koin.standalone.inject
class UserPreferenceRepository : KoinComponent {
private val preferences: SharedPreferences by inject("userPreferences")
companion object {
private const val LOGGED_IN_USER_NAME = "user_name"
private const val IS_ADMIN_USER_ = "is_admin_user"
}
var loggedInUserName: String
get() = preferences.getString(LOGGED_IN_USER_NAME, "") ?: ""
set(userName) = preferences.edit().putString(LOGGED_IN_USER_NAME, userName).apply()
var isAdminUser: Boolean
get() = preferences.getBoolean(IS_ADMIN_USER_, false)
set(isAdmin) = preferences.edit().putBoolean(IS_ADMIN_USER_, isAdmin).apply()
}
import android.content.SharedPreferences import org.koin.standalone.KoinComponent import org.koin.standalone.inject class UserPreferenceRepository : KoinComponent { private val preferences: SharedPreferences by inject("userPreferences") companion object { private const val LOGGED_IN_USER_NAME = "user_name" private const val IS_ADMIN_USER_ = "is_admin_user" } var loggedInUserName: String get() = preferences.getString(LOGGED_IN_USER_NAME, "") ?: "" set(userName) = preferences.edit().putString(LOGGED_IN_USER_NAME, userName).apply() var isAdminUser: Boolean get() = preferences.getBoolean(IS_ADMIN_USER_, false) set(isAdmin) = preferences.edit().putBoolean(IS_ADMIN_USER_, isAdmin).apply() }
import android.content.SharedPreferences
import org.koin.standalone.KoinComponent
import org.koin.standalone.inject

class UserPreferenceRepository : KoinComponent {

    private val preferences: SharedPreferences by inject("userPreferences")

    companion object {
        private const val LOGGED_IN_USER_NAME = "user_name"
        private const val IS_ADMIN_USER_ = "is_admin_user"
    }

    var loggedInUserName: String
        get() = preferences.getString(LOGGED_IN_USER_NAME, "") ?: ""
        set(userName) = preferences.edit().putString(LOGGED_IN_USER_NAME, userName).apply()


    var isAdminUser: Boolean
        get() = preferences.getBoolean(IS_ADMIN_USER_, false)
        set(isAdmin) = preferences.edit().putBoolean(IS_ADMIN_USER_, isAdmin).apply()
}

Declare Koin module into “AppApplication.kt”

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class AppApplication : Application(), LifecycleObserver {
override fun onCreate() {
super.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
//Initialze Koin
initKoin()
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun onComingToForgeround() {
//Initialize something here
}
private fun initKoin() {
startKoin(
this,
listOf(
repositoryModule
)
)
}
}
class AppApplication : Application(), LifecycleObserver { override fun onCreate() { super.onCreate() ProcessLifecycleOwner.get().lifecycle.addObserver(this) //Initialze Koin initKoin() } @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) fun onComingToForgeround() { //Initialize something here } private fun initKoin() { startKoin( this, listOf( repositoryModule ) ) } }
class AppApplication : Application(), LifecycleObserver {

    override fun onCreate() {
        super.onCreate()

        ProcessLifecycleOwner.get().lifecycle.addObserver(this)

        //Initialze Koin
        initKoin()
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun onComingToForgeround() {
        //Initialize something here
    }

    private fun initKoin() {
        startKoin(
            this,
            listOf(
                repositoryModule
            )
        )
    }
}

1 Comment

  1. Not really clear why you wrapped creating SharedPreferences outside repository inside module 😕
    why not to use this design :

    import android.content.Context
    import android.content.SharedPreferences

    class UserPreferences(val context: Context) {
    private val preferences: SharedPreferences =
    context.getSharedPreferences(“userPreferences”, Context.MODE_PRIVATE)

    var loggedInUserName: String
    get() = preferences.getString(LOGGED_IN_USER_NAME, “”) ?: “”
    set(userName) = preferences.edit().putString(LOGGED_IN_USER_NAME, userName).apply()
    var isAdminUser: Boolean
    get() = preferences.getBoolean(IS_ADMIN_USER_, false)
    set(isAdmin) = preferences.edit().putBoolean(IS_ADMIN_USER_, isAdmin).apply()

    companion object {
    private const val LOGGED_IN_USER_NAME = “user_name”
    private const val IS_ADMIN_USER_ = “is_admin_user”
    }
    }

    And module:

    val storageModule = module {
    single { UserPreferences(get()) }
    }

Leave a Reply

Your email address will not be published.


*