2 Commits 26a0b0890e ... 7105ec7591

Author SHA1 Message Date
  Adam Pioterek 7105ec7591 vm messages 5 years ago
  Adam Pioterek 5946e4a543 sorting search results by similarity & departures with onStop 5 years ago

+ 9 - 0
app/src/main/java/ml/adamsprogs/bimba/ProviderProxy.kt

@@ -85,6 +85,15 @@ class ProviderProxy(context: Context? = null) {
         }
     }
 
+    fun getVmMessage(shed: String, callback: (String?) -> Unit) {
+        GlobalScope.launch {
+            val message = vmClient.getMessage(shed)
+            launch(Dispatchers.Main) {
+                callback(message)
+            }
+        }
+    }
+
     fun subscribeForDepartures(stopSegments: Set<StopSegment>, listener: OnDeparturesReadyListener, context: Context): String {
         stopSegments.forEach {
             val intent = Intent(context, VmService::class.java)

+ 1 - 1
app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt

@@ -151,7 +151,7 @@ class DashActivity : AppCompatActivity(), MessageReceiver.OnTimetableDownloadLis
                     suggestionsAdapter.clearSuggestions()
                     suggestionsAdapter.addSuggestion(EmptySuggestion())
                 } else {
-                    suggestionsAdapter.updateSuggestions(suggestions)
+                    suggestionsAdapter.updateSuggestions(suggestions, query)
                 }
                 searchView.showSuggestionsList()
             }

+ 30 - 1
app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt

@@ -1,16 +1,21 @@
 package ml.adamsprogs.bimba.activities
 
+import android.content.DialogInterface
 import android.content.Intent
 import android.content.IntentFilter
+import android.os.Build
 import android.os.Bundle
+import android.text.Html
 import android.view.Menu
 import android.view.MenuItem
 import android.view.View
 import android.widget.AdapterView
+import androidx.appcompat.app.AlertDialog
 import androidx.appcompat.app.AppCompatActivity
 import androidx.core.content.res.ResourcesCompat
 import com.google.android.material.snackbar.Snackbar
 import kotlinx.android.synthetic.main.activity_stop.*
+import kotlinx.android.synthetic.main.banner.*
 import ml.adamsprogs.bimba.*
 import ml.adamsprogs.bimba.collections.FavouriteStorage
 import ml.adamsprogs.bimba.datasources.TimetableDownloader
@@ -89,6 +94,30 @@ class StopActivity : AppCompatActivity(), MessageReceiver.OnTimetableDownloadLis
             }
         })
 
+        if (stopCode != "")
+            providerProxy.getVmMessage(stopCode) { message ->
+                if (message != null) {
+                    val rendered =
+                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+                                Html.fromHtml(message, Html.FROM_HTML_MODE_LEGACY)
+                            } else {
+                                @Suppress("DEPRECATION")
+                                Html.fromHtml(message)
+                            }
+
+                    banner.visibility = View.VISIBLE
+                    banner_text.text = rendered
+                    banner_more.setOnClickListener {
+                        AlertDialog.Builder(context)
+                                .setPositiveButton(context.getText(android.R.string.ok))
+                                { dialog: DialogInterface, _: Int -> dialog.cancel() }
+                                .setCancelable(true)
+                                .setMessage(rendered)
+                                .create().show()
+                    }
+                }
+            }
+
         prepareOnDownloadListener()
         subscribeForDepartures()
     }
@@ -172,7 +201,7 @@ class StopActivity : AppCompatActivity(), MessageReceiver.OnTimetableDownloadLis
         } else {
             val now = Calendar.getInstance()
             val seconds = now.secondsAfterMidnight()
-            adapter.departures = this.departures.flatMap { it.value }.sortedBy { it.timeTill(seconds) }  // todo sorted by also onStop
+            adapter.departures = this.departures.flatMap { it.value }.sortedBy { (if (it.onStop) 0 else 1) * 172800 + it.timeTill(seconds) }  // todo sorted by also onStop
         }
         adapter.notifyDataSetChanged()
     }

+ 12 - 0
app/src/main/java/ml/adamsprogs/bimba/datasources/VmClient.kt

@@ -139,4 +139,16 @@ class VmClient {
                     }
                 }.toSet())
     }
+
+    suspend fun getMessage(shed: String): String? {
+        val (_, response) = makeRequest("findMessagesForBollard", """{"symbol": "$shed"}""")
+
+        if (!response.has("success"))
+            return null
+
+        if (response["success"].asJsonArray.size() == 0)
+            return null
+
+        return response["success"].asJsonArray[0].asJsonObject["content"].asString
+    }
 }

