Permalink
1741c5a Jun 15, 2018
3 contributors

Users who have contributed to this file

@JoseAlcerreca @thagikura @jdkoren
88 lines (78 sloc) 3.02 KB
/*
* Copyright 2018 Google LLC
*
* 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
*
* https://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.
*/
package com.google.samples.apps.iosched.shared.domain
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.samples.apps.iosched.shared.domain.internal.DefaultScheduler
import com.google.samples.apps.iosched.shared.domain.internal.Scheduler
import com.google.samples.apps.iosched.shared.result.Result
import timber.log.Timber
/**
* Executes business logic synchronously or asynchronously using a [Scheduler].
*/
abstract class UseCase<in P, R> {
private val taskScheduler = DefaultScheduler
/** Executes the use case asynchronously and places the [Result] in a MutableLiveData
*
* @param parameters the input parameters to run the use case with
* @param result the MutableLiveData where the result is posted to
*
*/
operator fun invoke(parameters: P, result: MutableLiveData<Result<R>>) {
// result.value = Result.Loading TODO: add data to Loading to avoid glitches
try {
taskScheduler.execute {
try {
execute(parameters).let { useCaseResult ->
result.postValue(Result.Success(useCaseResult))
}
} catch (e: Exception) {
Timber.e(e)
result.postValue(Result.Error(e))
}
}
} catch (e: Exception) {
Timber.d(e)
result.postValue(Result.Error(e))
}
}
/** Executes the use case asynchronously and returns a [Result] in a new LiveData object.
*
* @return an observable [LiveData] with a [Result].
*
* @param parameters the input parameters to run the use case with
*/
operator fun invoke(parameters: P): LiveData<Result<R>> {
val liveCallback: MutableLiveData<Result<R>> = MutableLiveData()
this(parameters, liveCallback)
return liveCallback
}
/** Executes the use case synchronously */
fun executeNow(parameters: P): Result<R> {
return try {
Result.Success(execute(parameters))
} catch (e: Exception) {
Result.Error(e)
}
}
/**
* Override this to set the code to be executed.
*/
@Throws(RuntimeException::class)
protected abstract fun execute(parameters: P): R
}
operator fun <R> UseCase<Unit, R>.invoke(): LiveData<Result<R>> = this(Unit)
operator fun <R> UseCase<Unit, R>.invoke(result: MutableLiveData<Result<R>>) = this(Unit, result)