Move data package to its own module

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-09-08 06:35:38 +05:30
parent b5a3d6489b
commit 19817bf254
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
10 changed files with 50 additions and 19 deletions

1
data/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/build

38
data/build.gradle Normal file
View file

@ -0,0 +1,38 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'kotlin-kapt'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
minSdkVersion 23
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
kapt "androidx.room:room-compiler:$room_version"
api "androidx.room:room-runtime:$room_version"
api "androidx.room:room-ktx:$room_version"
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
coreLibraryDesugaring "com.android.tools:desugar_jdk_libs:1.0.10"
}

View file

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dev.msfjarvis.todo.data">
</manifest>

View file

@ -0,0 +1,14 @@
package dev.msfjarvis.todo.data.model
import androidx.room.Entity
import androidx.room.PrimaryKey
import java.time.LocalDateTime
import java.time.ZoneId
@Entity(
tableName = "todo_items",
)
data class TodoItem(
@PrimaryKey val title: String,
val time: LocalDateTime = LocalDateTime.now(ZoneId.of("GMT")),
)

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
}