Skip to content

Commit 611650a

Browse files
Merge pull request #65 from robert-muriithi/devv
2 parents 875c0da + 0848ae8 commit 611650a

File tree

30 files changed

+392
-103
lines changed

30 files changed

+392
-103
lines changed

app/src/main/java/dev/robert/composetodo/navigation/MainApp.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ fun MainApp(
8787
var selectedIndex by remember {
8888
mutableIntStateOf(0)
8989
}
90-
Timber.d("User Object: $userObject")
9190

9291
// TODO: FIX THIS/ OR FIND BETTER APPROACH.. it's always reseting to 0 when user navigates back
9392
LaunchedEffect(currentDestination) {

core/database/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ android {
4343
}
4444

4545
dependencies {
46-
46+
implementation(project(path = ":core:design-system"))
4747
implementation(libs.androidx.core.ktx)
4848
// room
4949
implementation(libs.androidx.room.runtime)

core/database/src/main/java/dev/robert/database/ConstUtils.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ package dev.robert.database
1717

1818
object ConstUtils {
1919
const val TODO_TABLE_NAME = "todos"
20+
const val TODO_TASK_CATEGORY_TABLE_NAME = "task_categories"
2021
const val TODO_DATABASE = "todos_database.db"
2122
}

core/database/src/main/java/dev/robert/database/data/todo/TodoDatabase.kt renamed to core/database/src/main/java/dev/robert/database/data/TodoDatabase.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,27 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package dev.robert.database.data.todo
16+
package dev.robert.database.data
1717

1818
import androidx.room.Database
1919
import androidx.room.RoomDatabase
2020
import androidx.room.TypeConverters
2121
import dev.robert.database.TasksTypeConverter
22+
import dev.robert.database.data.categories.CategoriesDao
23+
import dev.robert.database.data.categories.CategoryEntity
24+
import dev.robert.database.data.todo.TodoDao
25+
import dev.robert.database.data.todo.TodoEntity
2226

2327
@Database(
2428
exportSchema = false,
2529
entities = [
2630
TodoEntity::class,
31+
CategoryEntity::class
2732
],
2833
version = 4,
2934
)
3035
@TypeConverters(TasksTypeConverter::class)
3136
abstract class TodoDatabase : RoomDatabase() {
32-
abstract val dao: TodoDao
37+
abstract val tasksDao: TodoDao
38+
abstract val categoryDao: CategoriesDao
3339
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package dev.robert.database.data.categories
2+
3+
import androidx.room.Dao
4+
import androidx.room.Insert
5+
import androidx.room.Query
6+
import androidx.room.Upsert
7+
8+
@Dao
9+
interface CategoriesDao {
10+
@Upsert
11+
suspend fun saveCategory(entity: CategoryEntity)
12+
13+
@Query("SELECT * FROM task_categories")
14+
fun getCategories(): List<CategoryEntity>
15+
16+
@Query("DELETE FROM task_categories")
17+
suspend fun clear()
18+
19+
@Query("UPDATE task_categories SET name =:name WHERE id =:id")
20+
suspend fun updateCategoryName(name: String, id: Int)
21+
22+
@Insert
23+
suspend fun insertAll(categories: List<CategoryEntity>)
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package dev.robert.database.data.categories
2+
3+
import androidx.annotation.DrawableRes
4+
import androidx.room.Entity
5+
import androidx.room.PrimaryKey
6+
import dev.robert.database.ConstUtils.TODO_TASK_CATEGORY_TABLE_NAME
7+
8+
@Entity(tableName = TODO_TASK_CATEGORY_TABLE_NAME)
9+
data class CategoryEntity(
10+
@PrimaryKey(autoGenerate = true)
11+
val id: Int? = null,
12+
val name: String,
13+
val color: String,
14+
@field:DrawableRes
15+
val icon: Int,
16+
)

core/database/src/main/java/dev/robert/database/data/todo/TodoEntity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ data class TodoEntity(
3737

3838
data class TaskCategoryModelEntity(
3939
val name: String,
40+
val color: String? = null,
41+
val icon: Int? = null
4042
)

core/database/src/main/java/dev/robert/database/domain/DatabaseModule.kt

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,22 @@ package dev.robert.database.domain
1717

1818
import android.content.Context
1919
import androidx.room.Room
20+
import androidx.room.RoomDatabase
21+
import androidx.sqlite.db.SupportSQLiteDatabase
2022
import com.google.gson.Gson
2123
import dagger.Module
2224
import dagger.Provides
2325
import dagger.hilt.InstallIn
2426
import dagger.hilt.android.qualifiers.ApplicationContext
2527
import dagger.hilt.components.SingletonComponent
2628
import dev.robert.database.ConstUtils.TODO_DATABASE
29+
import dev.robert.design_system.R
2730
import dev.robert.database.TasksTypeConverter
28-
import dev.robert.database.data.todo.TodoDatabase
31+
import dev.robert.database.data.TodoDatabase
32+
import dev.robert.database.data.categories.CategoryEntity
33+
import kotlinx.coroutines.CoroutineScope
34+
import kotlinx.coroutines.Dispatchers
35+
import kotlinx.coroutines.launch
2936
import javax.inject.Singleton
3037

3138
@Module
@@ -41,7 +48,13 @@ object DatabaseModule {
4148
Provides
4249
Singleton
4350
]
44-
fun provideTodoDao(db: TodoDatabase) = db.dao
51+
fun provideTodoDao(db: TodoDatabase) = db.tasksDao
52+
53+
@[
54+
Provides
55+
Singleton
56+
]
57+
fun provideCategoryDao(db: TodoDatabase) = db.categoryDao
4558

4659
@[
4760
Provides
@@ -63,7 +76,29 @@ object DatabaseModule {
6376
TODO_DATABASE,
6477
).addTypeConverter(converter)
6578
.fallbackToDestructiveMigration()
66-
.allowMainThreadQueries()
79+
.addCallback(object : RoomDatabase.Callback() {
80+
override fun onCreate(db: SupportSQLiteDatabase) {
81+
super.onCreate(db)
82+
// db.execSQL("INSERT INTO task_categories (name, color, icon) VALUES ('Work', '#FF5733', '')")
83+
// db.execSQL("INSERT INTO task_categories (name, color, icon) VALUES ('Personal', '#33FF57', '')")
84+
// db.execSQL("INSERT INTO task_categories (name, color, icon) VALUES ('Shopping', '#3357FF', '')")
85+
// db.execSQL("INSERT INTO task_categories (name, color, icon) VALUES ('Health', '#FF33F6', '')")
86+
// db.execSQL("INSERT INTO task_categories (name, color, icon) VALUES ('Miscellaneous', '#33FFF6', '')")
87+
CoroutineScope(Dispatchers.IO).launch {
88+
provideCategoryDao(provideTodoDatabase(context, converter))
89+
.insertAll(getInitialCategories())
90+
}
91+
}
92+
})
6793
.build()
6894
}
95+
96+
97+
private fun getInitialCategories(): List<CategoryEntity> = listOf(
98+
CategoryEntity(name = "Work", color = "#FF5733", icon = R.drawable.ic_work_outline),
99+
CategoryEntity(name = "Personal", color = "#33FF57", icon = R.drawable.ic_personal),
100+
CategoryEntity(name = "Shopping", color = "#3357FF", icon = R.drawable.ic_shopping),
101+
CategoryEntity(name = "Health", color = "#FF33F6", icon = R.drawable.ic_health_safety),
102+
CategoryEntity(name = "Miscellaneous", color = "#33FFF6", icon = R.drawable.ic_miscellaneous_services)
103+
)
69104
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#FFFFFF" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">
2+
3+
<path android:fillColor="@android:color/white" android:pathData="M10.5,13H8v-3h2.5V7.5h3V10H16v3h-2.5v2.5h-3V13zM12,2L4,5v6.09c0,5.05 3.41,9.76 8,10.91c4.59,-1.15 8,-5.86 8,-10.91V5L12,2z"/>
4+
5+
</vector>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#FFFFFF" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">
2+
3+
<path android:fillColor="@android:color/white" android:pathData="M14.17,13.71l1.4,-2.42c0.09,-0.15 0.05,-0.34 -0.08,-0.45l-1.48,-1.16c0.03,-0.22 0.05,-0.45 0.05,-0.68s-0.02,-0.46 -0.05,-0.69l1.48,-1.16c0.13,-0.11 0.17,-0.3 0.08,-0.45l-1.4,-2.42c-0.09,-0.15 -0.27,-0.21 -0.43,-0.15L12,4.83c-0.36,-0.28 -0.75,-0.51 -1.18,-0.69l-0.26,-1.85C10.53,2.13 10.38,2 10.21,2h-2.8C7.24,2 7.09,2.13 7.06,2.3L6.8,4.15C6.38,4.33 5.98,4.56 5.62,4.84l-1.74,-0.7c-0.16,-0.06 -0.34,0 -0.43,0.15l-1.4,2.42C1.96,6.86 2,7.05 2.13,7.16l1.48,1.16C3.58,8.54 3.56,8.77 3.56,9s0.02,0.46 0.05,0.69l-1.48,1.16C2,10.96 1.96,11.15 2.05,11.3l1.4,2.42c0.09,0.15 0.27,0.21 0.43,0.15l1.74,-0.7c0.36,0.28 0.75,0.51 1.18,0.69l0.26,1.85C7.09,15.87 7.24,16 7.41,16h2.8c0.17,0 0.32,-0.13 0.35,-0.3l0.26,-1.85c0.42,-0.18 0.82,-0.41 1.18,-0.69l1.74,0.7C13.9,13.92 14.08,13.86 14.17,13.71zM8.81,11c-1.1,0 -2,-0.9 -2,-2c0,-1.1 0.9,-2 2,-2s2,0.9 2,2C10.81,10.1 9.91,11 8.81,11z"/>
4+
5+
<path android:fillColor="@android:color/white" android:pathData="M21.92,18.67l-0.96,-0.74c0.02,-0.14 0.04,-0.29 0.04,-0.44c0,-0.15 -0.01,-0.3 -0.04,-0.44l0.95,-0.74c0.08,-0.07 0.11,-0.19 0.05,-0.29l-0.9,-1.55c-0.05,-0.1 -0.17,-0.13 -0.28,-0.1l-1.11,0.45c-0.23,-0.18 -0.48,-0.33 -0.76,-0.44l-0.17,-1.18C18.73,13.08 18.63,13 18.53,13h-1.79c-0.11,0 -0.21,0.08 -0.22,0.19l-0.17,1.18c-0.27,0.12 -0.53,0.26 -0.76,0.44l-1.11,-0.45c-0.1,-0.04 -0.22,0 -0.28,0.1l-0.9,1.55c-0.05,0.1 -0.04,0.22 0.05,0.29l0.95,0.74c-0.02,0.14 -0.03,0.29 -0.03,0.44c0,0.15 0.01,0.3 0.03,0.44l-0.95,0.74c-0.08,0.07 -0.11,0.19 -0.05,0.29l0.9,1.55c0.05,0.1 0.17,0.13 0.28,0.1l1.11,-0.45c0.23,0.18 0.48,0.33 0.76,0.44l0.17,1.18c0.02,0.11 0.11,0.19 0.22,0.19h1.79c0.11,0 0.21,-0.08 0.22,-0.19l0.17,-1.18c0.27,-0.12 0.53,-0.26 0.75,-0.44l1.12,0.45c0.1,0.04 0.22,0 0.28,-0.1l0.9,-1.55C22.03,18.86 22,18.74 21.92,18.67zM17.63,18.83c-0.74,0 -1.35,-0.6 -1.35,-1.35s0.6,-1.35 1.35,-1.35s1.35,0.6 1.35,1.35S18.37,18.83 17.63,18.83z"/>
6+
7+
</vector>

0 commit comments

Comments
 (0)