1
1
package info.papdt.express.helper.dao
2
2
3
+ import android.content.Context
3
4
import androidx.room.*
4
5
import androidx.room.OnConflictStrategy.*
6
+ import info.papdt.express.helper.R
5
7
import info.papdt.express.helper.model.Category
6
8
7
9
@Dao
8
- interface CategoryDao {
10
+ abstract class CategoryDao {
11
+
12
+ companion object {
13
+
14
+ private val DEFAULT_CATEGORIES_PAIRS = mapOf (
15
+ R .string.default_category_art to " insert_photo" ,
16
+ R .string.default_category_books to " book" ,
17
+ R .string.default_category_daily_necessities to " home" ,
18
+ R .string.default_category_digital_product to " phonelink" ,
19
+ R .string.default_category_entertainment to " games" ,
20
+ R .string.default_category_food_snack to " restaurant_menu" ,
21
+ R .string.default_category_home_appliance to " power" ,
22
+ R .string.default_category_letters to " email"
23
+ )
24
+
25
+ }
26
+
27
+ suspend fun add (title : String , iconCode : String ): Category {
28
+ return Category (title, iconCode).also { add(it) }
29
+ }
9
30
10
31
@Insert(onConflict = REPLACE )
11
- suspend fun add (category : Category )
32
+ abstract suspend fun add (category : Category )
12
33
13
34
@Insert(onConflict = REPLACE )
14
- suspend fun addAll (categories : List <Category >)
35
+ abstract suspend fun addAll (categories : List <Category >)
15
36
16
37
@Update(onConflict = REPLACE )
17
- suspend fun update (category : Category )
38
+ abstract suspend fun update (category : Category )
18
39
19
40
@Query(" SELECT * FROM category" )
20
- suspend fun getAll (): List <Category >
41
+ abstract suspend fun getAll (): List <Category >
21
42
22
43
@Query(" SELECT * FROM category WHERE title = :title LIMIT 1" )
23
- suspend fun get (title : String ): Category ?
44
+ abstract suspend fun get (title : String ): Category ?
24
45
25
46
@Delete
26
- suspend fun delete (category : Category )
47
+ abstract suspend fun delete (category : Category )
27
48
28
49
@Delete
29
- suspend fun delete (categories : List <Category >)
50
+ abstract suspend fun delete (categories : List <Category >)
51
+
52
+ suspend fun clear () {
53
+ delete(getAll())
54
+ }
55
+
56
+ suspend fun deleteWithUpdatingPackages (context : Context , category : Category ) {
57
+ val packDatabase = PackageDatabase .getInstance(context)
58
+ for (item in packDatabase.data) {
59
+ if (item.categoryTitle == category.title) {
60
+ item.categoryTitle = null
61
+ }
62
+ }
63
+ packDatabase.save()
64
+ delete(category)
65
+ }
66
+
67
+ suspend fun clearWithUpdatingPackages (context : Context ) {
68
+ val packDatabase = PackageDatabase .getInstance(context)
69
+ for (item in packDatabase.data) {
70
+ item.categoryTitle = null
71
+ }
72
+ packDatabase.save()
73
+ clear()
74
+ }
75
+
76
+ suspend fun addDefaultCategories (context : Context ) {
77
+ for ((titleRes, iconCode) in DEFAULT_CATEGORIES_PAIRS ) {
78
+ add(context.getString(titleRes), iconCode)
79
+ }
80
+ }
30
81
31
82
}
0 commit comments