-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add room library. Create Entity+Dao+Database
- Loading branch information
1 parent
5eee482
commit 72e7232
Showing
6 changed files
with
135 additions
and
25 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
app/schemas/com.karumi.jetpack.superheroes.common.SuperHeroesDatabase/1.json
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,58 @@ | ||
{ | ||
"formatVersion": 1, | ||
"database": { | ||
"version": 1, | ||
"identityHash": "3298ad790ab4e90668532dfc9d342b54", | ||
"entities": [ | ||
{ | ||
"tableName": "superheroes", | ||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `name` TEXT NOT NULL, `photo` TEXT, `isAvenger` INTEGER NOT NULL, `description` TEXT NOT NULL, PRIMARY KEY(`id`))", | ||
"fields": [ | ||
{ | ||
"fieldPath": "id", | ||
"columnName": "id", | ||
"affinity": "TEXT", | ||
"notNull": true | ||
}, | ||
{ | ||
"fieldPath": "name", | ||
"columnName": "name", | ||
"affinity": "TEXT", | ||
"notNull": true | ||
}, | ||
{ | ||
"fieldPath": "photo", | ||
"columnName": "photo", | ||
"affinity": "TEXT", | ||
"notNull": false | ||
}, | ||
{ | ||
"fieldPath": "isAvenger", | ||
"columnName": "isAvenger", | ||
"affinity": "INTEGER", | ||
"notNull": true | ||
}, | ||
{ | ||
"fieldPath": "description", | ||
"columnName": "description", | ||
"affinity": "TEXT", | ||
"notNull": true | ||
} | ||
], | ||
"primaryKey": { | ||
"columnNames": [ | ||
"id" | ||
], | ||
"autoGenerate": false | ||
}, | ||
"indices": [], | ||
"foreignKeys": [] | ||
} | ||
], | ||
"views": [], | ||
"setupQueries": [ | ||
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", | ||
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"3298ad790ab4e90668532dfc9d342b54\")" | ||
] | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
app/src/main/java/com/karumi/jetpack/superheroes/common/SuperHeroesDatabase.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,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 | ||
} |
40 changes: 16 additions & 24 deletions
40
app/src/main/java/com/karumi/jetpack/superheroes/data/repository/LocalSuperHeroDataSource.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 |
---|---|---|
@@ -1,42 +1,34 @@ | ||
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 java.util.concurrent.ExecutorService | ||
|
||
class LocalSuperHeroDataSource( | ||
private val dao: SuperHeroDao, | ||
private val executor: ExecutorService | ||
) { | ||
companion object { | ||
private const val BIT_TIME = 250L | ||
} | ||
|
||
private val superHeroes: MutableMap<String, SuperHero> = mutableMapOf() | ||
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>) = executor.execute { | ||
waitABit() | ||
superHeroes.clear() | ||
superHeroes.putAll(all.associateBy { it.id }) | ||
dao.deleteAll() | ||
dao.insertAll(all.map { it.toEntity() }) | ||
} | ||
|
||
fun save(superHero: SuperHero): SuperHero { | ||
executor.execute { | ||
waitABit() | ||
superHeroes[superHero.id] = superHero | ||
} | ||
executor.execute { 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) | ||
} |
21 changes: 21 additions & 0 deletions
21
app/src/main/java/com/karumi/jetpack/superheroes/data/repository/room/SuperHeroDao.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 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 ORDER BY id ASC") | ||
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() | ||
} |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/karumi/jetpack/superheroes/data/repository/room/SuperHeroEntity.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,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 | ||
) |