Skip to content

Commit a587521

Browse files
committed
slowly adding category support
1 parent 7ff19d7 commit a587521

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1148
-616
lines changed

app/build.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ android {
1919

2020
kapt {
2121
arguments {
22-
arg("room.schemaLocation", "")
23-
arg("room.exportSchema", "false")
22+
arg("room.schemaLocation", "$projectDir/schemas")
23+
arg("room.exportSchema", "true")
2424
}
2525
}
2626
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"formatVersion": 1,
3+
"database": {
4+
"version": 1,
5+
"identityHash": "dd6c87d009809e7cd6f2f504a6eba8d2",
6+
"entities": [
7+
{
8+
"tableName": "items",
9+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `startTime` TEXT NOT NULL, `endTime` TEXT NOT NULL, `ongoing` INTEGER NOT NULL, `pause` INTEGER NOT NULL)",
10+
"fields": [
11+
{
12+
"fieldPath": "id",
13+
"columnName": "id",
14+
"affinity": "INTEGER",
15+
"notNull": true
16+
},
17+
{
18+
"fieldPath": "startTime",
19+
"columnName": "startTime",
20+
"affinity": "TEXT",
21+
"notNull": true
22+
},
23+
{
24+
"fieldPath": "endTime",
25+
"columnName": "endTime",
26+
"affinity": "TEXT",
27+
"notNull": true
28+
},
29+
{
30+
"fieldPath": "ongoing",
31+
"columnName": "ongoing",
32+
"affinity": "INTEGER",
33+
"notNull": true
34+
},
35+
{
36+
"fieldPath": "pause",
37+
"columnName": "pause",
38+
"affinity": "INTEGER",
39+
"notNull": true
40+
}
41+
],
42+
"primaryKey": {
43+
"autoGenerate": true,
44+
"columnNames": [
45+
"id"
46+
]
47+
},
48+
"indices": [],
49+
"foreignKeys": []
50+
}
51+
],
52+
"views": [],
53+
"setupQueries": [
54+
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
55+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'dd6c87d009809e7cd6f2f504a6eba8d2')"
56+
]
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"formatVersion": 1,
3+
"database": {
4+
"version": 2,
5+
"identityHash": "bb6e146c83819d370d9c303a8dc57a8b",
6+
"entities": [
7+
{
8+
"tableName": "categories",
9+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `color` TEXT NOT NULL, `emoji` TEXT NOT NULL, `isDefault` INTEGER NOT NULL, `name` TEXT NOT NULL)",
10+
"fields": [
11+
{
12+
"fieldPath": "id",
13+
"columnName": "id",
14+
"affinity": "INTEGER",
15+
"notNull": true
16+
},
17+
{
18+
"fieldPath": "color",
19+
"columnName": "color",
20+
"affinity": "TEXT",
21+
"notNull": true
22+
},
23+
{
24+
"fieldPath": "emoji",
25+
"columnName": "emoji",
26+
"affinity": "TEXT",
27+
"notNull": true
28+
},
29+
{
30+
"fieldPath": "isDefault",
31+
"columnName": "isDefault",
32+
"affinity": "INTEGER",
33+
"notNull": true
34+
},
35+
{
36+
"fieldPath": "name",
37+
"columnName": "name",
38+
"affinity": "TEXT",
39+
"notNull": true
40+
}
41+
],
42+
"primaryKey": {
43+
"autoGenerate": true,
44+
"columnNames": [
45+
"id"
46+
]
47+
},
48+
"indices": [],
49+
"foreignKeys": []
50+
},
51+
{
52+
"tableName": "items",
53+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `startTime` TEXT NOT NULL, `endTime` TEXT NOT NULL, `ongoing` INTEGER NOT NULL, `pause` INTEGER NOT NULL, `categoryId` INTEGER NOT NULL DEFAULT 0)",
54+
"fields": [
55+
{
56+
"fieldPath": "id",
57+
"columnName": "id",
58+
"affinity": "INTEGER",
59+
"notNull": true
60+
},
61+
{
62+
"fieldPath": "startTime",
63+
"columnName": "startTime",
64+
"affinity": "TEXT",
65+
"notNull": true
66+
},
67+
{
68+
"fieldPath": "endTime",
69+
"columnName": "endTime",
70+
"affinity": "TEXT",
71+
"notNull": true
72+
},
73+
{
74+
"fieldPath": "ongoing",
75+
"columnName": "ongoing",
76+
"affinity": "INTEGER",
77+
"notNull": true
78+
},
79+
{
80+
"fieldPath": "pause",
81+
"columnName": "pause",
82+
"affinity": "INTEGER",
83+
"notNull": true
84+
},
85+
{
86+
"fieldPath": "categoryId",
87+
"columnName": "categoryId",
88+
"affinity": "INTEGER",
89+
"notNull": true,
90+
"defaultValue": "0"
91+
}
92+
],
93+
"primaryKey": {
94+
"autoGenerate": true,
95+
"columnNames": [
96+
"id"
97+
]
98+
},
99+
"indices": [],
100+
"foreignKeys": []
101+
}
102+
],
103+
"views": [],
104+
"setupQueries": [
105+
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
106+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'bb6e146c83819d370d9c303a8dc57a8b')"
107+
]
108+
}
109+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package cloud.pablos.overload.data
2+
3+
import cloud.pablos.overload.data.category.CategoryState
4+
import cloud.pablos.overload.data.item.ItemState
5+
import com.google.gson.Gson
6+
import java.time.LocalDateTime
7+
8+
class Backup {
9+
data class DatabaseBackup(
10+
val data: Map<String, List<Map<String, Any>>>,
11+
val backupVersion: Int,
12+
val backupDate: String,
13+
)
14+
15+
companion object {
16+
fun backupToJson(
17+
categoryState: CategoryState,
18+
itemState: ItemState,
19+
): String {
20+
val gson = Gson()
21+
22+
val categoriesTable =
23+
categoryState.categories.map { category ->
24+
mapOf(
25+
"id" to category.id,
26+
"color" to category.color,
27+
"emoji" to category.emoji,
28+
"isDefault" to category.isDefault,
29+
"name" to category.name,
30+
)
31+
}
32+
33+
val itemsTable =
34+
itemState.items.map { item ->
35+
mapOf(
36+
"id" to item.id,
37+
"startTime" to item.startTime,
38+
"endTime" to item.endTime,
39+
"ongoing" to item.ongoing,
40+
"pause" to item.pause,
41+
"categoryId" to item.categoryId,
42+
)
43+
}
44+
45+
val data =
46+
mapOf(
47+
"categories" to categoriesTable,
48+
"items" to itemsTable,
49+
)
50+
51+
val backup =
52+
DatabaseBackup(
53+
data,
54+
2,
55+
LocalDateTime.now().toString(),
56+
)
57+
58+
return try {
59+
val json = gson.toJson(backup)
60+
json
61+
} catch (e: Exception) {
62+
"{}"
63+
}
64+
}
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cloud.pablos.overload.data
2+
3+
class Converters {
4+
/*@TypeConverter
5+
fun iconToString(icon: ImageVector): String {
6+
return icon.name.split(".")[1]
7+
}
8+
9+
@TypeConverter
10+
fun stringToIcon(name: String): ImageVector {
11+
val cl = Class.forName("androidx.compose.material.icons.filled.${name}Kt")
12+
val method = cl.declaredMethods.first()
13+
return method.invoke(null, Icons.Filled) as ImageVector
14+
}
15+
16+
companion object {
17+
fun iconToString(icon: ImageVector): String {
18+
return icon.name.split(".")[1]
19+
}
20+
21+
fun stringToIcon(name: String): ImageVector {
22+
val cl = Class.forName("androidx.compose.material.icons.filled.${name}Kt")
23+
val method = cl.declaredMethods.first()
24+
return method.invoke(null, Icons.Filled) as ImageVector
25+
}
26+
}*/
27+
}

app/src/main/java/cloud/pablos/overload/data/OverloadDatabase.kt

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cloud.pablos.overload.data
22

33
import android.content.Context
4+
import androidx.room.AutoMigration
45
import androidx.room.Database
56
import androidx.room.Room
67
import androidx.room.RoomDatabase
@@ -9,14 +10,21 @@ import cloud.pablos.overload.data.category.CategoryDao
910
import cloud.pablos.overload.data.item.Item
1011
import cloud.pablos.overload.data.item.ItemDao
1112

12-
@Database(entities = [Category::class, Item::class], version = 1)
13+
@Database(
14+
entities = [Category::class, Item::class],
15+
version = 2,
16+
exportSchema = true,
17+
autoMigrations = [
18+
AutoMigration(from = 1, to = 2),
19+
],
20+
)
1321
abstract class OverloadDatabase : RoomDatabase() {
1422
abstract fun categoryDao(): CategoryDao
1523

1624
abstract fun itemDao(): ItemDao
1725

1826
companion object {
19-
const val DATABASE_NAME = "overload"
27+
const val DATABASE_NAME = "items"
2028

2129
@Volatile private var instance: OverloadDatabase? = null
2230

Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
package cloud.pablos.overload.data.category
22

3+
import androidx.room.Embedded
34
import androidx.room.Entity
45
import androidx.room.PrimaryKey
6+
import androidx.room.Relation
7+
import cloud.pablos.overload.data.item.Item
58

69
@Entity(tableName = "categories")
710
data class Category(
8-
@PrimaryKey(autoGenerate = true) val id: Int = 0,
11+
@PrimaryKey(autoGenerate = true) val id: Int = 1,
12+
val color: String,
13+
val emoji: String,
14+
val isDefault: Boolean,
915
val name: String,
10-
// val color: Color,
11-
// val default: Boolean,
16+
)
17+
18+
data class CategoryWithItems(
19+
@Embedded val category: Category,
20+
@Relation(
21+
parentColumn = "id",
22+
entityColumn = "categoryId",
23+
)
24+
val items: List<Item>,
1225
)

app/src/main/java/cloud/pablos/overload/data/category/CategoryDao.kt

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cloud.pablos.overload.data.category
33
import androidx.room.Dao
44
import androidx.room.Delete
55
import androidx.room.Query
6+
import androidx.room.Transaction
67
import androidx.room.Upsert
78
import kotlinx.coroutines.flow.Flow
89

@@ -22,4 +23,8 @@ interface CategoryDao {
2223

2324
@Query("SELECT * FROM categories")
2425
fun getAllCategories(): Flow<List<Category>>
26+
27+
@Transaction
28+
@Query("SELECT * FROM categories")
29+
fun getCategoryWithItems(): Flow<List<CategoryWithItems>>
2530
}

app/src/main/java/cloud/pablos/overload/data/category/CategoryEvent.kt

+9-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ sealed interface CategoryEvent {
55

66
data class SetId(val id: Int) : CategoryEvent
77

8-
data class SetName(val name: String) : CategoryEvent
8+
data class SetColor(val color: String) : CategoryEvent
9+
10+
data class SetEmoji(val emoji: String) : CategoryEvent
911

10-
// data class SetColor(val color: Color) : CategoryEvent
12+
data class SetName(val name: String) : CategoryEvent
1113

12-
// data class SetDefault(val default: Boolean) : CategoryEvent
14+
data class SetIsDefault(val isDefault: Boolean) : CategoryEvent
1315

1416
data class DeleteCategory(val category: Category) : CategoryEvent
17+
18+
data class SetSelectedCategoryConfigurations(val selectedCategoryConfigurations: Int) : CategoryEvent
19+
20+
data class SetSelectedCategory(val selectedCategory: Int) : CategoryEvent
1521
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
package cloud.pablos.overload.data.category
22

3+
import androidx.compose.material.icons.filled.Category
4+
import androidx.compose.ui.graphics.Color
5+
36
data class CategoryState(
47
val categories: List<Category> = emptyList(),
5-
val id: Int = 0,
8+
val categoryWithItems: List<CategoryWithItems> = emptyList(),
9+
// --
10+
val id: Int = 1,
11+
val color: String = Color.Unspecified.toString(),
12+
val emoji: String = "🕣",
613
val name: String = "Default",
7-
// val color: Color = Color.Unspecified,
8-
// val default: Boolean = true,
14+
val isDefault: Boolean = true,
15+
// --
16+
val selectedCategoryConfigurations: Int = 1,
17+
val selectedCategory: Int = 1,
918
)

0 commit comments

Comments
 (0)