forked from github/szkolny
[Timetable/Generate] Add automatic timetable sync when no timetable for the selected week.
This commit is contained in:
parent
0b186a754a
commit
098beb14fe
@ -30,7 +30,6 @@
|
||||
Staramy się usuwać takie przypadki, jednak na chwilę obecną mogą występować błędy w:
|
||||
<ul>
|
||||
<li>Wysyłanie wiadomości może nie działać w pełni prawidłowo - proszę o zgłaszanie wszystkich błędów na naszym serwerze Discord</li>
|
||||
<li>Cisza nocna w powiadomieniach jeszcze nie działa.</li>
|
||||
</ul>
|
||||
<br>
|
||||
<br>
|
||||
|
@ -21,7 +21,14 @@ import androidx.core.content.FileProvider
|
||||
import com.google.android.material.datepicker.MaterialDatePicker
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
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.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.db.entity.Lesson
|
||||
import pl.szczodrzynski.edziennik.data.db.full.LessonFull
|
||||
import pl.szczodrzynski.edziennik.databinding.DialogGenerateBlockTimetableBinding
|
||||
@ -64,11 +71,16 @@ class GenerateBlockTimetableDialog(
|
||||
private var showTeachersNames: Boolean = true
|
||||
private var noColors: Boolean = false
|
||||
|
||||
private var enqueuedWeekDialog: AlertDialog? = null
|
||||
private var enqueuedWeekStart = Date.getToday()
|
||||
private var enqueuedWeekEnd = Date.getToday()
|
||||
|
||||
init { run {
|
||||
if (activity.isFinishing)
|
||||
return@run
|
||||
job = Job()
|
||||
onShowListener?.invoke(TAG)
|
||||
EventBus.getDefault().register(this)
|
||||
|
||||
val weekCurrentStart = Week.getWeekStart()
|
||||
val weekCurrentEnd = Week.getWeekEnd()
|
||||
@ -88,16 +100,20 @@ class GenerateBlockTimetableDialog(
|
||||
.setTitle(R.string.timetable_generate_range)
|
||||
.setView(b.root)
|
||||
.setNeutralButton(R.string.cancel) { dialog, _ -> dialog.dismiss() }
|
||||
.setPositiveButton(R.string.save) { dialog, _ ->
|
||||
dialog.dismiss()
|
||||
.setPositiveButton(R.string.save, null)
|
||||
.setOnDismissListener {
|
||||
onDismissListener?.invoke(TAG)
|
||||
EventBus.getDefault().unregister(this@GenerateBlockTimetableDialog)
|
||||
}
|
||||
.show()
|
||||
|
||||
dialog.getButton(AlertDialog.BUTTON_POSITIVE)?.onClick {
|
||||
when (b.weekSelectionRadioGroup.checkedRadioButtonId) {
|
||||
R.id.withChangesCurrentWeekRadio -> generateBlockTimetable(weekCurrentStart, weekCurrentEnd)
|
||||
R.id.withChangesNextWeekRadio -> generateBlockTimetable(weekNextStart, weekNextEnd)
|
||||
R.id.forSelectedWeekRadio -> selectDate()
|
||||
}
|
||||
}
|
||||
.setOnDismissListener { onDismissListener?.invoke(TAG) }
|
||||
.show()
|
||||
}}
|
||||
|
||||
private fun selectDate() {
|
||||
@ -115,12 +131,26 @@ class GenerateBlockTimetableDialog(
|
||||
.show(activity.supportFragmentManager, "MaterialDatePicker")
|
||||
}
|
||||
|
||||
private fun generateBlockTimetable(weekStart: Date, weekEnd: Date) { launch {
|
||||
val progressDialog = MaterialAlertDialogBuilder(activity)
|
||||
.setTitle(R.string.timetable_generate_progress_title)
|
||||
.setMessage(R.string.timetable_generate_progress_text)
|
||||
.show()
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
fun onApiTaskFinishedEvent(event: ApiTaskFinishedEvent) {
|
||||
if (event.profileId == App.profileId) {
|
||||
enqueuedWeekDialog?.dismiss()
|
||||
generateBlockTimetable(enqueuedWeekStart, enqueuedWeekEnd)
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
fun onApiTaskAllFinishedEvent(event: ApiTaskAllFinishedEvent) {
|
||||
enqueuedWeekDialog?.dismiss()
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
fun onApiTaskErrorEvent(event: ApiTaskErrorEvent) {
|
||||
dialog.dismiss()
|
||||
enqueuedWeekDialog?.dismiss()
|
||||
}
|
||||
|
||||
private fun generateBlockTimetable(weekStart: Date, weekEnd: Date) { launch {
|
||||
val weekDays = mutableListOf<MutableList<Lesson>>()
|
||||
for (i in weekStart.weekDay..weekEnd.weekDay) {
|
||||
weekDays.add(mutableListOf())
|
||||
@ -157,12 +187,46 @@ class GenerateBlockTimetableDialog(
|
||||
return@mapNotNull lesson
|
||||
}
|
||||
|
||||
if (lessons.isEmpty()) {
|
||||
if (enqueuedWeekDialog != null) {
|
||||
return@launch
|
||||
}
|
||||
enqueuedWeekDialog = MaterialAlertDialogBuilder(activity)
|
||||
.setTitle(R.string.please_wait)
|
||||
.setMessage(R.string.timetable_generate_syncing_text)
|
||||
.setCancelable(false)
|
||||
.show()
|
||||
|
||||
enqueuedWeekStart = weekStart
|
||||
enqueuedWeekEnd = weekEnd
|
||||
|
||||
EdziennikTask.syncProfile(
|
||||
profileId = App.profileId,
|
||||
viewIds = listOf(
|
||||
MainActivity.DRAWER_ITEM_TIMETABLE to 0
|
||||
),
|
||||
arguments = JsonObject(
|
||||
"weekStart" to weekStart.stringY_m_d
|
||||
)
|
||||
).enqueue(activity)
|
||||
return@launch
|
||||
}
|
||||
|
||||
val progressDialog = MaterialAlertDialogBuilder(activity)
|
||||
.setTitle(R.string.timetable_generate_progress_title)
|
||||
.setMessage(R.string.timetable_generate_progress_text)
|
||||
.show()
|
||||
|
||||
if (minTime == null) {
|
||||
progressDialog.dismiss()
|
||||
// TODO: Toast
|
||||
return@launch
|
||||
}
|
||||
|
||||
dialog.dismiss()
|
||||
|
||||
val uri = withContext(Dispatchers.Default) {
|
||||
|
||||
val diff = Time.diff(maxTime, minTime)
|
||||
|
||||
val imageWidth = WIDTH_CONSTANT + maxWeekDay * (WIDTH_WEEKDAY + WIDTH_SPACING) - WIDTH_SPACING
|
||||
@ -220,11 +284,13 @@ class GenerateBlockTimetableDialog(
|
||||
if (!showTeachersNames) teacherName.visibility = View.GONE
|
||||
|
||||
when (lesson.type) {
|
||||
Lesson.TYPE_NORMAL -> {}
|
||||
Lesson.TYPE_NORMAL -> {
|
||||
}
|
||||
Lesson.TYPE_CANCELLED, Lesson.TYPE_SHIFTED_SOURCE -> {
|
||||
card.setCardBackgroundColor(Color.BLACK)
|
||||
subjectName.setTextColor(Color.WHITE)
|
||||
subjectName.text = lesson.displaySubjectName?.asStrikethroughSpannable() ?: ""
|
||||
subjectName.text = lesson.displaySubjectName?.asStrikethroughSpannable()
|
||||
?: ""
|
||||
}
|
||||
else -> {
|
||||
card.setCardBackgroundColor(0xff234158.toInt())
|
||||
@ -318,7 +384,7 @@ class GenerateBlockTimetableDialog(
|
||||
fos.close()
|
||||
} catch (e: Exception) {
|
||||
Log.e("SAVE_IMAGE", e.message, e)
|
||||
return@launch
|
||||
return@withContext null
|
||||
}
|
||||
|
||||
val uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
@ -326,6 +392,8 @@ class GenerateBlockTimetableDialog(
|
||||
} else {
|
||||
Uri.parse("file://" + outputFile.absolutePath)
|
||||
}
|
||||
uri
|
||||
}
|
||||
|
||||
progressDialog.dismiss()
|
||||
MaterialAlertDialogBuilder(activity)
|
||||
|
@ -48,6 +48,7 @@ class ErrorSnackbar(val activity: AppCompatActivity) : CoroutineScope {
|
||||
fun addError(apiError: ApiError): ErrorSnackbar {
|
||||
errors.add(apiError)
|
||||
snackbar?.setText(apiError.getStringReason(activity))
|
||||
snackbar?.duration = 15000
|
||||
return this
|
||||
}
|
||||
|
||||
|
@ -55,7 +55,8 @@ class TimeDropdown : TextInputDropDown {
|
||||
isEnabled = false
|
||||
}
|
||||
|
||||
suspend fun loadItems() {
|
||||
suspend fun loadItems(): Boolean {
|
||||
var noLessons = false
|
||||
val hours = withContext(Dispatchers.Default) {
|
||||
val hours = mutableListOf<Item>()
|
||||
|
||||
@ -90,6 +91,7 @@ class TimeDropdown : TextInputDropDown {
|
||||
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),
|
||||
@ -158,6 +160,8 @@ class TimeDropdown : TextInputDropDown {
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
|
||||
return !noLessons
|
||||
}
|
||||
|
||||
fun pickerDialog() {
|
||||
|
@ -9,7 +9,8 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:paddingHorizontal="20dp">
|
||||
android:paddingHorizontal="20dp"
|
||||
android:paddingTop="16dp">
|
||||
|
||||
<RadioGroup
|
||||
android:id="@+id/weekSelectionRadioGroup"
|
||||
@ -36,10 +37,17 @@
|
||||
android:text="@string/timetable_generate_selected_week" />
|
||||
</RadioGroup>
|
||||
|
||||
<View
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="1dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:background="@drawable/divider"/>
|
||||
|
||||
<CheckBox
|
||||
android:id="@+id/showProfileNameCheckbox"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:minHeight="0dp"
|
||||
android:text="@string/timetable_generate_show_profile_name" />
|
||||
|
||||
<CheckBox
|
||||
@ -47,12 +55,14 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:checked="true"
|
||||
android:minHeight="0dp"
|
||||
android:text="@string/timetable_generate_show_teachers_names" />
|
||||
|
||||
<CheckBox
|
||||
android:id="@+id/noColorsCheckbox"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:minHeight="0dp"
|
||||
android:text="@string/timetable_generate_no_colors" />
|
||||
</LinearLayout>
|
||||
</layout>
|
||||
|
@ -1255,4 +1255,5 @@
|
||||
<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>
|
||||
</resources>
|
||||
|
Loading…
x
Reference in New Issue
Block a user