A library to create shared preference, data store and block store. it has the ability to encrypt on a go, by just signifying on the builder class.
- Include jitpack in your root
settings.gradlefile.
pluginManagement {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
...
maven { url 'https://jitpack.io' }
}
}- And add it's dependency to your app level
build.gradlefile:
dependencies {
implementation 'com.github.RhymezxCode:SimpleStore:1.1.1'
}- First initialize the builder class:
val store = SimpleStore.Builder()
.context(context = this)
.storeName("AnyName of your choice")
.encryption(encrypted = false)
.build()- To save a string:
lifecycleScope.launch {
store.getType<DatastorePreference>().saveStringToStore(
"name",
"My name"
)
}- To save a boolean:
lifecycleScope.launch {
store.getType<DatastorePreference>().saveBooleanToStore(
"default",
true
)
}- Get a string that you saved:
lifecycleScope.launchWhenCreated {
binding.sharedPreferenceValue.text = store.getType<DatastorePreference>()
.getStringFromStore("name").first()
}- Get a boolean that you saved:
val default = false
lifecycleScope.launchWhenCreated {
default = store.getType<DatastorePreference>()
.getBooleanFromStore("default").first()
}- First initialize the builder class:
val store = SimpleStore.Builder()
.context(context = this)
.storeName("AnyName of your choice")
.encryption(encrypted = false)
.build()- First initialize the builder class:
val store = SimpleStore.Builder()
.context(context = this)
.storeName("AnyName of your choice")
.encryption(encrypted = true)
.build()- To save a string:
lifecycleScope.launch {
store.getType<SharedPreference>().saveStringToStore(
"name",
"My name"
)
}- To save a boolean:
lifecycleScope.launch {
store.getType<SharedPreference>().saveBooleanToStore(
"default",
true
)
}- First initialize the builder class:
val store = SimpleStore.Builder()
.context(context = this)
.build()- enable cloud with E2E encryption from the builder class:
val store = SimpleStore.Builder()
.context(context = this)
.enableCloudForBlockStore(true)
.build()- To save a byteArray:
lifecycleScope.launch {
store.getType<BlockStore>().saveByteArrayToStore(
"name",
byteArrayOf(1, 2, 3, 4)
)
}- Get the byteArray that you saved:
lifecycleScope.launch{
binding.sharedPreferenceValue.text = store.getType<BlockStore>()
.getByteArrayFromStore("name")- To clear your BlockStore:
lifecycleScope.launch{
val default = store.getType<BlockStore>()
.clearAllTheStore()
if(default){
//your data is cleared
}else{
//your data is not cleared
}
}- To delete a specific key data from your BlockStore:
lifecycleScope.launch{
val default = store.getType<BlockStore>()
.deleteByKey("key name")
if(default){
//your specific key data is deleted
}else{
//your specific key data is not deleted
}
}- Create an object for SimpleStoreModule in your di package:
@InstallIn(SingletonComponent::class)
@Module
object SimpleStoreModule{
@Provides
@Singleton
fun provideStore(
@ApplicationContext context: Context
) = SimpleStore.Builder()
.context(context = context)
.storeName("AnyName")
.encryption(encrypted = false)
.build()
}- Declare the variable in your class either a fragment or activity, it works in both:
@RequiresApi(Build.VERSION_CODES.M)
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
@Inject
lateinit var store: SimpleStore
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.store.setOnClickListener {
lifecycleScope.launch {
store.getType<DatastorePreference>().saveStringToStore(
"name",
binding.editTextTextPersonName.text.toString()
)
}
lifecycleScope.launchWhenCreated {
binding.sharedPreferenceValue.text =
store.getType<DatastorePreference>()
.getStringFromStore("name").first()
}
}
}
}Please note: Encrypted datastore is still in development, I will push a new version when it is ready!
π Please, feel free to give me a star π, I also love sparkles β¨
Developed with π by
Awodire Babajide Samuel