0

The LoginPreferences is used to persist data with sharedPreference.

private const val PREF_LOGIN_NAME = "loginName"
private const val PREF_LOGIN_PASS = "loginPass"

object LoginPreferences {

    fun getStoredName(context: Context): String {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        return prefs.getString(PREF_LOGIN_NAME, "")!!
    }

    fun setStoredName(context: Context, query: String) {
        PreferenceManager.getDefaultSharedPreferences(context)
            .edit()
            .putString(PREF_LOGIN_NAME, query)
            .apply()
    }

    fun getStoredPass(context: Context): String {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        return prefs.getString(PREF_LOGIN_PASS, "")!!
    }

    fun setStoredPass(context: Context, query: String) {
        PreferenceManager.getDefaultSharedPreferences(context)
            .edit()
            .putString(PREF_LOGIN_PASS, query)
            .apply()
    }
}

The LoginViewModel is a ViewModel use sharedPreferences to be observable data binding in fragment_login.xml.

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="loginViewModel"
            type="com.example.login.LoginViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout>
        ...
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

But if I want LoginViewModel to work with sharedPreference and observable data binding, it report a one super class error.

// (private val app: Application) : AndroidViewModel(app) is used to access to the sharedPreference.
// BaseObservable() is used to observe the data binding in xml file.
class LoginViewModel(private val app: Application) : AndroidViewModel(app), BaseObservable() {

    ...
}
Kanzariya Hitesh
  • 118
  • 1
  • 15
ccd
  • 5,788
  • 10
  • 46
  • 96
  • why not use livedata https://stackoverflow.com/questions/50649014/livedata-with-shared-preferences – Raghunandan Jan 06 '20 at 09:06
  • This is because You can only extend 1 class. So extend AndroidViewModel Remove BaseObservable and instead use LiveData for observing data changes. – Kishan Maurya Jan 06 '20 at 10:07

0 Answers0