Created
August 22, 2018 14:25
-
-
Save pasmat/f1f8f04eeab4dde7dc9b534a51f67d15 to your computer and use it in GitHub Desktop.
Data-binded values that are boxed(such as ones that come from Kotlin, as there's no primitive types in Kotlin..) requires safe unboxing, so there's no NPE(null pointer exception) when binding data. Theres a method for this safeUnbox inside Android SDK, however this doesn't support two-way data binding. Therefore we have to define these two-way s…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fi.matalamaki.util; | |
import androidx.databinding.InverseMethod; | |
/** | |
* Data-binded values that are boxed(such as ones that come from Kotlin, | |
* as there's no primitive types in Kotlin..) requires safe unboxing, | |
* so there's no NPE(null pointer exception) when binding data. | |
* | |
* Theres a method for this safeUnbox inside Android SDK, | |
* however this doesn't support two-way data binding. | |
* | |
* Therefore we have to define these two-way safe unboxing methods by ourselves. | |
* | |
* example usage inside xml | |
* | |
* | |
<?xml version="1.0" encoding="utf-8"?> | |
<layout xmlns:android="http://schemas.android.com/apk/res/android" | |
<data> | |
<import type="fi.matalamaki.util.Boxer"/> | |
<variable | |
name="intParameter" | |
type="java.lang.Integer"/> | |
</data> | |
<SeekBar | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:progress="@={Boxer.unbox(intParameter)}"/> | |
</layout> | |
* | |
* | |
*/ | |
public class Boxer { | |
public static final char DEAULT_CHAR = ' '; | |
private static final int DEFAULT_NUMBER = 0; | |
@InverseMethod("boxBoolean") | |
public static boolean unbox(Boolean b) { | |
return (b != null) && b; | |
} | |
public static Boolean boxBoolean(boolean b) { | |
return b; | |
} | |
@InverseMethod("boxChar") | |
public static char unbox(Character c) { | |
return c != null ? c : DEAULT_CHAR; | |
} | |
public static Character boxChar(char c) { | |
return c; | |
} | |
@InverseMethod("boxByte") | |
public static byte unbox(Byte b) { | |
return b != null ? b : DEFAULT_NUMBER; | |
} | |
public static Byte boxByte(byte b) { | |
return b; | |
} | |
@InverseMethod("boxShort") | |
public static short unbox(Short s) { | |
return s != null ? s : DEFAULT_NUMBER; | |
} | |
public static Short boxShort(short s) { | |
return s; | |
} | |
@InverseMethod("boxInteger") | |
public static int unbox(Integer i) { | |
return i != null ? i : DEFAULT_NUMBER; | |
} | |
public static Integer boxInteger(int i) { | |
return i; | |
} | |
@InverseMethod("boxInteger") | |
public static long unbox(Long l) { | |
return l != null ? l : DEFAULT_NUMBER; | |
} | |
public static Long boxLong(long l) { | |
return l; | |
} | |
@InverseMethod("boxFloat") | |
public static float unbox(Float f) { | |
return f != null ? f : DEFAULT_NUMBER; | |
} | |
public static Float boxFloat(float f) { | |
return f; | |
} | |
@InverseMethod("boxDouble") | |
public static double unbox(Double d) { | |
return (d != null) ? d : DEFAULT_NUMBER; | |
} | |
public static Double boxDouble(double d) { | |
return d; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
viewModel must be null in this case, so you'd need to check whether the viewModel is null or make sure the viewModel is never null