-
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 LiveData and ViewModels and implement detail screen with it
Migrate edit super hero screen Migrate main screen to view model WIP
- Loading branch information
1 parent
6e0488d
commit 6100eb2
Showing
30 changed files
with
360 additions
and
414 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
app/src/androidTest/java/com/karumi/jetpack/superheroes/data/singleValueLiveData.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,7 @@ | ||
package com.karumi.jetpack.superheroes.data | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
|
||
fun <T> singleValueLiveData(value: T): LiveData<T> = | ||
MutableLiveData<T>().apply { postValue(value) } |
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
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
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
37 changes: 37 additions & 0 deletions
37
app/src/main/java/com/karumi/jetpack/superheroes/common/ViewModelFactory.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,37 @@ | ||
package com.karumi.jetpack.superheroes.common | ||
|
||
import android.app.Application | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.ViewModelProviders | ||
import com.karumi.jetpack.superheroes.ui.view.BaseActivity | ||
import org.kodein.di.DKodein | ||
import org.kodein.di.Kodein | ||
import org.kodein.di.KodeinAware | ||
import org.kodein.di.direct | ||
import org.kodein.di.erased.bind | ||
import org.kodein.di.erased.instance | ||
import org.kodein.di.erased.instanceOrNull | ||
|
||
class ViewModelFactory( | ||
private val injector: DKodein, | ||
private val application: Application | ||
) : ViewModelProvider.Factory { | ||
@Suppress("UNCHECKED_CAST") | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
return injector.instanceOrNull<ViewModel>(tag = modelClass.simpleName) as T? | ||
?: modelClass.getConstructor(Application::class.java).newInstance(application) | ||
} | ||
} | ||
|
||
inline fun <reified VM : ViewModel, T> T.viewModel(): Lazy<VM> | ||
where T : KodeinAware, | ||
T : BaseActivity<*> { | ||
return lazy { ViewModelProviders.of(this, direct.instance()).get(VM::class.java) } | ||
} | ||
|
||
inline fun <reified T : ViewModel> Kodein.Builder.bindViewModel( | ||
overrides: Boolean? = null | ||
): Kodein.Builder.TypeBinder<T> { | ||
return bind<T>(T::class.java.simpleName, overrides) | ||
} |
21 changes: 12 additions & 9 deletions
21
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,30 +1,33 @@ | ||
package com.karumi.jetpack.superheroes.data.repository | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.Transformations | ||
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 dao: SuperHeroDao, | ||
private val executor: ExecutorService | ||
) { | ||
fun getAllSuperHeroes(): List<SuperHero> = | ||
dao.getAll() | ||
.map { it.toSuperHero() } | ||
fun getAllSuperHeroes(): LiveData<List<SuperHero>> = | ||
Transformations.map(dao.getAll()) { it.toSuperHeroes() } | ||
|
||
fun get(id: String): SuperHero? = | ||
dao.getById(id)?.toSuperHero() | ||
fun get(id: String): LiveData<SuperHero?> = | ||
Transformations.map(dao.getById(id)) { it?.toSuperHero() } | ||
|
||
fun saveAll(all: List<SuperHero>) { | ||
fun saveAll(all: List<SuperHero>) = executor.execute { | ||
dao.deleteAll() | ||
dao.insertAll(all.map { it.toEntity() }) | ||
} | ||
|
||
fun save(superHero: SuperHero): SuperHero { | ||
dao.update(superHero.toEntity()) | ||
executor.execute { dao.update(superHero.toEntity()) } | ||
return superHero | ||
} | ||
|
||
private fun List<SuperHeroEntity>.toSuperHeroes(): List<SuperHero> = map { it.toSuperHero() } | ||
private fun SuperHeroEntity.toSuperHero(): SuperHero = superHero | ||
|
||
private fun SuperHero.toEntity(): SuperHeroEntity = SuperHeroEntity(this) | ||
} |
40 changes: 26 additions & 14 deletions
40
...src/main/java/com/karumi/jetpack/superheroes/data/repository/RemoteSuperHeroDataSource.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
34 changes: 27 additions & 7 deletions
34
app/src/main/java/com/karumi/jetpack/superheroes/data/repository/SuperHeroRepository.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,24 +1,44 @@ | ||
package com.karumi.jetpack.superheroes.data.repository | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MediatorLiveData | ||
import com.karumi.jetpack.superheroes.domain.model.SuperHero | ||
|
||
class SuperHeroRepository( | ||
private val local: LocalSuperHeroDataSource, | ||
private val remote: RemoteSuperHeroDataSource | ||
) { | ||
fun getAllSuperHeroes(): List<SuperHero> = | ||
local.getAllSuperHeroes().ifEmpty { | ||
remote.getAllSuperHeroes() | ||
.also { local.saveAll(it) } | ||
fun getAllSuperHeroes(): LiveData<List<SuperHero>> = MediatorLiveData<List<SuperHero>>().apply { | ||
val localSource = local.getAllSuperHeroes() | ||
val remoteSource = remote.getAllSuperHeroes() | ||
|
||
addSource(remoteSource) { superHeroes -> | ||
removeSource(remoteSource) | ||
addSource(localSource) { postValue(it) } | ||
local.saveAll(superHeroes) | ||
} | ||
} | ||
|
||
fun get(id: String): SuperHero? = | ||
local.get(id) | ||
?: remote.get(id)?.also { local.save(it) } | ||
fun get(id: String): LiveData<SuperHero?> = MediatorLiveData<SuperHero?>().apply { | ||
addSource(local.get(id)) { | ||
if (it == null) { | ||
syncSuperHeroFromRemote(id) | ||
} else { | ||
postValue(it) | ||
} | ||
} | ||
} | ||
|
||
fun save(superHero: SuperHero): SuperHero { | ||
local.save(superHero) | ||
remote.save(superHero) | ||
return superHero | ||
} | ||
|
||
private fun MediatorLiveData<SuperHero?>.syncSuperHeroFromRemote(id: String) { | ||
addSource(remote.get(id)) { superHero -> | ||
superHero ?: return@addSource | ||
local.save(superHero) | ||
} | ||
} | ||
} |
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
5 changes: 2 additions & 3 deletions
5
app/src/main/java/com/karumi/jetpack/superheroes/domain/usecase/GetSuperHeroById.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,10 +1,9 @@ | ||
package com.karumi.jetpack.superheroes.domain.usecase | ||
|
||
import androidx.annotation.WorkerThread | ||
import androidx.lifecycle.LiveData | ||
import com.karumi.jetpack.superheroes.data.repository.SuperHeroRepository | ||
import com.karumi.jetpack.superheroes.domain.model.SuperHero | ||
|
||
class GetSuperHeroById(private val superHeroesRepository: SuperHeroRepository) { | ||
@WorkerThread | ||
operator fun invoke(id: String): SuperHero? = superHeroesRepository.get(id) | ||
operator fun invoke(id: String): LiveData<SuperHero?> = superHeroesRepository.get(id) | ||
} |
5 changes: 2 additions & 3 deletions
5
app/src/main/java/com/karumi/jetpack/superheroes/domain/usecase/GetSuperHeroes.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,10 +1,9 @@ | ||
package com.karumi.jetpack.superheroes.domain.usecase | ||
|
||
import androidx.annotation.WorkerThread | ||
import androidx.lifecycle.LiveData | ||
import com.karumi.jetpack.superheroes.data.repository.SuperHeroRepository | ||
import com.karumi.jetpack.superheroes.domain.model.SuperHero | ||
|
||
class GetSuperHeroes(private val superHeroesRepository: SuperHeroRepository) { | ||
@WorkerThread | ||
operator fun invoke(): List<SuperHero> = superHeroesRepository.getAllSuperHeroes() | ||
operator fun invoke(): LiveData<List<SuperHero>> = superHeroesRepository.getAllSuperHeroes() | ||
} |
Oops, something went wrong.