Bimba.git

commit 901167d1b7c4a65b06680a89b35ea7b8a8bbba89

Author: Adam Pioterek <adam.pioterek@protonmail.ch>

edit favourites

%!v(PANIC=String method: strings: negative Repeat count)


diff --git a/app/src/main/java/ml/adamsprogs/bimba/activities/EditFavouriteActivity.kt b/app/src/main/java/ml/adamsprogs/bimba/activities/EditFavouriteActivity.kt
index 372f6448099ab199590f3e2e2241039aff3c3af1..d5c812f0e1dccc101297f852d23cd0756d39f300 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/activities/EditFavouriteActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/activities/EditFavouriteActivity.kt
@@ -42,7 +42,7 @@         val layoutManager = LinearLayoutManager(this)
         recyclerView!!.layoutManager = layoutManager
         val dividerItemDecoration = DividerItemDecoration(this, layoutManager.orientation)
         recyclerView.addItemDecoration(dividerItemDecoration)
-        recyclerView.adapter = FavouriteEditRowAdapter(favourite!!)
+        recyclerView.adapter = FavouriteEditRowAdapter(favourite!!, favourite_edit_loading, favourite_edit_list)
         setSupportActionBar(toolbar)
         supportActionBar?.title = getString(R.string.edit_favourite_title, favourite!!.name)
         nameEdit = favourite_name_edit




diff --git a/app/src/main/java/ml/adamsprogs/bimba/collections/FavouriteStorage.kt b/app/src/main/java/ml/adamsprogs/bimba/collections/FavouriteStorage.kt
index d937e039aa9cc926d197f7bebe913883845281a5..daec3aed4d3ab117418b0dd364102ab657f2b2cb 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/collections/FavouriteStorage.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/collections/FavouriteStorage.kt
@@ -121,7 +121,6 @@         val favouritesString = Gson().toJson(rootObject)
         val editor = preferences.edit()
         editor.putString("favourites", favouritesString)
         editor.apply()
