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
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) |
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
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() | |
} |
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
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) { |
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
/** | |
* 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 | |
*/ |
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
/** | |
* 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.*/ |
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
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 = {} | |
) { |
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
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 -> |
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
$ ffmpeg -i input.mp4 -vcodec h264 -acodec mp2 output.mp4 |
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
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}") | |
} |
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
// 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 | |
/** |