Skip to content

Instantly share code, notes, and snippets.

View cesarferreira's full-sized avatar

César Ferreira cesarferreira

View GitHub Profile
class FriendsFragment : BaseFragment() {
override fun layoutId(): Int = R.layout.fragment_friends
private lateinit var friendsViewModel: FriendsViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
appComponent.inject(this)
class FriendsViewModel @Inject constructor(
private val getFriendsUseCase: GetFriendsUseCase
) : BaseViewModel() {
sealed class FriendsState {
object Loading : FriendsState()
object Empty : FriendsState()
data class Success(val users: List<UserUiModel>) : FriendsState()
}
class GetFriendsUseCase(
private val friendsRepository: FriendsRepository
) : BaseUseCase<List<User>, GetFriendsUseCase.Params>() {
override suspend fun run(params: Params): Either<Failure, List<User>> {
return try {
val friends = friendsRepository.getFriends(params.maxNumberOfFriends)
Either.Right(friends)
} catch (exp: Exception) {
/**
* Represents a value of one of two possible types (a disjoint union).
* Instances of [Either] are either an instance of [Left] or [Right].
* FP Convention dictates that [Left] is used for "failure"
* and [Right] is used for "success".
*
* @see Left
* @see Right
*/
/**
* Base Class for handling errors/failures/exceptions.
* Every feature specific failure should extend [FeatureFailure] class.
*/
sealed class Failure(val exception: Exception = Exception("Failure")) {
object None : Failure()
object NetworkConnection : Failure()
object ServerError : Failure()
/** * Extend this class for feature specific failures.*/
abstract class BaseUseCase<out Type, in Params> where Type : Any {
abstract suspend fun run(params: Params): Either<Failure, Type>
open operator fun invoke(
scope: CoroutineScope,
params: Params,
onResult: (Either<Failure, Type>) -> Unit = {}
) {
@cesarferreira
cesarferreira / DownloadRequest.kt
Created April 5, 2019 10:29 — forked from PrashamTrivedi/DownloadRequest.kt
Download File with progress indicator, written in Kotlin with Co-routines
suspend fun downloadFile(url: String,
downloadFile: File,
downloadProgressFun: (bytesRead: Long, contentLength: Long, isDone: Boolean) -> Unit) {
async(CommonPool) {
val request = with(Request.Builder()) {
url(url)
}.build()
val client = with(OkHttpClient.Builder()) {
addNetworkInterceptor { chain ->
@cesarferreira
cesarferreira / ffmpeg-compress-mp4
Created November 7, 2018 15:04 — forked from lukehedger/ffmpeg-compress-mp4
Compress mp4 using FFMPEG
$ ffmpeg -i input.mp4 -vcodec h264 -acodec mp2 output.mp4
@cesarferreira
cesarferreira / AndroidWorkerInjection.kt
Created November 7, 2018 14:25 — forked from ferrerojosh/AndroidWorkerInjection.kt
androidx workmanager injector temporary impl
import androidx.work.Worker
object AndroidWorkerInjection {
fun inject(worker: Worker) {
checkNotNull(worker, { "worker" })
val application = worker.applicationContext
if (application !is HasWorkerInjector) {
throw RuntimeException("${application.javaClass.canonicalName} does not implement ${HasWorkerInjector::class.java.canonicalName}")
}
@cesarferreira
cesarferreira / RxLiveData.kt
Created September 28, 2018 10:39 — forked from rubixhacker/RxLiveData.kt
Kotlin extension functions to convert LiveData to and from a Flowable
// Add the following to your build.gradle file
// implementation "android.arch.lifecycle:reactivestreams:1.0.0-beta1"
import android.arch.lifecycle.LifecycleOwner
import android.arch.lifecycle.LiveData
import android.arch.lifecycle.LiveDataReactiveStreams
import io.reactivex.Flowable
/**