Skip to content

Instantly share code, notes, and snippets.

View ChristopherME's full-sized avatar

Christopher Elias ChristopherME

View GitHub Profile
/**
* Calculates the Y pixel coordinate for a given transaction rate.
*
* @param higherTransactionRateValue the highest rate value in the whole list of transactions.
* @param currentTransactionRate the current transaction RATE while iterating the list of transactions.
* @param canvasHeight the canvas HEIGHT for draw the linear chart.
*
* @return [Float] Y coordinate for a transaction rate.
*/
@Composable
fun LinearTransactionsChart(
modifier: Modifier = Modifier,
transactionsPerSecond: TransactionsPerSecond
) {
if (transactionsPerSecond.transactions.isEmpty()) return
Canvas(modifier = modifier) {
// Total number of transactions.
val totalRecords = transactionsPerSecond.transactions.size
@HiltViewModel
class ProductDetailViewModel @Inject constructor(
private val savedStateHandle: SavedStateHandle,
...
) : ViewModel() {
...
private fun getProductFromArgs(): ProductDetailArgs {
@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 {
@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 / 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 / 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 / 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
) {
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))
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