From 9329bdc0b518658b0deae48ddf68859b0b9786ca Mon Sep 17 00:00:00 2001 From: Christophe Beyls Date: Sat, 6 Jan 2024 00:21:12 +0100 Subject: [PATCH] feat: change "upcoming only" bookmarks filter to "hide past sessions". closes #78 --- .../be/digitalia/fosdem/db/BookmarksDao.kt | 6 ++--- .../fosdem/fragments/BookmarksListFragment.kt | 22 +++++++++---------- .../fosdem/viewmodels/BookmarksViewModel.kt | 22 ++++++++----------- app/src/main/res/menu/bookmarks.xml | 4 ++-- app/src/main/res/values/strings.xml | 2 +- 5 files changed, 26 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/be/digitalia/fosdem/db/BookmarksDao.kt b/app/src/main/java/be/digitalia/fosdem/db/BookmarksDao.kt index 4d61070f..804d65d8 100644 --- a/app/src/main/java/be/digitalia/fosdem/db/BookmarksDao.kt +++ b/app/src/main/java/be/digitalia/fosdem/db/BookmarksDao.kt @@ -22,7 +22,7 @@ abstract class BookmarksDao(appDatabase: AppDatabase) { /** * Returns the bookmarks. * - * @param minStartTime Only return the events starting after this time. + * @param minEndTime Only return the events ending after this time. */ @Query("""SELECT e.id, e.start_time, e.end_time, e.room_name, e.slug, et.title, et.subtitle, e.abstract, e.description, GROUP_CONCAT(p.name, ', ') AS persons, e.day_index, d.date AS day_date, d.start_time AS day_start_time, d.end_time AS day_end_time, @@ -34,11 +34,11 @@ abstract class BookmarksDao(appDatabase: AppDatabase) { JOIN tracks t ON e.track_id = t.id LEFT JOIN events_persons ep ON e.id = ep.event_id LEFT JOIN persons p ON ep.person_id = p.`rowid` - WHERE e.start_time > :minStartTime + WHERE e.end_time > :minEndTime GROUP BY e.id ORDER BY e.start_time ASC""") @TypeConverters(NonNullInstantTypeConverters::class) - abstract suspend fun getBookmarks(minStartTime: Instant = Instant.EPOCH): List + abstract suspend fun getBookmarks(minEndTime: Instant = Instant.EPOCH): List @Query("""SELECT b.event_id, e.start_time FROM bookmarks b diff --git a/app/src/main/java/be/digitalia/fosdem/fragments/BookmarksListFragment.kt b/app/src/main/java/be/digitalia/fosdem/fragments/BookmarksListFragment.kt index e3d95094..d82bc5d3 100644 --- a/app/src/main/java/be/digitalia/fosdem/fragments/BookmarksListFragment.kt +++ b/app/src/main/java/be/digitalia/fosdem/fragments/BookmarksListFragment.kt @@ -99,29 +99,29 @@ class BookmarksListFragment : Fragment(R.layout.recyclerview) { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - viewModel.upcomingOnly = preferences.getBoolean(UPCOMING_ONLY_PREF_KEY, false) + viewModel.hidePastEvents = preferences.getBoolean(HIDE_PAST_EVENTS_PREF_KEY, false) requireActivity().addMenuProvider(BookmarksMenuProvider(), this) } private inner class BookmarksMenuProvider : MenuProvider { private var filterMenuItem: MenuItem? = null - private var upcomingOnlyMenuItem: MenuItem? = null + private var hidePastEventsMenuItem: MenuItem? = null override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) { menuInflater.inflate(R.menu.bookmarks, menu) filterMenuItem = menu.findItem(R.id.filter) - upcomingOnlyMenuItem = menu.findItem(R.id.upcoming_only) + hidePastEventsMenuItem = menu.findItem(R.id.hide_past_events) updateMenuItems() } override fun onMenuItemSelected(menuItem: MenuItem) = when (menuItem.itemId) { - R.id.upcoming_only -> { - val upcomingOnly = !viewModel.upcomingOnly - viewModel.upcomingOnly = upcomingOnly + R.id.hide_past_events -> { + val hidePastEvents = !viewModel.hidePastEvents + viewModel.hidePastEvents = hidePastEvents updateMenuItems() preferences.edit { - putBoolean(UPCOMING_ONLY_PREF_KEY, upcomingOnly) + putBoolean(HIDE_PAST_EVENTS_PREF_KEY, hidePastEvents) } true } @@ -138,9 +138,9 @@ class BookmarksListFragment : Fragment(R.layout.recyclerview) { } private fun updateMenuItems() { - val upcomingOnly = viewModel.upcomingOnly - filterMenuItem?.setIcon(if (upcomingOnly) R.drawable.ic_filter_list_selected_white_24dp else R.drawable.ic_filter_list_white_24dp) - upcomingOnlyMenuItem?.isChecked = upcomingOnly + val hidePastEvents = viewModel.hidePastEvents + filterMenuItem?.setIcon(if (hidePastEvents) R.drawable.ic_filter_list_selected_white_24dp else R.drawable.ic_filter_list_white_24dp) + hidePastEventsMenuItem?.isChecked = hidePastEvents } } @@ -208,6 +208,6 @@ class BookmarksListFragment : Fragment(R.layout.recyclerview) { } companion object { - private const val UPCOMING_ONLY_PREF_KEY = "bookmarks_upcoming_only" + private const val HIDE_PAST_EVENTS_PREF_KEY = "bookmarks_upcoming_only" } } \ No newline at end of file diff --git a/app/src/main/java/be/digitalia/fosdem/viewmodels/BookmarksViewModel.kt b/app/src/main/java/be/digitalia/fosdem/viewmodels/BookmarksViewModel.kt index 90d90687..65761480 100644 --- a/app/src/main/java/be/digitalia/fosdem/viewmodels/BookmarksViewModel.kt +++ b/app/src/main/java/be/digitalia/fosdem/viewmodels/BookmarksViewModel.kt @@ -27,7 +27,6 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import okio.buffer import okio.source -import java.time.Duration import java.time.Instant import javax.inject.Inject import kotlin.time.Duration.Companion.minutes @@ -40,16 +39,16 @@ class BookmarksViewModel @Inject constructor( private val application: Application ) : ViewModel() { - private val upcomingOnlyStateFlow = MutableStateFlow(null) + private val hidePastEventsStateFlow = MutableStateFlow(null) @OptIn(ExperimentalCoroutinesApi::class) val bookmarks: StateFlow?> = stateFlow(viewModelScope, null) { - upcomingOnlyStateFlow.filterNotNull().flatMapLatest { upcomingOnly -> - if (upcomingOnly) { + hidePastEventsStateFlow.filterNotNull().flatMapLatest { hidePastEvents -> + if (hidePastEvents) { // Refresh upcoming bookmarks every 2 minutes synchronizedTickerFlow(REFRESH_PERIOD) .flatMapLatest { - getObservableBookmarks(Instant.now() - TIME_OFFSET) + getObservableBookmarks(Instant.now()) } } else { getObservableBookmarks(Instant.EPOCH) @@ -57,15 +56,15 @@ class BookmarksViewModel @Inject constructor( } } - private fun SharedFlowContext.getObservableBookmarks(minStartTime: Instant): Flow> = + private fun SharedFlowContext.getObservableBookmarks(minEndTime: Instant): Flow> = versionedResourceFlow(bookmarksDao.version) { - bookmarksDao.getBookmarks(minStartTime) + bookmarksDao.getBookmarks(minEndTime) } - var upcomingOnly: Boolean - get() = upcomingOnlyStateFlow.value == true + var hidePastEvents: Boolean + get() = hidePastEventsStateFlow.value == true set(value) { - upcomingOnlyStateFlow.value = value + hidePastEventsStateFlow.value = value } fun removeBookmarks(eventIds: LongArray) { @@ -85,8 +84,5 @@ class BookmarksViewModel @Inject constructor( companion object { private val REFRESH_PERIOD = 2.minutes - - // In upcomingOnly mode, events that just started are still shown for 5 minutes - private val TIME_OFFSET = Duration.ofMinutes(5L) } } \ No newline at end of file diff --git a/app/src/main/res/menu/bookmarks.xml b/app/src/main/res/menu/bookmarks.xml index e04f0977..6b37cb74 100644 --- a/app/src/main/res/menu/bookmarks.xml +++ b/app/src/main/res/menu/bookmarks.xml @@ -9,9 +9,9 @@ app:showAsAction="ifRoom"> + android:title="@string/hide_past_events" /> Filter - Upcoming only + Hide past sessions Export bookmarks FOSDEM %1$d bookmarks.ics Import bookmarks