2019-09-01 11:54:12 +02:00
|
|
|
package com.denizd.substitutionplan.adapters
|
2019-05-21 18:55:29 +02:00
|
|
|
|
2019-08-06 17:08:43 +02:00
|
|
|
import android.content.ActivityNotFoundException
|
2019-09-23 17:23:59 +02:00
|
|
|
import android.content.Context
|
2019-08-06 17:08:43 +02:00
|
|
|
import android.net.Uri
|
2019-08-07 08:10:34 +02:00
|
|
|
import android.text.SpannableString
|
|
|
|
|
import android.text.style.StrikethroughSpan
|
2019-05-21 18:55:29 +02:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
|
import android.view.View
|
|
|
|
|
import android.view.ViewGroup
|
|
|
|
|
import android.widget.ImageView
|
|
|
|
|
import android.widget.TextView
|
2019-08-06 17:08:43 +02:00
|
|
|
import android.widget.Toast
|
|
|
|
|
import androidx.browser.customtabs.CustomTabsIntent
|
2019-05-21 18:55:29 +02:00
|
|
|
import androidx.core.content.ContextCompat
|
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2019-10-19 19:48:21 +02:00
|
|
|
import com.denizd.substitutionplan.data.SubstUtil
|
2019-09-01 11:54:12 +02:00
|
|
|
import com.denizd.substitutionplan.R
|
2019-09-23 17:23:59 +02:00
|
|
|
import com.denizd.substitutionplan.models.Substitution
|
2019-06-02 17:57:57 +02:00
|
|
|
import com.google.android.material.card.MaterialCardView
|
2019-08-18 17:53:22 +02:00
|
|
|
import java.util.*
|
2019-05-21 18:55:29 +02:00
|
|
|
|
2019-09-23 17:23:59 +02:00
|
|
|
/**
|
|
|
|
|
* Adapter class for the Recycler View used to display the substitution plan in
|
|
|
|
|
* PlanFragment.kt and its subclasses
|
|
|
|
|
*
|
|
|
|
|
* @param substitutions a list of all substitutions of data type Substitution
|
2019-10-23 21:15:12 +02:00
|
|
|
* @param colours a map of all user-defined colours for the individual courses
|
2019-09-23 17:23:59 +02:00
|
|
|
*/
|
2019-10-23 21:15:12 +02:00
|
|
|
internal class SubstitutionAdapter(private var substitutions: List<Substitution>, private val colours: Map<String, String>) : RecyclerView.Adapter<SubstitutionAdapter.CardViewHolder>() {
|
2019-05-21 18:55:29 +02:00
|
|
|
|
2019-10-01 10:38:41 +02:00
|
|
|
/**
|
|
|
|
|
* The ViewHolder class used by SubstitutionAdapter.kt to resolve references to views in
|
|
|
|
|
* course_card.xml that will be inflated and populated with data in
|
|
|
|
|
* SubstitutionAdapter#onBindViewHolder
|
|
|
|
|
*
|
|
|
|
|
* @param view a reference to the xml that is to be inflated
|
|
|
|
|
*/
|
2019-09-23 17:23:59 +02:00
|
|
|
internal class CardViewHolder(view: View) : RecyclerView.ViewHolder(view), View.OnClickListener {
|
2019-08-07 07:38:50 +02:00
|
|
|
|
2019-09-11 20:59:55 +02:00
|
|
|
var iconView: ImageView = view.findViewById(R.id.iconView)
|
|
|
|
|
var group: TextView = view.findViewById(R.id.group)
|
|
|
|
|
var date: TextView = view.findViewById(R.id.date)
|
|
|
|
|
var time: TextView = view.findViewById(R.id.time)
|
|
|
|
|
var course: TextView = view.findViewById(R.id.course)
|
|
|
|
|
var room: TextView = view.findViewById(R.id.room)
|
|
|
|
|
var additional: TextView = view.findViewById(R.id.additional)
|
2019-09-04 18:46:25 +02:00
|
|
|
var teacher: TextView = view.findViewById(R.id.teacher)
|
2019-09-11 20:59:55 +02:00
|
|
|
var spacer: TextView = view.findViewById(R.id.spacer)
|
|
|
|
|
var card: MaterialCardView = view.findViewById(R.id.planCard)
|
2019-09-23 17:23:59 +02:00
|
|
|
val context: Context = card.context
|
2019-10-01 10:38:41 +02:00
|
|
|
|
2019-08-07 07:38:50 +02:00
|
|
|
init { view.setOnClickListener(this) }
|
2019-10-01 10:38:41 +02:00
|
|
|
|
2019-08-07 07:38:50 +02:00
|
|
|
override fun onClick(v: View?) {
|
2019-09-11 20:59:55 +02:00
|
|
|
if (date.text.toString().length > 7 && date.text.toString().substring(3, 7) == "http") {
|
2019-08-07 07:38:50 +02:00
|
|
|
try {
|
2019-09-23 17:23:59 +02:00
|
|
|
CustomTabsIntent.Builder().build().launchUrl(context,
|
|
|
|
|
Uri.parse(date.text.substring(3)))
|
2019-08-07 07:38:50 +02:00
|
|
|
} catch (e: ActivityNotFoundException) {
|
2019-09-23 17:23:59 +02:00
|
|
|
Toast.makeText(context, context.getString(R.string.chrome_not_found), Toast.LENGTH_LONG).show()
|
2019-08-07 07:38:50 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-21 18:55:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardViewHolder {
|
|
|
|
|
val v = LayoutInflater.from(parent.context).inflate(R.layout.course_card, parent, false)
|
|
|
|
|
return CardViewHolder(v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onBindViewHolder(holder: CardViewHolder, position: Int) {
|
2019-09-23 17:23:59 +02:00
|
|
|
val currentItem = substitutions[position]
|
2019-08-06 17:08:43 +02:00
|
|
|
var psa = false
|
2019-08-09 09:52:05 +02:00
|
|
|
val strings = arrayOf(SpannableString(currentItem.group), SpannableString(currentItem.time),
|
2019-09-13 12:03:14 +02:00
|
|
|
SpannableString(currentItem.course), SpannableString(currentItem.room),
|
|
|
|
|
SpannableString(currentItem.teacher))
|
2019-09-23 17:23:59 +02:00
|
|
|
val cardBackgroundColour: Int
|
|
|
|
|
var colour = 0
|
2019-09-11 20:59:55 +02:00
|
|
|
|
|
|
|
|
for (string in strings) {
|
|
|
|
|
val questionMarkIndex = string.indexOf("?")
|
|
|
|
|
if (questionMarkIndex != -1) {
|
|
|
|
|
string.setSpan(StrikethroughSpan(), 0, questionMarkIndex, 0)
|
2019-08-07 08:10:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-09-13 12:03:14 +02:00
|
|
|
|
2019-10-01 10:38:41 +02:00
|
|
|
val add = currentItem.additional
|
2019-09-13 12:03:14 +02:00
|
|
|
val type = currentItem.type.toLowerCase(Locale.ROOT)
|
|
|
|
|
if (add.isNotEmpty()) {
|
2019-10-19 19:48:21 +02:00
|
|
|
if (SubstUtil.checkStringForArray(add, SubstUtil.cancellations, true)) {
|
|
|
|
|
strings.strikeThrough()
|
2019-09-13 12:03:14 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
2019-10-19 19:48:21 +02:00
|
|
|
if (SubstUtil.checkStringForArray(type, SubstUtil.cancellations, true)) {
|
|
|
|
|
strings.strikeThrough()
|
2019-08-21 10:26:54 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-05-21 18:55:29 +02:00
|
|
|
|
2019-10-19 19:48:21 +02:00
|
|
|
var icon = SubstUtil.getIconForCourse(currentItem.course)
|
2019-09-11 20:59:55 +02:00
|
|
|
holder.group.setText(strings[0], TextView.BufferType.SPANNABLE)
|
|
|
|
|
holder.time.setText(strings[1], TextView.BufferType.SPANNABLE)
|
|
|
|
|
holder.course.setText(strings[2], TextView.BufferType.SPANNABLE)
|
|
|
|
|
holder.room.setText(strings[3], TextView.BufferType.SPANNABLE)
|
2019-09-04 18:46:25 +02:00
|
|
|
holder.teacher.setText(strings[4], TextView.BufferType.SPANNABLE)
|
2019-09-13 12:03:14 +02:00
|
|
|
holder.additional.text = if (currentItem.additional.isNotEmpty()) currentItem.additional else currentItem.type
|
2019-05-21 18:55:29 +02:00
|
|
|
|
2019-09-11 20:59:55 +02:00
|
|
|
holder.date.visibility = if (currentItem.date.isNotEmpty() && currentItem.date.substring(0, 3) == "psa") {
|
2019-08-06 17:08:43 +02:00
|
|
|
psa = true
|
2019-09-22 13:39:20 +02:00
|
|
|
holder.time.text = " " // creating a margin on the right side
|
2019-09-11 20:59:55 +02:00
|
|
|
icon = R.drawable.ic_idea
|
|
|
|
|
cardBackgroundColour = R.color.colorAccent
|
|
|
|
|
View.GONE
|
2019-08-06 17:08:43 +02:00
|
|
|
} else {
|
2019-10-19 19:48:21 +02:00
|
|
|
val colourString = SubstUtil.getColourString(holder.course.text.toString())
|
2019-10-23 21:15:12 +02:00
|
|
|
val colourPrefsInt = colours[colourString] ?: ""
|
2019-10-19 19:48:21 +02:00
|
|
|
colour = SubstUtil.getColourForString(colourPrefsInt, holder.context)
|
2019-09-11 20:59:55 +02:00
|
|
|
cardBackgroundColour = if (colour != 0) {
|
|
|
|
|
colour
|
2019-08-06 17:08:43 +02:00
|
|
|
} else {
|
2019-09-11 20:59:55 +02:00
|
|
|
R.color.colorBackgroundLight
|
2019-05-21 18:55:29 +02:00
|
|
|
}
|
2019-09-11 20:59:55 +02:00
|
|
|
View.VISIBLE
|
2019-05-21 18:55:29 +02:00
|
|
|
}
|
2019-09-11 20:59:55 +02:00
|
|
|
holder.date.text = currentItem.date
|
2019-09-04 18:46:25 +02:00
|
|
|
holder.spacer.visibility = if (strings[2].isEmpty() || strings[4].isEmpty()) {
|
|
|
|
|
View.GONE
|
|
|
|
|
} else {
|
|
|
|
|
View.VISIBLE
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-11 20:59:55 +02:00
|
|
|
val textColor = ContextCompat.getColor(holder.iconView.context, if (psa) {
|
2019-08-06 17:08:43 +02:00
|
|
|
R.color.colorBackground
|
2019-06-02 17:57:57 +02:00
|
|
|
} else {
|
2019-08-06 17:08:43 +02:00
|
|
|
when (colour) {
|
|
|
|
|
R.color.bgPureWhite -> R.color.colorTextDark
|
|
|
|
|
R.color.bgPureBlack -> R.color.colorTextLight
|
|
|
|
|
else -> R.color.colorText
|
|
|
|
|
}
|
2019-08-18 17:53:22 +02:00
|
|
|
})
|
2019-09-22 19:46:20 +02:00
|
|
|
|
|
|
|
|
holder.iconView.visibility = if (icon == R.drawable.ic_empty) {
|
|
|
|
|
View.GONE
|
|
|
|
|
} else {
|
|
|
|
|
View.VISIBLE
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-11 20:59:55 +02:00
|
|
|
holder.card.setCardBackgroundColor(ContextCompat.getColor(holder.iconView.context, cardBackgroundColour))
|
|
|
|
|
holder.iconView.setImageResource(icon)
|
|
|
|
|
holder.iconView.setColorFilter(textColor)
|
|
|
|
|
holder.group.setTextColor(textColor)
|
|
|
|
|
holder.date.setTextColor(textColor)
|
|
|
|
|
holder.time.setTextColor(textColor)
|
|
|
|
|
holder.course.setTextColor(textColor)
|
|
|
|
|
holder.room.setTextColor(textColor)
|
|
|
|
|
holder.additional.setTextColor(textColor)
|
2019-09-04 18:46:25 +02:00
|
|
|
holder.spacer.setTextColor(textColor)
|
|
|
|
|
holder.teacher.setTextColor(textColor)
|
2019-05-21 18:55:29 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-23 17:23:59 +02:00
|
|
|
override fun getItemCount(): Int = substitutions.size
|
2019-05-21 18:55:29 +02:00
|
|
|
|
2019-09-23 17:23:59 +02:00
|
|
|
fun setSubst(substitutions: List<Substitution>) {
|
|
|
|
|
this.substitutions = substitutions
|
2019-05-21 18:55:29 +02:00
|
|
|
notifyDataSetChanged()
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-01 10:38:41 +02:00
|
|
|
/// Strikes through the strings for course, room and teacher
|
2019-10-19 19:48:21 +02:00
|
|
|
private fun Array<SpannableString>.strikeThrough() {
|
|
|
|
|
for (i in 2..4) { this[i].setSpan(StrikethroughSpan(), 0, this[i].length, 0) }
|
2019-09-13 12:03:14 +02:00
|
|
|
}
|
2019-05-21 18:55:29 +02:00
|
|
|
}
|