Skip to content

Commit

Permalink
feat: change "upcoming only" bookmarks filter to "hide past sessions".
Browse files Browse the repository at this point in the history
closes #78
  • Loading branch information
cbeyls committed Jan 5, 2024
1 parent 711d9fb commit 9329bdc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 30 deletions.
6 changes: 3 additions & 3 deletions app/src/main/java/be/digitalia/fosdem/db/BookmarksDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<Event>
abstract suspend fun getBookmarks(minEndTime: Instant = Instant.EPOCH): List<Event>

@Query("""SELECT b.event_id, e.start_time
FROM bookmarks b
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,32 +39,32 @@ class BookmarksViewModel @Inject constructor(
private val application: Application
) : ViewModel() {

private val upcomingOnlyStateFlow = MutableStateFlow<Boolean?>(null)
private val hidePastEventsStateFlow = MutableStateFlow<Boolean?>(null)

@OptIn(ExperimentalCoroutinesApi::class)
val bookmarks: StateFlow<List<Event>?> = 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)
}
}
}

private fun SharedFlowContext.getObservableBookmarks(minStartTime: Instant): Flow<List<Event>> =
private fun SharedFlowContext.getObservableBookmarks(minEndTime: Instant): Flow<List<Event>> =
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) {
Expand All @@ -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)
}
}
4 changes: 2 additions & 2 deletions app/src/main/res/menu/bookmarks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
app:showAsAction="ifRoom">
<menu>
<item
android:id="@+id/upcoming_only"
android:id="@+id/hide_past_events"
android:checkable="true"
android:title="@string/upcoming_only" />
android:title="@string/hide_past_events" />
</menu>
</item>
<item
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

<!-- Bookmarks -->
<string name="filter">Filter</string>
<string name="upcoming_only">Upcoming only</string>
<string name="hide_past_events">Hide past sessions</string>
<string name="export_bookmarks">Export bookmarks</string>
<string name="export_bookmarks_file_name">FOSDEM %1$d bookmarks.ics</string>
<string name="import_bookmarks">Import bookmarks</string>
Expand Down

0 comments on commit 9329bdc

Please sign in to comment.