forked from github/szkolny
[Events/Manual] Implement syncing timetable when no lessons for the selected date.
This commit is contained in:
parent
e05b483f5c
commit
ae89b33fb7
@ -16,8 +16,15 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
|||||||
import com.jaredrummler.android.colorpicker.ColorPickerDialog
|
import com.jaredrummler.android.colorpicker.ColorPickerDialog
|
||||||
import com.jaredrummler.android.colorpicker.ColorPickerDialogListener
|
import com.jaredrummler.android.colorpicker.ColorPickerDialogListener
|
||||||
import kotlinx.coroutines.*
|
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.*
|
||||||
import pl.szczodrzynski.edziennik.MainActivity.Companion.DRAWER_ITEM_AGENDA
|
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.api.szkolny.SzkolnyApi
|
||||||
import pl.szczodrzynski.edziennik.data.db.entity.Event
|
import pl.szczodrzynski.edziennik.data.db.entity.Event
|
||||||
import pl.szczodrzynski.edziennik.data.db.entity.EventType
|
import pl.szczodrzynski.edziennik.data.db.entity.EventType
|
||||||
@ -48,7 +55,7 @@ class EventManualDialog(
|
|||||||
private const val TAG = "EventManualDialog"
|
private const val TAG = "EventManualDialog"
|
||||||
}
|
}
|
||||||
|
|
||||||
private lateinit var job: Job
|
private val job: Job = Job()
|
||||||
override val coroutineContext: CoroutineContext
|
override val coroutineContext: CoroutineContext
|
||||||
get() = job + Dispatchers.Main
|
get() = job + Dispatchers.Main
|
||||||
|
|
||||||
@ -66,11 +73,14 @@ class EventManualDialog(
|
|||||||
SzkolnyApi(app)
|
SzkolnyApi(app)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private var enqueuedWeekDialog: AlertDialog? = null
|
||||||
|
private var enqueuedWeekStart = Date.getToday()
|
||||||
|
|
||||||
init { run {
|
init { run {
|
||||||
if (activity.isFinishing)
|
if (activity.isFinishing)
|
||||||
return@run
|
return@run
|
||||||
job = Job()
|
|
||||||
onShowListener?.invoke(TAG)
|
onShowListener?.invoke(TAG)
|
||||||
|
EventBus.getDefault().register(this)
|
||||||
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)
|
||||||
@ -84,6 +94,7 @@ class EventManualDialog(
|
|||||||
}
|
}
|
||||||
.setOnDismissListener {
|
.setOnDismissListener {
|
||||||
onDismissListener?.invoke(TAG)
|
onDismissListener?.invoke(TAG)
|
||||||
|
EventBus.getDefault().unregister(this@EventManualDialog)
|
||||||
}
|
}
|
||||||
.setCancelable(false)
|
.setCancelable(false)
|
||||||
.create()
|
.create()
|
||||||
@ -140,6 +151,57 @@ class EventManualDialog(
|
|||||||
b.shareDetails.setText(text, editingEvent?.sharedByName ?: "")
|
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 {
|
private fun loadLists() { launch {
|
||||||
with (b.dateDropdown) {
|
with (b.dateDropdown) {
|
||||||
db = app.db
|
db = app.db
|
||||||
@ -159,7 +221,8 @@ class EventManualDialog(
|
|||||||
b.timeDropdown.deselect()
|
b.timeDropdown.deselect()
|
||||||
b.timeDropdown.lessonsDate = date
|
b.timeDropdown.lessonsDate = date
|
||||||
this@EventManualDialog.launch {
|
this@EventManualDialog.launch {
|
||||||
b.timeDropdown.loadItems()
|
if (!b.timeDropdown.loadItems())
|
||||||
|
syncTimetable(date)
|
||||||
lesson?.displayStartTime?.let { b.timeDropdown.selectTime(it) }
|
lesson?.displayStartTime?.let { b.timeDropdown.selectTime(it) }
|
||||||
lesson?.displaySubjectId?.let { b.subjectDropdown.selectSubject(it) } ?: b.subjectDropdown.deselect()
|
lesson?.displaySubjectId?.let { b.subjectDropdown.selectSubject(it) } ?: b.subjectDropdown.deselect()
|
||||||
lesson?.displayTeacherId?.let { b.teacherDropdown.selectTeacher(it) } ?: b.teacherDropdown.deselect()
|
lesson?.displayTeacherId?.let { b.teacherDropdown.selectTeacher(it) } ?: b.teacherDropdown.deselect()
|
||||||
@ -175,7 +238,8 @@ class EventManualDialog(
|
|||||||
showCustomTime = true
|
showCustomTime = true
|
||||||
lessonsDate = b.dateDropdown.getSelected() as? Date ?: Date.getToday()
|
lessonsDate = b.dateDropdown.getSelected() as? Date ?: Date.getToday()
|
||||||
displayMode = DISPLAY_LESSONS
|
displayMode = DISPLAY_LESSONS
|
||||||
loadItems()
|
if (!loadItems())
|
||||||
|
syncTimetable(lessonsDate ?: Date.getToday())
|
||||||
selectDefault(editingEvent?.startTime)
|
selectDefault(editingEvent?.startTime)
|
||||||
selectDefault(defaultLesson?.displayStartTime ?: defaultTime)
|
selectDefault(defaultLesson?.displayStartTime ?: defaultTime)
|
||||||
onLessonSelected = { lesson ->
|
onLessonSelected = { lesson ->
|
||||||
|
@ -60,7 +60,7 @@ class GenerateBlockTimetableDialog(
|
|||||||
|
|
||||||
private val app by lazy { activity.application as App }
|
private val app by lazy { activity.application as App }
|
||||||
|
|
||||||
private lateinit var job: Job
|
private val job: Job = Job()
|
||||||
override val coroutineContext: CoroutineContext
|
override val coroutineContext: CoroutineContext
|
||||||
get() = job + Dispatchers.Main
|
get() = job + Dispatchers.Main
|
||||||
|
|
||||||
@ -78,7 +78,6 @@ class GenerateBlockTimetableDialog(
|
|||||||
init { run {
|
init { run {
|
||||||
if (activity.isFinishing)
|
if (activity.isFinishing)
|
||||||
return@run
|
return@run
|
||||||
job = Job()
|
|
||||||
onShowListener?.invoke(TAG)
|
onShowListener?.invoke(TAG)
|
||||||
EventBus.getDefault().register(this)
|
EventBus.getDefault().register(this)
|
||||||
|
|
||||||
@ -193,7 +192,7 @@ class GenerateBlockTimetableDialog(
|
|||||||
}
|
}
|
||||||
enqueuedWeekDialog = MaterialAlertDialogBuilder(activity)
|
enqueuedWeekDialog = MaterialAlertDialogBuilder(activity)
|
||||||
.setTitle(R.string.please_wait)
|
.setTitle(R.string.please_wait)
|
||||||
.setMessage(R.string.timetable_generate_syncing_text)
|
.setMessage(R.string.timetable_syncing_text)
|
||||||
.setCancelable(false)
|
.setCancelable(false)
|
||||||
.show()
|
.show()
|
||||||
|
|
||||||
|
@ -189,6 +189,7 @@ class DateDropdown : TextInputDropDown {
|
|||||||
addOnPositiveButtonClickListener {
|
addOnPositiveButtonClickListener {
|
||||||
val dateSelected = Date.fromMillis(it)
|
val dateSelected = Date.fromMillis(it)
|
||||||
selectDate(dateSelected)
|
selectDate(dateSelected)
|
||||||
|
onDateSelected?.invoke(dateSelected, null)
|
||||||
}
|
}
|
||||||
this@DateDropdown.activity ?: return@apply
|
this@DateDropdown.activity ?: return@apply
|
||||||
show(this@DateDropdown.activity!!.supportFragmentManager, "MaterialDatePicker")
|
show(this@DateDropdown.activity!!.supportFragmentManager, "MaterialDatePicker")
|
||||||
|
@ -56,7 +56,7 @@ class TimeDropdown : TextInputDropDown {
|
|||||||
}
|
}
|
||||||
|
|
||||||
suspend fun loadItems(): Boolean {
|
suspend fun loadItems(): Boolean {
|
||||||
var noLessons = false
|
var noTimetable = false
|
||||||
val hours = withContext(Dispatchers.Default) {
|
val hours = withContext(Dispatchers.Default) {
|
||||||
val hours = mutableListOf<Item>()
|
val hours = mutableListOf<Item>()
|
||||||
|
|
||||||
@ -88,10 +88,19 @@ class TimeDropdown : TextInputDropDown {
|
|||||||
else if (displayMode == DISPLAY_LESSONS && lessonsDate != null) {
|
else if (displayMode == DISPLAY_LESSONS && lessonsDate != null) {
|
||||||
val lessons = db.timetableDao().getForDateNow(profileId, lessonsDate!!)
|
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 ->
|
hours += lessons.map { lesson ->
|
||||||
if (lesson.type == Lesson.TYPE_NO_LESSONS) {
|
if (lesson.type == Lesson.TYPE_NO_LESSONS) {
|
||||||
// indicate there are no lessons this day
|
// indicate there are no lessons this day
|
||||||
noLessons = true
|
|
||||||
return@map Item(
|
return@map Item(
|
||||||
-2L,
|
-2L,
|
||||||
context.getString(R.string.dialog_event_manual_no_lessons),
|
context.getString(R.string.dialog_event_manual_no_lessons),
|
||||||
@ -161,7 +170,7 @@ class TimeDropdown : TextInputDropDown {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return !noLessons
|
return !noTimetable
|
||||||
}
|
}
|
||||||
|
|
||||||
fun pickerDialog() {
|
fun pickerDialog() {
|
||||||
|
@ -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_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_minus_value">Własna wartość minusa</string>
|
||||||
<string name="grades_config_plus_value">Własna wartość plusa</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>
|
</resources>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user