Skip to content

Commit

Permalink
Add room library. Create Entity+Dao+Database
Browse files Browse the repository at this point in the history
  • Loading branch information
Serchinastico committed Feb 4, 2019
1 parent 174dab9 commit ff24d45
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 24 deletions.
4 changes: 4 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"
apply plugin: "shot"

android {
Expand Down Expand Up @@ -40,6 +41,8 @@ dependencies {
implementation "androidx.constraintlayout:constraintlayout:2.0.0-alpha3"
implementation "androidx.recyclerview:recyclerview:1.0.0"
implementation "androidx.lifecycle:lifecycle-runtime:2.0.0"
implementation "androidx.room:room-runtime:2.1.0-alpha04"
kapt "androidx.room:room-compiler:2.1.0-alpha04"

/* DI */
implementation "org.kodein.di:kodein-di-erased-jvm:6.0.1"
Expand All @@ -60,6 +63,7 @@ dependencies {
exclude group: "com.android.support"
}
androidTestImplementation "com.android.support.test.espresso:espresso-intents:3.0.2"
testImplementation "androidx.room:room-testing:2.1.0-alpha04"
}

shot {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package com.karumi.jetpack.superheroes

import android.app.Application
import android.content.Context
import androidx.room.Room
import com.karumi.jetpack.superheroes.common.SuperHeroesDatabase
import com.karumi.jetpack.superheroes.common.module
import com.karumi.jetpack.superheroes.data.repository.LocalSuperHeroDataSource
import com.karumi.jetpack.superheroes.data.repository.RemoteSuperHeroDataSource
import com.karumi.jetpack.superheroes.data.repository.SuperHeroRepository
import com.karumi.jetpack.superheroes.data.repository.room.SuperHeroDao
import org.kodein.di.Kodein
import org.kodein.di.KodeinAware
import org.kodein.di.android.androidModule
Expand All @@ -31,11 +34,23 @@ class SuperHeroesApplication : Application(), KodeinAware {
}

private fun appDependencies(): Kodein.Module = module {
bind<SuperHeroesDatabase>() with singleton {
Room.databaseBuilder(
this@SuperHeroesApplication,
SuperHeroesDatabase::class.java,
"superheroes-db"
).fallbackToDestructiveMigration()
.build()
}
bind<SuperHeroDao>() with provider {
val database: SuperHeroesDatabase = instance()
database.superHeroesDao()
}
bind<SuperHeroRepository>() with provider {
SuperHeroRepository(instance(), instance())
}
bind<LocalSuperHeroDataSource>() with singleton {
LocalSuperHeroDataSource()
LocalSuperHeroDataSource(instance())
}
bind<RemoteSuperHeroDataSource>() with provider {
RemoteSuperHeroDataSource()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.karumi.jetpack.superheroes.common

import androidx.room.Database
import androidx.room.RoomDatabase
import com.karumi.jetpack.superheroes.data.repository.room.SuperHeroDao
import com.karumi.jetpack.superheroes.data.repository.room.SuperHeroEntity

@Database(entities = [SuperHeroEntity::class], version = 1)
abstract class SuperHeroesDatabase : RoomDatabase() {
abstract fun superHeroesDao(): SuperHeroDao
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
package com.karumi.jetpack.superheroes.data.repository

import com.karumi.jetpack.superheroes.data.repository.room.SuperHeroDao
import com.karumi.jetpack.superheroes.data.repository.room.SuperHeroEntity
import com.karumi.jetpack.superheroes.domain.model.SuperHero

class LocalSuperHeroDataSource {
companion object {
private const val BIT_TIME = 250L
}

private val superHeroes: MutableMap<String, SuperHero> = mutableMapOf()
class LocalSuperHeroDataSource(
private val dao: SuperHeroDao
) {
fun getAllSuperHeroes(): List<SuperHero> =
dao.getAll()
.map { it.toSuperHero() }

fun getAllSuperHeroes(): List<SuperHero> {
waitABit()
return superHeroes.values.toList()
}

fun get(id: String): SuperHero? {
waitABit()
return superHeroes[id]
}
fun get(id: String): SuperHero? =
dao.getById(id)?.toSuperHero()

fun saveAll(all: List<SuperHero>) {
waitABit()
superHeroes.clear()
superHeroes.putAll(all.associateBy { it.id })
dao.deleteAll()
dao.insertAll(all.map { it.toEntity() })
}

fun save(superHero: SuperHero): SuperHero {
waitABit()
superHeroes[superHero.id] = superHero
dao.insertAll(listOf(superHero.toEntity()))
return superHero
}

private fun waitABit() {
Thread.sleep(BIT_TIME)
}
private fun SuperHeroEntity.toSuperHero(): SuperHero =
SuperHero(id, name, photo, isAvenger, description)

private fun SuperHero.toEntity(): SuperHeroEntity =
SuperHeroEntity(id, name, photo, isAvenger, description)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.karumi.jetpack.superheroes.data.repository.room

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query

@Dao
interface SuperHeroDao {
@Query("SELECT * FROM superheroes")
fun getAll(): List<SuperHeroEntity>

@Query("SELECT * FROM superheroes WHERE id = :id")
fun getById(id: String): SuperHeroEntity?

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(superHeroes: List<SuperHeroEntity>)

@Query("DELETE FROM superheroes")
fun deleteAll()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.karumi.jetpack.superheroes.data.repository.room

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

@Entity(tableName = "superheroes")
data class SuperHeroEntity(
@PrimaryKey val id: String,
val name: String,
val photo: String?,
val isAvenger: Boolean,
val description: String
)

0 comments on commit ff24d45

Please sign in to comment.