KrawlDB is a nosql json database library which has a livedata prebuilt containing the elements from the database which you can observe on the main thread
Add the dependency for KrawlDB:
In your root gradle file add:
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
In your app module add:
dependencies {
// Replace $krawldb_version with the version in the release
implementation "com.github.Tanay360:KrawlDB:$krawldb_version"
}
You must initialize the database in a coroutine scope. The factory method takes in the context, lifecycleowner of fragment, activity or a composable, and the KClass object of the entity you will be using.
val db = KrawlDB.getDB<YourEntity>(context, lifecycleOwner, YourEntity::class)
If you are using compose, add the following dependencies to your app/build.gradle file
implementation 'androidx.compose.runtime:runtime-livedata:1.2.0-alpha01'
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.4.0"
To observe data in an activity or fragment:
db.data.observe(lifecycleOwner) {
// Your code here
}
To observe data in a composable:
db.data.observeAsState()
db.addToDatabase(
elements = arrayOf(/* Your elements here */),
listener = object: KrawlDB.OnTaskDoneListener {
override fun onCompleted() {
// Task is successful
}
override fun onError(cause: Throwable) {
// Error occurred in executing task
}
}
)
db.getElement(
index = indexOfElement,
listener = object : OnTaskCompletedListener<YourEntity> {
override fun onTaskCompleted(value: YourEntity) {
// Task Completed Successfully
}
override fun onTaskFailed(cause: Throwable) {
// Task failed to execute
}
})
db.updateElement(
index = indexOfElement,
newElement = objectOfYourEntity,
listener = object: KrawlDB.OnTaskDoneListener {
override fun onCompleted() {
// Task is successful
}
override fun onError(cause: Throwable) {
// Error occurred in executing task
}
}
)
db.deleteElement(
index = indexOfElement,
listener = object : KrawlDB.OnTaskDoneListener {
override fun onCompleted() {
// Task Completed Successfully!
}
override fun onError(cause: Throwable) {
// Task failed to execute!
}
}
)
db.deleteDatabase(object : KrawlDB.OnTaskDoneListener {
override fun onCompleted() {
// Task Completed Successfully!
}
override fun onError(cause: Throwable) {
// Task failed to execute!
}
}
)
Please note that all functions given above are called only after all tasks are executed! To call them before all tasks are done, just add the word sync after every function name
You can check out an example as well in the app directory of this repository although it is not documented.