LoginPreferences используется для сохранения данных с 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()
}
}
LoginViewModel - это ViewModel, использующий sharedPreferences для наблюдаемой привязки данных в frag_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>
Но если я хочу, чтобы LoginViewModel работал с sharedPreference и наблюдаемой привязкой данных, он сообщает об ошибке one super class
.
// (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() {
...
}