Add backing infra for Room

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-08-27 20:55:03 +05:30
parent e16b4cb82d
commit 3d8935dc2b
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
3 changed files with 87 additions and 0 deletions

View file

@ -0,0 +1,34 @@
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.msfjarvis.todo.data.source
import androidx.room.TypeConverter
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
object DateTimeTypeConverters {
@TypeConverter
@JvmStatic
fun toLocalDateTime(value: String?): LocalDateTime? {
return value?.let { LocalDateTime.parse(value) }
}
@TypeConverter
@JvmStatic
fun fromLocalDateTime(value: LocalDateTime?): String? {
return value?.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
}
}

View file

@ -0,0 +1,18 @@
package dev.msfjarvis.todo.data.source
import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import dev.msfjarvis.todo.data.model.TodoItem
@Database(
entities = [
TodoItem::class,
],
version = 1,
exportSchema = false,
)
@TypeConverters(DateTimeTypeConverters::class)
abstract class TodoDatabase : RoomDatabase() {
abstract fun todoItemsDao(): TodoItemDao
}

View file

@ -0,0 +1,35 @@
package dev.msfjarvis.todo.data.source
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import dev.msfjarvis.todo.data.model.TodoItem
import kotlinx.coroutines.flow.Flow
/**
* Room [Dao] for [TodoItem]
*/
@Dao
abstract class TodoItemDao {
@Query("SELECT * FROM todo_items")
abstract fun getAllItems(): Flow<List<TodoItem>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
abstract suspend fun insert(entity: TodoItem): Long
@Insert(onConflict = OnConflictStrategy.REPLACE)
abstract suspend fun insertAll(vararg entity: TodoItem)
@Insert(onConflict = OnConflictStrategy.REPLACE)
abstract suspend fun insertAll(entities: Collection<TodoItem>)
@Update(onConflict = OnConflictStrategy.REPLACE)
abstract suspend fun update(entity: TodoItem)
@Delete
abstract suspend fun delete(entity: TodoItem): Int
}