1
1
package fr.free.nrw.commons.upload.depicts
2
2
3
- import androidx.room.*
3
+ import androidx.room.Dao
4
+ import androidx.room.Delete
5
+ import androidx.room.Insert
6
+ import androidx.room.OnConflictStrategy
7
+ import androidx.room.Query
4
8
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
9
+ import kotlinx.coroutines.CoroutineScope
10
+ import kotlinx.coroutines.Deferred
5
11
import kotlinx.coroutines.Dispatchers
12
+ import kotlinx.coroutines.async
6
13
import kotlinx.coroutines.launch
7
- import kotlinx.coroutines.runBlocking
8
- import java.util.*
14
+ import java.util.Date
9
15
10
16
/* *
11
17
* Dao class for DepictsRoomDataBase
12
18
*/
13
19
@Dao
14
20
abstract class DepictsDao {
15
-
16
- /* *
17
- * insert Depicts in DepictsRoomDataBase
18
- */
21
+ /* * The maximum number of depicts allowed in the database. */
22
+ private val maxItemsAllowed = 10
23
+
19
24
@Insert(onConflict = OnConflictStrategy .REPLACE )
20
25
abstract suspend fun insert (depictedItem : Depicts )
21
-
22
- /* *
23
- * get all Depicts from roomdatabase
24
- */
26
+
25
27
@Query(" Select * From depicts_table order by lastUsed DESC" )
26
- abstract suspend fun getAllDepict (): List <Depicts >
28
+ abstract suspend fun getAllDepicts (): List <Depicts >
27
29
28
- /* *
29
- * get all Depicts which need to delete from roomdatabase
30
- */
31
30
@Query(" Select * From depicts_table order by lastUsed DESC LIMIT :n OFFSET 10" )
32
- abstract suspend fun getItemToDelete (n : Int ): List <Depicts >
31
+ abstract suspend fun getDepictsForDeletion (n : Int ): List <Depicts >
33
32
34
- /* *
35
- * Delete Depicts from roomdatabase
36
- */
37
33
@Delete
38
34
abstract suspend fun delete (depicts : Depicts )
39
35
40
- lateinit var allDepict: List <Depicts >
41
- lateinit var listOfDelete: List <Depicts >
42
-
43
36
/* *
44
- * get all depicts from DepictsRoomDatabase
37
+ * Gets all Depicts objects from the database, ordered by lastUsed in descending order.
38
+ *
39
+ * @return A list of Depicts objects.
45
40
*/
46
- fun depictsList (): List <Depicts > {
47
- runBlocking {
48
- launch(Dispatchers .IO ) {
49
- allDepict = getAllDepict()
50
- }
51
- }
52
- return allDepict
41
+ fun depictsList (): Deferred <List <Depicts >> = CoroutineScope (Dispatchers .IO ).async {
42
+ getAllDepicts()
53
43
}
54
44
55
45
/* *
56
- * insert Depicts in DepictsRoomDataBase
46
+ * Inserts a Depicts object into the database.
47
+ *
48
+ * @param depictedItem The Depicts object to insert.
57
49
*/
58
- fun insertDepict (depictes : Depicts ) {
59
- runBlocking {
60
- launch(Dispatchers .IO ) {
61
- insert(depictes)
62
- }
63
- }
50
+ private fun insertDepict (depictedItem : Depicts ) = CoroutineScope (Dispatchers .IO ).launch {
51
+ insert(depictedItem)
64
52
}
65
53
66
54
/* *
67
- * get all Depicts item which need to delete
55
+ * Gets a list of Depicts objects that need to be deleted from the database.
56
+ *
57
+ * @param n The number of depicts to delete.
58
+ * @return A list of Depicts objects to delete.
68
59
*/
69
- fun getItemTodelete (number : Int ): List <Depicts > {
70
- runBlocking {
71
- launch(Dispatchers .IO ) {
72
- listOfDelete = getItemToDelete(number)
73
- }
74
- }
75
- return listOfDelete
60
+ private suspend fun depictsForDeletion (n : Int ): Deferred <List <Depicts >> = CoroutineScope (Dispatchers .IO ).async {
61
+ getDepictsForDeletion(n)
76
62
}
77
63
78
64
/* *
79
- * delete Depicts in DepictsRoomDataBase
65
+ * Deletes a Depicts object from the database.
66
+ *
67
+ * @param depicts The Depicts object to delete.
80
68
*/
81
- fun deleteDepicts (depictes : Depicts ) {
82
- runBlocking {
83
- launch(Dispatchers .IO ) {
84
- delete(depictes)
85
- }
86
- }
69
+ private suspend fun deleteDepicts (depicts : Depicts ) = CoroutineScope (Dispatchers .IO ).launch {
70
+ delete(depicts)
87
71
}
88
72
89
73
/* *
90
- * save Depicts in DepictsRoomDataBase
74
+ * Saves a list of DepictedItems in the DepictsRoomDataBase.
91
75
*/
92
76
fun savingDepictsInRoomDataBase (listDepictedItem : List <DepictedItem >) {
93
- var numberofItemInRoomDataBase: Int
94
- val maxNumberOfItemSaveInRoom = 10
77
+ CoroutineScope (Dispatchers .IO ).launch {
78
+ for (depictsItem in listDepictedItem) {
79
+ depictsItem.isSelected = false
80
+ insertDepict(Depicts (depictsItem, Date ()))
81
+ }
95
82
96
- for (depictsItem in listDepictedItem) {
97
- depictsItem.isSelected = false
98
- insertDepict( Depicts (depictsItem, Date ()) )
83
+ // Deletes old Depicts objects from the database if
84
+ // the number of depicts exceeds the maximum allowed.
85
+ deleteOldDepictions(depictsList().await().size )
99
86
}
87
+ }
100
88
101
- numberofItemInRoomDataBase = depictsList().size
102
- // delete the depictItem from depictsroomdataBase when number of element in depictsroomdataBase is greater than 10
103
- if (numberofItemInRoomDataBase > maxNumberOfItemSaveInRoom) {
89
+ private suspend fun deleteOldDepictions ( depictsListSize : Int ) {
90
+ if (depictsListSize > maxItemsAllowed) {
91
+ val depictsForDeletion = depictsForDeletion(depictsListSize).await()
104
92
105
- val listOfDepictsToDelete: List <Depicts > =
106
- getItemTodelete(numberofItemInRoomDataBase)
107
- for (i in listOfDepictsToDelete) {
108
- deleteDepicts(i)
93
+ for (depicts in depictsForDeletion) {
94
+ deleteDepicts(depicts)
109
95
}
110
96
}
111
97
}
112
- }
98
+ }
99
+
0 commit comments