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 either a shared preference or data 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' }
    }
}
  • And add it’s dependency to your app level build.gradle file:

dependencies {
    implementation 'com.github.RhymezxCode:SimpleStore:1.0.4'
}

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 = it
                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
                )
            }
  • And Lastly, to clear your store for Datastore or SharedPreference:

       lifecycleScope.launchWhenCreated {
        val default = store.getType<DatastorePreference>()
                    .clearAllTheStore()
            }
            
            lifecycleScope.launchWhenCreated {
        val default = store.getType<SharedPreference>()
                    .clearAllTheStore()
            }

3. You can also inject SimpleStore, and use it everywhere in your app with Hilt ? :

  • Create an object for the NetworkStateModule in your di package:

@InstallIn(SingletonComponent::class)
@Module
object AppModule {

    @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

GitHub

View Github