MVVM on Android

Sep 1 2022 · Kotlin 1.6, Android 12, Android Studio Chipmunk | 2021.2.1 Patch 1

Part 1: MVVM on Android

04. Model the Repository

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 03. Build the Model Next episode: 05. Explore the View

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 04. Model the Repository

The version of the architecture components to add is 2.5.0. Check the download materials for the updated ViewModel and LiveData dependencies.

suspend should be added to the saveCreature and clearAllCreatures methods definition in the CreatureRepository interface. So the interface should look like this:

interface CreatureRepository {
  suspend fun saveCreature(creature: Creature)
  fun getAllCreatures(): LiveData<List<Creature>>
  suspend fun clearAllCreatures()
}

The RoomRepository class no longer uses AsyncTask. Instead it used Kotlin Coroutines. Check the download materials for the updated code or take a look at what it looks like below:

class RoomRepository : CreatureRepository {

  private val creatureDao: CreatureDao = CreaturemonApplication.database.creatureDao()

  private val allCreatures: LiveData<List<Creature>>

  init {
    allCreatures = creatureDao.getAllCreatures()
  }

  override suspend fun saveCreature(creature: Creature) {
    creatureDao.insert(creature)
  }

  override fun getAllCreatures() = allCreatures

  override suspend fun clearAllCreatures() {
    val creatureArray = allCreatures.value?.toTypedArray()
    if (creatureArray != null) {
      creatureDao.clearCreatures(*creatureArray)
    }
  }
}

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this video, we’re going to setup a Repository in which we’ll save our Model data. Our concrete repository is going to be a Room database, and we’re going to use LiveData from the Android Architecture Components library to retrieve data from the database.

arch_comp_version = '1.1.1'
  // ViewModel and LiveData
  implementation "android.arch.lifecycle:extensions:$arch_comp_version"
interface CreatureRepository {
  fun saveCreature(creature: Creature)
  fun getAllCreatures(): LiveData<List<Creature>>
  fun clearAllCreatures()
}
@Entity(tableName = "creature_table")
data class Creature(
    val attributes: CreatureAttributes = CreatureAttributes(),
    val hitPoints: Int = 0,
    @PrimaryKey @NonNull val name: String,
    val drawable: Int = 0
)
interface CreatureDao {
   @Insert(onConflict = OnConflictStrategy.REPLACE)
  fun insert(creature: Creature)
   @Delete
  fun clearCreatures(vararg creature: Creature)
   @Query("SELECT * FROM creature_table ORDER BY name ASC")
  fun getAllCreatures(): LiveData<List<Creature>>
}
@TypeConverters(CreatureAttributesConverter::class)
  private val allCreatures: LiveData<List<Creature>>
   init {
    allCreatures = creatureDao.getAllCreatures()
  }

   override fun saveCreature(creature: Creature) {
    InsertAsyncTask(creatureDao).execute(creature)
  }
   override fun getAllCreatures() = allCreatures
   override fun clearAllCreatures() {
    val creatureArray = allCreatures.value?.toTypedArray()
    if (creatureArray != null) {
      DeleteAsyncTask(creatureDao).execute(*creatureArray)
    }
  }
dao.clearCreatures(*params)
    database = Room.databaseBuilder(this, CreatureDatabase::class.java, "creature_database")
        .build()