+ 9 - 2
app/src/main/java/ml/adamsprogs/bimba/models/adapters/SuggestionsAdapter.kt

@@ -58,12 +58,19 @@ class SuggestionsAdapter(inflater: LayoutInflater, private val onSuggestionClick
 
     }
 
-    fun updateSuggestions(newSuggestions: List<GtfsSuggestion>) {
-        suggestions = newSuggestions
+    fun updateSuggestions(newSuggestions: List<GtfsSuggestion>, query: String) {
+        suggestions = sort(newSuggestions, query).take(6)
         suggestions_clone = suggestions
         notifyDataSetChanged()
     }
 
+    private fun sort(suggestions: List<GtfsSuggestion>, query: String): List<GtfsSuggestion> {
+        val r = Regex(query, RegexOption.IGNORE_CASE)
+        return suggestions.sortedBy {
+            (r.find(it.name)?.range?.start?.toString()?.padStart(128, '0') ?: "")+it.name
+        }
+    }
+
     operator fun contains(suggestion: GtfsSuggestion): Boolean {
         return suggestion in suggestions //|| suggestion in suggestions_clone
     }

+ 9 - 0
app/src/main/res/drawable/ic_message.xml

@@ -0,0 +1,9 @@
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="24dp"
+        android:height="24dp"
+        android:viewportWidth="24.0"
+        android:viewportHeight="24.0">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M20,2L4,2c-1.1,0 -1.99,0.9 -1.99,2L2,22l4,-4h14c1.1,0 2,-0.9 2,-2L22,4c0,-1.1 -0.9,-2 -2,-2zM18,14L6,14v-2h12v2zM18,11L6,11L6,9h12v2zM18,8L6,8L6,6h12v2z"/>
+</vector>

+ 51 - 42
app/src/main/res/layout/activity_stop.xml

@@ -8,6 +8,47 @@
     android:fitsSystemWindows="true"
     tools:context="ml.adamsprogs.bimba.activities.StopActivity">
 
+    <com.google.android.material.appbar.AppBarLayout
+        android:id="@+id/appbar"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:paddingTop="@dimen/appbar_padding_top"
+        android:theme="@style/AppTheme.AppBarOverlay">
+
+        <androidx.appcompat.widget.Toolbar
+            android:id="@+id/toolbar"
+            android:layout_width="match_parent"
+            android:layout_height="?attr/actionBarSize"
+            android:layout_weight="1"
+            android:background="?attr/colorPrimary"
+            app:layout_scrollFlags="scroll|enterAlways"
+            app:popupTheme="@style/AppTheme.PopupOverlay"
+            app:title="@string/app_name">
+
+        </androidx.appcompat.widget.Toolbar>
+
+        <Spinner
+            android:id="@+id/dateSpinner"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_marginStart="8dp"
+            android:layout_marginLeft="8dp"
+            android:layout_marginEnd="8dp"
+            android:layout_marginRight="8dp"
+            android:layout_marginBottom="8dp"
+            android:layout_weight="1"
+            android:visibility="gone" />
+    </com.google.android.material.appbar.AppBarLayout>
+
+    <include
+        android:visibility="gone"
+        android:id="@+id/banner"
+        layout="@layout/banner"
+        android:layout_height="120dp"
+        android:layout_width="match_parent"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toBottomOf="@id/appbar" />
 
     <ImageView
         android:id="@+id/emptyStateIcon"
@@ -17,13 +58,13 @@
         android:layout_marginTop="8dp"
         android:layout_marginEnd="8dp"
         android:layout_marginBottom="8dp"
+        android:contentDescription="@string/departures_empty_state_icon"
+        android:visibility="gone"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toBottomOf="@+id/appbar"
-        app:srcCompat="@drawable/ic_traffic"
-        android:visibility="gone"
-        android:contentDescription="@string/departures_empty_state_icon" />
+        app:layout_constraintTop_toBottomOf="@+id/banner"
+        app:srcCompat="@drawable/ic_traffic" />
 
     <TextView
         android:id="@+id/emptyStateText"
@@ -33,9 +74,9 @@
         android:layout_marginTop="8dp"
         android:layout_marginEnd="8dp"
         android:text="@string/no_departures"
+        android:visibility="gone"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
-        android:visibility="gone"
         app:layout_constraintTop_toBottomOf="@+id/emptyStateIcon" />
 
     <ProgressBar
@@ -50,51 +91,18 @@
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toBottomOf="@+id/appbar" />
-
-    <com.google.android.material.appbar.AppBarLayout
-        android:id="@+id/appbar"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:paddingTop="@dimen/appbar_padding_top"
-        android:theme="@style/AppTheme.AppBarOverlay">
-
-        <androidx.appcompat.widget.Toolbar
-            android:id="@+id/toolbar"
-            android:layout_width="match_parent"
-            android:layout_height="?attr/actionBarSize"
-            android:layout_weight="1"
-            android:background="?attr/colorPrimary"
-            app:layout_scrollFlags="scroll|enterAlways"
-            app:popupTheme="@style/AppTheme.PopupOverlay"
-            app:title="@string/app_name">
-
-        </androidx.appcompat.widget.Toolbar>
-
-        <Spinner
-            android:id="@+id/dateSpinner"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:layout_marginBottom="8dp"
-            android:layout_marginEnd="8dp"
-            android:layout_marginLeft="8dp"
-            android:layout_marginRight="8dp"
-            android:layout_marginStart="8dp"
-            android:layout_weight="1"
-            android:visibility="gone" />
-
-
-    </com.google.android.material.appbar.AppBarLayout>
+        app:layout_constraintTop_toBottomOf="@+id/banner" />
 
     <androidx.recyclerview.widget.RecyclerView
         android:id="@+id/departuresList"
         android:layout_width="0dp"
         android:layout_height="0dp"
+        android:layout_marginTop="8dp"
         android:visibility="gone"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toBottomOf="@+id/appbar" />
+        app:layout_constraintTop_toBottomOf="@+id/banner" />
 
     <com.google.android.material.floatingactionbutton.FloatingActionButton
         android:id="@+id/fab"
@@ -102,9 +110,10 @@
         android:layout_height="wrap_content"
         android:layout_gravity="end|bottom"
         android:layout_margin="@dimen/fab_margin"
-        android:layout_marginBottom="16dp"
         android:layout_marginEnd="16dp"
+        android:layout_marginBottom="16dp"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:srcCompat="@drawable/ic_favourite" />
+
 </androidx.constraintlayout.widget.ConstraintLayout>

+ 57 - 0
app/src/main/res/layout/banner.xml

@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    android:layout_width="match_parent"
+    android:layout_height="120dp"
+    android:orientation="vertical">
+
+    <View
+        android:id="@+id/banner_separator"
+        android:layout_width="fill_parent"
+        android:layout_height="1dp"
+        android:background="#90909090"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintEnd_toStartOf="parent"
+        app:layout_constraintStart_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent" />
+
+    <Button
+        android:id="@+id/banner_more"
+        style="@style/Widget.AppCompat.Button.Borderless.Colored"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginEnd="8dp"
+        android:layout_marginBottom="8dp"
+        android:text="@string/more"
+        android:textAppearance="@style/TextAppearance.AppCompat.Button"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintEnd_toEndOf="parent" />
+
+    <TextView
+        android:id="@+id/banner_text"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginStart="16dp"
+        android:layout_marginTop="24dp"
+        android:layout_marginBottom="20dp"
+        android:ellipsize="end"
+        android:text=""
+        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
+        app:layout_constraintBottom_toTopOf="@+id/banner_more"
+        app:layout_constraintStart_toEndOf="@+id/banner_icon"
+        app:layout_constraintTop_toTopOf="parent" />
+
+    <ImageView
+        android:id="@+id/banner_icon"
+        android:layout_width="40dp"
+        android:layout_height="40dp"
+        android:layout_marginStart="16dp"
+        android:layout_marginTop="8dp"
+        android:layout_marginBottom="8dp"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent"
+        app:srcCompat="@drawable/ic_message"
+        android:contentDescription="@string/vm_message_icon" />
+</androidx.constraintlayout.widget.ConstraintLayout>

+ 3 - 0
app/src/main/res/values-de/strings.xml

@@ -52,4 +52,7 @@
     <string name="title_timetable_automatic_update">Automatische Updates</string>
     <string name="summary_timetable_automatic_update">Automatisch nach Fahrplanaktualisierungen suchen und diese herunterladen</string>
     <string name="server_error">Serverfehler</string>
+    <string name="nothing_found">Nicht gefunden</string>
+    <string name="in_a_moment">Gleich</string>
+    <string name="more">Mehr</string>
 </resources>

+ 0 - 0
app/src/main/res/values-it/strings.xml


Some files were not shown because too many files changed in this diff