1 /* 2 * Copyright 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.navigation.safe.args.generator 18 19 import androidx.navigation.safe.args.generator.models.ResReference 20 21 interface NavType { bundlePutMethodnull22 fun bundlePutMethod(): String 23 24 fun bundleGetMethod(): String 25 26 fun allowsNullable(): Boolean 27 28 companion object { 29 fun from(name: String?, rFilePackage: String? = null) = 30 when (name) { 31 "integer" -> IntType 32 "integer[]" -> IntArrayType 33 "long" -> LongType 34 "long[]" -> LongArrayType 35 "float" -> FloatType 36 "float[]" -> FloatArrayType 37 "boolean" -> BoolType 38 "boolean[]" -> BoolArrayType 39 "reference" -> ReferenceType 40 "reference[]" -> ReferenceArrayType 41 "string" -> StringType 42 "string[]" -> StringArrayType 43 null -> StringType 44 else -> { 45 val prependPackageName = 46 if (name.startsWith(".") && rFilePackage != null) { 47 rFilePackage 48 } else { 49 "" 50 } 51 if (name.endsWith("[]")) { 52 ObjectArrayType(prependPackageName + name.substringBeforeLast("[]")) 53 } else { 54 ObjectType(prependPackageName + name) 55 } 56 } 57 } 58 } 59 } 60 61 object IntType : NavType { bundlePutMethodnull62 override fun bundlePutMethod() = "putInt" 63 64 override fun bundleGetMethod() = "getInt" 65 66 override fun toString() = "integer" 67 68 override fun allowsNullable() = false 69 } 70 71 object IntArrayType : NavType { 72 override fun bundlePutMethod() = "putIntArray" 73 74 override fun bundleGetMethod() = "getIntArray" 75 76 override fun toString() = "integer[]" 77 78 override fun allowsNullable() = true 79 } 80 81 object LongType : NavType { bundlePutMethodnull82 override fun bundlePutMethod() = "putLong" 83 84 override fun bundleGetMethod() = "getLong" 85 86 override fun toString() = "long" 87 88 override fun allowsNullable() = false 89 } 90 91 object LongArrayType : NavType { 92 override fun bundlePutMethod() = "putLongArray" 93 94 override fun bundleGetMethod() = "getLongArray" 95 96 override fun toString() = "long[]" 97 98 override fun allowsNullable() = true 99 } 100 101 object FloatType : NavType { bundlePutMethodnull102 override fun bundlePutMethod() = "putFloat" 103 104 override fun bundleGetMethod() = "getFloat" 105 106 override fun toString() = "float" 107 108 override fun allowsNullable() = false 109 } 110 111 object FloatArrayType : NavType { 112 override fun bundlePutMethod() = "putFloatArray" 113 114 override fun bundleGetMethod() = "getFloatArray" 115 116 override fun toString() = "float[]" 117 118 override fun allowsNullable() = true 119 } 120 121 object StringType : NavType { bundlePutMethodnull122 override fun bundlePutMethod() = "putString" 123 124 override fun bundleGetMethod() = "getString" 125 126 override fun toString() = "string" 127 128 override fun allowsNullable() = true 129 } 130 131 object StringArrayType : NavType { 132 override fun bundlePutMethod() = "putStringArray" 133 134 override fun bundleGetMethod() = "getStringArray" 135 136 override fun toString() = "string[]" 137 138 override fun allowsNullable() = true 139 } 140 141 object BoolType : NavType { bundlePutMethodnull142 override fun bundlePutMethod() = "putBoolean" 143 144 override fun bundleGetMethod() = "getBoolean" 145 146 override fun toString() = "boolean" 147 148 override fun allowsNullable() = false 149 } 150 151 object BoolArrayType : NavType { 152 override fun bundlePutMethod() = "putBooleanArray" 153 154 override fun bundleGetMethod() = "getBooleanArray" 155 156 override fun toString() = "boolean" 157 158 override fun allowsNullable() = true 159 } 160 161 object ReferenceType : NavType { bundlePutMethodnull162 override fun bundlePutMethod() = "putInt" 163 164 override fun bundleGetMethod() = "getInt" 165 166 override fun toString() = "reference" 167 168 override fun allowsNullable() = false 169 } 170 171 object ReferenceArrayType : NavType { 172 override fun bundlePutMethod() = "putIntArray" 173 174 override fun bundleGetMethod() = "getIntArray" 175 176 override fun toString() = "reference[]" 177 178 override fun allowsNullable() = true 179 } 180 181 data class ObjectType(val canonicalName: String) : NavType { bundlePutMethodnull182 override fun bundlePutMethod() = 183 throw UnsupportedOperationException("Use addBundlePutStatement instead.") 184 185 override fun bundleGetMethod() = 186 throw UnsupportedOperationException("Use addBundleGetStatement instead.") 187 188 override fun toString() = "parcelable or serializable" 189 190 override fun allowsNullable() = true 191 } 192 193 data class ObjectArrayType(val canonicalName: String) : NavType { 194 override fun bundlePutMethod() = "putParcelableArray" 195 196 override fun bundleGetMethod() = "getParcelableArray" 197 198 override fun toString() = "parcelable array" 199 200 override fun allowsNullable() = true 201 } 202 203 interface WritableValue 204 205 data class ReferenceValue(val resReference: ResReference) : WritableValue 206 207 data class StringValue(val value: String) : WritableValue 208 209 // keeping value as String, it will help to preserve client format of it: hex, dec 210 data class IntValue(val value: String) : WritableValue 211 212 // keeping value as String, it will help to preserve client format of it: hex, dec 213 data class LongValue(val value: String) : WritableValue 214 215 // keeping value as String, it will help to preserve client format of it: scientific, dot 216 data class FloatValue(val value: String) : WritableValue 217 218 data class BooleanValue(val value: String) : WritableValue 219 220 object NullValue : WritableValue 221 222 data class EnumValue(val type: ObjectType, val value: String) : WritableValue 223