/SimpleStore

An Android library that helps you handle all the code for shared preference, datastore and blockstore.

Primary LanguageKotlinMIT LicenseMIT

SimpleStore Android Library

SimpleStore Android Library least API level SimpleStore Android Library on jitpack.io SimpleStore Android Library License. SimpleStore Android Library Stars SimpleStore Android Library Forks SimpleStore Android Library Issues SimpleStore Android Library Issues

SimpleStore Android Library

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.

1. Adding SimpleStore to your project

  • Include jitpack in your root settings.gradle file.
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.gradle file:
dependencies {
    implementation 'com.github.RhymezxCode:SimpleStore:1.1.1'
}

Sync your project, and 😱 boom πŸ”₯ you have added SimpleStore successfully. ❗

2. Usage

Using datastore

  • 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()
            }

Using shared preference

  • First initialize the builder class:
     val store = SimpleStore.Builder()
        .context(context = this)
        .storeName("AnyName of your choice")
        .encryption(encrypted = false)
        .build()

Using encrypted shared preference

  • 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
                )
            }

Using BlockStore

  • 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
                 }  
            }

3. You can also inject SimpleStore, and use it everywhere in your app with Hilt πŸ’‰ :

  • 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