Skip to content

Commit

Permalink
Merge pull request b4tchkn#8 from ShebangDog/implement-local
Browse files Browse the repository at this point in the history
Implement local
  • Loading branch information
asagi71 committed Sep 1, 2020
2 parents eeda52e + e2ff569 commit 458b5a4
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 4 deletions.
6 changes: 6 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 29
Expand Down Expand Up @@ -33,6 +34,8 @@ android {
}

dependencies {
def room_version = "2.2.5"

implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
Expand All @@ -47,4 +50,7 @@ dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.5"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.3.5'

implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-ktx:$room_version"
}
37 changes: 37 additions & 0 deletions app/src/main/java/jp/co/cyberagent/dojo2020/DI.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package jp.co.cyberagent.dojo2020

import android.content.Context
import androidx.room.Room
import jp.co.cyberagent.dojo2020.data.DataSource
import jp.co.cyberagent.dojo2020.data.Repository
import jp.co.cyberagent.dojo2020.data.local.LocalDataSource
import jp.co.cyberagent.dojo2020.data.local.MemoDataBase

object DI {
fun injectRepository(context: Context): DataSource {
val localDataSource = injectLocalDataSource(context)
val remoteDataSource = injectRemoteDataSource()

return Repository(localDataSource, remoteDataSource)
}

fun injectLocalDataSource(context: Context): DataSource {
val database = injectDatabase(context)

return LocalDataSource(database)
}

fun injectRemoteDataSource(): DataSource {
TODO()
}

private fun injectDatabase(context: Context): MemoDataBase {
val database = Room.databaseBuilder(
context,
MemoDataBase::class.java,
"ca-memo-database"
).build()

return database
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import jp.co.cyberagent.dojo2020.data.model.Memo
import kotlinx.coroutines.flow.Flow

interface DataSource {
suspend fun insert(memo: Memo)
suspend fun save(memo: Memo)

suspend fun fetchAll(): Flow<List<Memo>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package jp.co.cyberagent.dojo2020.data
import jp.co.cyberagent.dojo2020.data.model.Memo
import kotlinx.coroutines.flow.Flow

class Repository(localDataSource: DataSource, remoteDataSource: DataSource) : DataSource {
override suspend fun insert(memo: Memo) {
class Repository(private val localDataSource: DataSource, private val remoteDataSource: DataSource) : DataSource {
override suspend fun save(memo: Memo) {
TODO("Not yet implemented")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package jp.co.cyberagent.dojo2020.data.local

import jp.co.cyberagent.dojo2020.data.DataSource
import jp.co.cyberagent.dojo2020.data.model.Memo
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

class LocalDataSource(private val dataBase: MemoDataBase) : DataSource {

override suspend fun save(memo: Memo) {
val forInsertMemo = MemoEntity.createForInsert(memo.title, memo.contents, memo.time)

dataBase.memoDao().insert(forInsertMemo)
}

override suspend fun fetchAll(): Flow<List<Memo>> {
return dataBase.memoDao().fetchAll().map { memoEntityList ->
memoEntityList.map { Memo(it.title, it.contents, it.time) }
}
}
}
16 changes: 16 additions & 0 deletions app/src/main/java/jp/co/cyberagent/dojo2020/data/local/MemoDao.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package jp.co.cyberagent.dojo2020.data.local

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import kotlinx.coroutines.flow.Flow

@Dao
interface MemoDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(memoEntity: MemoEntity)

@Query(value = "SELECT * FROM memos")
fun fetchAll(): Flow<List<MemoEntity>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package jp.co.cyberagent.dojo2020.data.local

import androidx.room.Database
import androidx.room.RoomDatabase

@Database(entities = [MemoEntity::class], version = 1)
abstract class MemoDataBase : RoomDatabase() {
abstract fun memoDao(): MemoDao
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package jp.co.cyberagent.dojo2020.data.local

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "memos")
data class MemoEntity(
@PrimaryKey(autoGenerate = true)
val id: Int,

@ColumnInfo(name = "title")
val title: String,

@ColumnInfo(name = "contents")
val contents: String,

@ColumnInfo(name = "time")
val time: Double
) {
companion object {
fun createForInsert(title: String, contents: String, time: Double): MemoEntity {
return MemoEntity(0, title, contents, time)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package jp.co.cyberagent.dojo2020.data.model

data class Memo(val title: String, val contents: String)
data class Memo(val title: String, val contents: String, val time: Double)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package jp.co.cyberagent.dojo2020.ui.test

object Constants {
const val REPOSITORY = "REPOSITORY"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package jp.co.cyberagent.dojo2020.ui.test

import android.util.Log
import jp.co.cyberagent.dojo2020.data.model.Memo
import kotlinx.coroutines.flow.flow

object FakeRepository : jp.co.cyberagent.dojo2020.data.DataSource {
override suspend fun save(memo: Memo) {
Log.d(Constants.REPOSITORY, "insert: $memo")
}

override suspend fun fetchAll() = flow {
val dataList = MemoData.list

dataList.forEach { Log.d(Constants.REPOSITORY, "fetchAll: $it") }
emit(dataList)
}
}
19 changes: 19 additions & 0 deletions app/src/main/java/jp/co/cyberagent/dojo2020/ui/test/MemoData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package jp.co.cyberagent.dojo2020.ui.test

import jp.co.cyberagent.dojo2020.data.model.Memo

object MemoData {
val list = listOf(
Memo("", "", 0.0),
Memo("title", "contents", 0.0),
Memo("title", "cccccccccccccccccccccccccccccccccccccc aaaaaaaaaaaaaaaaaaaaaaaaa", 0.0),

Memo("", "", -1.1),
Memo("title", "contents", -1.1),
Memo("title", "cccccccccccccccccccccccccccccccccccccc aaaaaaaaaaaaaaaaaaaaaaaaa", -1.1),

Memo("", "", 1.1),
Memo("title", "contents", 1.1),
Memo("title", "cccccccccccccccccccccccccccccccccccccc aaaaaaaaaaaaaaaaaaaaaaaaa", 1.1)
)
}

0 comments on commit 458b5a4

Please sign in to comment.