Skip to content

Instantly share code, notes, and snippets.

View marcelpinto's full-sized avatar

Marcel Pintó Biescas marcelpinto

View GitHub Profile
@marcelpinto
marcelpinto / FirebaseLiveData.kt
Last active October 14, 2018 11:55
Example of Firebase LiveData mechanism
class FirebaseLiveData<T>(
private val reference: DatabaseReference,
private val resourceType: Class<T>
) : LiveData<Resource<T>>(), ValueEventListener {
override fun onActive() {
super.onActive()
if (value == null) {
value = Resource.loading()
}
class NetworkFactoryTest {
private val baseUrl = "/"
private val testData = TestData("hello!")
private val testDataJson = "{\"name\":\"${testData.name}\"}"
private val mockWebServer = MockWebServer()
private val httpUrl = mockWebServer.url(baseUrl)
private val moshi = createMoshi()
@marcelpinto
marcelpinto / NetworkFactory.kt
Last active July 21, 2018 19:01
Testing your Network
class NetworkFactory(moshi: Moshi, authInterceptor: AuthenticationInterceptor, authenticator: Authenticator) {
private val okHttpClient: OkHttpClient
private val retrofitBuilder: Retrofit.Builder
init {
val level = if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE
val loggingInterceptor = HttpLoggingInterceptor().setLevel(level)
okHttpClient = OkHttpClient.Builder()
@marcelpinto
marcelpinto / new_map_xkcd.html
Last active May 29, 2018 12:13
New Map xkcd
<!DOCTYPE html>
<meta charset="utf-8">
<link rel="stylesheet" href="http://cmx.io/v/0.1/cmx.css">
<script src="http://cmx.io/v/0.1/cmx.js" charset="utf-8"></script>
<style>.cmx-user-scene4 .cmx-text-border .cmx-path {stroke: orange}</style>
<body>
<div style="max-width:900px; -webkit-transform:rotate(0deg)">
<scene id="scene1">
<label t="translate(0,346)">
@marcelpinto
marcelpinto / ApiResponse.kt
Created April 30, 2018 09:20
WeatherExample that shows the getObservation method
sealed class ApiResponse<out T, out S> {
data class Success<out T>(val result: T) : ApiResponse<T, Any>()
data class Failure<out T, out S>(val code: Int, val errorBody: S? = null) : ApiResponse<T, S>()
data class Exception<out T, out S>(val throwable: Throwable) : ApiResponse<T, S>()
}
@marcelpinto
marcelpinto / WeatherRepository.kt
Created April 30, 2018 09:18
WeatherExample on how to create the retrofit instance
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
val retrofit = Retrofit.Builder()
.baseUrl("https://weather.api.here.com")
.addConverterFactory(MoshiConverterFactory.create(moshi))
.build()
weatherApi = retrofit.create(WeatherApi::class.java)
package com.here.example.weather
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Query
interface WeatherApi {
@GET("/weather/1.0/report.json?app_id={YOUR_APP_ID}&app_code={YOUR_APP_CODE}")
fun getObservations(@Query("product") product: String,
dependencies {
// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
// Views
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
// Network
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
@marcelpinto
marcelpinto / RxViewUtils.java
Last active August 29, 2015 14:26
Class use to create the RxForms explained in ...
package com.eatfirst.android.utils;
import android.support.design.widget.TextInputLayout;
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.android.widget.OnTextChangeEvent;
import rx.android.widget.WidgetObservable;
import rx.functions.Func1;
/*
* Copyright (c) 2015 Express Quality Food Global Service GmbH. All rights reserved.
*
* 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