-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
02-room #6
02-room #6
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.karumi.jetpack.superheroes.common | ||
|
||
import android.content.Context | ||
import androidx.room.Database | ||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
import androidx.room.migration.Migration | ||
import androidx.sqlite.db.SupportSQLiteDatabase | ||
import com.karumi.jetpack.superheroes.data.repository.room.SuperHeroDao | ||
import com.karumi.jetpack.superheroes.data.repository.room.SuperHeroEntity | ||
|
||
@Database(entities = [SuperHeroEntity::class], version = 2) | ||
abstract class SuperHeroesDatabase : RoomDatabase() { | ||
abstract fun superHeroesDao(): SuperHeroDao | ||
|
||
companion object { | ||
fun build(context: Context): SuperHeroesDatabase = | ||
Room.databaseBuilder(context, SuperHeroesDatabase::class.java, "superheroes-db") | ||
.addMigrations(from1To2) | ||
.build() | ||
} | ||
} | ||
|
||
private val from1To2 = object : Migration(1, 2) { | ||
override fun migrate(database: SupportSQLiteDatabase) { | ||
database.execSQL("DROP TABLE superheroes") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was there a superheroes table before ? |
||
database.execSQL( | ||
""" | ||
CREATE TABLE `superheroes` ( | ||
`id` TEXT NOT NULL, | ||
`superhero_id` TEXT NOT NULL, | ||
`superhero_name` TEXT NOT NULL, | ||
`superhero_photo` TEXT, | ||
`superhero_isAvenger` INTEGER NOT NULL DEFAULT 0, | ||
`superhero_description` TEXT NOT NULL, | ||
PRIMARY KEY(`id`) | ||
) | ||
""" | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,30 @@ | ||
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 | ||
import kotlinx.coroutines.delay | ||
|
||
class LocalSuperHeroDataSource { | ||
companion object { | ||
private const val BIT_TIME = 250L | ||
} | ||
|
||
private val superHeroes: MutableMap<String, SuperHero> = mutableMapOf() | ||
class LocalSuperHeroDataSource( | ||
private val dao: SuperHeroDao | ||
) { | ||
suspend fun getAllSuperHeroes(): List<SuperHero> = | ||
dao.getAll() | ||
.map { it.toSuperHero() } | ||
|
||
suspend fun getAllSuperHeroes(): List<SuperHero> { | ||
waitABit() | ||
return superHeroes.values.toList() | ||
} | ||
|
||
suspend fun get(id: String): SuperHero? { | ||
waitABit() | ||
return superHeroes[id] | ||
} | ||
suspend fun get(id: String): SuperHero? = | ||
dao.getById(id)?.toSuperHero() | ||
|
||
suspend fun saveAll(all: List<SuperHero>) { | ||
waitABit() | ||
superHeroes.clear() | ||
superHeroes.putAll(all.associateBy { it.id }) | ||
dao.deleteAll() | ||
dao.insertAll(all.map { it.toEntity() }) | ||
} | ||
|
||
suspend fun save(superHero: SuperHero): SuperHero { | ||
waitABit() | ||
superHeroes[superHero.id] = superHero | ||
dao.update(superHero.toEntity()) | ||
return superHero | ||
} | ||
|
||
private suspend fun waitABit() { | ||
delay(BIT_TIME) | ||
} | ||
private fun SuperHeroEntity.toSuperHero(): SuperHero = superHero | ||
|
||
private fun SuperHero.toEntity(): SuperHeroEntity = SuperHeroEntity(id, this) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.karumi.jetpack.superheroes.data.repository.room | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.Query | ||
import androidx.room.Update | ||
|
||
@Dao | ||
interface SuperHeroDao { | ||
@Query("SELECT * FROM superheroes") | ||
suspend fun getAll(): List<SuperHeroEntity> | ||
|
||
@Query("SELECT * FROM superheroes WHERE id = :id") | ||
suspend fun getById(id: String): SuperHeroEntity? | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insertAll(superHeroes: List<SuperHeroEntity>) | ||
|
||
@Update | ||
suspend fun update(superHero: SuperHeroEntity) | ||
|
||
@Query("DELETE FROM superheroes") | ||
suspend fun deleteAll() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.karumi.jetpack.superheroes.data.repository.room | ||
|
||
import androidx.room.Embedded | ||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
import com.karumi.jetpack.superheroes.domain.model.SuperHero | ||
|
||
@Entity(tableName = "superheroes") | ||
data class SuperHeroEntity( | ||
@PrimaryKey val id: String, | ||
@Embedded(prefix = "superhero_") val superHero: SuperHero | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems to me that Embedded is an advanced feature of Room, we could simplify this for the course, even if it's more verbose. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also not sure about the effect you want to have as SuperHero as already an id and the prefix of these fields. the table will have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually it was just a test to see how If we go this way we can remove the outer id and configure the primary key from the |
||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a neat way to test database migration would be nice to have it included, even if not required during the exercises.
https://developer.android.com/training/data-storage/room/migrating-db-versions#export-schema
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the PR is mid development, yesterday I started creating all tests and I'm using that approach, I only exported v2 schema because I did it late in the process but I'm adding schema-v1 as well