ref: 4cb4f45d696585f3d02864102ff408e35784e8bf
app/src/main/java/ml/adamsprogs/bimba/models/FavouritesAdapter.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
package ml.adamsprogs.bimba.models import android.app.Activity import android.content.Context import android.content.Intent import android.support.design.widget.AppBarLayout import android.support.v4.content.res.ResourcesCompat import android.support.v7.widget.* import android.support.v7.widget.PopupMenu import android.view.* import android.widget.* import ml.adamsprogs.bimba.R import android.view.LayoutInflater import java.util.* import kotlin.concurrent.thread import android.util.TypedValue import com.arlib.floatingsearchview.FloatingSearchView import ml.adamsprogs.bimba.Declinator import ml.adamsprogs.bimba.activities.StopActivity import ml.adamsprogs.bimba.getColour import kotlin.collections.ArrayList class FavouritesAdapter(val context: Context, var favourites: List<Favourite>, private val onMenuItemClickListener: FavouritesAdapter.OnMenuItemClickListener) : RecyclerView.Adapter<FavouritesAdapter.ViewHolder>() { private val isSelecting: Boolean get() { return selected.any { it } } private val selected = ArrayList<Boolean>() val selectedNames: ArrayList<String> get() { val l = ArrayList<String>() for ((i, it) in selected.withIndex()) { if (it) l.add(favourites[i].name) } return l } init { favourites.forEach { selected.add(false) } } override fun getItemCount(): Int { return favourites.size } override fun onBindViewHolder(holder: ViewHolder?, position: Int) { thread { val favourite = favourites[position] val nextDeparture: Departure? try { nextDeparture = favourite.nextDeparture() } catch (e: ConcurrentModificationException) { return@thread } val nextDepartureText: String val nextDepartureLineText: String if (nextDeparture != null) { val interval = nextDeparture.timeTill(true) if (interval < 0) return@thread nextDepartureText = context.getString(Declinator.decline(interval), interval.toString()) nextDepartureLineText = context.getString(R.string.departure_to_line, nextDeparture.line, nextDeparture.direction) } else { nextDepartureText = context.getString(R.string.no_next_departure) nextDepartureLineText = "" } (context as Activity).runOnUiThread { holder?.root?.setOnLongClickListener { toggleSelected(it as CardView, position) true } holder?.root?.setOnClickListener { val intent = Intent(context, StopActivity::class.java) intent.putExtra(StopActivity.SOURCE_TYPE, StopActivity.SOURCE_TYPE_FAV) intent.putExtra(StopActivity.EXTRA_FAVOURITE, favourite) context.startActivity(intent) } holder?.nameTextView?.text = favourite.name holder?.timeTextView?.text = nextDepartureText holder?.lineTextView?.text = nextDepartureLineText if(nextDeparture!=null) { if (nextDeparture.vm) holder?.typeIcon?.setImageDrawable(ResourcesCompat.getDrawable(context.resources, R.drawable.ic_departure_vm, context.theme)) else holder?.typeIcon?.setImageDrawable(ResourcesCompat.getDrawable(context.resources, R.drawable.ic_departure_timetable, context.theme)) } holder?.moreButton?.setOnClickListener { unSelect(holder.root, position) val popup = PopupMenu(context, it) val inflater = popup.menuInflater popup.setOnMenuItemClickListener { when (it.itemId) { R.id.favourite_edit -> onMenuItemClickListener.edit(favourite.name) R.id.favourite_delete -> onMenuItemClickListener.delete(favourite.name) else -> false } } inflater.inflate(R.menu.favourite_actions, popup.menu) popup.show() } } } } private fun toggleSelected(view: CardView, position: Int) { growSelected(position) if (selected[position]) unSelect(view, position) else select(view, position) } private fun growSelected(position: Int) { while (position >= selected.size) selected.add(false) } private fun select(view: CardView, position: Int) { growSelected(position) view.setCardBackgroundColor(getColour(R.color.colorAccent, context)) selected[position] = true setSelecting() } private fun unSelect(view: CardView, position: Int) { growSelected(position) val colour = TypedValue() context.theme.resolveAttribute(R.attr.cardBackgroundColor, colour, true) view.setCardBackgroundColor(colour.data) selected[position] = false setSelecting() } private fun setSelecting() { context as Activity if (isSelecting) { context.findViewById<FloatingSearchView>(R.id.search_view).visibility = View.INVISIBLE context.findViewById<AppBarLayout>(R.id.appbar).visibility = View.VISIBLE } else { context.findViewById<FloatingSearchView>(R.id.search_view).visibility = View.VISIBLE context.findViewById<AppBarLayout>(R.id.appbar).visibility = View.INVISIBLE } } override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { val context = parent?.context val inflater = LayoutInflater.from(context) val rowView = inflater.inflate(R.layout.row_favourite, parent, false) return ViewHolder(rowView) } fun stopSelecting(name: String) { selected.clear() favourites.forEach { if (it.name == name) selected.add(true) else selected.add(false) } } inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val root:CardView = itemView.findViewById(R.id.favourite_card) val nameTextView:TextView = itemView.findViewById(R.id.favourite_name) val timeTextView:TextView = itemView.findViewById(R.id.favourite_time) val lineTextView:TextView = itemView.findViewById(R.id.favourite_line) val moreButton:ImageView = itemView.findViewById(R.id.favourite_more_button) val typeIcon:ImageView = itemView.findViewById(R.id.departureTypeIcon) } interface OnMenuItemClickListener { fun edit(name: String): Boolean fun delete(name: String): Boolean } } |