Skip to content

Instantly share code, notes, and snippets.

View lgawin's full-sized avatar

Łukasz Gawin lgawin

  • FoQuS
  • Warsaw, Poland
View GitHub Profile
@lgawin
lgawin / gist:6118253
Created July 31, 2013 00:16
Sample code for xmpp client
'''
Created on Jul 31, 2013
@author: gawcio
'''
import sys, json, xmpp, random, string
SERVER = 'gcm.googleapis.com'
PORT = 5235
public static void main(String... args) throws IOException {
String host = args[0];
int port = Integer.parseInt(args[1]);
String botName = args[2];
String botKey = args[3];
System.out.println("Connecting to " + host + ":" + port + " as "
+ botName + "/" + botKey);
//final Socket socket = new Socket(host, port);
@lgawin
lgawin / RemoteObject.java
Last active August 29, 2015 14:15
Example usage of guava's Equivalence, when in some cases, we just want to compare host (authority) parts of URLs
package pl.lgawin.guavasandbox.equivalence;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Objects.equal;
import com.google.common.base.Objects;
import com.google.common.net.MediaType;
public class RemoteObject {
@lgawin
lgawin / ic_android_black_24dp.xml
Created December 1, 2017 18:28
This is the way to have vector drawable that uses `?colorControlNormal`
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#ff000000"
android:pathData="M6,18c0,0.55 0.45,1 1,1h1v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L11,19h2v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L16,19h1c0.55,0 1,-0.45 1,-1L18,8L6,8v10zM3.5,8C2.67,8 2,8.67 2,9.5v7c0,0.83 0.67,1.5 1.5,1.5S5,17.33 5,16.5v-7C5,8.67 4.33,8 3.5,8zM20.5,8c-0.83,0 -1.5,0.67 -1.5,1.5v7c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5v-7c0,-0.83 -0.67,-1.5 -1.5,-1.5zM15.53,2.16l1.3,-1.3c0.2,-0.2 0.2,-0.51 0,-0.71 -0.2,-0.2 -0.51,-0.2 -0.71,0l-1.48,1.48C13.85,1.23 12.95,1 12,1c-0.96,0 -1.86,0.23 -2.66,0.63L7.85,0.15c-0.2,-0.2 -0.51,-0.2 -0.71,0 -0.2,0.2 -0.2,0.51 0,0.71l1.31,1.31C6.97,3.26 6,5.01 6,7h12c0,-1.99 -0.97,-3.75 -2.47,-4.84zM10,5L9,5L9,4h1v1zM15,5h-1L14,4h1v1z" />
</vector>
private inline val Calendar.year: Int
get() = get(Calendar.YEAR)
private inline val Calendar.month: Int
get() = get(Calendar.MONTH)
private inline val Calendar.day: Int
get() = get(Calendar.DAY_OF_MONTH)
@lgawin
lgawin / DatePickerBindingAdapter.kt
Created April 11, 2019 11:11
Two-way adapter for `DatePicker` when using ThreeTenAbp
@BindingAdapter("date")
fun setDate(datePicker: DatePicker, value: LocalDate) {
if (value != LocalDate.of(datePicker.year, datePicker.month + 1, datePicker.dayOfMonth)) {
datePicker.updateDate(value.year, value.monthValue - 1, value.dayOfMonth)
}
}
@BindingAdapter(value = ["dateAttrChanged", "onDateChanged"], requireAll = false)
fun setDateChangeListener(
datePicker: DatePicker,
@lgawin
lgawin / DiffObservableListKtx.kt
Last active September 26, 2019 21:48
ObservableList extensions for `me.tatarka.bindingcollectionadapter2.collections`
import androidx.databinding.ObservableList
import androidx.recyclerview.widget.DiffUtil
import me.tatarka.bindingcollectionadapter2.collections.AsyncDiffObservableList
import me.tatarka.bindingcollectionadapter2.collections.DiffObservableList
@Suppress("NOTHING_TO_INLINE", "FunctionName")
inline fun <T : Any> DiffObservableList(
itemCallback: DiffUtil.ItemCallback<T>,
async: Boolean = true
): DiffList<T> =
@lgawin
lgawin / DiffUtilKtx.kt
Last active January 24, 2020 13:32
Some helper methods for RecyclerView's `DiffUtil`s
import androidx.recyclerview.widget.DiffUtil
import kotlin.reflect.KProperty1
@Suppress("NOTHING_TO_INLINE")
inline fun <T : Any, R: Any> byProperty(kProperty: KProperty1<T, R>): (T, T) -> Boolean =
{ old, new -> kProperty.get(old) == kProperty.get(new) }
@Suppress("FunctionName")
inline fun <T : Any> DiffItemCallback(
crossinline areItemsTheSame: (T, T) -> Boolean,
@lgawin
lgawin / FindViewKtx.kt
Created May 6, 2020 08:28
Delegate for finding view by id
import android.app.Activity
import android.view.View
import androidx.annotation.IdRes
import androidx.fragment.app.Fragment
fun <T : View> Activity.findView(@IdRes res : Int) : Lazy<T> {
@Suppress("UNCHECKED_CAST")
return lazy(LazyThreadSafetyMode.NONE){ findViewById<T>(res) }
}
package com.example.android.testing.blueprint
import android.content.res.AssetManager
import okhttp3.Interceptor
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Protocol
import okhttp3.Request
import okhttp3.Response
import okhttp3.ResponseBody.Companion.toResponseBody