2019-05-21 18:55:29 +02:00
|
|
|
package com.denizd.substitutionplan
|
|
|
|
|
|
2019-08-06 17:08:43 +02:00
|
|
|
import android.content.ActivityNotFoundException
|
|
|
|
|
import android.net.Uri
|
2019-05-21 18:55:29 +02:00
|
|
|
import android.preference.PreferenceManager
|
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-06-02 17:57:57 +02:00
|
|
|
import com.google.android.material.card.MaterialCardView
|
2019-05-21 18:55:29 +02:00
|
|
|
|
2019-05-22 20:33:07 +02:00
|
|
|
class CardAdapter(private var mSubst: List<Subst>) : 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-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-05-21 18:55:29 +02:00
|
|
|
var mImageView: ImageView = itemView.findViewById(R.id.iconView)
|
|
|
|
|
var mGroup: TextView = itemView.findViewById(R.id.group)
|
|
|
|
|
var mDate: TextView = itemView.findViewById(R.id.date)
|
|
|
|
|
var mTime: TextView = itemView.findViewById(R.id.time)
|
|
|
|
|
var mCourse: TextView = itemView.findViewById(R.id.course)
|
|
|
|
|
var mRoom: TextView = itemView.findViewById(R.id.room)
|
|
|
|
|
var mAdditional: TextView = itemView.findViewById(R.id.additional)
|
2019-08-16 10:31:22 +02:00
|
|
|
var mCard: MaterialCardView = itemView.findViewById(R.id.planCard)
|
2019-08-07 07:38:50 +02:00
|
|
|
init { view.setOnClickListener(this) }
|
|
|
|
|
override fun onClick(v: View?) {
|
|
|
|
|
if (mDate.text.toString().length > 7 && mDate.text.toString().substring(3, 7) == "http") {
|
|
|
|
|
try {
|
|
|
|
|
CustomTabsIntent.Builder().build().launchUrl(mCard.context,
|
|
|
|
|
Uri.parse(mDate.text.toString().substring(3)))
|
|
|
|
|
} catch (e: ActivityNotFoundException) {
|
|
|
|
|
Toast.makeText(mCard.context, mCard.context.getString(R.string.chromeCompatibleNotFound), Toast.LENGTH_LONG).show()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
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]
|
|
|
|
|
val prefs = PreferenceManager.getDefaultSharedPreferences(holder.mImageView.context)
|
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),
|
|
|
|
|
SpannableString(currentItem.course), SpannableString(currentItem.room))
|
|
|
|
|
for (item in strings) {
|
|
|
|
|
val qmark = item.indexOf("?")
|
|
|
|
|
if (qmark != -1) {
|
|
|
|
|
item.setSpan(StrikethroughSpan(), 0, qmark, 0)
|
2019-08-07 08:10:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-05-21 18:55:29 +02:00
|
|
|
|
|
|
|
|
holder.mImageView.setImageResource(currentItem.icon)
|
2019-08-09 09:52:05 +02:00
|
|
|
holder.mGroup.setText(strings[0], TextView.BufferType.SPANNABLE)
|
|
|
|
|
holder.mTime.setText(strings[1], TextView.BufferType.SPANNABLE)
|
|
|
|
|
holder.mCourse.setText(strings[2], TextView.BufferType.SPANNABLE)
|
|
|
|
|
holder.mRoom.setText(strings[3], TextView.BufferType.SPANNABLE)
|
2019-05-21 18:55:29 +02:00
|
|
|
holder.mAdditional.text = currentItem.additional
|
|
|
|
|
|
2019-08-06 17:08:43 +02:00
|
|
|
if (currentItem.date.isNotEmpty() && currentItem.date.substring(0, 3) == "psa") {
|
|
|
|
|
psa = true
|
2019-08-07 07:38:50 +02:00
|
|
|
holder.mDate.visibility = View.GONE
|
|
|
|
|
holder.mDate.text = currentItem.date
|
2019-08-11 20:25:01 +02:00
|
|
|
holder.mTime.text = " "
|
2019-08-06 17:08:43 +02:00
|
|
|
holder.mImageView.setImageResource(R.drawable.ic_idea)
|
|
|
|
|
holder.mCard.setCardBackgroundColor(ContextCompat.getColor(holder.mImageView.context, R.color.colorAccent))
|
|
|
|
|
} else {
|
2019-08-07 07:38:50 +02:00
|
|
|
holder.mDate.visibility = View.VISIBLE
|
2019-08-06 17:08:43 +02:00
|
|
|
holder.mDate.text = currentItem.date
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!psa) {
|
2019-05-21 18:55:29 +02:00
|
|
|
try {
|
2019-08-06 17:08:43 +02:00
|
|
|
colorCheck = holder.mCourse.text.toString().toLowerCase().substring(0, 3)
|
2019-08-09 21:18:27 +02:00
|
|
|
colourString = when (colorCheck) {
|
|
|
|
|
"deu", "dep", "daz", "fda" -> prefs.getString("cardGerman", "") ?: ""
|
|
|
|
|
"mat", "map" -> prefs.getString("cardMaths", "") ?: ""
|
|
|
|
|
"eng", "enp", "ena" -> prefs.getString("cardEnglish", "") ?: ""
|
|
|
|
|
"spo", "spp", "spth" -> prefs.getString("cardPhysEd", "") ?: ""
|
|
|
|
|
"pol", "pop" -> prefs.getString("cardPolitics", "") ?: ""
|
|
|
|
|
"dar", "dap" -> prefs.getString("cardTheatre", "") ?: ""
|
|
|
|
|
"phy", "php" -> prefs.getString("cardPhysics", "") ?: ""
|
|
|
|
|
"bio", "bip", "nw1", "nw2", "nw3", "nw4" -> prefs.getString("cardBiology", "") ?: ""
|
|
|
|
|
"che", "chp" -> prefs.getString("cardChemistry", "") ?: ""
|
|
|
|
|
"phi", "psp" -> prefs.getString("cardPhilosophy", "") ?: ""
|
|
|
|
|
"laa", "laf", "lat" -> prefs.getString("cardLatin", "") ?: ""
|
|
|
|
|
"spa", "spf" -> prefs.getString("cardSpanish", "") ?: ""
|
|
|
|
|
"fra", "frf", "frz" -> prefs.getString("cardFrench", "") ?: ""
|
|
|
|
|
"inf" -> prefs.getString("cardCompsci", "") ?: ""
|
|
|
|
|
"ges" -> prefs.getString("cardHistory", "") ?: ""
|
|
|
|
|
"rel" -> prefs.getString("cardReligion", "") ?: ""
|
|
|
|
|
"geg" -> prefs.getString("cardGeography", "") ?: ""
|
|
|
|
|
"kun" -> prefs.getString("cardArts", "") ?: ""
|
|
|
|
|
"mus" -> prefs.getString("cardMusic", "") ?: ""
|
|
|
|
|
"tue" -> prefs.getString("cardTurkish", "") ?: ""
|
|
|
|
|
"chi" -> prefs.getString("cardChinese", "") ?: ""
|
|
|
|
|
"gll" -> prefs.getString("cardGLL", "") ?: ""
|
|
|
|
|
"wat" -> prefs.getString("cardWAT", "") ?: ""
|
|
|
|
|
"för" -> prefs.getString("cardForder", "") ?: ""
|
|
|
|
|
"met", "wpb" -> prefs.getString("cardWP", "") ?: ""
|
|
|
|
|
else -> ""
|
2019-05-21 18:55:29 +02:00
|
|
|
}
|
2019-08-06 17:08:43 +02:00
|
|
|
} catch (e: StringIndexOutOfBoundsException) {
|
|
|
|
|
try {
|
|
|
|
|
colorCheck = holder.mCourse.text.toString().toLowerCase().substring(0, 2)
|
2019-08-09 21:18:27 +02:00
|
|
|
colourString = when (colorCheck) {
|
|
|
|
|
"nw" -> prefs.getString("cardBiology", "") ?: ""
|
|
|
|
|
"wp" -> prefs.getString("cardWP", "") ?: ""
|
|
|
|
|
else -> ""
|
2019-08-06 17:08:43 +02:00
|
|
|
}
|
|
|
|
|
} catch (e2: StringIndexOutOfBoundsException) {
|
2019-08-09 21:18:27 +02:00
|
|
|
colourString = ""
|
2019-08-06 17:08:43 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-08-09 21:18:27 +02:00
|
|
|
colour = MiscData.getColourForString(colourString)
|
2019-08-06 17:08:43 +02:00
|
|
|
if (colour != 0) {
|
|
|
|
|
holder.mCard.setCardBackgroundColor(ContextCompat.getColor(holder.mCourse.context, colour))
|
|
|
|
|
} else {
|
|
|
|
|
holder.mCard.setCardBackgroundColor(ContextCompat.getColor(holder.mCourse.context, R.color.colorBackgroundLight))
|
2019-05-21 18:55:29 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-06 17:08:43 +02:00
|
|
|
val textColor = if (psa) {
|
|
|
|
|
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-06 11:56:44 +02:00
|
|
|
}
|
|
|
|
|
holder.mImageView.setColorFilter(ContextCompat.getColor(holder.mImageView.context, textColor))
|
|
|
|
|
holder.mGroup.setTextColor(ContextCompat.getColor(holder.mImageView.context, textColor))
|
|
|
|
|
holder.mDate.setTextColor(ContextCompat.getColor(holder.mImageView.context, textColor))
|
|
|
|
|
holder.mTime.setTextColor(ContextCompat.getColor(holder.mImageView.context, textColor))
|
|
|
|
|
holder.mCourse.setTextColor(ContextCompat.getColor(holder.mImageView.context, textColor))
|
|
|
|
|
holder.mRoom.setTextColor(ContextCompat.getColor(holder.mImageView.context, textColor))
|
|
|
|
|
holder.mAdditional.setTextColor(ContextCompat.getColor(holder.mImageView.context, 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()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|