-
     }
 
     fun merge(names: List<String>, context: Context) {
@@ -168,6 +167,11 @@     }
 
     operator fun get(position: Int): Favourite? {
         return favourites[positionIndex[position]]
+    }
+
+    operator fun set(name: String, value: Favourite) {
+        favourites[name] = value
+        serialize()
     }
 
     fun indexOf(name: String): Int {




diff --git a/app/src/main/java/ml/adamsprogs/bimba/models/adapters/FavouriteEditRowAdapter.kt b/app/src/main/java/ml/adamsprogs/bimba/models/adapters/FavouriteEditRowAdapter.kt
index d9a29e8872e4a7f99c68c282ef20cdf823791fb4..cb78df9d44a97addec4f60b2c1539551311584aa 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/models/adapters/FavouriteEditRowAdapter.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/adapters/FavouriteEditRowAdapter.kt
@@ -18,46 +18,63 @@ import ml.adamsprogs.bimba.models.Plate
 import ml.adamsprogs.bimba.models.StopSegment
 
 
-class FavouriteEditRowAdapter(private var favourite: Favourite) :
+class FavouriteEditRowAdapter(private var favourite: Favourite, private val loadingView: View, private val listView: View) :
         RecyclerView.Adapter<FavouriteEditRowAdapter.ViewHolder>() {
 
     private val segments = HashMap<String, StopSegment>()
     private val providerProxy = ProviderProxy()
+    private val favourites = FavouriteStorage.getFavouriteStorage()
+    private val platesList = ArrayList<Plate.ID>()
+    private val namesList = HashMap<Plate.ID, String>()
 
     init {
         launch(UI) {
             withContext(DefaultDispatcher) {
                 favourite.segments.forEach {
-                    segments[it.stop] = providerProxy.fillStopSegment(it) ?: it
+                    if (it.plates == null) {
+                        (providerProxy.fillStopSegment(it) ?: it).let { segment ->
+                            segments[segment.stop] = segment
+                            it.plates = segment.plates
+                        }
+                    } else {
+                        segments[it.stop] = it
+                    }
+                }
+                favourites[favourite.name] = favourite
+
+                segments.flatMap {
+                    it.value.plates ?: emptyList<Plate.ID>()
+                }.sortedBy { "${it.line}${it.stop}" }.forEach {
+                    platesList.add(it)
+                    namesList[it] = providerProxy.getStopName(it.stop).let { name ->
+                        "${name ?: ""} (${it.stop}):\n${it.line} → ${it.headsign}"
+                    }
+                }
+                launch(UI) {
+                    loadingView.visibility = View.GONE
+                    listView.visibility = View.VISIBLE
+                    this@FavouriteEditRowAdapter.notifyDataSetChanged()
                 }
             }
-            this@FavouriteEditRowAdapter.notifyDataSetChanged()
         }
     }
 
 
-    override fun getItemCount(): Int {
-        return segments.flatMap { it.value.plates ?: emptyList<Plate.ID>() }.size
-    }
+    override fun getItemCount(): Int = platesList.size
 
     override fun onBindViewHolder(holder: ViewHolder, position: Int) {
         launch(UI) {
-            val plates = segments.flatMap { it.value.plates ?: emptyList<Plate.ID>() }
-            val favourites = FavouriteStorage.getFavouriteStorage()
-            val id = plates.sortedBy { "${it.line}${it.stop}" }[position]
-            val favouriteElement = withContext(DefaultDispatcher) {
-                providerProxy.getStopName(id.stop).let {
-                    "${it ?: ""} (${id.stop}):\n${id.line} → ${id.headsign}"
-                }
-            }
+            val id = platesList[position]
+            val favouriteElement = namesList[id]
+
             holder.rowTextView.text = favouriteElement
             holder.deleteButton.setOnClickListener {
                 launch(UI) {
-                    favourite.segments.clear()
-                    favourite.segments.addAll(segments.map { it.value })
                     favourites.delete(favourite.name, id)
                     favourite = favourites.favourites[favourite.name]!!
-                    notifyDataSetChanged()
+                    notifyItemRemoved(platesList.indexOf(id))
+                    platesList.remove(id)
+                    namesList.remove(id)
                 }
             }
         }




diff --git a/app/src/main/res/layout/activity_edit_favourite.xml b/app/src/main/res/layout/activity_edit_favourite.xml
index b93a989607cf125b465c9631cbb173bec3502693..8788c2dcff597cb72bf74e8478c1040bde28a941 100644
--- a/app/src/main/res/layout/activity_edit_favourite.xml
+++ b/app/src/main/res/layout/activity_edit_favourite.xml
@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="utf-8"?>
 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/dialog_favourite"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
@@ -38,28 +39,36 @@              android:id="@+id/favourite_name_edit"
         android:layout_width="0dp"
         android:layout_height="wrap_content"
+        android:layout_marginEnd="8dp"
         android:layout_marginStart="8dp"
         android:ems="10"
         android:inputType="text"
         android:text=""
-        app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toBottomOf="@+id/name_label"
         app:layout_constraintEnd_toEndOf="parent"
-        android:layout_marginEnd="8dp" />
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toBottomOf="@+id/name_label" />
 
     <android.support.v7.widget.RecyclerView
         android:id="@+id/favourite_edit_list"
         android:layout_width="0dp"
         android:layout_height="0dp"
-        android:layout_marginBottom="8dp"
-        android:layout_marginEnd="8dp"
-        android:layout_marginStart="8dp"
         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/favourite_name_edit">
 
     </android.support.v7.widget.RecyclerView>
+
+    <ProgressBar
+        android:id="@+id/favourite_edit_loading"
+        style="@style/Widget.AppCompat.ProgressBar"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        app:layout_constraintBottom_toBottomOf="@+id/favourite_edit_list"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toBottomOf="@+id/favourite_name_edit" />
 
 </android.support.constraint.ConstraintLayout>
\ No newline at end of file




diff --git a/app/src/main/res/layout/row_favourite_edit.xml b/app/src/main/res/layout/row_favourite_edit.xml
index 7de8a6a53aa15b716770b036dba2906bd855bfe1..1d48cfacec0056c94b5756ed01064e09d3de2e6e 100644
--- a/app/src/main/res/layout/row_favourite_edit.xml
+++ b/app/src/main/res/layout/row_favourite_edit.xml
@@ -28,15 +28,13 @@         android:id="@+id/favourite_edit_delete"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginBottom="16dp"
-        android:layout_marginStart="9dp"
+        android:layout_marginEnd="8dp"
         android:layout_marginTop="16dp"
+        android:contentDescription="@string/favourite_element_delete_button"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintTop_toTopOf="parent"
-        app:srcCompat="@drawable/ic_delete"
-        tools:layout_editor_absoluteX="335dp"
-        tools:layout_editor_absoluteY="16dp"
-        android:contentDescription="@string/favourite_element_delete_button" />
+        app:srcCompat="@drawable/ic_delete" />
 
     <!--<ImageView-->
         <!--android:id="@+id/favourite_edit_split"-->