168 lines
7.7 KiB
Kotlin
168 lines
7.7 KiB
Kotlin
|
package com.adlerosn.brasilfurfest.schedule
|
||
|
|
||
|
import android.content.res.ColorStateList
|
||
|
import android.os.Bundle
|
||
|
import android.support.v7.app.AlertDialog
|
||
|
import android.support.v7.app.AppCompatActivity
|
||
|
import android.support.v7.widget.LinearLayoutManager
|
||
|
import android.text.Editable
|
||
|
import android.text.TextWatcher
|
||
|
import com.adlerosn.brasilfurfest.R
|
||
|
import com.adlerosn.brasilfurfest.helper.ActivitiesForFragments
|
||
|
import com.adlerosn.brasilfurfest.helper.ScheduleManagerGetter
|
||
|
import com.adlerosn.brasilfurfest.helper.inflate
|
||
|
import com.adlerosn.brasilfurfest.helper.runtimeLanguage
|
||
|
import com.adlerosn.brasilfurfest.schedule.abstractDataTypes.convention.Place
|
||
|
import com.adlerosn.brasilfurfest.schedule.abstractDataTypes.convention.Tag
|
||
|
import com.adlerosn.brasilfurfest.schedule.abstractDataTypes.dataholder.PeekableEvent
|
||
|
import com.adlerosn.brasilfurfest.schedule.adapters.PeekableEventRecyclerViewAdapter
|
||
|
import com.adlerosn.brasilfurfest.schedule.adapters.TagTripleListAdapter
|
||
|
import kotlinx.android.synthetic.main.activity_search_filter_criteria_item.view.*
|
||
|
import kotlinx.android.synthetic.main.activity_search_filter.*
|
||
|
import kotlinx.android.synthetic.main.activity_search_filter_circle.view.*
|
||
|
import org.jetbrains.anko.doAsync
|
||
|
import org.jetbrains.anko.uiThread
|
||
|
import java.util.Observer
|
||
|
|
||
|
class SearchFilterActivity : AppCompatActivity() {
|
||
|
val tags = mutableListOf<Tag>()
|
||
|
val places = mutableListOf<Place>()
|
||
|
var query = ""
|
||
|
lateinit var scheduleManager: ScheduleManager
|
||
|
lateinit var adapter: PeekableEventRecyclerViewAdapter
|
||
|
lateinit var everyPeekableEvent: List<PeekableEvent>
|
||
|
val observer = Observer { _, _ ->
|
||
|
runOnUiThread {
|
||
|
updateView()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||
|
ActivitiesForFragments[this.javaClass.simpleName] = this
|
||
|
super.onCreate(savedInstanceState)
|
||
|
setContentView(R.layout.activity_search_filter)
|
||
|
scheduleManager = ScheduleManagerGetter[this]
|
||
|
scheduleManager.addObserver(observer)
|
||
|
queryText.addTextChangedListener(object: TextWatcher{
|
||
|
override fun afterTextChanged(s: Editable?) {
|
||
|
query = s.toString()
|
||
|
updateView()
|
||
|
}
|
||
|
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
|
||
|
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
|
||
|
})
|
||
|
val rl = recyclerList!!
|
||
|
adapter = PeekableEventRecyclerViewAdapter(mutableListOf(), scheduleManager, this) {updateView()}
|
||
|
rl.adapter = adapter
|
||
|
rl.layoutManager = LinearLayoutManager(this@SearchFilterActivity)
|
||
|
everyPeekableEvent = (
|
||
|
scheduleManager.convention.events.map { PeekableEvent(it) } +
|
||
|
scheduleManager.attendeeFavorites.toList().filter { it.isCustom }.map { PeekableEvent(it, applicationContext) }
|
||
|
).sortedWith(compareBy(
|
||
|
{ it.timeRange.start.timeInMillis },
|
||
|
{ it.timeRange.finish.timeInMillis },
|
||
|
{ it.title[runtimeLanguage] }
|
||
|
))
|
||
|
updateView()
|
||
|
}
|
||
|
|
||
|
override fun onBackPressed() {
|
||
|
scheduleManager.deleteObserver(observer)
|
||
|
super.onBackPressed()
|
||
|
}
|
||
|
|
||
|
fun updateView() {
|
||
|
val fcp = filtercriteriaplaces!!
|
||
|
val fct = filtercriteriatags!!
|
||
|
val rl = recyclerList!!
|
||
|
doAsync {
|
||
|
val placeChoices = scheduleManager.convention.places.filterNot {
|
||
|
it in places
|
||
|
}.map {
|
||
|
Triple(it.names[runtimeLanguage]!!, it.color, { places.add(it) })
|
||
|
}.sortedWith(compareBy({it.first}, {it.second}))
|
||
|
uiThread {
|
||
|
fcp.removeAllViews()
|
||
|
places.forEach {
|
||
|
layoutInflater.inflate(R.layout.activity_search_filter_criteria_item).apply {
|
||
|
tag_label.text = it.names[runtimeLanguage]
|
||
|
tag_color.imageTintList = ColorStateList.valueOf(it.color)
|
||
|
setOnClickListener { _ -> places.remove(it); updateView() }
|
||
|
fcp.addView(this)
|
||
|
}
|
||
|
}
|
||
|
if (placeChoices.isNotEmpty()) {
|
||
|
layoutInflater.inflate(R.layout.activity_search_filter_criteria_add).apply {
|
||
|
fcp.addView(this)
|
||
|
setOnClickListener {
|
||
|
val choicesAdapted = TagTripleListAdapter(placeChoices)
|
||
|
AlertDialog.Builder(this@SearchFilterActivity)
|
||
|
.setTitle(R.string.schedule_event_key_places)
|
||
|
.setAdapter(choicesAdapted) { dialog, which: Int ->
|
||
|
placeChoices[which].third()
|
||
|
dialog.dismiss()
|
||
|
updateView()
|
||
|
}
|
||
|
.show()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
doAsync {
|
||
|
val tagChoices = scheduleManager.convention.tags.filterNot {
|
||
|
it in tags
|
||
|
}.map {
|
||
|
Triple(it.names[runtimeLanguage]!!, it.color, { tags.add(it) })
|
||
|
}.sortedWith(compareBy({it.first}, {it.second}))
|
||
|
uiThread {
|
||
|
fct.removeAllViews()
|
||
|
tags.forEach {
|
||
|
layoutInflater.inflate(R.layout.activity_search_filter_criteria_item).apply {
|
||
|
tag_label.text = it.names[runtimeLanguage]
|
||
|
tag_color.imageTintList = ColorStateList.valueOf(it.color)
|
||
|
setOnClickListener { _ -> tags.remove(it); updateView() }
|
||
|
fct.addView(this)
|
||
|
}
|
||
|
}
|
||
|
if (tagChoices.isNotEmpty()) {
|
||
|
layoutInflater.inflate(R.layout.activity_search_filter_criteria_add).apply {
|
||
|
fct.addView(this)
|
||
|
setOnClickListener {
|
||
|
val choicesAdapted = TagTripleListAdapter(tagChoices)
|
||
|
AlertDialog.Builder(this@SearchFilterActivity)
|
||
|
.setTitle(R.string.schedule_event_key_categories)
|
||
|
.setAdapter(choicesAdapted) { dialog, which: Int ->
|
||
|
tagChoices[which].third()
|
||
|
dialog.dismiss()
|
||
|
updateView()
|
||
|
}
|
||
|
.show()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
doAsync {
|
||
|
val toRemoveByName = if (query.isEmpty()) listOf() else
|
||
|
everyPeekableEvent.filter {
|
||
|
(query.toLowerCase() !in it.title[runtimeLanguage]?.toLowerCase()?:"")
|
||
|
&&
|
||
|
(query.toLowerCase() !in it.subtitles?.get(runtimeLanguage)?.toLowerCase()?:"")
|
||
|
&&
|
||
|
(query.toLowerCase() !in it.descriptions?.get(runtimeLanguage)?.toLowerCase()?:"")
|
||
|
}
|
||
|
val toRemoveByTag = if(tags.isEmpty()) listOf() else
|
||
|
everyPeekableEvent.filter { event ->
|
||
|
!tags.any { event.tags?.contains(it) ?: false }
|
||
|
}
|
||
|
val toRemoveByPlace = if(places.isEmpty()) listOf() else
|
||
|
everyPeekableEvent.filter { event ->
|
||
|
!places.any { event.places?.contains(it) ?: false }
|
||
|
}
|
||
|
val toDisplay = everyPeekableEvent - (toRemoveByName+toRemoveByTag+toRemoveByPlace).toSet()
|
||
|
adapter.onDataChanged(toDisplay)
|
||
|
}
|
||
|
}
|
||
|
}
|