From 6a4994b9c285e54842e0f963af00298855e0373e Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Sat, 23 Nov 2019 21:57:30 +0100 Subject: [PATCH] [APIv2/Timetable] Make swipe refresh download timetable for the selected week --- .../main/java/pl/szczodrzynski/edziennik/MainActivity.kt | 9 ++++++++- .../edziennik/ui/modules/home/CounterActivity.java | 9 +++++---- .../ui/modules/timetable/TimetableFragment.java | 2 +- .../ui/modules/timetable/v2/TimetablePagerAdapter.kt | 4 ++-- .../ui/modules/timetable/v2/day/TimetableDayFragment.kt | 2 +- .../pl/szczodrzynski/edziennik/utils/models/Date.java | 4 ++++ 6 files changed, 21 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt b/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt index 85d2f275..02f45192 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt @@ -537,9 +537,16 @@ class MainActivity : AppCompatActivity() { DRAWER_ITEM_MESSAGES -> MessagesFragment.pageSelection else -> 0 } + val arguments = when (navTargetId) { + DRAWER_ITEM_TIMETABLE -> JsonObject().apply { + addProperty("weekStart", TimetableFragment.pageSelection?.weekStart?.stringY_m_d) + } + else -> null + } EdziennikTask.syncProfile( App.profileId, - listOf(navTargetId to fragmentParam) + listOf(navTargetId to fragmentParam), + arguments ).enqueue(this) } @Subscribe(threadMode = ThreadMode.MAIN) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/CounterActivity.java b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/CounterActivity.java index 14a6b71c..426774ca 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/CounterActivity.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/CounterActivity.java @@ -1,11 +1,12 @@ package pl.szczodrzynski.edziennik.ui.modules.home; -import androidx.databinding.DataBindingUtil; import android.os.AsyncTask; import android.os.Bundle; +import android.util.Log; + import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; -import android.util.Log; +import androidx.databinding.DataBindingUtil; import java.util.ArrayList; import java.util.List; @@ -14,8 +15,8 @@ import java.util.TimerTask; import pl.szczodrzynski.edziennik.App; import pl.szczodrzynski.edziennik.R; -import pl.szczodrzynski.edziennik.databinding.ActivityCounterBinding; import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonFull; +import pl.szczodrzynski.edziennik.databinding.ActivityCounterBinding; import pl.szczodrzynski.edziennik.utils.models.Date; import pl.szczodrzynski.edziennik.utils.models.Time; @@ -88,7 +89,7 @@ public class CounterActivity extends AppCompatActivity { private void findLessons(Time syncedNow) { AsyncTask.execute(() -> { Date today = Date.getToday(); - lessons = app.db.lessonDao().getAllNearestNow(App.profileId, today.clone().stepForward(0, 0, -today.getWeekDay()), today, syncedNow); + lessons = app.db.lessonDao().getAllNearestNow(App.profileId, today.getWeekStart(), today, syncedNow); if (lessons != null && lessons.size() != 0) { Date displayingDate = lessons.get(0).lessonDate; diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/timetable/TimetableFragment.java b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/timetable/TimetableFragment.java index 82c803f0..aa45da14 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/timetable/TimetableFragment.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/timetable/TimetableFragment.java @@ -184,7 +184,7 @@ public class TimetableFragment extends Fragment { if (app == null || app.profile == null || activity == null || b == null || !isAdded()) return; - List lessons = app.db.lessonDao().getAllWeekNow(App.profileId, today.clone().stepForward(0, 0, -today.getWeekDay()), today); + List lessons = app.db.lessonDao().getAllWeekNow(App.profileId, today.getWeekStart(), today); displayingDate = HomeFragment.findDateWithLessons(App.profileId, lessons); pageSelection = app.appConfig.timetableDisplayDaysBackward + Date.diffDays(displayingDate, today); // DEFAULT HERE diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/timetable/v2/TimetablePagerAdapter.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/timetable/v2/TimetablePagerAdapter.kt index 645a2b3e..9cb6f43c 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/timetable/v2/TimetablePagerAdapter.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/timetable/v2/TimetablePagerAdapter.kt @@ -19,7 +19,7 @@ class TimetablePagerAdapter( } private val today by lazy { Date.getToday() } - private val weekStart by lazy { today.clone().stepForward(0, 0, -today.weekDay) } + private val weekStart by lazy { today.weekStart } private val weekEnd by lazy { weekStart.clone().stepForward(0, 0, 6) } override fun getItem(position: Int): Fragment { @@ -49,4 +49,4 @@ class TimetablePagerAdapter( } return pageTitle } -} \ No newline at end of file +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/timetable/v2/day/TimetableDayFragment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/timetable/v2/day/TimetableDayFragment.kt index 3ca45828..cbf09bde 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/timetable/v2/day/TimetableDayFragment.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/timetable/v2/day/TimetableDayFragment.kt @@ -120,7 +120,7 @@ class TimetableDayFragment : Fragment(), CoroutineScope { parent?.removeAllViews() parent?.addView(view) val b = TimetableNoTimetableBinding.bind(view) - val weekStart = date.clone().stepForward(0, 0, -date.weekDay).stringY_m_d + val weekStart = date.weekStart.stringY_m_d b.noTimetableSync.onClick { it.isEnabled = false EdziennikTask.syncProfile( diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Date.java b/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Date.java index 89d7a613..fd62d3e4 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Date.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Date.java @@ -41,6 +41,10 @@ public class Date implements Comparable { return new Date(this.year, this.month, this.day); } + public Date getWeekStart() { + return clone().stepForward(0, 0, -getWeekDay()); + } + public static Date fromYmd(String dateTime) { return new Date(Integer.parseInt(dateTime.substring(0, 4)), Integer.parseInt(dateTime.substring(4, 6)), Integer.parseInt(dateTime.substring(6, 8))); }