conventionschedule-android/app/src/main/java/com/adlerosn/brasilfurfest/schedule/adapters/PeekableEventRecyclerViewAd...

147 lines
6.9 KiB
Kotlin
Raw Normal View History

2018-12-12 04:30:34 +00:00
package com.adlerosn.brasilfurfest.schedule.adapters
import android.app.Activity
import android.content.Intent
import android.content.res.ColorStateList
import android.support.v4.content.ContextCompat
import android.support.v7.util.DiffUtil
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import com.adlerosn.brasilfurfest.R
import com.adlerosn.brasilfurfest.helper.*
import com.adlerosn.brasilfurfest.schedule.EventActivity
import com.adlerosn.brasilfurfest.schedule.ScheduleManager
import com.adlerosn.brasilfurfest.schedule.abstractDataTypes.convention.Event
import com.adlerosn.brasilfurfest.schedule.abstractDataTypes.dataholder.PeekableEvent
import com.adlerosn.brasilfurfest.schedule.abstractDataTypes.managed.AttendeeConFavorite
import com.adlerosn.brasilfurfest.schedule.viewHolder.RecyclerViewHolder
import kotlinx.android.synthetic.main.activity_search_filter_circle.view.*
import kotlinx.android.synthetic.main.activity_search_filter_item.view.*
import java.text.SimpleDateFormat
class PeekableEventRecyclerViewAdapter(
var items: MutableList<PeekableEvent>,
val scheduleManager: ScheduleManager,
val activity: Activity,
val triggerViewRefresh: ()->Unit
): RecyclerView.Adapter<RecyclerViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
return RecyclerViewHolder(parent.context.layoutInflater.inflate(R.layout.activity_search_filter_item))
}
fun onDataChanged(newData: List<PeekableEvent>){
val oldData = items
activity.runOnUiThread {
items = newData.toMutableList()
val diff = DiffUtil.calculateDiff(ListDiffCallback(oldData, newData))
diff.dispatchUpdatesTo(this)
}
}
override fun getItemCount(): Int = items.size
override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
val it = items[position]
holder.itemView.apply {
val dowFormatter = SimpleDateFormat("E", context.runtimeLanguage.locale)
val hourMinuteFormatter = SimpleDateFormat("HH:mm", context.runtimeLanguage.locale)
eventTitle.text = it.title[context.runtimeLanguage]
startDay.text = dowFormatter.format(it.timeRange.start.timeInMillis).capitalize()
startText.text = hourMinuteFormatter.format(it.timeRange.start.timeInMillis)
finishText.text = hourMinuteFormatter.format(it.timeRange.finish.timeInMillis)
val ccls = circles!!
ccls.removeAllViews()
it.tags?.forEach {
context.layoutInflater.inflate(R.layout.activity_search_filter_circle).apply {
tag_color.imageTintList = ColorStateList.valueOf(it.color)
ccls.addView(this)
}
}
it.places?.forEach {
context.layoutInflater.inflate(R.layout.activity_search_filter_circle).apply {
tag_color.imageTintList = ColorStateList.valueOf(it.color)
ccls.addView(this)
}
}
context.layoutInflater.inflate(R.layout.activity_search_filter_circle).apply {
tag_color.imageTintList = ColorStateList.valueOf(0x00000000)
ccls.addView(this)
}
if (it.language == null) {
flag.visibility = View.INVISIBLE
} else {
flag.visibility = View.VISIBLE
flag.setImageDrawable(ContextCompat.getDrawable(context, it.language.drawableId))
}
val favorites = scheduleManager.attendeeFavorites.toList()
val event = scheduleManager.convention.events.firstOrNull { ev -> ev.conbookId == it.id }
val showsStar = event?.let{
it.attendableBy.map { it.level }.contains(scheduleManager.attendeeFavorites.registrationTier.level)
&& !it.hiddenFromTimeTable
} ?: true
var hasStar = favorites.any { fav -> it.similarEnough(fav, context) }
if (showsStar)
starImage.visibility = View.VISIBLE
else
starImage.visibility = View.INVISIBLE
if (hasStar)
starImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_star_black_24dp))
else
starImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_star_border_black_24dp))
val starValue = if(!showsStar) null else hasStar
this.starButton.setOnClickListener { _ ->
if(showsStar){
if(hasStar)
scheduleManager.attendeeFavorites.toList().firstOrNull { fav ->
it.similarEnough(fav, context)
}?.let {
scheduleManager.attendeeFavorites.remove(it).apply {
starImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_star_border_black_24dp))
hasStar = false
triggerViewRefresh()
}
}
else
event?.let {
scheduleManager.attendeeFavorites.add(
AttendeeConFavorite(
context.runtimeLanguage,
scheduleManager.convention,
it
)
).apply {
starImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_star_black_24dp))
hasStar = true
triggerViewRefresh()
}
}
}
}
this.setOnClickListener { _ ->
val favorite = scheduleManager.attendeeFavorites.toList().firstOrNull {
fav -> it.similarEnough(fav, context)
} ?: AttendeeConFavorite(
context.runtimeLanguage,
scheduleManager.convention,
event!!
)
activity.startActivity(
Intent(activity, EventActivity::class.java).apply {
putExtra("favorite", favorite)
putExtra("offerStar", showsStar)
}
)
}
bindPeekAndPop(
activity,
this as ViewGroup,
this,
starValue,
{this.starButton.callOnClick()},
{this.callOnClick()},
it
)
}
}
}