A class that encapsulates a successful result with a value of type [T] or a failure result with an [Throwable] exception
A class that encapsulates a successful result with a value of type [T] or a failure result with an [Throwable] exception
allprojects {
repositories {
google()
mavenCentral()
}
}
dependencies {
implementation "io.github.taptappub:reaction:$version"
}
class MainRepository {
fun getData(): Reaction<String> = Reaction.on { "some calculations" }
fun getAnotherData(firstData: String): Reaction<String> = Reaction.on { "some another calculations based on $firstData" }
}
fun getData() {
viewModelScope.launch(Dispatchers.IO) {
repository.getData()
.map { "Convert to another string" }
.doOnError { Log.d("LOG", "it is an error") }
.fold(
success = {
State.Success(it)
},
error = {
State.Error
}
)
.let { liveData.postValue(it) }
}
}
fun getAnotherData() {
viewModelScope.launch(Dispatchers.IO) {
val data = repository.getData()
.check { it.isNotEmpty() }
.flatMap { Reaction.of { "Flatmapped data" } }
.takeOrReturn {
Log.d("LOG", "it is an error again")
return@launch
}
repository.getAnotherData(data)
.handle(
success = { liveData.postValue(State.Success(it)) },
error = { Log.d("LOG", "Error too") }
)
}
}
Reaction.on { "something" }
Reaction.onCondition { it == "what you want" }
Reaction.tryReaction { Reaction.on { "something" } }
repository.getData()
.map { "Convert to another string" }
repository.getData()
.mapReaction { s, e -> "Convert to another string: $s + $e" }
repository.getData()
.flatMap { Reaction.of { "Flatmapped data" } }
repository.getData()
.errorMap { IllegalStateException("something went wrong") }
repository.getData()
.recover { "New reaction, much better then old" }
repository.getData()
.doOnSuccess { Log.d("Success! Let's dance!") }
repository.getData()
.doOnError { Log.d("Error! Let's dance but sadly =(") }
repository.getData()
.doOnComplete { Log.d("Let's dance in any case!") }
repository.getData(data)
.handle(
success = { liveData.postValue(it) },
error = { Log.d("LOG", "Error. That's a shame") }
)
repository.getData(data)
.flatHandle { success, error ->
Log.d("LOG", "Let's combine results ${success.toString() + error.toString()}")
}
repository.getData()
.fold(
success = { State.Success(it) },
error = { State.Error }
)
repository.getData()
.check { it.isNotEmpty() }
val data = repository.getData()
.takeOrReturn {
Log.d("LOG", "it is an error again")
return
}
val data = repository.getData()
.takeOrDefault {
"default data"
}
val data = repository.getData()
.takeOrThrow()
val isSuccess = repository.getData()
.isSuccess()
val isError = repository.getData()
.isError()
override fun success() {
continuation.resumeWithReactionSuccess { true }
}
override fun failure(error: Throwable?) {
continuation.resumeWithReactionError { error }
}
Copyright 2021 Aleksey Potapov
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.