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

332 lines
15 KiB
Kotlin
Raw Normal View History

package com.adlerosn.brasilfurfest.schedule
2018-12-12 04:30:34 +00:00
import android.annotation.SuppressLint
2019-05-23 14:58:10 +00:00
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
2018-07-18 15:01:16 +00:00
import android.content.Intent
2018-07-31 23:26:09 +00:00
import android.content.res.Resources
2018-07-27 20:24:09 +00:00
import android.graphics.Color
2019-05-23 14:58:10 +00:00
import android.net.Uri
import android.os.Bundle
2018-12-12 04:30:34 +00:00
import android.provider.CalendarContract
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
2018-12-28 00:28:48 +00:00
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.view.View
import android.view.ViewGroup
2018-07-27 20:24:09 +00:00
import android.view.WindowManager
2018-07-27 03:39:36 +00:00
import com.adlerosn.brasilfurfest.CountdownActivity
import com.adlerosn.brasilfurfest.R
2018-07-18 15:40:37 +00:00
import com.adlerosn.brasilfurfest.SplashActivity
2018-12-12 04:30:34 +00:00
import com.adlerosn.brasilfurfest.helper.*
import com.adlerosn.brasilfurfest.helper.observables.Observable
import com.adlerosn.brasilfurfest.helper.observables.Observer
import com.adlerosn.brasilfurfest.schedule.abstractDataTypes.convention.FilterableNamedColoredAttribute
import com.adlerosn.brasilfurfest.schedule.abstractDataTypes.managed.AttendeeConFavorite
2018-12-13 02:04:29 +00:00
import com.adlerosn.brasilfurfest.schedule.managers.ScheduleManagerGetter
2019-05-23 14:58:10 +00:00
import com.google.gson.GsonBuilder
import kotlinx.android.synthetic.main.activity_event.*
import kotlinx.android.synthetic.main.activity_event_content.*
import kotlinx.android.synthetic.main.activity_event_content_keypair.view.*
2019-05-23 14:58:10 +00:00
import kotlinx.android.synthetic.main.activity_event_share.view.*
2018-12-28 00:28:48 +00:00
import org.jetbrains.anko.doAsync
import kotlin.math.roundToInt
2018-12-12 04:30:34 +00:00
class EventActivity : AppCompatActivity() {
2018-07-18 15:01:16 +00:00
private var fromNotification = false
2018-12-12 04:30:34 +00:00
override fun onCreate(savedInstanceState: Bundle?) {
2018-12-12 04:30:34 +00:00
ActivitiesForFragments[this.javaClass.simpleName] = this
super.onCreate(savedInstanceState)
2018-07-27 20:24:09 +00:00
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
window.statusBarColor = Color.TRANSPARENT
setContentView(R.layout.activity_event)
2018-12-28 00:28:48 +00:00
resourcesAliased = this.resources
setSupportActionBar(toolbar)
toolbar_layout.setCollapsedTitleTextAppearance(R.style.CollapsedAppBar)
toolbar_layout.setExpandedTitleTextAppearance(R.style.ExpandedAppBar)
2018-12-12 04:30:34 +00:00
val scheduleManager = ScheduleManagerGetter[this]
val favorite = intent.getSerializableExtra("favorite") as AttendeeConFavorite
val offerStar = intent.getBooleanExtra("offerStar", false)
2018-07-18 15:01:16 +00:00
fromNotification = intent.getBooleanExtra("fromNotification", false)
val event = favorite.findInConvention(scheduleManager.convention)
2018-12-28 00:28:48 +00:00
toolbar_layout.title = event?.titles?.solved ?: favorite.getString()
doAsync { uiThreadWithLogger {
event?.assetImage?.solved?.fromCacheManagerToImageOf(app_bar_image)
?:
scheduleManager.convention.imageDefaultEvent?.solved?.fromCacheManagerToImageOf(app_bar_image)
} }
2018-07-27 03:39:36 +00:00
2018-12-28 00:28:48 +00:00
val timerClickListener = View.OnClickListener{ _ ->
2018-07-27 03:39:36 +00:00
Intent(this, CountdownActivity::class.java).let {
2018-12-28 00:28:48 +00:00
it.putExtra("label", favorite.getString())
2018-07-27 03:39:36 +00:00
it.putExtra("counter", favorite.timeRange.start.timeInMillis)
2018-07-27 20:24:09 +00:00
it.putExtra("finishesAt", favorite.timeRange.finish.timeInMillis)
2018-07-27 03:39:36 +00:00
startActivity(it)
}
}
keypairs.removeAllViews()
inflateKeyPair(
2018-12-28 00:28:48 +00:00
R.string.schedule_event_key_activity.getString(),
favorite.getString(),
keypairs
)
2018-12-28 00:28:48 +00:00
if(event?.subtitles?.solved?.isNotEmpty() == true)
inflateKeyPair(
2018-12-28 00:28:48 +00:00
R.string.schedule_event_key_detail.getString(),
event.subtitles.solved!!,
keypairs
)
2018-07-31 23:26:09 +00:00
val whenButton = inflateKeyPair(
2018-12-28 00:28:48 +00:00
R.string.schedule_event_key_when.getString(),
R.string.schedule_event_key_when_formatted.getString().format(
favorite.timeRange.start.timeInMillis.formatAsLongDayOfWeekWithNumber(),
favorite.timeRange.start.timeInMillis.formatAsHoursAndMinutes(),
favorite.timeRange.finish.timeInMillis.formatAsHoursAndMinutes()
).capitalize(),
keypairs
2018-07-27 03:39:36 +00:00
).apply {
touchableIndicator.visibility = View.VISIBLE
setOnClickListener(timerClickListener)
}
2019-05-23 14:58:10 +00:00
if(favorite.isSecondaryStringPlace){
inflateKeyPair(
R.string.schedule_event_key_place.getString(),
favorite.getSecondaryString(),
keypairs
)
}
if(event!=null) {
if(event.places.isNotEmpty())
inflateKeyPair(
2018-12-28 00:28:48 +00:00
event.places.size.pluralize(R.string.schedule_event_key_place, R.string.schedule_event_key_places).getString(),
formatSpannableWithColor(event.places),
keypairs
)
if(event.language!=null)
inflateKeyPair(
2018-12-28 00:28:48 +00:00
R.string.schedule_event_key_language.getString(),
event.language.language.getString(),
keypairs
)
2018-12-28 00:28:48 +00:00
event.descriptions.solved?.let {
if(it.isNotEmpty())
inflateKeyPair(
2018-12-28 00:28:48 +00:00
R.string.schedule_event_key_description.getString(),
it,
keypairs
)
if(event.tags.isNotEmpty())
inflateKeyPair(
2018-12-28 00:28:48 +00:00
event.tags.size.pluralize(R.string.schedule_event_key_category, R.string.schedule_event_key_categories).getString(),
formatSpannableWithColor(event.tags),
keypairs
)
if(event.conbookPages.isNotEmpty())
inflateKeyPair(
2018-12-28 00:28:48 +00:00
event.conbookPages.size.pluralize(R.string.schedule_event_key_on_conbooks_page, R.string.schedule_event_key_on_conbooks_pages).getString(),
event.conbookPages.map { it.toString() }.textuallyJoined,
keypairs
)
if(event.conbookId.isNotEmpty())
inflateKeyPair(
2018-12-28 00:28:48 +00:00
R.string.schedule_event_key_identified_on_conbook_as.getString(),
event.conbookId,
keypairs
)
if(event.attendableBy.isNotEmpty())
inflateKeyPair(
2018-12-28 00:28:48 +00:00
event.attendableBy.size.pluralize(R.string.schedule_event_key_elegible_registration, R.string.schedule_event_key_elegible_registrations).getString(),
event.attendableBy.map { it.names.solved!! }.textuallyJoined,
keypairs
)
}
2018-12-12 04:30:34 +00:00
@SuppressLint("RestrictedApi")
2018-12-28 00:28:48 +00:00
if(event.hiddenFromTimeTable or scheduleManager.canAttend(event).not()) {
fab.visibility = View.GONE
2018-12-12 04:30:34 +00:00
fabCalendar.visibility = View.GONE
2019-05-23 14:58:10 +00:00
fabShare.visibility = View.GONE
2018-12-12 04:30:34 +00:00
}
}
@SuppressLint("RestrictedApi")
if(!offerStar){
fab.visibility = View.GONE
fabCalendar.visibility = View.GONE
2019-05-23 14:58:10 +00:00
fabShare.visibility = View.GONE
}
fab.setOnClickListener { _ ->
2018-12-28 00:28:48 +00:00
R.drawable.ic_star_half_white_24dp.asImageOf(fab)
if (favorite in scheduleManager.attendeeFavorites)
scheduleManager.attendeeFavorites.remove(favorite)
else
scheduleManager.attendeeFavorites.add(favorite)
}
2018-07-31 23:26:09 +00:00
fab.isLongClickable = true
fab.setOnLongClickListener {
Snackbar.make(it.rootView, R.string.add_or_remove_favorite, Snackbar.LENGTH_LONG).apply {
2018-12-28 00:28:48 +00:00
setActionTextColor(this@EventActivity.getColorCompat(R.color.colorBase20))
view.setBackgroundColor(this@EventActivity.getColorCompat(R.color.colorBase03))
2018-07-31 23:26:09 +00:00
view.let {
it.setPadding(
it.paddingLeft,
it.paddingTop,
it.paddingRight,
it.paddingBottom + (48 * Resources.getSystem().displayMetrics.density).toInt()
2018-12-12 04:30:34 +00:00
)
show()
}
}
true
}
fabCalendar.setOnClickListener { _ ->
val intent = Intent(Intent.ACTION_EDIT)
intent.type = "vnd.android.cursor.item/event"
2018-12-28 00:28:48 +00:00
intent.putExtra(CalendarContract.Events.TITLE, favorite.getString())
2018-12-12 04:30:34 +00:00
if(event!=null) {
2018-12-28 00:28:48 +00:00
intent.putExtra(CalendarContract.Events.EVENT_LOCATION, event.places.map { it.names.solved ?: "" }.textuallyJoined)
intent.putExtra(CalendarContract.Events.DESCRIPTION, event.descriptions.solved ?: "")
2018-12-12 04:30:34 +00:00
}
2018-12-28 00:28:48 +00:00
intent.putExtra(
CalendarContract.EXTRA_EVENT_BEGIN_TIME,
favorite.timeRange.start.timeInMillis
)
intent.putExtra(
CalendarContract.EXTRA_EVENT_END_TIME,
favorite.timeRange.finish.timeInMillis
)
2018-12-12 04:30:34 +00:00
startActivity(intent)
}
2019-05-23 14:58:10 +00:00
fabShare.isLongClickable = true
fabShare.setOnClickListener {
val gson = GsonBuilder().create()
val link = "https://brasilfurfest.com.br/app/roomparty#"+ Uri.encode(gson.toJson(favorite))
val dialog = AlertDialog.Builder(this).create()
dialog.setTitle(R.string.event_share_question.getString())
val view = layoutInflater.inflate(R.layout.activity_event_share)
view.btnLink.setOnClickListener {
val clipboardManager = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clipData = ClipData.newPlainText("Room party link", link)
clipboardManager.setPrimaryClip(clipData)
2019-05-23 14:58:10 +00:00
startActivity(Intent.createChooser(Intent(Intent.ACTION_SEND).apply { type="text/plain"; putExtra(Intent.EXTRA_TEXT, link) }, null))
dialog.dismiss()
}
view.btnQr.setOnClickListener {
startActivity(Intent(this, QRCodeViewerActivity::class.java).apply { putExtra("data", link) })
dialog.dismiss()
}
dialog.setView(view)
dialog.show()
}
fabShare.setOnLongClickListener {
Snackbar.make(it.rootView, R.string.shows_you_how_you_can_share_this, Snackbar.LENGTH_LONG).apply {
setActionTextColor(this@EventActivity.getColorCompat(R.color.colorBase20))
view.setBackgroundColor(this@EventActivity.getColorCompat(R.color.colorBase03))
view.let {
it.setPadding(
it.paddingLeft,
it.paddingTop,
it.paddingRight,
it.paddingBottom + 48.asDpToPx.roundToInt()
)
show()
}
}
true
}
if(favorite.isShareable.not())
@SuppressLint("RestrictedApi")
fabShare.visibility = View.INVISIBLE
2018-12-12 04:30:34 +00:00
fabCalendar.isLongClickable = true
fabCalendar.setOnLongClickListener {
Snackbar.make(it.rootView, R.string.adds_to_your_agenda_app, Snackbar.LENGTH_LONG).apply {
2018-12-28 00:28:48 +00:00
setActionTextColor(this@EventActivity.getColorCompat(R.color.colorBase20))
view.setBackgroundColor(this@EventActivity.getColorCompat(R.color.colorBase03))
2018-12-12 04:30:34 +00:00
view.let {
it.setPadding(
it.paddingLeft,
it.paddingTop,
it.paddingRight,
2018-12-28 00:28:48 +00:00
it.paddingBottom + 48.asDpToPx.roundToInt()
2018-07-31 23:26:09 +00:00
)
show()
}
}
true
}
whenButton.isLongClickable = true
whenButton.setOnLongClickListener {
Snackbar.make(it.rootView, R.string.opens_timer, Snackbar.LENGTH_LONG).apply {
2018-12-28 00:28:48 +00:00
setActionTextColor(this@EventActivity.getColorCompat(R.color.colorBase20))
view.setBackgroundColor(this@EventActivity.getColorCompat(R.color.colorBase03))
2018-07-31 23:26:09 +00:00
view.let {
it.setPadding(
it.paddingLeft,
it.paddingTop,
it.paddingRight,
2018-12-28 00:28:48 +00:00
it.paddingBottom + 48.asDpToPx.roundToInt()
2018-07-31 23:26:09 +00:00
)
show()
}
}
true
}
scheduleManager.addObserver (object : Observer<Observable<Any?>, Any?> {
override fun update(observable: Observable<Any?>, args: Any?) {
if (favorite in scheduleManager.attendeeFavorites)
R.drawable.ic_star_white_24dp.asImageOf(fab)
else
R.drawable.ic_star_border_white_24dp.asImageOf(fab)
}
}.apply { this.update(Observable(), null) })
}
private fun inflateKeyPair(key: String, value: CharSequence, into: ViewGroup? = null) =
layoutInflater.inflate(R.layout.activity_event_content_keypair).apply {
textKey.text = key
textValue.text = value
into?.addView(this)
2018-07-27 03:39:36 +00:00
touchableIndicator.visibility = View.GONE
}
2018-07-18 15:01:16 +00:00
override fun onBackPressed() {
if (!fromNotification)
super.onBackPressed()
else {
2018-07-18 15:40:37 +00:00
val intent = Intent(this, SplashActivity::class.java)
2018-12-28 00:28:48 +00:00
intent.flags =
Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_TASK_ON_HOME
2018-07-18 15:40:37 +00:00
intent.putExtra("fastStart", true)
2018-07-18 15:01:16 +00:00
this.startActivity(intent)
this.finish()
}
}
private fun formatSpannableWithColor(collection: List<FilterableNamedColoredAttribute>): Spannable =
2018-12-28 00:28:48 +00:00
collection.map {
SpannableString(
it.names.solved!!,
RoundedBackgroundAndForegroundColorsSpan(
it.color,
it.color.decideBestForegroundColor(
applicationContext.getColorCompat(R.color.colorBase03),
applicationContext.getColorCompat(R.color.colorBase21)
)
)
)
}.textuallyJoined.let { SpannableStringBuilder().append(it).append(" ") }
}