Skip to content

Instantly share code, notes, and snippets.

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

Jens Hohl macsystems

🏠
Working from home
View GitHub Profile
@macsystems
macsystems / ViewDebounceClickListenerExt.kt
Created January 28, 2020 15:33
Debounce Click Listener Extension which prevents user invoking some function to often
private const val defaultDebounce: Long = 500
/**
* Prevents that the click events gets called too often by fast clicking user
*/
@UiThread
fun View.setDebounceOnClickListener(@IntRange(from = 0, to = Integer.MAX_VALUE.toLong()) debounceTime: Long = defaultDebounce, listener: (View) -> Unit) {
this.setOnClickListener(object : View.OnClickListener {
private var lastClickTime: Long = 0
override fun onClick(v: View) {
@macsystems
macsystems / ProgressBarExt.kt
Last active January 22, 2020 11:47
Using sealed class to change progressbar visibility
fun ProgressBar.handleState(resource: Resource<Any>) {
visibility = when (resource) {
is Loading -> View.VISIBLE
is Error -> View.GONE
is Success -> View.GONE
}
}
@macsystems
macsystems / Channel.java
Last active January 20, 2020 08:30
If you want to parse an RSS Feed using SimpleXML you can use this as an Start. I used this to parse RSS using RetroFit from Square
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.NamespaceList;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Text;
import java.util.List;
@macsystems
macsystems / BottomSheetExt.kt
Created January 19, 2020 14:35
Extension for BottomSheet.BottomSheetBehavior which invokes an function when sheet is collapsed, this can be useful is the sheet need to display new data
/**
* Call to populate new data for Bottomsheet.
* If Bottomsheet is shown it will collapse first, then it will invoke doWhenCollapsed
*/
inline fun <reified T : View> BottomSheetBehavior<T>.colapseAndShow(noinline doWhenCollapsed: () -> Unit) {
when (state) {
STATE_EXPANDED,
STATE_HALF_EXPANDED -> {
addBottomSheetCallback(WaitForCollapsedState(this, doWhenCollapsed))
@macsystems
macsystems / IntentExt.kt
Created December 1, 2019 16:52
write any type in an Intent
fun Intent.putAnyExtra(name: String, value: Any) {
when (value) {
is Int -> putExtra(name, value)
is Byte -> putExtra(name, value)
is Short -> putExtra(name, value)
is Long -> putExtra(name, value)
is Char -> putExtra(name, value)
is Float -> putExtra(name, value)
is Double -> putExtra(name, value)
is Boolean -> putExtra(name, value)
@macsystems
macsystems / AndroidFontFamiliesByVersion.mk
Last active August 15, 2017 20:28
Font Families on Android Versions
Added in Android Jelly Bean (4.1) - API 16 :
Regular (default):
<item name="android:fontFamily">sans-serif</item>
<item name="android:textStyle">normal</item>
Italic:
<item name="android:fontFamily">sans-serif</item>
@macsystems
macsystems / RxJavaRule.java
Created December 12, 2016 15:20
Custom JUnit Test Rule which allows to override the Main Thread and oder Threads used by RxJava
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import io.reactivex.android.plugins.RxAndroidPlugins;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.schedulers.Schedulers;
@macsystems
macsystems / Timezones.java
Last active April 14, 2016 08:06
Timezones problem
@Test
public void timezoneProblem() throws ParseException {
TimeZone utcTime = TimeZone.getTimeZone("UTC");
TimeZone berlinTime = TimeZone.getTimeZone("Europe/Berlin");
Calendar calendar = new GregorianCalendar();
calendar.setTimeZone(utcTime);
int hourOfDay_1 = calendar.get(Calendar.HOUR_OF_DAY);
calendar.setTimeZone(berlinTime);
int hourOfDay_2 = calendar.get(Calendar.HOUR_OF_DAY);
@macsystems
macsystems / SharedPreferencesUtil
Last active December 17, 2015 13:49
Some class i wrote today to transparent update the BackupAgent when using the SharedPreferences Editor and the BackupAgent. The Basic Idea is to have an Util class which provides access to the SharedPreferences and its Editor
/**
* Utils class which allows to write more easy access of the {@link SharedPreferences}
*
* @author Jens Hohl
*
*/
public final class SharedPreferencesUtil
{
@macsystems
macsystems / gist:5262760
Created March 28, 2013 12:30
Some Proxy class to get Logging for register/unregister of Objects in greenrobots EventBus
import android.util.Log;
import com.google.common.eventbus.EventBus;
/**
* An Proxy which allows better log control