conventionschedule-android/app/src/main/java/com/adlerosn/brasilfurfest/schedule/ScheduleActivity.kt

127 lines
4.4 KiB
Kotlin
Raw Normal View History

2018-07-09 17:16:33 +00:00
package com.adlerosn.brasilfurfest.schedule
import android.app.AlertDialog
import android.content.DialogInterface
import android.os.Bundle
import android.os.Handler
import android.support.v4.view.GravityCompat
import android.support.v7.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import android.widget.ListView
import com.adlerosn.brasilfurfest.R
import com.adlerosn.brasilfurfest.schedule.abstractDataTypes.convention.adapter.HourlyPlannedEventListAdapter
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_schedule.*
import java.util.*
class ScheduleActivity : AppCompatActivity() {
companion object {
val pageFlipListeners: MutableMap<String, ()->Unit> = mutableMapOf()
}
init {
pageFlipListeners.clear()
val pageFlipHandler = Handler()
val triggerPageFlipListeners = Runnable { pageFlipListeners.forEach { it.value.invoke() } }
Timer().schedule(object : TimerTask() {
override fun run() { pageFlipHandler.post(triggerPageFlipListeners) }
}, 1, 1750)
}
private var oneShotRan = false
private lateinit var scheduleManager: ScheduleManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
onCreateOneShotGuard()
setContentView(R.layout.activity_schedule)
setSupportActionBar(toolbar)
toolbar.title = getString(R.string.schedule)
toolbar.subtitle = getString(R.string.app_name)
updateActivitiesView()
}
override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_redownload-> {
redownloadSchedule()
true
}
R.id.action_clear_planning-> {
val dialog = AlertDialog.Builder(this, R.style.Theme_AppCompat_Light_Dialog_Alert).create()
dialog.setTitle(getString(R.string.schedule_clear_planning_question))
dialog.setMessage(getString(R.string.this_cannot_be_undone))
dialog.setButton(AlertDialog.BUTTON_NEGATIVE, getString(R.string.dialog_cancel)) {
_: DialogInterface, _: Int ->
dialog.dismiss()
}
dialog.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.dialog_confirm)) {
_: DialogInterface, _: Int ->
dialog.dismiss()
scheduleManager.clearIntentions()
onAttendeeIntentionsChanged()
}
dialog.show()
true
}
else -> super.onOptionsItemSelected(item)
}
}
fun updateActivitiesView() {
pageFlipListeners.clear()
conventionDaysHolder.removeAllViews()
conventionDaysHolder.addView(scheduleManager.render)
}
private fun onCreateOneShotGuard(){
if(!oneShotRan){
oneShotRan = true
onCreateOneShot()
}
}
fun onCreateOneShot(){
scheduleManager = ScheduleManager(this)
if (!scheduleManager.usable) {
redownloadSchedule()
}
}
fun redownloadSchedule(){
val alertDialog = AlertDialog.Builder(this, R.style.Theme_AppCompat_Light_Dialog_Alert).create()
alertDialog.setTitle(getString(R.string.dialog_pleasewait))
alertDialog.setMessage(getString(R.string.dialog_schedule_downloading4u))
alertDialog.setCancelable(false)
alertDialog.show()
scheduleManager.downloadAgain {
alertDialog.setCancelable(true)
alertDialog.dismiss()
updateActivitiesView()
}
}
fun onAttendeeIntentionsChanged() {
scheduleManager.onAttendeeIntentionsChanged()
updateActivitiesView()
}
}