Skip to content

Instantly share code, notes, and snippets.

View cdmunoz's full-sized avatar

Carlos Daniel cdmunoz

View GitHub Profile
/// this is part of a stateful widget, that uses Isolates.compute to do a heavy task, in this case download and
/// display an image from internet in parallel.
/// this will help to improve performance for our scenario when a chat messages screen should display all messages
class _MessagesImageState extends State<MessagesImage> {
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
// onTap logic
@cdmunoz
cdmunoz / MaxInversions.kt
Created March 12, 2020 15:44
Solution to Hackerrank's Max Inversions problem
// Complete the maxInversions function below.
fun maxInversions(prices: Array<Int>): Long {
var inversionsCounter = 0L
val arraySize = prices.size
for (i in 0 until arraySize - 1) {
var smaller = 0 //smaller elements on right
for (j in i + 1 until arraySize) {
if (prices[i] > prices[j]) {
smaller++
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@cdmunoz
cdmunoz / Result.kt
Created April 22, 2020 20:54
Generic Result sealed class to UI state management for a network result
sealed class Result<out T : Any> {
data class Success<out T : Any>(val data: T) : Result<T>()
data class Error(val exception: Exception) : Result<Nothing>()
object InProgress : Result<Nothing>()
val extractData: T?
get() = when (this) {
is Success -> data
is Error -> null
is InProgress -> null
@cdmunoz
cdmunoz / TextInputLayoutWithItemHint.java
Created October 13, 2018 23:16
TextInputLayoutWithItemHint: Verifies if a text matches the text of a TextInputLayout hint
public static Matcher<View> textInputLayoutwithItemHint(final String matcherText) {
return new BoundedMatcher<View, TextInputLayout>(TextInputLayout.class) {
@Override
public void describeTo(Description description) {
description.appendText("with item hint: " + matcherText);
}
@Override
protected boolean matchesSafely(TextInputLayout editTextField) {
@cdmunoz
cdmunoz / TestNetworkModule.kt
Created September 4, 2020 00:22
Koin's test network module
fun testNetworkModule(baseUrl: String) = module {
single { provideTestRetrofit(baseUrl) }
single { provideTestApiService(get(), ApiService::class.java) }
}
fun provideTestRetrofit(baseUrl: String): Retrofit =
Retrofit.Builder().baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create()).build()
fun provideTestApiService(retrofit: Retrofit, apiService: Class<ApiService>) =
@cdmunoz
cdmunoz / NasaRoverPhotosApp.kt
Created September 3, 2020 23:47
Koin's start within Application class
class NasaRoverPhotosApp : Application() {
override fun onCreate() {
super.onCreate()
Timber.plant(DebugTree())
startKoin {
androidContext(this@NasaRoverPhotosApp)
modules(appModules)
}
}
@cdmunoz
cdmunoz / ViewModelModule.kt
Created September 3, 2020 23:40
Koin's view model module
val viewModelModule = module {
viewModel { PhotosViewModel(get()) }
}
@cdmunoz
cdmunoz / RepositoryModule.kt
Created September 3, 2020 23:39
Koin's Repository module
val repositoryModule = module {
factory {
PhotosRepository(get())
}
}
@cdmunoz
cdmunoz / NetworkModule.kt
Created September 3, 2020 23:34
Koin's network module
val networkModule = module {
single { provideOkHttpClient() }
single { provideRetrofit(get()) }
single { provideApiService(get(), ApiService::class.java) }
}
fun provideOkHttpClient(): OkHttpClient = OkHttpClient.Builder().build()
fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit =
Retrofit.Builder().baseUrl(BuildConfig.BASE_URL)