Created
December 24, 2024 08:42
-
-
Save mzennis/a9773a0e1d2540fa1ac44cd68049d8a3 to your computer and use it in GitHub Desktop.
Sample Implementation of SecurePreferences (https://gist.github.com/mzennis/26d10513829efe9b5e3e7a6c8fe08d01)
This file contains hidden or 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
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
... | |
// todo: singleton? | |
val preference = SecurePreferences.create(this@MainActivity) | |
binding.btnSave.setOnClickListener { | |
val code = binding.etSecretCode.text.toString() | |
val msg = binding.etSecretMsg.text.toString() | |
preference.saveString(code, msg) | |
} | |
binding.btnGet.setOnClickListener { | |
val code = binding.etCode.text.toString() | |
val msg = preference.getString(code) | |
if (msg == null) { | |
Toast.makeText( | |
this@MainActivity, | |
"No secret message stored with code: $code", | |
Toast.LENGTH_SHORT | |
).show() | |
} else { | |
binding.tvMsg.text = msg | |
} | |
} | |
binding.btnClear.setOnClickListener { | |
preference.clearPreferences() | |
Toast.makeText( | |
this@MainActivity, | |
"All Clear", | |
Toast.LENGTH_SHORT | |
).show() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment