[Events/Manual] Implement syncing timetable when no lessons for the selected date.

This commit is contained in:
Kuba Szczodrzyński 2020-03-10 21:49:02 +01:00
parent e05b483f5c
commit ae89b33fb7
5 changed files with 85 additions and 11 deletions

View File

@ -16,8 +16,15 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.jaredrummler.android.colorpicker.ColorPickerDialog
import com.jaredrummler.android.colorpicker.ColorPickerDialogListener
import kotlinx.coroutines.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.MainActivity.Companion.DRAWER_ITEM_AGENDA
import pl.szczodrzynski.edziennik.data.api.edziennik.EdziennikTask
import pl.szczodrzynski.edziennik.data.api.events.ApiTaskAllFinishedEvent
import pl.szczodrzynski.edziennik.data.api.events.ApiTaskErrorEvent
import pl.szczodrzynski.edziennik.data.api.events.ApiTaskFinishedEvent
import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApi
import pl.szczodrzynski.edziennik.data.db.entity.Event
import pl.szczodrzynski.edziennik.data.db.entity.EventType
@ -48,7 +55,7 @@ class EventManualDialog(
private const val TAG = "EventManualDialog"
}
private lateinit var job: Job
private val job: Job = Job()
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Main
@ -66,11 +73,14 @@ class EventManualDialog(
SzkolnyApi(app)
}
private var enqueuedWeekDialog: AlertDialog? = null
private var enqueuedWeekStart = Date.getToday()
init { run {
if (activity.isFinishing)
return@run
job = Job()
onShowListener?.invoke(TAG)
EventBus.getDefault().register(this)
b = DialogEventManualV2Binding.inflate(activity.layoutInflater)
dialog = MaterialAlertDialogBuilder(activity)
.setTitle(R.string.dialog_event_manual_title)
@ -84,6 +94,7 @@ class EventManualDialog(
}
.setOnDismissListener {
onDismissListener?.invoke(TAG)
EventBus.getDefault().unregister(this@EventManualDialog)
}
.setCancelable(false)
.create()
@ -140,6 +151,57 @@ class EventManualDialog(
b.shareDetails.setText(text, editingEvent?.sharedByName ?: "")
}
private fun syncTimetable(date: Date) {
if (enqueuedWeekDialog != null) {
return
}
if (app.profile.getStudentData("timetableNotPublic", false)) {
return
}
val weekStart = date.weekStart
enqueuedWeekDialog = MaterialAlertDialogBuilder(activity)
.setTitle(R.string.please_wait)
.setMessage(R.string.timetable_syncing_text)
.setCancelable(false)
.show()
enqueuedWeekStart = weekStart
EdziennikTask.syncProfile(
profileId = App.profileId,
viewIds = listOf(
MainActivity.DRAWER_ITEM_TIMETABLE to 0
),
arguments = JsonObject(
"weekStart" to weekStart.stringY_m_d
)
).enqueue(activity)
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun onApiTaskFinishedEvent(event: ApiTaskFinishedEvent) {
if (event.profileId == App.profileId) {
enqueuedWeekDialog?.dismiss()
enqueuedWeekDialog = null
launch {
b.timeDropdown.loadItems()
}
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun onApiTaskAllFinishedEvent(event: ApiTaskAllFinishedEvent) {
enqueuedWeekDialog?.dismiss()
enqueuedWeekDialog = null
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun onApiTaskErrorEvent(event: ApiTaskErrorEvent) {
dialog.dismiss()
enqueuedWeekDialog?.dismiss()
enqueuedWeekDialog = null
}
private fun loadLists() { launch {
with (b.dateDropdown) {
db = app.db
@ -159,7 +221,8 @@ class EventManualDialog(
b.timeDropdown.deselect()
b.timeDropdown.lessonsDate = date
this@EventManualDialog.launch {
b.timeDropdown.loadItems()
if (!b.timeDropdown.loadItems())
syncTimetable(date)
lesson?.displayStartTime?.let { b.timeDropdown.selectTime(it) }
lesson?.displaySubjectId?.let { b.subjectDropdown.selectSubject(it) } ?: b.subjectDropdown.deselect()
lesson?.displayTeacherId?.let { b.teacherDropdown.selectTeacher(it) } ?: b.teacherDropdown.deselect()
@ -175,7 +238,8 @@ class EventManualDialog(
showCustomTime = true
lessonsDate = b.dateDropdown.getSelected() as? Date ?: Date.getToday()
displayMode = DISPLAY_LESSONS
loadItems()
if (!loadItems())
syncTimetable(lessonsDate ?: Date.getToday())
selectDefault(editingEvent?.startTime)
selectDefault(defaultLesson?.displayStartTime ?: defaultTime)
onLessonSelected = { lesson ->

View File

@ -60,7 +60,7 @@ class GenerateBlockTimetableDialog(
private val app by lazy { activity.application as App }
private lateinit var job: Job
private val job: Job = Job()
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Main
@ -78,7 +78,6 @@ class GenerateBlockTimetableDialog(
init { run {
if (activity.isFinishing)
return@run
job = Job()
onShowListener?.invoke(TAG)
EventBus.getDefault().register(this)
@ -193,7 +192,7 @@ class GenerateBlockTimetableDialog(
}
enqueuedWeekDialog = MaterialAlertDialogBuilder(activity)
.setTitle(R.string.please_wait)
.setMessage(R.string.timetable_generate_syncing_text)
.setMessage(R.string.timetable_syncing_text)
.setCancelable(false)
.show()

View File

@ -189,6 +189,7 @@ class DateDropdown : TextInputDropDown {
addOnPositiveButtonClickListener {
val dateSelected = Date.fromMillis(it)
selectDate(dateSelected)
onDateSelected?.invoke(dateSelected, null)
}
this@DateDropdown.activity ?: return@apply
show(this@DateDropdown.activity!!.supportFragmentManager, "MaterialDatePicker")

View File

@ -56,7 +56,7 @@ class TimeDropdown : TextInputDropDown {
}
suspend fun loadItems(): Boolean {
var noLessons = false
var noTimetable = false
val hours = withContext(Dispatchers.Default) {
val hours = mutableListOf<Item>()
@ -88,10 +88,19 @@ class TimeDropdown : TextInputDropDown {
else if (displayMode == DISPLAY_LESSONS && lessonsDate != null) {
val lessons = db.timetableDao().getForDateNow(profileId, lessonsDate!!)
if (lessons.isEmpty()) {
hours += Item(
-2L,
context.getString(R.string.dialog_event_manual_no_timetable),
tag = -2L
)
noTimetable = true
return@withContext hours
}
hours += lessons.map { lesson ->
if (lesson.type == Lesson.TYPE_NO_LESSONS) {
// indicate there are no lessons this day
noLessons = true
return@map Item(
-2L,
context.getString(R.string.dialog_event_manual_no_lessons),
@ -161,7 +170,7 @@ class TimeDropdown : TextInputDropDown {
}
}
return !noLessons
return !noTimetable
}
fun pickerDialog() {

View File

@ -1255,5 +1255,6 @@
<string name="grades_config_average_without_weight_message">Pozwala na liczenie średniej arytmetycznej z przedmiotów, w których wszystkie wystawione oceny mają wagę 0 (nie są liczone do średniej).\n\nJeśli taki przedmiot celowo nie powinien być liczony, odznacz okienko przy tym ustawieniu.</string>
<string name="grades_config_minus_value">Własna wartość minusa</string>
<string name="grades_config_plus_value">Własna wartość plusa</string>
<string name="timetable_generate_syncing_text">Pobieranie planu lekcji na wybrany tydzień...</string>
<string name="timetable_syncing_text">Pobieranie planu lekcji na wybrany tydzień...</string>
<string name="dialog_event_manual_no_timetable">Nie pobrano planu lekcji...</string>
</resources>