ref: 2b948f4e23bba5facf231aae48cb1185adacd702
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 |
package ml.adamsprogs.bimba.models import android.app.Activity import android.content.Context import android.os.Build import android.support.v7.widget.CardView import android.support.v7.widget.PopupMenu import android.support.v7.widget.RecyclerView import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView import ml.adamsprogs.bimba.R import android.view.LayoutInflater import java.util.* import kotlin.concurrent.thread import android.util.TypedValue import kotlin.collections.ArrayList //todo list to storage class FavouritesAdapter(val context: Context, var favourites: List<Favourite>, val onMenuItemClickListener: FavouritesAdapter.OnMenuItemClickListener) : RecyclerView.Adapter<FavouritesAdapter.ViewHolder>() { val isSelecting: Boolean get() { return selected.any { it } } 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] holder?.nameTextView?.text = favourite.name val nextDeparture = favourite.nextDeparture val nextDepartureText: String val nextDepartureLineText: String if (nextDeparture != null) { val now = Calendar.getInstance() val departureTime = Calendar.getInstance() departureTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(nextDeparture.time.split(":")[0])) departureTime.set(Calendar.MINUTE, Integer.parseInt(nextDeparture.time.split(":")[1])) if (nextDeparture.tomorrow) departureTime.add(Calendar.DAY_OF_MONTH, 1) val interval = ((departureTime.timeInMillis - now.timeInMillis) / (1000 * 60)).toString() nextDepartureText = context.getString(R.string.departure_in, interval) 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?.timeTextView?.text = nextDepartureText holder?.lineTextView?.text = nextDepartureLineText 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() } } } } fun toggleSelected(view: CardView, position: Int) { if (selected[position]) unSelect(view, position) else select(view, position) } fun select(view: CardView, position: Int) { @Suppress("DEPRECATION") if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) view.setCardBackgroundColor(context.resources.getColor(R.color.colorAccent, null)) else view.setCardBackgroundColor(context.resources.getColor(R.color.colorAccent)) selected[position] = true setSelecting() } fun unSelect(view: CardView, position: Int) { val colour = TypedValue() context.theme.resolveAttribute(R.attr.cardBackgroundColor, colour, true) view.setCardBackgroundColor(colour.data) selected[position] = false setSelecting() } fun setSelecting() { context as Activity if (isSelecting) { context.findViewById(R.id.search_view).visibility = View.INVISIBLE context.findViewById(R.id.appbar).visibility = View.VISIBLE } else { context.findViewById(R.id.search_view).visibility = View.VISIBLE context.findViewById(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) val viewHolder = ViewHolder(rowView) return viewHolder } 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 = itemView.findViewById(R.id.favourite_card) as CardView val nameTextView = itemView.findViewById(R.id.favourite_name) as TextView val timeTextView = itemView.findViewById(R.id.favourite_time) as TextView val lineTextView = itemView.findViewById(R.id.favourite_line) as TextView val moreButton = itemView.findViewById(R.id.favourite_more_button) as ImageView } interface OnMenuItemClickListener { fun edit(name: String): Boolean fun delete(name: String): Boolean } } |