|
1 | 1 | package net.frozendevelopment.core.architecture.navigation |
2 | 2 |
|
| 3 | +import android.net.Uri |
| 4 | +import androidx.navigation3.runtime.NavKey |
| 5 | +import kotlinx.serialization.ExperimentalSerializationApi |
| 6 | +import kotlinx.serialization.KSerializer |
| 7 | +import kotlinx.serialization.descriptors.SerialDescriptor |
| 8 | +import kotlinx.serialization.encoding.AbstractDecoder |
| 9 | +import kotlinx.serialization.encoding.CompositeDecoder |
| 10 | +import kotlinx.serialization.modules.EmptySerializersModule |
| 11 | +import kotlinx.serialization.modules.SerializersModule |
| 12 | + |
3 | 13 | const val DEEP_LINK_URI: String = "openletters://net.frozendevelopment.openletters" |
| 14 | + |
| 15 | +data class Deeplink<T : NavKey>( |
| 16 | + val serializer: KSerializer<T>, |
| 17 | + val uri: Uri, |
| 18 | +) |
| 19 | + |
| 20 | +class DeeplinkResolver( |
| 21 | + private val deepLinks: Collection<Deeplink<out NavKey>>, |
| 22 | +) { |
| 23 | + constructor(vararg deepLinks: Deeplink<out NavKey>) : this(deepLinks.toList()) |
| 24 | + |
| 25 | + fun resolve(uri: Uri): NavKey? { |
| 26 | + for (deeplink in deepLinks) { |
| 27 | + val params = match(deeplink.uri, uri) |
| 28 | + if (params != null) { |
| 29 | + return try { |
| 30 | + val decoder = MapDecoder(params) |
| 31 | + deeplink.serializer.deserialize(decoder) |
| 32 | + } catch (e: Exception) { |
| 33 | + null |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + return null |
| 38 | + } |
| 39 | + |
| 40 | + private fun match( |
| 41 | + template: Uri, |
| 42 | + actual: Uri, |
| 43 | + ): Map<String, String>? { |
| 44 | + if (template.scheme != actual.scheme) return null |
| 45 | + val params = mutableMapOf<String, String>() |
| 46 | + |
| 47 | + if (template.host != actual.host) { |
| 48 | + if (template.host != null && template.host!!.startsWith("{") && template.host!!.endsWith("}")) { |
| 49 | + val key = template.host!!.substring(1, template.host!!.length - 1) |
| 50 | + if (actual.host != null) { |
| 51 | + params[key] = actual.host!! |
| 52 | + } |
| 53 | + } else if (template.host != actual.host) { |
| 54 | + return null |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + val templatePathSegments = template.pathSegments |
| 59 | + val actualPathSegments = actual.pathSegments |
| 60 | + |
| 61 | + var templateIdx = 0 |
| 62 | + var actualIdx = 0 |
| 63 | + |
| 64 | + while (templateIdx < templatePathSegments.size && actualIdx < actualPathSegments.size) { |
| 65 | + val templateSegment = templatePathSegments[templateIdx] |
| 66 | + val actualSegment = actualPathSegments[actualIdx] |
| 67 | + |
| 68 | + if (templateSegment.startsWith("{") && templateSegment.endsWith("}")) { |
| 69 | + val key = templateSegment.substring(1, templateSegment.length - 1) |
| 70 | + params[key] = actualSegment |
| 71 | + templateIdx++ |
| 72 | + actualIdx++ |
| 73 | + } else if (templateSegment == actualSegment) { |
| 74 | + templateIdx++ |
| 75 | + actualIdx++ |
| 76 | + } else { |
| 77 | + return null |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (actualIdx < actualPathSegments.size) return null |
| 82 | + if (templateIdx < templatePathSegments.size) return null |
| 83 | + |
| 84 | + val templateQueryNames = template.queryParameterNames |
| 85 | + for (queryName in templateQueryNames) { |
| 86 | + val templateValue = template.getQueryParameter(queryName) |
| 87 | + if (templateValue != null && templateValue.startsWith("{") && templateValue.endsWith("}")) { |
| 88 | + val key = templateValue.substring(1, templateValue.length - 1) |
| 89 | + val actualValue = actual.getQueryParameter(queryName) |
| 90 | + if (actualValue != null) { |
| 91 | + params[key] = actualValue |
| 92 | + } |
| 93 | + } else if (templateValue != null) { |
| 94 | + if (actual.getQueryParameter(queryName) != templateValue) return null |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + actual.queryParameterNames.forEach { name -> |
| 99 | + if (!params.containsKey(name)) { |
| 100 | + val value = actual.getQueryParameter(name) |
| 101 | + if (value != null) { |
| 102 | + params[name] = value |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return params |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +@OptIn(ExperimentalSerializationApi::class) |
| 112 | +private class MapDecoder( |
| 113 | + private val map: Map<String, String>, |
| 114 | +) : AbstractDecoder() { |
| 115 | + private var index = 0 |
| 116 | + private lateinit var descriptor: SerialDescriptor |
| 117 | + |
| 118 | + override val serializersModule: SerializersModule = EmptySerializersModule() |
| 119 | + |
| 120 | + override fun decodeElementIndex(descriptor: SerialDescriptor): Int { |
| 121 | + this.descriptor = descriptor |
| 122 | + while (index < descriptor.elementsCount) { |
| 123 | + val name = descriptor.getElementName(index) |
| 124 | + if (map.containsKey(name)) { |
| 125 | + return index++ |
| 126 | + } |
| 127 | + if (descriptor.isElementOptional(index)) { |
| 128 | + index++ |
| 129 | + continue |
| 130 | + } |
| 131 | + return index++ |
| 132 | + } |
| 133 | + return CompositeDecoder.DECODE_DONE |
| 134 | + } |
| 135 | + |
| 136 | + private fun currentValue(): String? { |
| 137 | + val name = descriptor.getElementName(index - 1) |
| 138 | + return map[name] |
| 139 | + } |
| 140 | + |
| 141 | + override fun decodeString(): String = currentValue() ?: "" |
| 142 | + |
| 143 | + override fun decodeBoolean(): Boolean = currentValue()?.toBoolean() ?: false |
| 144 | + |
| 145 | + override fun decodeByte(): Byte = currentValue()?.toByte() ?: 0 |
| 146 | + |
| 147 | + override fun decodeShort(): Short = currentValue()?.toShort() ?: 0 |
| 148 | + |
| 149 | + override fun decodeInt(): Int = currentValue()?.toInt() ?: 0 |
| 150 | + |
| 151 | + override fun decodeLong(): Long = currentValue()?.toLong() ?: 0L |
| 152 | + |
| 153 | + override fun decodeFloat(): Float = currentValue()?.toFloat() ?: 0.0f |
| 154 | + |
| 155 | + override fun decodeDouble(): Double = currentValue()?.toDouble() ?: 0.0 |
| 156 | + |
| 157 | + override fun decodeChar(): Char = currentValue()?.firstOrNull() ?: '\u0000' |
| 158 | + |
| 159 | + override fun decodeEnum(enumDescriptor: SerialDescriptor): Int { |
| 160 | + return enumDescriptor.getElementIndex(currentValue() ?: "") |
| 161 | + } |
| 162 | + |
| 163 | + override fun decodeNotNullMark(): Boolean { |
| 164 | + val name = descriptor.getElementName(index - 1) |
| 165 | + return map.containsKey(name) && map[name] != null |
| 166 | + } |
| 167 | + |
| 168 | + override fun decodeNull(): Nothing? = null |
| 169 | +} |
0 commit comments