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-11 20:59:55 +02:00
|
|
|
import android.content.SharedPreferences
|
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-09-03 18:18:21 +02:00
|
|
|
import com.denizd.substitutionplan.data.HelperFunctions
|
2019-09-01 11:54:12 +02:00
|
|
|
import com.denizd.substitutionplan.R
|
|
|
|
|
import com.denizd.substitutionplan.models.Subst
|
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-11 20:59:55 +02:00
|
|
|
internal class CardAdapter(private var mSubst: List<Subst>, private val prefs: SharedPreferences) : RecyclerView.Adapter<CardAdapter.CardViewHolder>() {
|
2019-05-21 18:55:29 +02:00
|
|
|
|
2019-06-02 17:57:57 +02:00
|
|
|
private var colour = 0
|
2019-08-09 21:18:27 +02:00
|
|
|
private var colourString = ""
|
2019-06-02 17:57:57 +02:00
|
|
|
private var colorCheck = ""
|
2019-09-13 12:03:14 +02:00
|
|
|
private val cancellations = arrayOf("eigenverantwortliches arbeiten", "entfall", "entfällt", "fällt aus", "freisetzung", "vtr. ohne lehrer")
|
2019-05-21 18:55:29 +02:00
|
|
|
|
2019-08-11 20:25:01 +02:00
|
|
|
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)
|
2019-09-04 18:46:25 +02:00
|
|
|
|
2019-09-11 20:59:55 +02:00
|
|
|
var card: MaterialCardView = view.findViewById(R.id.planCard)
|
2019-08-07 07:38:50 +02:00
|
|
|
init { view.setOnClickListener(this) }
|
|
|
|
|
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-11 20:59:55 +02:00
|
|
|
CustomTabsIntent.Builder().build().launchUrl(card.context,
|
|
|
|
|
Uri.parse(date.text.toString().substring(3)))
|
2019-08-07 07:38:50 +02:00
|
|
|
} catch (e: ActivityNotFoundException) {
|
2019-09-11 20:59:55 +02:00
|
|
|
Toast.makeText(card.context, card.context.getString(R.string.chromeCompatibleNotFound), 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) {
|
|
|
|
|
val currentItem = mSubst[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-11 20:59:55 +02:00
|
|
|
var cardBackgroundColour = 0
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
val add = currentItem.additional.toLowerCase(Locale.ROOT)
|
|
|
|
|
val type = currentItem.type.toLowerCase(Locale.ROOT)
|
|
|
|
|
if (add.isNotEmpty()) {
|
|
|
|
|
if (HelperFunctions.checkStringForArray(add, cancellations)) {
|
|
|
|
|
strikeThrough(strings)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (HelperFunctions.checkStringForArray(type, cancellations)) {
|
|
|
|
|
strikeThrough(strings)
|
2019-08-21 10:26:54 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-05-21 18:55:29 +02:00
|
|
|
|
2019-09-11 20:59:55 +02:00
|
|
|
var icon = HelperFunctions.getIconForCourse(currentItem.course)
|
|
|
|
|
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-11 20:59:55 +02:00
|
|
|
holder.time.text = " "
|
|
|
|
|
icon = R.drawable.ic_idea
|
|
|
|
|
cardBackgroundColour = R.color.colorAccent
|
|
|
|
|
View.GONE
|
2019-08-06 17:08:43 +02:00
|
|
|
} else {
|
2019-09-11 20:59:55 +02:00
|
|
|
colourString = getColourString(holder.course.text.toString())
|
2019-08-18 17:53:22 +02:00
|
|
|
val colourPrefsInt = if (colourString.isNotEmpty()) {
|
|
|
|
|
prefs.getString("card$colourString", "") ?: ""
|
|
|
|
|
} else {
|
|
|
|
|
""
|
2019-08-06 17:08:43 +02:00
|
|
|
}
|
2019-09-03 18:18:21 +02:00
|
|
|
colour = HelperFunctions.getColourForString(colourPrefsInt)
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun getItemCount(): Int = mSubst.size
|
|
|
|
|
|
2019-05-22 20:33:07 +02:00
|
|
|
fun setSubst(subst: List<Subst>) {
|
2019-05-21 18:55:29 +02:00
|
|
|
mSubst = subst
|
|
|
|
|
notifyDataSetChanged()
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-18 17:53:22 +02:00
|
|
|
private fun getColourString(course: String): String {
|
|
|
|
|
return try {
|
|
|
|
|
colorCheck = course.toLowerCase(Locale.ROOT).substring(0, 3)
|
|
|
|
|
when (colorCheck) {
|
|
|
|
|
"deu", "dep", "daz", "fda" -> "German"
|
|
|
|
|
"mat", "map" -> "Maths"
|
|
|
|
|
"eng", "enp", "ena" -> "English"
|
|
|
|
|
"spo", "spp", "spth" -> "PhysEd"
|
2019-08-21 10:26:54 +02:00
|
|
|
"pol", "pop" -> "Politics"
|
2019-08-18 17:53:22 +02:00
|
|
|
"dar", "dap" -> "Theatre"
|
|
|
|
|
"phy", "php" -> "Physics"
|
|
|
|
|
"bio", "bip", "nw1", "nw2", "nw3", "nw4" -> "Biology"
|
|
|
|
|
"che", "chp" -> "Chemistry"
|
|
|
|
|
"phi", "psp" -> "Philosophy"
|
|
|
|
|
"laa", "laf", "lat" -> "Latin"
|
|
|
|
|
"spa", "spf" -> "Spanish"
|
|
|
|
|
"fra", "frf", "frz" -> "French"
|
2019-09-12 16:55:49 +02:00
|
|
|
"inf" -> "CompSci"
|
2019-08-18 17:53:22 +02:00
|
|
|
"ges" -> "History"
|
|
|
|
|
"rel" -> "Religion"
|
|
|
|
|
"geg" -> "Geography"
|
|
|
|
|
"kun" -> "Arts"
|
|
|
|
|
"mus" -> "Music"
|
|
|
|
|
"tue" -> "Turkish"
|
|
|
|
|
"chi" -> "Chinese"
|
|
|
|
|
"gll" -> "GLL"
|
|
|
|
|
"wat" -> "WAT"
|
|
|
|
|
"för" -> "Forder"
|
|
|
|
|
"met", "wpb" -> "WP"
|
|
|
|
|
else -> ""
|
|
|
|
|
}
|
|
|
|
|
} catch (e: StringIndexOutOfBoundsException) {
|
|
|
|
|
try {
|
|
|
|
|
colorCheck = course.toLowerCase(Locale.ROOT).substring(0, 2)
|
|
|
|
|
when (colorCheck) {
|
|
|
|
|
"nw" -> "Biology"
|
|
|
|
|
"wp" -> "WP"
|
|
|
|
|
else -> ""
|
|
|
|
|
}
|
|
|
|
|
} catch (e2: StringIndexOutOfBoundsException) {
|
|
|
|
|
""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-13 12:03:14 +02:00
|
|
|
private fun strikeThrough(strings: Array<SpannableString>) {
|
|
|
|
|
for (i in 2..4) { strings[i].setSpan(StrikethroughSpan(), 0, strings[i].length, 0) }
|
|
|
|
|
}
|
2019-05-21 18:55:29 +02:00
|
|
|
}
|