We have created this library to help us stick to clean architecure principle creating a domain layer were to place all the business logic and final entities and at the same time to handle threads, coroutine, livedata and all the update from the process like loading, error and result.
You can create your own usecase extending BaseAsyncUsecase or BaseSyncUsecase and when you need to execute the method you call executeAndDispose for async and executeWithCatch for sync.
To listen to the result you can call observeWithResource that help to differentiet from loading, error or result values.
add in the Gradle
dependencies {
implementation('com.github.wereDevelopers:usecase:{LastTag}')
}import com.weredev.binding_ui.viewBinding
import com.weredev.bindingui.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var homeViewModel: HomeViewModel
...
import com.weredev.binding_ui.viewBinding
import com.weredev.bindingui.databinding.FragmentMainBinding
class HomeViewModel : BaseViewModel() {
private val repoCache = RepositoryCacheImpl()
private val repoBE = RepositoryBackEndImpl()
private val getMessageFromBackEndUseCase = GetMessageFromBackEndUseCase(repoBE)
private val getMessageFromCacheUseCase = GetMessageFromCacheUseCase(repoCache)
val messageBELiveData: MutableLiveData<Resource<String>> = MutableLiveData()
fun getMessageFromBackEnd(id: String) {
getMessageFromBackEndUseCase.executeAndDispose(messageBELiveData, id)
}
fun getMessageCache(id: String) {
getMessageFromCacheUseCase.invoke(id)
}
fun getStartMessage(responseInterface: ResponseInterface): String? {
return getStartMessageUseCase.executeWithCatch(responseInterface)
}
}