-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
167 additions
and
10 deletions.
There are no files selected for viewing
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
33 changes: 33 additions & 0 deletions
33
app/src/main/java/com/github/zsoltk/pokedex/common/Observe.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,33 @@ | ||
package com.github.zsoltk.pokedex.common | ||
|
||
import androidx.annotation.CheckResult | ||
import androidx.compose.effectOf | ||
import androidx.compose.memo | ||
import androidx.compose.onCommit | ||
import androidx.compose.state | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.Observer | ||
|
||
|
||
sealed class AsyncState<T> { | ||
class Initialised<T>: AsyncState<T>() | ||
class Loading<T>: AsyncState<T>() | ||
data class Error<T>(val error: Throwable): AsyncState<T>() | ||
data class Result<T>(val result: T): AsyncState<T>() | ||
} | ||
|
||
/** | ||
* Based on https://medium.com/swlh/android-mvi-with-jetpack-compose-b0890f5156ac | ||
*/ | ||
@CheckResult(suggest = "+") | ||
fun <T> observe(data: LiveData<T>) = effectOf<T?> { | ||
var result by +state { data.value } | ||
val observer = +memo { Observer<T> { result = it } } | ||
|
||
+onCommit(data) { | ||
data.observeForever(observer) | ||
onDispose { data.removeObserver(observer) } | ||
} | ||
|
||
result | ||
} |
34 changes: 34 additions & 0 deletions
34
app/src/main/java/com/github/zsoltk/pokedex/entity/PokemonApi.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,34 @@ | ||
package com.github.zsoltk.pokedex.entity | ||
|
||
import androidx.annotation.FloatRange | ||
import io.reactivex.Observable | ||
import io.reactivex.android.schedulers.AndroidSchedulers | ||
import java.lang.RuntimeException | ||
import java.util.concurrent.TimeUnit | ||
import kotlin.random.Random | ||
|
||
object PokemonApi { | ||
|
||
/** | ||
* Just for demonstration purposes. | ||
* You could do the same with a proper API. | ||
* You could also do the same with LiveData if you wish. | ||
*/ | ||
fun loadPokemon(): Observable<List<Pokemon>> = | ||
Observable | ||
.just(pokemons) | ||
.delay(200, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread()) | ||
.map { | ||
if (Random.nextFloat() < randomFailureChance) throw FakeApiException() | ||
it | ||
} | ||
|
||
|
||
/** | ||
* Feel free to try different values to test error screen | ||
*/ | ||
@FloatRange(from = 0.0, to = 1.0) | ||
val randomFailureChance: Float = 0.1f | ||
|
||
class FakeApiException : RuntimeException("Test exception, please ignore") | ||
} |
26 changes: 26 additions & 0 deletions
26
app/src/main/java/com/github/zsoltk/pokedex/entity/PokemonLiveData.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,26 @@ | ||
package com.github.zsoltk.pokedex.entity | ||
|
||
import androidx.lifecycle.MutableLiveData | ||
import com.github.zsoltk.pokedex.common.AsyncState | ||
import io.reactivex.android.schedulers.AndroidSchedulers | ||
import io.reactivex.disposables.Disposable | ||
|
||
class PokemonLiveData : MutableLiveData<AsyncState<List<Pokemon>>>() { | ||
|
||
init { | ||
reload() | ||
} | ||
|
||
private var disposable: Disposable? = null | ||
|
||
fun reload() { | ||
disposable?.dispose() | ||
disposable = PokemonApi | ||
.loadPokemon() | ||
.observeOn(AndroidSchedulers.mainThread()) | ||
.map { AsyncState.Result(it) as AsyncState<List<Pokemon>> } | ||
.startWith(AsyncState.Loading()) | ||
.onErrorReturn { AsyncState.Error(it) } | ||
.subscribe { value = it } | ||
} | ||
} |
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