1 /*
<lambda>null2 * Copyright (C) 2022 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 com.android.settingslib.spa.framework.util
18
19 import android.os.Bundle
20 import androidx.navigation.NamedNavArgument
21 import androidx.navigation.NavType
22
23 const val RUNTIME_PARAM_PREFIX = "rt_"
24 const val UNSET_PARAM_PREFIX = "unset_"
25 const val UNSET_PARAM_VALUE = "[unset]"
26
27 fun List<NamedNavArgument>.navRoute(): String {
28 return this.joinToString("") { argument -> "/{${argument.name}}" }
29 }
30
Listnull31 fun List<NamedNavArgument>.navLink(arguments: Bundle? = null): String {
32 return normalizeArgList(arguments).joinToString("") { arg -> "/$arg" }
33 }
34
Listnull35 fun List<NamedNavArgument>.normalizeArgList(
36 arguments: Bundle? = null,
37 eraseRuntimeValues: Boolean = false
38 ): List<String> {
39 val argsArray = mutableListOf<String>()
40 for (navArg in this) {
41 if (eraseRuntimeValues && navArg.isRuntimeParam()) continue
42 if (arguments == null || !arguments.containsKey(navArg.name)) {
43 argsArray.add(UNSET_PARAM_VALUE)
44 continue
45 }
46 when (navArg.argument.type) {
47 NavType.StringType -> {
48 argsArray.add(arguments.getString(navArg.name, ""))
49 }
50 NavType.IntType -> {
51 argsArray.add(arguments.getInt(navArg.name).toString())
52 }
53 }
54 }
55 return argsArray
56 }
57
normalizenull58 fun List<NamedNavArgument>.normalize(
59 arguments: Bundle? = null,
60 eraseRuntimeValues: Boolean = false
61 ): Bundle? {
62 if (this.isEmpty()) return null
63 val normArgs = Bundle()
64 for (navArg in this) {
65 // Erase value of runtime parameters.
66 if (eraseRuntimeValues && navArg.isRuntimeParam()) {
67 normArgs.putString(navArg.name, null)
68 continue
69 }
70
71 when (navArg.argument.type) {
72 NavType.StringType -> {
73 val value = arguments?.getString(navArg.name)
74 if (value != null)
75 normArgs.putString(navArg.name, value)
76 else
77 normArgs.putString(UNSET_PARAM_PREFIX + navArg.name, null)
78 }
79 NavType.IntType -> {
80 if (arguments != null && arguments.containsKey(navArg.name))
81 normArgs.putInt(navArg.name, arguments.getInt(navArg.name))
82 else
83 normArgs.putString(UNSET_PARAM_PREFIX + navArg.name, null)
84 }
85 }
86 }
87 return normArgs
88 }
89
Listnull90 fun List<NamedNavArgument>.getStringArg(name: String, arguments: Bundle? = null): String? {
91 if (this.containsStringArg(name) && arguments != null) {
92 return arguments.getString(name)
93 }
94 return null
95 }
96
Listnull97 fun List<NamedNavArgument>.getIntArg(name: String, arguments: Bundle? = null): Int? {
98 if (this.containsIntArg(name) && arguments != null && arguments.containsKey(name)) {
99 return arguments.getInt(name)
100 }
101 return null
102 }
103
containsStringArgnull104 fun List<NamedNavArgument>.containsStringArg(name: String): Boolean {
105 for (navArg in this) {
106 if (navArg.argument.type == NavType.StringType && navArg.name == name) return true
107 }
108 return false
109 }
110
containsIntArgnull111 fun List<NamedNavArgument>.containsIntArg(name: String): Boolean {
112 for (navArg in this) {
113 if (navArg.argument.type == NavType.IntType && navArg.name == name) return true
114 }
115 return false
116 }
117
NamedNavArgumentnull118 fun NamedNavArgument.isRuntimeParam(): Boolean {
119 return this.name.startsWith(RUNTIME_PARAM_PREFIX)
120 }
121