-
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
52e37d2
commit 5a956ff
Showing
29 changed files
with
345 additions
and
400 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) | ||
} |
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
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
3 changes: 2 additions & 1 deletion
3
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,8 +1,9 @@ | ||
package com.karumi.jetpack.superheroes.domain.usecase | ||
|
||
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) { | ||
operator fun invoke(id: String): SuperHero? = superHeroesRepository.get(id) | ||
operator fun invoke(id: String): LiveData<SuperHero?> = superHeroesRepository.get(id) | ||
} |
3 changes: 2 additions & 1 deletion
3
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,8 +1,9 @@ | ||
package com.karumi.jetpack.superheroes.domain.usecase | ||
|
||
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) { | ||
operator fun invoke(): List<SuperHero> = superHeroesRepository.getAllSuperHeroes() | ||
operator fun invoke(): LiveData<List<SuperHero>> = superHeroesRepository.getAllSuperHeroes() | ||
} |
Oops, something went wrong.