Migrated ContributionDao

This commit is contained in:
Sujal-Gupta-SG 2025-01-29 16:25:50 +05:30
parent 506588d264
commit 8b8f70c338
2 changed files with 63 additions and 60 deletions

View file

@ -1,52 +1,51 @@
package fr.free.nrw.commons.contributions; package fr.free.nrw.commons.contributions
import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteException
import androidx.paging.DataSource; import androidx.paging.DataSource
import androidx.room.Dao; import androidx.room.Dao
import androidx.room.Delete; import androidx.room.Delete
import androidx.room.Insert; import androidx.room.Insert
import androidx.room.OnConflictStrategy; import androidx.room.OnConflictStrategy
import androidx.room.Query; import androidx.room.Query
import androidx.room.Transaction; import androidx.room.Transaction
import androidx.room.Update; import androidx.room.Update
import io.reactivex.Completable; import io.reactivex.Completable
import io.reactivex.Single; import io.reactivex.Single
import java.util.Calendar; import java.util.Calendar
import java.util.List;
import timber.log.Timber;
@Dao @Dao
public abstract class ContributionDao { abstract class ContributionDao {
@Query("SELECT * FROM contribution order by media_dateUploaded DESC") @Query("SELECT * FROM contribution order by media_dateUploaded DESC")
abstract DataSource.Factory<Integer, Contribution> fetchContributions(); abstract fun fetchContributions(): DataSource.Factory<Int, Contribution>
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
public abstract void saveSynchronous(Contribution contribution); abstract fun saveSynchronous(contribution: Contribution)
public Completable save(final Contribution contribution) { fun save(contribution: Contribution): Completable {
return Completable return Completable
.fromAction(() -> { .fromAction {
contribution.setDateModified(Calendar.getInstance().getTime()); contribution.dateModified = Calendar.getInstance().time
if (contribution.getDateUploadStarted() == null) { if (contribution.dateUploadStarted == null) {
contribution.setDateUploadStarted(Calendar.getInstance().getTime()); contribution.dateUploadStarted = Calendar.getInstance().time
} }
saveSynchronous(contribution); saveSynchronous(contribution)
}); }
} }
@Transaction @Transaction
public void deleteAndSaveContribution(final Contribution oldContribution, open fun deleteAndSaveContribution(
final Contribution newContribution) { oldContribution: Contribution,
deleteSynchronous(oldContribution); newContribution: Contribution
saveSynchronous(newContribution); ) {
deleteSynchronous(oldContribution)
saveSynchronous(newContribution)
} }
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
public abstract Single<List<Long>> save(List<Contribution> contribution); abstract fun save(contribution: List<Contribution>): Single<List<Long>>
@Delete @Delete
public abstract void deleteSynchronous(Contribution contribution); abstract fun deleteSynchronous(contribution: Contribution)
/** /**
* Deletes contributions with specific states from the database. * Deletes contributions with specific states from the database.
@ -55,12 +54,12 @@ public abstract class ContributionDao {
* @throws SQLiteException If an SQLite error occurs. * @throws SQLiteException If an SQLite error occurs.
*/ */
@Query("DELETE FROM contribution WHERE state IN (:states)") @Query("DELETE FROM contribution WHERE state IN (:states)")
public abstract void deleteContributionsWithStatesSynchronous(List<Integer> states) @Throws(SQLiteException::class)
throws SQLiteException; abstract fun deleteContributionsWithStatesSynchronous(states: List<Int>)
public Completable delete(final Contribution contribution) { fun delete(contribution: Contribution): Completable {
return Completable return Completable
.fromAction(() -> deleteSynchronous(contribution)); .fromAction { deleteSynchronous(contribution) }
} }
/** /**
@ -69,19 +68,19 @@ public abstract class ContributionDao {
* @param states The states of the contributions to delete. * @param states The states of the contributions to delete.
* @return A Completable indicating the result of the operation. * @return A Completable indicating the result of the operation.
*/ */
public Completable deleteContributionsWithStates(List<Integer> states) { fun deleteContributionsWithStates(states: List<Int>): Completable {
return Completable return Completable
.fromAction(() -> deleteContributionsWithStatesSynchronous(states)); .fromAction { deleteContributionsWithStatesSynchronous(states) }
} }
@Query("SELECT * from contribution WHERE media_filename=:fileName") @Query("SELECT * from contribution WHERE media_filename=:fileName")
public abstract List<Contribution> getContributionWithTitle(String fileName); abstract fun getContributionWithTitle(fileName: String): List<Contribution>
@Query("SELECT * from contribution WHERE pageId=:pageId") @Query("SELECT * from contribution WHERE pageId=:pageId")
public abstract Contribution getContribution(String pageId); abstract fun getContribution(pageId: String): Contribution
@Query("SELECT * from contribution WHERE state IN (:states) order by media_dateUploaded DESC") @Query("SELECT * from contribution WHERE state IN (:states) order by media_dateUploaded DESC")
public abstract Single<List<Contribution>> getContribution(List<Integer> states); abstract fun getContribution(states: List<Int>): Single<List<Contribution>>
/** /**
* Gets contributions with specific states in descending order by the date they were uploaded. * Gets contributions with specific states in descending order by the date they were uploaded.
@ -90,8 +89,9 @@ public abstract class ContributionDao {
* @return A DataSource factory for paginated contributions with the specified states. * @return A DataSource factory for paginated contributions with the specified states.
*/ */
@Query("SELECT * from contribution WHERE state IN (:states) order by media_dateUploaded DESC") @Query("SELECT * from contribution WHERE state IN (:states) order by media_dateUploaded DESC")
public abstract DataSource.Factory<Integer, Contribution> getContributions( abstract fun getContributions(
List<Integer> states); states: List<Int>
): DataSource.Factory<Int, Contribution>
/** /**
* Gets contributions with specific states in ascending order by the date the upload started. * Gets contributions with specific states in ascending order by the date the upload started.
@ -100,17 +100,19 @@ public abstract class ContributionDao {
* @return A DataSource factory for paginated contributions with the specified states. * @return A DataSource factory for paginated contributions with the specified states.
*/ */
@Query("SELECT * from contribution WHERE state IN (:states) order by dateUploadStarted ASC") @Query("SELECT * from contribution WHERE state IN (:states) order by dateUploadStarted ASC")
public abstract DataSource.Factory<Integer, Contribution> getContributionsSortedByDateUploadStarted( abstract fun getContributionsSortedByDateUploadStarted(
List<Integer> states); states: List<Int>
): DataSource.Factory<Int, Contribution>
@Query("SELECT COUNT(*) from contribution WHERE state in (:toUpdateStates)") @Query("SELECT COUNT(*) from contribution WHERE state in (:toUpdateStates)")
public abstract Single<Integer> getPendingUploads(int[] toUpdateStates); abstract fun getPendingUploads(toUpdateStates: IntArray): Single<Int>
@Query("Delete FROM contribution") @Query("Delete FROM contribution")
public abstract void deleteAll() throws SQLiteException; @Throws(SQLiteException::class)
abstract fun deleteAll()
@Update @Update
public abstract void updateSynchronous(Contribution contribution); abstract fun updateSynchronous(contribution: Contribution)
/** /**
* Updates the state of contributions with specific states. * Updates the state of contributions with specific states.
@ -119,16 +121,17 @@ public abstract class ContributionDao {
* @param newState The new state to set. * @param newState The new state to set.
*/ */
@Query("UPDATE contribution SET state = :newState WHERE state IN (:states)") @Query("UPDATE contribution SET state = :newState WHERE state IN (:states)")
public abstract void updateContributionsState(List<Integer> states, int newState); abstract fun updateContributionsState(states: List<Int>, newState: Int)
public Completable update(final Contribution contribution) { fun update(contribution: Contribution): Completable {
return Completable return Completable.fromAction {
.fromAction(() -> { contribution.dateModified = Calendar.getInstance().time
contribution.setDateModified(Calendar.getInstance().getTime()); updateSynchronous(contribution)
updateSynchronous(contribution); }
});
} }
/** /**
* Updates the state of contributions with specific states asynchronously. * Updates the state of contributions with specific states asynchronously.
* *
@ -136,10 +139,10 @@ public abstract class ContributionDao {
* @param newState The new state to set. * @param newState The new state to set.
* @return A Completable indicating the result of the operation. * @return A Completable indicating the result of the operation.
*/ */
public Completable updateContributionsWithStates(List<Integer> states, int newState) { fun updateContributionsWithStates(states: List<Int>, newState: Int): Completable {
return Completable return Completable
.fromAction(() -> { .fromAction {
updateContributionsState(states, newState); updateContributionsState(states, newState)
}); }
} }
} }

View file

@ -149,7 +149,7 @@ class UploadWorker(
currentNotification.build(), currentNotification.build(),
) )
contribution!!.transferred = transferred contribution!!.transferred = transferred
contributionDao.update(contribution).blockingAwait() contributionDao.update(contribution!!).blockingAwait()
} }
open fun onChunkUploaded( open fun onChunkUploaded(