Last active
August 12, 2022 17:14
-
-
Save KryptKode/658a0e96f7b3895e341ce25bfb6baeb9 to your computer and use it in GitHub Desktop.
Different Shared Prefs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.SharedPreferences | |
import android.net.Credentials | |
class CredentialsPreferences @Inject constructor( | |
@SharedPref(SharedPrefsType.Credentials) | |
private val sharedPreferences: SharedPreferences, | |
) { | |
fun getCredentials(): Credentials { | |
return sharedPreferences.getString(CRED_KEY, "").toCredentials() | |
} | |
fun setCredentials(credentials: Credentials) { | |
sharedPreferences.edit().putString(CRED_KEY, credentials.toString()).apply() | |
} | |
companion object { | |
private const val CRED_KEY = "cred_key" | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.SharedPreferences | |
class LoginPreferences @Inject constructor( | |
@SharedPref(SharedPrefsType.Login) | |
private val sharedPreferences: SharedPreferences, | |
) { | |
fun isLoggedIn(): Boolean { | |
return sharedPreferences.getBoolean(LOGGED_IN_KEY, false) | |
} | |
fun setLoggedIn(): Boolean { | |
sharedPreferences.edit().putBoolean(LOGGED_IN_KEY, true).apply() | |
} | |
private companion object { | |
const val LOGGED_IN_KEY = "log_in" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context | |
import android.content.SharedPreferences | |
@Module | |
class PrefsModule { | |
@Provides | |
@SharedPref(SharedPrefsType.Credentials) | |
fun provideCredentialsPreferences(context: Context): SharedPreferences{ | |
return EncryptedSharedPreferences.create( | |
"credentails_prefs", | |
context, | |
) | |
} | |
@Provides | |
@SharedPref(SharedPrefsType.Login) | |
fun provideLoginPreferences(context: Context): SharedPreferences{ | |
return EncryptedSharedPreferences.create( | |
"login_prefs", | |
context, | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment