add suspend keyword and refactor code

This commit is contained in:
Rohit Verma 2024-10-27 13:06:48 +05:30
parent 61d5010900
commit cc8ea6e692
2 changed files with 11 additions and 17 deletions

View file

@ -37,15 +37,11 @@ public abstract class PlaceDao {
*/ */
public Completable save(final Place place) { public Completable save(final Place place) {
return Completable return Completable
.fromAction(() -> { .fromAction(() -> saveSynchronous(place));
saveSynchronous(place);
});
} }
/** /**
* Deletes all Place objects from the database. * Deletes all Place objects from the database.
*
* @return A Completable that completes once the deletion operation is done.
*/ */
@Query("DELETE FROM place") @Query("DELETE FROM place")
public abstract void deleteAllSynchronous(); public abstract void deleteAllSynchronous();
@ -53,11 +49,9 @@ public abstract class PlaceDao {
/** /**
* Deletes all Place objects from the database. * Deletes all Place objects from the database.
* *
* @return A Completable that completes once the deletion operation is done.
*/ */
public Completable deleteAll() { public Completable deleteAll() {
return Completable return Completable.fromAction(this::deleteAllSynchronous);
.fromAction(() -> {
deleteAllSynchronous();
});
} }
} }

View file

@ -22,21 +22,21 @@ abstract class DepictsDao {
private val maxItemsAllowed = 10 private val maxItemsAllowed = 10
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun insert(depictedItem: Depicts) abstract suspend fun insert(depictedItem: Depicts)
@Query("Select * From depicts_table order by lastUsed DESC") @Query("Select * From depicts_table order by lastUsed DESC")
abstract fun getAllDepicts(): List<Depicts> abstract suspend fun getAllDepicts(): List<Depicts>
@Query("Select * From depicts_table order by lastUsed DESC LIMIT :n OFFSET 10") @Query("Select * From depicts_table order by lastUsed DESC LIMIT :n OFFSET 10")
abstract fun getDepictsForDeletion(n: Int): List<Depicts> abstract suspend fun getDepictsForDeletion(n: Int): List<Depicts>
@Delete @Delete
abstract fun delete(depicts: Depicts) abstract suspend fun delete(depicts: Depicts)
/** /**
* Gets all Depicts objects from the database, ordered by lastUsed in descending order. * Gets all Depicts objects from the database, ordered by lastUsed in descending order.
* *
* @return A list of Depicts objects. * @return Deferred list of Depicts objects.
*/ */
fun depictsList(): Deferred<List<Depicts>> = fun depictsList(): Deferred<List<Depicts>> =
CoroutineScope(Dispatchers.IO).async { CoroutineScope(Dispatchers.IO).async {
@ -48,7 +48,7 @@ abstract class DepictsDao {
* *
* @param depictedItem The Depicts object to insert. * @param depictedItem The Depicts object to insert.
*/ */
private fun insertDepict(depictedItem: Depicts) = fun insertDepict(depictedItem: Depicts) =
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
insert(depictedItem) insert(depictedItem)
} }
@ -59,7 +59,7 @@ abstract class DepictsDao {
* @param n The number of depicts to delete. * @param n The number of depicts to delete.
* @return A list of Depicts objects to delete. * @return A list of Depicts objects to delete.
*/ */
private suspend fun depictsForDeletion(n: Int): Deferred<List<Depicts>> = fun depictsForDeletion(n: Int): Deferred<List<Depicts>> =
CoroutineScope(Dispatchers.IO).async { CoroutineScope(Dispatchers.IO).async {
getDepictsForDeletion(n) getDepictsForDeletion(n)
} }
@ -69,7 +69,7 @@ abstract class DepictsDao {
* *
* @param depicts The Depicts object to delete. * @param depicts The Depicts object to delete.
*/ */
private suspend fun deleteDepicts(depicts: Depicts) = fun deleteDepicts(depicts: Depicts) =
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
delete(depicts) delete(depicts)
} }