summaryrefslogtreecommitdiff
path: root/compiler/src/main/kotlin/android/databinding/tool/writer/DynamicUtilWriter.kt
blob: 8d76b030c315d1d3149a1fdfd3148fdc83253b0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package android.databinding.tool.writer;

class DynamicUtilWriter() {
    public fun write(targetSdk : kotlin.Int) : KCode = kcode("package android.databinding;") {
        nl("")
        nl("import android.os.Build.VERSION;")
        nl("import android.os.Build.VERSION_CODES;")
        nl("")
        block("public class DynamicUtil") {
            nl("@SuppressWarnings(\"deprecation\")")
            block("public static int getColorFromResource(final android.view.View root, final int resourceId)") {
                if (targetSdk >= 23) {
                    block("if (VERSION.SDK_INT >= VERSION_CODES.M)") {
                        nl("return root.getContext().getColor(resourceId);")
                    }
                }
                nl("return root.getResources().getColor(resourceId);")
            }

            block("public static boolean parse(String str, boolean fallback)") {
                block("if (str == null)") {
                    nl("return fallback;");
                }
                nl("return Boolean.parseBoolean(str);")
            }
            block("public static byte parse(String str, byte fallback)") {
                block("try") {
                    nl("return Byte.parseByte(str);")
                }
                block("catch (NumberFormatException e)") {
                    nl("return fallback;")
                }
            }
            block("public static short parse(String str, short fallback)") {
                block("try") {
                    nl("return Short.parseShort(str);")
                }
                block("catch (NumberFormatException e)") {
                    nl("return fallback;")
                }
            }
            block("public static int parse(String str, int fallback)") {
                block("try") {
                    nl("return Integer.parseInt(str);")
                }
                block("catch (NumberFormatException e)") {
                    nl("return fallback;")
                }
            }
            block("public static long parse(String str, long fallback)") {
                block("try") {
                    nl("return Long.parseLong(str);")
                }
                block("catch (NumberFormatException e)") {
                    nl("return fallback;")
                }
            }
            block("public static float parse(String str, float fallback)") {
                block("try") {
                    nl("return Float.parseFloat(str);")
                }
                block("catch (NumberFormatException e)") {
                    nl("return fallback;")
                }
            }
            block("public static double parse(String str, double fallback)") {
                block("try") {
                    nl("return Double.parseDouble(str);")
                }
                block("catch (NumberFormatException e)") {
                    nl("return fallback;")
                }
            }
            block("public static char parse(String str, char fallback)") {
                block ("if (str == null || str.isEmpty())") {
                    nl("return fallback;")
                }
                nl("return str.charAt(0);")
            }
        }
   }
}