Skip to content

Instantly share code, notes, and snippets.

View ChristopherME's full-sized avatar

Christopher Elias ChristopherME

View GitHub Profile
@ChristopherME
ChristopherME / FileSelector.txt
Created January 29, 2020 19:42
File Selector
import android.content.ContentUris
import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.DocumentsContract
import android.provider.MediaStore
import android.util.Log
import java.io.*
class SomeViewModel(private val getUserUseCase: GetUserUseCase) : ViewModel() {
// Only your viewmodel should update the value.
private val _userName: MutableLiveData<String> = MutableLiveData()
val userName: LiveData<String>
get() = _userName
...
private fun executeUseCase() {
class SomeViewModel(private val getUserUseCase: GetUserUseCase) : ViewModel() {
// Only your viewmodel should update the value.
private val _user: MutableLiveData<User> = MutableLiveData()
val userName: LiveData<String> = _user.map {
resultUser -> "${resultUser.name} + ${resultUser.lastName}"
}
...
class SomeViewModel(
private val getUsersUseCase: GetUserUseCase,
private val mapper: PresentationMapper
) : ViewModel() {
// Only your viewmodel should update the value.
private val _users: MutableLiveData<List<PresentationUser>> = MutableLiveData()
val users: LiveData<List<PresentationUser>>
get() = _users
class SomeViewModel(
private val getUsersUseCase: GetUserUseCase,
private val mapper: PresentationMapper
) : ViewModel() {
// Only your viewmodel should update the value.
private val _domainUsers: MutableLiveData<List<DomainUser>> = MutableLiveData()
val users: LiveData<List<PresentationUser>> = _domainUsers.switchMap { domainUsers ->
liveData {
emit(mapper.mapDomainUsersToPresentation(domainUsers))
@ChristopherME
ChristopherME / fragment-backpressed-listener.kt
Created January 28, 2021 11:50
Handle activity backpressed from fragment.
/**
* Extension function for the fragment to handle the activity backPressed().
* Call this inside [Fragment.onAttach] method.
*
* @param backPressed is invoked when the activity triggers onBackPressed().
* @see [https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher]
*/
inline fun Fragment.handleActivityBackPressed(
crossinline backPressed: () -> Unit
) {
@ChristopherME
ChristopherME / recyclerview-fix-leak-extension.kt
Last active March 25, 2023 14:05
This extension method is a solution for a memory leak problem with recyclerviews.
/**
* Set the adapter and call [clearReference] extension function in one call.
* Use this extension if the current Fragment is going to be REPLACED. (When using fragmentTransaction.add is not necessary) the back stack.
*/
fun <VH : RecyclerView.ViewHolder> RecyclerView.setNullableAdapter(
adapter: RecyclerView.Adapter<VH>
) {
this.adapter = adapter
this.clearReference()
@ChristopherME
ChristopherME / MovieListUiState.kt
Last active May 7, 2021 01:56
Holds the state of the MovieListFragment.
/**
* Represents the state to render the UI in MovieListFragment.
*
* @param isLoading if true we have to show a progress bar, else hide the progress bar.
* @param movies this list will be submited into recyclerview adapter.
* @param error OneTimeEvent that wraps a failure object for display a Toast, Snackbar, etc only once.
*/
data class MovieListUiState(
val isLoading: Boolean = false,
val movies: List<MovieUi> = emptyList(),
@ChristopherME
ChristopherME / MovieListFragment.kt
Created May 6, 2021 18:19
Fragment that shows a list of movies.
class MovieListFragment : Fragment(R.layout.fragment_movie_list) {
...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initView()
collectUiState()
}
@ChristopherME
ChristopherME / RetrofitSafeCall.kt
Created May 15, 2021 12:33
Safe retrofit call extension
/*
* This would be your remote data source implementation. Only the [ActorsRemoteDataSource] is injected in your repository implementation.
*/
internal class ActorsRemoteDataSourceImpl(
private val middlewareProvider: MiddlewareProvider,
private val ioDispatcher: CoroutineDispatcher,
private val errorAdapter: JsonAdapter<ResponseError>,
private val actorsService: ActorsService
) : ActorsRemoteDataSource {