[UI] Update indentation, again. Fix manual event color selecting.

This commit is contained in:
Kuba Szczodrzyński 2019-11-21 18:43:01 +01:00
parent ce06084e6f
commit 82852389fa

View File

@ -57,300 +57,292 @@ class EventManualV2Dialog(
private lateinit var event: Event private lateinit var event: Event
private var customColor: Int? = null private var customColor: Int? = null
init { init { run {
run { if (activity.isFinishing)
if (activity.isFinishing) return@run
return@run job = Job()
job = Job() onShowListener?.invoke(TAG)
onShowListener?.invoke(TAG) b = DialogEventManualV2Binding.inflate(activity.layoutInflater)
b = DialogEventManualV2Binding.inflate(activity.layoutInflater) dialog = MaterialAlertDialogBuilder(activity)
dialog = MaterialAlertDialogBuilder(activity) .setTitle(R.string.dialog_event_manual_title)
.setTitle(R.string.dialog_event_manual_title) .setView(b.root)
.setView(b.root) .setNegativeButton(R.string.cancel) { dialog, _ -> dialog.dismiss() }
.setNegativeButton(R.string.cancel) { dialog, _ -> dialog.dismiss() } .setPositiveButton(R.string.save) { _, _ -> saveEvent() }
.setPositiveButton(R.string.save) { _, _ -> saveEvent() } .setOnDismissListener {
.setOnDismissListener { onDismissListener?.invoke(TAG)
onDismissListener?.invoke(TAG) }
} .show()
.show()
event = editingEvent?.clone() ?: Event().also { event -> event = editingEvent?.clone() ?: Event().also { event ->
event.profileId = profileId event.profileId = profileId
/*defaultDate?.let { /*defaultDate?.let {
event.eventDate = it event.eventDate = it
b.date = it b.date = it
}
defaultTime?.let {
event.startTime = it
b.time = it
}
defaultType?.let {
event.type = it
}*/
} }
defaultTime?.let {
b.showMore.onClick { event.startTime = it
// TODO iconics is broken b.time = it
it.apply {
refreshDrawableState()
if (isChecked)
Anim.expand(b.moreLayout, 200, null)
else
Anim.collapse(b.moreLayout, 200, null)
}
} }
defaultType?.let {
loadLists() event.type = it
}*/
} }
}
private fun loadLists() { b.showMore.onClick { // TODO iconics is broken
launch { it.apply {
val deferred = async(Dispatchers.Default) { refreshDrawableState()
// get the team list
val teams = app.db.teamDao().getAllNow(profileId)
b.teamDropdown.clear()
b.teamDropdown += TextInputDropDown.Item(
-1,
activity.getString(R.string.dialog_event_manual_no_team),
""
)
b.teamDropdown += teams.map { TextInputDropDown.Item(it.id, it.name, tag = it) }
// get the subject list if (isChecked)
val subjects = app.db.subjectDao().getAllNow(profileId) Anim.expand(b.moreLayout, 200, null)
b.subjectDropdown.clear() else
b.subjectDropdown += TextInputDropDown.Item( Anim.collapse(b.moreLayout, 200, null)
-1,
activity.getString(R.string.dialog_event_manual_no_subject),
""
)
b.subjectDropdown += subjects.map { TextInputDropDown.Item(it.id, it.longName, tag = it) }
// get the teacher list
val teachers = app.db.teacherDao().getAllNow(profileId)
b.teacherDropdown.clear()
b.teacherDropdown += TextInputDropDown.Item(
-1,
activity.getString(R.string.dialog_event_manual_no_teacher),
""
)
b.teacherDropdown += teachers.map { TextInputDropDown.Item(it.id, it.fullName, tag = it) }
// get the event type list
val eventTypes = app.db.eventTypeDao().getAllNow(profileId)
b.typeDropdown.clear()
b.typeDropdown += eventTypes.map { TextInputDropDown.Item(it.id, it.name, tag = it) }
} }
deferred.await() }
b.teamDropdown.isEnabled = true loadLists()
b.subjectDropdown.isEnabled = true }}
b.teacherDropdown.isEnabled = true
b.typeDropdown.isEnabled = true
b.typeDropdown.selected?.let { item -> private fun loadLists() { launch {
val deferred = async(Dispatchers.Default) {
// get the team list
val teams = app.db.teamDao().getAllNow(profileId)
b.teamDropdown.clear()
b.teamDropdown += TextInputDropDown.Item(
-1,
activity.getString(R.string.dialog_event_manual_no_team),
""
)
b.teamDropdown += teams.map { TextInputDropDown.Item(it.id, it.name, tag = it) }
// get the subject list
val subjects = app.db.subjectDao().getAllNow(profileId)
b.subjectDropdown.clear()
b.subjectDropdown += TextInputDropDown.Item(
-1,
activity.getString(R.string.dialog_event_manual_no_subject),
""
)
b.subjectDropdown += subjects.map { TextInputDropDown.Item(it.id, it.longName, tag = it) }
// get the teacher list
val teachers = app.db.teacherDao().getAllNow(profileId)
b.teacherDropdown.clear()
b.teacherDropdown += TextInputDropDown.Item(
-1,
activity.getString(R.string.dialog_event_manual_no_teacher),
""
)
b.teacherDropdown += teachers.map { TextInputDropDown.Item(it.id, it.fullName, tag = it) }
// get the event type list
val eventTypes = app.db.eventTypeDao().getAllNow(profileId)
b.typeDropdown.clear()
b.typeDropdown += eventTypes.map { TextInputDropDown.Item(it.id, it.name, tag = it) }
}
deferred.await()
b.teamDropdown.isEnabled = true
b.subjectDropdown.isEnabled = true
b.teacherDropdown.isEnabled = true
b.typeDropdown.isEnabled = true
b.typeDropdown.selected?.let { item ->
customColor = (item.tag as EventType).color
}
// copy IDs from event being edited
editingEvent?.let {
b.teamDropdown.select(it.teamId)
b.subjectDropdown.select(it.subjectId)
b.teacherDropdown.select(it.teacherId)
b.typeDropdown.select(it.type)?.let { item ->
customColor = (item.tag as EventType).color customColor = (item.tag as EventType).color
} }
if (it.color != -1)
// copy IDs from event being edited customColor = it.color
editingEvent?.let {
b.teamDropdown.select(it.teamId)
b.subjectDropdown.select(it.subjectId)
b.teacherDropdown.select(it.teacherId)
b.typeDropdown.select(it.type)?.let { item ->
customColor = (item.tag as EventType).color
}
if (it.color != -1)
customColor = it.color
}
// copy IDs from the LessonFull
defaultLesson?.let {
b.teamDropdown.select(it.displayTeamId)
b.subjectDropdown.select(it.displaySubjectId)
b.teacherDropdown.select(it.displayTeacherId)
}
b.typeDropdown.setOnChangeListener {
b.typeDropdown.background.colorFilter = PorterDuffColorFilter((it.tag as EventType).color, PorterDuff.Mode.SRC_ATOP)
customColor = null
return@setOnChangeListener true
}
customColor?.let {
b.typeDropdown.background.colorFilter = PorterDuffColorFilter(it, PorterDuff.Mode.SRC_ATOP)
}
b.typeColor.onClick {
val currentColor = (b.typeDropdown?.selected?.tag as EventType?)?.color
?: Event.COLOR_DEFAULT
val colorPickerDialog = ColorPickerDialog.newBuilder()
.setColor(currentColor)
.create()
colorPickerDialog.setColorPickerDialogListener(
object : ColorPickerDialogListener {
override fun onDialogDismissed(dialogId: Int) {}
override fun onColorSelected(dialogId: Int, color: Int) {
b.typeDropdown.background.colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP)
customColor = color
}
})
colorPickerDialog.show(activity.fragmentManager, "color-picker-dialog")
}
loadDates()
} }
}
private fun loadDates() { // copy IDs from the LessonFull
launch { defaultLesson?.let {
val date = Date.getToday() b.teamDropdown.select(it.displayTeamId)
val today = date.value b.subjectDropdown.select(it.displaySubjectId)
var weekDay = date.weekDay b.teacherDropdown.select(it.displayTeacherId)
}
val deferred = async(Dispatchers.Default) { b.typeDropdown.setOnChangeListener {
val dates = mutableListOf<TextInputDropDown.Item>() b.typeColor.background.colorFilter = PorterDuffColorFilter((it.tag as EventType).color, PorterDuff.Mode.SRC_ATOP)
// item choosing the next lesson of specific subject customColor = null
b.subjectDropdown.selected?.let { return@setOnChangeListener true
if (it.tag is Subject) { }
dates += TextInputDropDown.Item( (customColor ?: Event.COLOR_DEFAULT).let {
-it.id, b.typeColor.background.colorFilter = PorterDuffColorFilter(it, PorterDuff.Mode.SRC_ATOP)
activity.getString(R.string.dialog_event_manual_date_next_lesson, it.tag.longName) }
) b.typeColor.onClick {
} val currentColor = (b.typeDropdown?.selected?.tag as EventType?)?.color ?: Event.COLOR_DEFAULT
val colorPickerDialog = ColorPickerDialog.newBuilder()
.setColor(currentColor)
.create()
colorPickerDialog.setColorPickerDialogListener(
object : ColorPickerDialogListener {
override fun onDialogDismissed(dialogId: Int) {}
override fun onColorSelected(dialogId: Int, color: Int) {
b.typeColor.background.colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP)
customColor = color
}
})
colorPickerDialog.show(activity.fragmentManager, "color-picker-dialog")
}
loadDates()
}}
private fun loadDates() { launch {
val date = Date.getToday()
val today = date.value
var weekDay = date.weekDay
val deferred = async(Dispatchers.Default) {
val dates = mutableListOf<TextInputDropDown.Item>()
// item choosing the next lesson of specific subject
b.subjectDropdown.selected?.let {
if (it.tag is Subject) {
dates += TextInputDropDown.Item(
-it.id,
activity.getString(R.string.dialog_event_manual_date_next_lesson, it.tag.longName)
)
} }
}
// TODAY // TODAY
dates += TextInputDropDown.Item(
date.value.toLong(),
activity.getString(R.string.dialog_event_manual_date_today, date.formattedString),
tag = date.clone()
)
// TOMORROW
if (weekDay < 4) {
date.stepForward(0, 0, 1)
weekDay++
dates += TextInputDropDown.Item( dates += TextInputDropDown.Item(
date.value.toLong(), date.value.toLong(),
activity.getString(R.string.dialog_event_manual_date_today, date.formattedString), activity.getString(R.string.dialog_event_manual_date_tomorrow, date.formattedString),
tag = date.clone() tag = date.clone()
) )
}
// TOMORROW // REMAINING SCHOOL DAYS OF THE CURRENT WEEK
if (weekDay < 4) { while (weekDay < 4) {
date.stepForward(0, 0, 1) date.stepForward(0, 0, 1) // step one day forward
weekDay++ weekDay++
dates += TextInputDropDown.Item(
date.value.toLong(),
activity.getString(R.string.dialog_event_manual_date_tomorrow, date.formattedString),
tag = date.clone()
)
}
// REMAINING SCHOOL DAYS OF THE CURRENT WEEK
while (weekDay < 4) {
date.stepForward(0, 0, 1) // step one day forward
weekDay++
dates += TextInputDropDown.Item(
date.value.toLong(),
activity.getString(R.string.dialog_event_manual_date_this_week, Week.getFullDayName(weekDay), date.formattedString),
tag = date.clone()
)
}
// go to next week Monday
date.stepForward(0, 0, -weekDay + 7)
weekDay = 0
// ALL SCHOOL DAYS OF THE NEXT WEEK
while (weekDay < 4) {
dates += TextInputDropDown.Item(
date.value.toLong(),
activity.getString(R.string.dialog_event_manual_date_next_week, Week.getFullDayName(weekDay), date.formattedString),
tag = date.clone()
)
date.stepForward(0, 0, 1) // step one day forward
weekDay++
}
dates += TextInputDropDown.Item( dates += TextInputDropDown.Item(
-1L, date.value.toLong(),
activity.getString(R.string.dialog_event_manual_date_other) activity.getString(R.string.dialog_event_manual_date_this_week, Week.getFullDayName(weekDay), date.formattedString),
tag = date.clone()
) )
dates
} }
// go to next week Monday
val dates = deferred.await() date.stepForward(0, 0, -weekDay + 7)
b.dateDropdown.clear().append(dates) weekDay = 0
// ALL SCHOOL DAYS OF THE NEXT WEEK
editingEvent?.eventDate?.let { while (weekDay < 4) {
b.dateDropdown.select(TextInputDropDown.Item( dates += TextInputDropDown.Item(
it.value.toLong(), date.value.toLong(),
it.formattedString, activity.getString(R.string.dialog_event_manual_date_next_week, Week.getFullDayName(weekDay), date.formattedString),
tag = it tag = date.clone()
)) )
date.stepForward(0, 0, 1) // step one day forward
weekDay++
} }
dates += TextInputDropDown.Item(
defaultLesson?.displayDate?.let { -1L,
b.dateDropdown.select(TextInputDropDown.Item( activity.getString(R.string.dialog_event_manual_date_other)
it.value.toLong(), )
it.formattedString, dates
tag = it
))
}
if (b.dateDropdown.selected == null) {
b.dateDropdown.select(today.toLong())
}
b.dateDropdown.isEnabled = true
b.dateDropdown.setOnChangeListener { item ->
when {
// next lesson with specified subject
item.id < -1 -> {
val teamId = defaultLesson?.teamId ?: -1
val selectedLessonDate = defaultLesson?.date ?: Date.getToday()
when (teamId) {
-1L -> app.db.timetableDao().getNextWithSubject(profileId, selectedLessonDate, -item.id)
else -> app.db.timetableDao().getNextWithSubjectAndTeam(profileId, selectedLessonDate, -item.id, teamId)
}.observeOnce(activity, Observer {
val lessonDate = it?.displayDate ?: return@Observer
b.dateDropdown.select(TextInputDropDown.Item(
lessonDate.value.toLong(),
lessonDate.formattedString,
tag = lessonDate
))
b.teamDropdown.select(it.displayTeamId)
b.subjectDropdown.select(it.displaySubjectId)
b.teacherDropdown.select(it.displayTeacherId)
defaultLoaded = false
loadHours(it.displayStartTime)
})
return@setOnChangeListener false
}
// custom date
item.id == -1L -> {
MaterialDatePicker.Builder
.datePicker()
.setSelection((b.dateDropdown.selectedId?.let { Date.fromValue(it.toInt()) }
?: Date.getToday()).inMillis)
.build()
.apply {
addOnPositiveButtonClickListener {
val dateSelected = Date.fromMillis(it)
b.dateDropdown.select(TextInputDropDown.Item(
dateSelected.value.toLong(),
dateSelected.formattedString,
tag = dateSelected
))
loadHours()
}
show(this@EventManualV2Dialog.activity.supportFragmentManager, "MaterialDatePicker")
}
return@setOnChangeListener false
}
// a specific date
else -> {
b.dateDropdown.select(item)
loadHours()
}
}
return@setOnChangeListener true
}
loadHours()
} }
}
val dates = deferred.await()
b.dateDropdown.clear().append(dates)
editingEvent?.eventDate?.let {
b.dateDropdown.select(TextInputDropDown.Item(
it.value.toLong(),
it.formattedString,
tag = it
))
}
defaultLesson?.displayDate?.let {
b.dateDropdown.select(TextInputDropDown.Item(
it.value.toLong(),
it.formattedString,
tag = it
))
}
if (b.dateDropdown.selected == null) {
b.dateDropdown.select(today.toLong())
}
b.dateDropdown.isEnabled = true
b.dateDropdown.setOnChangeListener { item ->
when {
// next lesson with specified subject
item.id < -1 -> {
val teamId = defaultLesson?.teamId ?: -1
val selectedLessonDate = defaultLesson?.date ?: Date.getToday()
when (teamId) {
-1L -> app.db.timetableDao().getNextWithSubject(profileId, selectedLessonDate, -item.id)
else -> app.db.timetableDao().getNextWithSubjectAndTeam(profileId, selectedLessonDate, -item.id, teamId)
}.observeOnce(activity, Observer {
val lessonDate = it?.displayDate ?: return@Observer
b.dateDropdown.select(TextInputDropDown.Item(
lessonDate.value.toLong(),
lessonDate.formattedString,
tag = lessonDate
))
b.teamDropdown.select(it.displayTeamId)
b.subjectDropdown.select(it.displaySubjectId)
b.teacherDropdown.select(it.displayTeacherId)
defaultLoaded = false
loadHours(it.displayStartTime)
})
return@setOnChangeListener false
}
// custom date
item.id == -1L -> {
MaterialDatePicker.Builder
.datePicker()
.setSelection((b.dateDropdown.selectedId?.let { Date.fromValue(it.toInt()) }
?: Date.getToday()).inMillis)
.build()
.apply {
addOnPositiveButtonClickListener {
val dateSelected = Date.fromMillis(it)
b.dateDropdown.select(TextInputDropDown.Item(
dateSelected.value.toLong(),
dateSelected.formattedString,
tag = dateSelected
))
loadHours()
}
show(this@EventManualV2Dialog.activity.supportFragmentManager, "MaterialDatePicker")
}
return@setOnChangeListener false
}
// a specific date
else -> {
b.dateDropdown.select(item)
loadHours()
}
}
return@setOnChangeListener true
}
loadHours()
}}
private fun loadHours(defaultHour: Time? = null) { private fun loadHours(defaultHour: Time? = null) {
b.timeDropdown.isEnabled = false b.timeDropdown.isEnabled = false
@ -378,7 +370,8 @@ class EventManualV2Dialog(
lesson.displayStartTime?.stringHM ?: "", lesson.displayStartTime?.stringHM ?: "",
lesson.displaySubjectName?.let { lesson.displaySubjectName?.let {
when { when {
lesson.type == Lesson.TYPE_CANCELLED -> it.asStrikethroughSpannable() lesson.type == Lesson.TYPE_CANCELLED
|| lesson.type == Lesson.TYPE_SHIFTED_SOURCE -> it.asStrikethroughSpannable()
lesson.type != Lesson.TYPE_NORMAL -> it.asItalicSpannable() lesson.type != Lesson.TYPE_NORMAL -> it.asItalicSpannable()
else -> it else -> it
} }
@ -405,7 +398,8 @@ class EventManualV2Dialog(
// clear subject, teacher selection // clear subject, teacher selection
b.subjectDropdown.deselect() b.subjectDropdown.deselect()
b.teacherDropdown.deselect() b.teacherDropdown.deselect()
} else { }
else {
editingEvent?.let { editingEvent?.let {
b.timeDropdown.select(it.startTime?.value?.toLong()) b.timeDropdown.select(it.startTime?.value?.toLong())
} }