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