-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
NavigationContextStacks.kt
210 lines (171 loc) · 8.04 KB
/
NavigationContextStacks.kt
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.chrynan.navigation
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.MapSerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
/**
* A component that wraps a [Map] of [NavigationContext] to [MutableStack] of [NavigationDestination]s and provides
* convenience functions for accessing and altering the values. Note that this component is mutable.
*/
@Serializable(with = NavigationContextStacksSerializer::class)
internal class NavigationContextStacks<Destination : NavigationDestination, Context : NavigationContext<Destination>> internal constructor(
internal val initialContext: Context,
contextStacks: Map<Context, Stack<Destination>> = emptyMap()
) {
private val destinationStacksByContext: MutableMap<Context, MutableStack<Destination>> =
mutableMapOf(initialContext to mutableStackOf(initialContext.initialDestination)).apply {
contextStacks.entries.forEach { entry ->
this[entry.key] = entry.value.toMutableStack()
}
}
/**
* Retrieves the [Stack] of [Destination]s for the provided [Context].
*/
operator fun get(context: Context): Stack<Destination> =
destinationStacksByContext[context]?.toStack() ?: stackOf(context.initialDestination)
/**
* Retrieves the current [Destination] on top of the [Stack] for the provided [Context] without removing it.
*/
fun peek(context: Context): Destination =
get(context = context).peekOrNull() ?: context.initialDestination
/**
* Removes and returns the [Destination] on top of the [Stack] for the provided [Context], or `null` if the item
* cannot be removed. There must always be at least one item within a [Destination] [Stack], attempting to remove
* that item will fail and return `null` instead.
*/
fun pop(context: Context): Destination? {
val stack = destinationStacksByContext[context] ?: return null
if (stack.size <= 1) return null
val removed = stack.pop()
destinationStacksByContext[context] = stack
return removed
}
/**
* Pushes the provided [destination] to the top of the [Stack] of [Destination]s for the provided [context].
*/
fun push(context: Context, destination: Destination) {
val stack = destinationStacksByContext[context] ?: mutableStackOf(context.initialDestination)
stack.push(destination)
destinationStacksByContext[context] = stack
}
/**
* Pushes the provided [destination] to the top of the [Stack] of [Destination]s for the provided [context], but
* if the provided [destination] already exists in the [Stack] of [Destination]s for the provided [context], all
* the items on top of it will be popped from the stack.
*/
fun pushDropping(context: Context, destination: Destination) {
val stack = destinationStacksByContext[context] ?: mutableStackOf(context.initialDestination)
val index = stack.indexOf(destination)
if (index == -1) {
stack.push(destination)
destinationStacksByContext[context] = stack
} else {
val dropCount = (stack.size - (stack.size - index)) + 1
val newStack = stack.toList().drop(dropCount).toMutableStack()
newStack.push(destination)
destinationStacksByContext[context] = newStack
}
}
/**
* Pushes all the provided [destinations] to the top of the [Stack] of [Destination]s for the provided [context].
*/
fun pushAll(context: Context, destinations: Stack<Destination>) {
val stack = destinationStacksByContext[context] ?: mutableStackOf(context.initialDestination)
destinations.forEach { destination -> stack.push(destination) }
destinationStacksByContext[context] = stack
}
/**
* Clears the stack for the provided [context] and resets it back to its initial state.
*/
fun clear(context: Context) {
destinationStacksByContext[context] = mutableStackOf(context.initialDestination)
}
/**
* Clears all the [Stack]s of [Destination]s for all the [Context]s. This resets the internal data structure back
* to its initial state.
*/
fun clearAll() {
destinationStacksByContext.clear()
destinationStacksByContext[initialContext] = mutableStackOf(initialContext.initialDestination)
}
/**
* Returns a [Map] of the [Context] to [Stack] of [Destination]s.
*/
fun toMap(): Map<Context, Stack<Destination>> = destinationStacksByContext
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is NavigationContextStacks<*, *>) return false
if (initialContext != other.initialContext) return false
return destinationStacksByContext == other.destinationStacksByContext
}
override fun hashCode(): Int {
var result = initialContext.hashCode()
result = 31 * result + destinationStacksByContext.hashCode()
return result
}
override fun toString(): String =
"NavigationContextStacks(" +
"initialContext=$initialContext, " +
"destinationStacksByContext=$destinationStacksByContext)"
}
/**
* Pops the top destination off the provided [context] stack and returns the new top destination, or `null` if the
* provided [context] stack could not be popped (there must be at least one item in the stack at all times).
*/
internal fun <Destination : NavigationDestination, Context : NavigationContext<Destination>> NavigationContextStacks<Destination, Context>.popToPreviousDestinationForContext(
context: Context
): Destination? {
this.pop(context) ?: return null
return this.peek(context)
}
/**
* A [KSerializer] for [NavigationContextStacks].
*/
internal class NavigationContextStacksSerializer<Destination : NavigationDestination, Context : NavigationContext<Destination>> internal constructor(
destinationSerializer: KSerializer<Destination>,
private val contextSerializer: KSerializer<Context>
) : KSerializer<NavigationContextStacks<Destination, Context>> {
private val stackSerializer = StackSerializer(destinationSerializer)
private val mapSerializer = MapSerializer(contextSerializer, stackSerializer)
override val descriptor: SerialDescriptor = buildClassSerialDescriptor(serialName = "NavigationContextStacks") {
element(elementName = "initialContext", descriptor = contextSerializer.descriptor)
element(elementName = "context_stacks", descriptor = mapSerializer.descriptor)
}
override fun serialize(encoder: Encoder, value: NavigationContextStacks<Destination, Context>) {
val compositeEncoder = encoder.beginStructure(descriptor)
compositeEncoder.encodeSerializableElement(
serializer = contextSerializer,
descriptor = descriptor,
index = 0,
value = value.initialContext
)
compositeEncoder.encodeSerializableElement(
serializer = mapSerializer,
descriptor = descriptor,
index = 1,
value = value.toMap()
)
compositeEncoder.endStructure(descriptor)
}
override fun deserialize(decoder: Decoder): NavigationContextStacks<Destination, Context> {
val compositeDecoder = decoder.beginStructure(descriptor)
val initialContext = compositeDecoder.decodeSerializableElement(
deserializer = contextSerializer,
descriptor = descriptor,
index = 0
)
val contextStacks = compositeDecoder.decodeSerializableElement(
deserializer = mapSerializer,
descriptor = descriptor,
index = 1
)
compositeDecoder.endStructure(descriptor)
return NavigationContextStacks(
initialContext = initialContext,
contextStacks = contextStacks
)
}
}