Author: Adam Evyčędo <git@apiote.xyz>
parse coordinates in journey search
%!v(PANIC=String method: strings: negative Repeat count)
diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/dashboard/ui/journey/JourneyFragment.kt b/app/src/main/java/xyz/apiote/bimba/czwek/dashboard/ui/journey/JourneyFragment.kt index ad046266f26f5a5fbe5ca3d90376e211172aa46f..c44244e56ec62ff11c4a8a21d033af5d62d7c37d 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/dashboard/ui/journey/JourneyFragment.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/dashboard/ui/journey/JourneyFragment.kt @@ -529,7 +529,6 @@ override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) outState.putString(DashboardViewModel.ORIGIN_KEY, getSearchText(DashboardViewModel.ORIGIN_KEY)) outState.putString(DashboardViewModel.DEST_KEY, getSearchText(DashboardViewModel.DEST_KEY)) - // TODO and rest of params } override fun onViewStateRestored(savedInstanceState: Bundle?) { @@ -542,7 +541,6 @@ if (searchString != "") { binding.origin.setText(searchString) } } - // TODO and rest of params } } diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/search/Query.kt b/app/src/main/java/xyz/apiote/bimba/czwek/search/Query.kt index bd2f303f39d626769cd2a9fda0a4e47d67aab570..2cc392e4faf4006abdab05d4e918544ee2055604 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/search/Query.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/search/Query.kt @@ -5,6 +5,7 @@ package xyz.apiote.bimba.czwek.search import android.content.Context +import android.location.Location import android.os.Build import android.os.Parcel import android.os.Parcelable @@ -92,14 +93,6 @@ fun parse(context: Context) { if (mode != Mode.UNKNOWN) { return } - /* TODO parse coordinates https://developer.android.com/reference/android/location/Location#convert(java.lang.String) - replace `"?` -> `` - replace `' ?` -> `:` - replace `° ?` -> `:` - split by space/locale-separator - if number has locale-digit-separator, replace with dot - if has E/S at end, set sign - */ if (OpenLocationCode.isValidCode(raw)) { val olc = OpenLocationCode(raw) if (!olc.isFull) { @@ -112,9 +105,41 @@ } } else if (OpenLocationCode.isValidCode(raw.trim().split(" ").first().trim(',').trim())) { mode = Mode.POSITION geocode(context) + } else if (seemsCoordinatesDegrees(raw)) { + val coords = raw.split(Regex(",| |, ")) + try { + position = Position(Location.convert(coords[0]), Location.convert(coords[1])) + mode = Mode.POSITION + } catch (_: IllegalArgumentException) { + mode = Mode.NAME + } + } else if (seemsCoordinatesDegreesMinutesSeconds(raw)) { + val coords = + raw.replace(Regex("° ?"), ":").replace(Regex("' ?"), ":").replace(Regex("""" ?"""), "") + .split(" ").toMutableList() + try { + val northSouth = if (coords[0].last() == 'N') 1 else -1 + val eastWest = if (coords[1].last() == 'E') 1 else -1 + coords[0] = coords[0].replace(Regex("[NS]"), "") + coords[1] = coords[1].replace(Regex("[EW]"), "") + position = + Position(Location.convert(coords[0]) * northSouth, Location.convert(coords[1]) * eastWest) + } catch (_: IllegalArgumentException) { + mode = Mode.NAME + } } else { mode = Mode.NAME } + } + + private fun seemsCoordinatesDegrees(s: String): Boolean { + return Regex("""[+-]?[0-9]+(\.[0-9]+)?(,| |, )[+-]?[0-9]+(\.[0-9]+)?""").matches(s) + } + + private fun seemsCoordinatesDegreesMinutesSeconds(s: String): Boolean { + return Regex("""[0-9]+° ?[0-9]+' ?[0-9]+(\.[0-9]+)?" ?[NS] [0-9]+° ?[0-9]+' ?[0-9]+(\.[0-9]+)?" ?[EW]""").matches( + s + ) } private fun geocode(context: Context) { @@ -142,7 +167,10 @@ override fun toString(): String { return when (mode) { Mode.UNKNOWN -> raw Mode.LOCATION -> "here" - Mode.POSITION -> "%.2f, %.2f".format(position!!.latitude, position!!.longitude) // TODO settings for position format + Mode.POSITION -> "%.2f, %.2f".format( + position!!.latitude, + position!!.longitude + ) // TODO settings for position format Mode.NAME -> raw Mode.LOCATION_PLUS_CODE -> raw } diff --git a/app/src/main/res/drawable/options.xml b/app/src/main/res/drawable/options.xml new file mode 100644 index 0000000000000000000000000000000000000000..fb0e685ef3cc625ec96a37d6a6ee328398dca2a3 --- /dev/null +++ b/app/src/main/res/drawable/options.xml @@ -0,0 +1,17 @@ +<!-- +SPDX-FileCopyrightText: Google + +SPDX-License-Identifier: Apache-2.0 +--> +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:width="24dp" + android:height="24dp" + android:tint="?attr/colorOnSurface" + android:viewportWidth="24" + android:viewportHeight="24"> + + <path + android:fillColor="@android:color/white" + android:pathData="M3,17v2h6v-2L3,17zM3,5v2h10L13,5L3,5zM13,21v-2h8v-2h-8v-2h-2v6h2zM7,9v2L3,11v2h4v2h2L9,9L7,9zM21,13v-2L11,11v2h10zM15,9h2L17,7h4L21,5h-4L17,3h-2v6z" /> + +</vector> diff --git a/app/src/main/res/layout/fragment_journey.xml b/app/src/main/res/layout/fragment_journey.xml index 568d127756bb13775605356cb572a6d9f4cf5484..2f1948ee67e81fee870fb3f12d410e27cfd31a1e 100644 --- a/app/src/main/res/layout/fragment_journey.xml +++ b/app/src/main/res/layout/fragment_journey.xml @@ -211,6 +211,16 @@ app:chipIcon="@drawable/bike" app:chipIconEnabled="true" tool:ignore="MissingConstraints" /> + <!--<com.google.android.material.chip.Chip + android:id="@+id/chip_options" + style="@style/Widget.Material3.Chip.Filter" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="@string/other_options" + app:chipIcon="@drawable/options" + app:chipIconEnabled="true" + tool:ignore="MissingConstraints" />--> + </com.google.android.material.chip.ChipGroup> <Button diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2530fb270cad59d4a51f216f271bcb98cc88904a..3d5a26a73168b2a9779a1887b10bbdd57ab24872 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -315,4 +315,5 @@here <string name="no_journeys_found">No journeys found</string> <string name="title_select_date_journey">Select date of the journey</string> <string name="title_select_time_journey">Select journey time</string> + <string name="other_options">Other options</string> </resources> diff --git a/app/src/main/res/values-en-rGB/strings.xml b/app/src/main/res/values-en-rGB/strings.xml index 8d60ef8d598857721c521b96aa430bf03311e0aa..b2a8a1ff850762b1090d502fcde6467ab4d36137 100644 --- a/app/src/main/res/values-en-rGB/strings.xml +++ b/app/src/main/res/values-en-rGB/strings.xml @@ -294,4 +294,5 @@approximately <string name="depart_after">Depart after</string> <string name="title_select_date_journey">Select date of the journey</string> <string name="title_select_time_journey">Select journey time</string> + <string name="other_options">Other options</string> </resources> diff --git a/app/src/main/res/values-en-rUS/strings.xml b/app/src/main/res/values-en-rUS/strings.xml index 682a4d16fca887b28b763a9c5638269edfcd1a53..16dca3dd6974ef527e4fa0a6a052883d8844b5bb 100644 --- a/app/src/main/res/values-en-rUS/strings.xml +++ b/app/src/main/res/values-en-rUS/strings.xml @@ -292,4 +292,5 @@approximately <string name="depart_after">Depart after</string> <string name="title_select_date_journey">Select date of the journey</string> <string name="title_select_time_journey">Select journey time</string> + <string name="other_options">Other options</string> </resources> \ No newline at end of file