Author: Adam Pioterek <adam.pioterek@protonmail.ch>
refreshing favourites list in main activity
%!v(PANIC=String method: strings: negative Repeat count)
diff --git a/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt b/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt index 43f017d67c36885dd9a1accfb6cf4d083773eb7c..e29da9681bc17c52d83898bf9589e5371d4c6a6a 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt @@ -334,6 +334,10 @@ val positionAfter = favourites.indexOf(name) favouritesList.adapter.notifyItemChanged(positionBefore) favouritesList.adapter.notifyItemMoved(positionBefore, positionAfter) } + adapter[name]?.let { + it.unsubscribeFromDepartures(context) + it.subscribeForDepartures(this, context) + } } } } @@ -404,12 +408,19 @@ return when (item.itemId) { R.id.action_merge -> { val selectedPositions = adapter.getSelectedItems() val selectedNames = selectedPositions.map { favourites[it]?.name }.filter { it != null }.map { it!! } - favourites.merge(selectedNames, this@DashActivity) - adapter.notifyItemChanged(selectedPositions.min()!!) - (1 until selectedPositions.size).forEach { - adapter.notifyItemRemoved(it) + (1 until selectedNames.size).forEach { + selectedNames[it].let { name -> + adapter.notifyItemRemoved(adapter.indexOf(name)) + adapter[name]?.unsubscribeFromDepartures(context) + } + } + favourites.merge(selectedNames, context) + adapter[selectedNames[0]]?.let { + it.unsubscribeFromDepartures(context) + it.subscribeForDepartures(this@DashActivity, context) } + adapter.notifyItemChanged(adapter.indexOf(selectedNames[0])) clearSelection() true diff --git a/app/src/main/java/ml/adamsprogs/bimba/datasources/VmService.kt b/app/src/main/java/ml/adamsprogs/bimba/datasources/VmService.kt index 9dfcc25f261319fbca5cad8912d608614a00347b..82e4f38941d51829f3e42771115968d48869b25a 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/datasources/VmService.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/datasources/VmService.kt @@ -119,7 +119,7 @@ private suspend fun downloadVM(stopCode: String) { if (!NetworkStateReceiver.isNetworkAvailable()) { vms[stopCode] = emptySet() - sendResult(stopCode, null, null) + sendResult(stopCode, null, null, 0) return } diff --git a/app/src/main/java/ml/adamsprogs/bimba/models/adapters/FavouritesAdapter.kt b/app/src/main/java/ml/adamsprogs/bimba/models/adapters/FavouritesAdapter.kt index 76206a2b5e93f9683ff8dc07aa7011fa48c2a29e..a30130f8c29ad35d2a33a8cefa0ef22786ca5282 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/models/adapters/FavouritesAdapter.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/models/adapters/FavouritesAdapter.kt @@ -14,10 +14,11 @@ import kotlinx.coroutines.experimental.* import java.util.* import ml.adamsprogs.bimba.Declinator import ml.adamsprogs.bimba.collections.FavouriteStorage +import ml.adamsprogs.bimba.models.Favourite import ml.adamsprogs.bimba.secondsAfterMidnight -class FavouritesAdapter(val appContext: Context, var favourites: FavouriteStorage, +class FavouritesAdapter(private val appContext: Context, var favourites: FavouriteStorage, private val onMenuItemClickListener: OnMenuItemClickListener, private val onClickListener: ViewHolder.OnClickListener) : RecyclerView.Adapter<FavouritesAdapter.ViewHolder>() { @@ -36,11 +37,8 @@ notifyItemChanged(position) } fun clearSelection() { - val selection = getSelectedItems() selectedItems.clear() - for (i in selection) { - notifyItemChanged(i) - } + notifyDataSetChanged() } fun getSelectedItemCount() = selectedItems.size() @@ -107,6 +105,14 @@ val inflater = LayoutInflater.from(context) val rowView = inflater.inflate(R.layout.row_favourite, parent, false) return ViewHolder(rowView, onClickListener) + } + + fun indexOf(name: String): Int { + return favourites.indexOf(name) + } + + operator fun get(index: String): Favourite? { + return favourites[index] } class ViewHolder(itemView: View, private val listener: OnClickListener) : RecyclerView.ViewHolder(itemView), View.OnClickListener, View.OnLongClickListener {