Skip to content

Commit

Permalink
add suspend keyword and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
rohit9625 committed Oct 27, 2024
1 parent 61d5010 commit cc8ea6e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
12 changes: 3 additions & 9 deletions app/src/main/java/fr/free/nrw/commons/nearby/PlaceDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,21 @@ public abstract class PlaceDao {
*/
public Completable save(final Place place) {
return Completable
.fromAction(() -> {
saveSynchronous(place);
});
.fromAction(() -> saveSynchronous(place));
}

/**
* Deletes all Place objects from the database.
*
* @return A Completable that completes once the deletion operation is done.
*/
@Query("DELETE FROM place")
public abstract void deleteAllSynchronous();

/**
* Deletes all Place objects from the database.
*
* @return A Completable that completes once the deletion operation is done.
*/
public Completable deleteAll() {
return Completable
.fromAction(() -> {
deleteAllSynchronous();
});
return Completable.fromAction(this::deleteAllSynchronous);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ abstract class DepictsDao {
private val maxItemsAllowed = 10

@Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun insert(depictedItem: Depicts)
abstract suspend fun insert(depictedItem: Depicts)

@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")
abstract fun getDepictsForDeletion(n: Int): List<Depicts>
abstract suspend fun getDepictsForDeletion(n: Int): List<Depicts>

@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.
*
* @return A list of Depicts objects.
* @return Deferred list of Depicts objects.
*/
fun depictsList(): Deferred<List<Depicts>> =
CoroutineScope(Dispatchers.IO).async {
Expand All @@ -48,7 +48,7 @@ abstract class DepictsDao {
*
* @param depictedItem The Depicts object to insert.
*/
private fun insertDepict(depictedItem: Depicts) =
fun insertDepict(depictedItem: Depicts) =
CoroutineScope(Dispatchers.IO).launch {
insert(depictedItem)
}
Expand All @@ -59,7 +59,7 @@ abstract class DepictsDao {
* @param n The number of depicts 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 {
getDepictsForDeletion(n)
}
Expand All @@ -69,7 +69,7 @@ abstract class DepictsDao {
*
* @param depicts The Depicts object to delete.
*/
private suspend fun deleteDepicts(depicts: Depicts) =
fun deleteDepicts(depicts: Depicts) =
CoroutineScope(Dispatchers.IO).launch {
delete(depicts)
}
Expand Down

0 comments on commit cc8ea6e

Please sign in to comment.