Skip to content

Instantly share code, notes, and snippets.

View vmax's full-sized avatar
🏠
Working from home

Max Vorobev vmax

🏠
Working from home
View GitHub Profile
@vmax
vmax / classproperty.py
Last active June 24, 2018 03:10
@classproperty decorator
class ClassPropertyDescriptor(object):
def __init__(self, fget, fset=None):
self.fget = fget
self.fset = fset
def __get__(self, obj, klass=None):
if klass is None:
klass = type(obj)
return self.fget.__get__(obj, klass)()
@vmax
vmax / searchable.swift
Last active March 1, 2017 20:19
Easy way to make data in UITableView searchable
class DataViewController: UITableViewController, UISearchResultsUpdating {
var data = [Data]()
var filteredData = [Data]()
@available(iOS 8.0, *)
public func updateSearchResults(for searchController: UISearchController) {
let query = searchController.searchBar.text!
filteredData = data.filter { element in
@vmax
vmax / DateTimeAwareJSONEncoder.py
Created August 31, 2016 11:51
Class that helps to serialize Python datetime/date/timedelta class
class DateTimeAwareJSONEncoder(json.JSONEncoder):
"""
A JSONEncoder subclass for handling objects of types `timedelta`, `datetime` and `date`
"""
def default(self, obj):
if isinstance(obj, datetime):
return {
'__type__': 'datetime',
'year': obj.year,
'month': obj.month,
@vmax
vmax / LayoutTransitionLogger.java
Created July 25, 2016 10:02
Class that helps to debug layout changes animation
package vmax.myway;
import android.animation.LayoutTransition;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
public class LayoutTransitionLogger implements LayoutTransition.TransitionListener {
private String logTAG;
@vmax
vmax / MoscowSubwayLineUtil.java
Created July 21, 2016 09:27
Class that helps converting Moscow Subway line names (in Russian) to their colors
import java.util.HashMap;
import android.graphics.Color;
import android.support.annotation.ColorInt;
/**
* Class that helps converting Moscow Subway line names (in Russian) to their colors
* Based on https://en.wikipedia.org/wiki/Moscow_Metro#Lines
*/
public class MoscowSubwayLineUtil {
@vmax
vmax / EmptyRecyclerView.java
Last active July 10, 2016 12:29 — forked from meoyawn/EmptyRecyclerView.java
RecyclerView doesn't have an emptyView support, we gotta fix that (but you may also need to show a view when it's non empty, that's why the fork)
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
public class EmptyRecyclerView extends RecyclerView {
private View emptyView, nonEmptyView;
final private AdapterDataObserver observer = new AdapterDataObserver() {
@vmax
vmax / DelayAutoCompleteTextView.java
Created July 10, 2016 12:26
AutoCompleteTextView that delays sending requests to adapter (needed if suggestions are being queried from external API). Also shows progress bar when loading suggestions.
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.ProgressBar;
public class DelayAutoCompleteTextView extends AutoCompleteTextView {
@vmax
vmax / ViewWeightAnimationWrapper.java
Created April 4, 2016 17:57
A wrapper for animating changes of android:weight property of views inside LinearLayout using ObjectAnimator
public class ViewWeightAnimationWrapper {
private View view;
public ViewWeightAnimationWrapper(View view) {
if (view.getLayoutParams() instanceof LinearLayout.LayoutParams) {
this.view = view;
} else {
throw new IllegalArgumentException("The view should have LinearLayout as parent");
}
}