forked from b4tchkn/20dojo_and
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request b4tchkn#8 from ShebangDog/implement-local
Implement local
- Loading branch information
Showing
12 changed files
with
161 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
app/src/main/java/jp/co/cyberagent/dojo2020/data/local/LocalDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
app/src/main/java/jp/co/cyberagent/dojo2020/data/local/MemoDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>> | ||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/jp/co/cyberagent/dojo2020/data/local/MemoDataBase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
26 changes: 26 additions & 0 deletions
26
app/src/main/java/jp/co/cyberagent/dojo2020/data/local/MemoEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
5 changes: 5 additions & 0 deletions
5
app/src/main/java/jp/co/cyberagent/dojo2020/ui/test/Constants.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
18 changes: 18 additions & 0 deletions
18
app/src/main/java/jp/co/cyberagent/dojo2020/ui/test/FakeRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
app/src/main/java/jp/co/cyberagent/dojo2020/ui/test/MemoData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} |