1 /*
2  * Copyright (C) 2017 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 package androidx.navigation
17 
18 import androidx.annotation.IdRes
19 import androidx.savedstate.SavedState
20 import androidx.savedstate.read
21 
22 /**
23  * Navigation actions provide a level of indirection between your navigation code and the underlying
24  * destinations. This allows you to define common actions that change their destination or
25  * [NavOptions] based on the current [NavDestination].
26  *
27  * The [NavOptions] associated with a NavAction are used by default when navigating to this action
28  * via [NavController.navigate].
29  *
30  * Actions should be added via [NavDestination.putAction].
31  *
32  * @param destinationId the ID of the destination that should be navigated to when this action is
33  *   used.
34  * @param navOptions special options for this action that should be used by default
35  * @param defaultArguments argument SavedState to be used by default
36  */
37 public class NavAction
38 @JvmOverloads
39 constructor(
40     /** The ID of the destination that should be navigated to when this action is used */
41     @field:IdRes @param:IdRes public val destinationId: Int,
42     /** The NavOptions to be used by default when navigating to this action. */
43     public var navOptions: NavOptions? = null,
44     /**
45      * The argument SavedState to be used by default when navigating to this action.
46      *
47      * @return SavedState of default argument values
48      */
49     public var defaultArguments: SavedState? = null
50 ) {
51 
equalsnull52     override fun equals(other: Any?): Boolean {
53         if (this === other) return true
54         if (other !is NavAction) return false
55 
56         if (destinationId != other.destinationId) return false
57         if (navOptions != other.navOptions) return false
58 
59         val args1 = defaultArguments
60         val args2 = other.defaultArguments
61 
62         if (args1 == args2) return true
63         return args1 != null && args2 != null && args1.read { contentDeepEquals(args2) }
64     }
65 
hashCodenull66     override fun hashCode(): Int {
67         var result = destinationId.hashCode()
68         result = 31 * result + navOptions.hashCode()
69         defaultArguments?.read { result = 31 * result + contentDeepHashCode() }
70         return result
71     }
72 
toStringnull73     override fun toString(): String {
74         val sb = StringBuilder()
75         sb.append(javaClass.simpleName)
76         sb.append("(0x")
77         sb.append(Integer.toHexString(destinationId))
78         sb.append(")")
79         if (navOptions != null) {
80             sb.append(" navOptions=")
81             sb.append(navOptions)
82         }
83         return sb.toString()
84     }
85 }
86