Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
Use followed only filter on Up Next (#1951)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbanes authored Jul 17, 2024
1 parent 1344218 commit 1f04706
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ interface TiviPreferences {
val useLessData: Preference<Boolean>

val libraryFollowedActive: Preference<Boolean>
val libraryWatchedActive: Preference<Boolean>

val upNextFollowedOnly: Preference<Boolean>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ class TiviPreferencesImpl(
override val libraryFollowedActive: Preference<Boolean> by lazy {
BooleanPreference(KEY_LIBRARY_FOLLOWED_ACTIVE)
}
override val libraryWatchedActive: Preference<Boolean> by lazy {
BooleanPreference(KEY_LIBRARY_WATCHED_ACTIVE)
}
override val upNextFollowedOnly: Preference<Boolean> by lazy {
BooleanPreference(KEY_UPNEXT_FOLLOWED_ONLY)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,22 @@ class SqlDelightLibraryShowsDao(
override fun pagedListLastWatched(
sort: SortOption,
filter: String?,
includeWatched: Boolean,
includeFollowed: Boolean,
onlyFollowed: Boolean,
): PagingSource<Int, LibraryShow> {
val searchQuery = when {
filter.isNullOrEmpty() -> null
else -> "%$filter%"
}
return QueryPagingSource(
countQuery = db.library_showsQueries.count(
includeWatched = includeWatched.sqlValue,
includeFollowed = includeFollowed.sqlValue,
onlyFollowed = onlyFollowed.sqlValue,
filter = searchQuery,
),
transacter = db.library_showsQueries,
context = dispatchers.io,
queryProvider = { limit: Long, offset: Long ->
db.library_showsQueries.entries(
includeWatched = includeWatched.sqlValue,
includeFollowed = includeFollowed.sqlValue,
onlyFollowed = onlyFollowed.sqlValue,
filter = searchQuery,
sort = sort.sqlValue,
limit = limit,
Expand Down Expand Up @@ -85,10 +82,10 @@ class SqlDelightLibraryShowsDao(
network_logo_path, runtime, genres, status, airs_day, airs_time, airs_tz,
),
stats = show_id_?.let {
ShowsWatchStats(show_id_, episode_count!!, watched_episode_count!!)
ShowsWatchStats(it, episode_count!!, watched_episode_count!!)
},
watchedEntry = id_?.let {
WatchedShowEntry(id_, show_id!!, last_watched!!, last_updated!!)
WatchedShowEntry(it, show_id!!, last_watched!!, last_updated!!)
},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ LEFT JOIN episodes AS eps ON eps.season_id = s.id
LEFT JOIN episode_watch_entries AS ew ON ew.episode_id = eps.id
WHERE
(s.number IS NULL OR s.number != 0)
AND (
(:includeWatched = 1 AND watched_entries.id IS NOT NULL) OR
(:includeFollowed = 1 AND myshows_entries.id IS NOT NULL)
)
AND (:onlyFollowed = 0 OR myshows_entries.id IS NOT NULL)
AND (:filter IS NULL OR shows.title LIKE :filter)
GROUP BY shows.id
ORDER BY CASE
Expand All @@ -28,8 +25,6 @@ count:
SELECT COUNT(DISTINCT shows.id) FROM shows
LEFT JOIN myshows_entries ON shows.id = myshows_entries.show_id
LEFT JOIN watched_entries ON shows.id = watched_entries.show_id
WHERE (
(:includeWatched = 1 AND watched_entries.id IS NOT NULL) OR
(:includeFollowed = 1 AND myshows_entries.id IS NOT NULL)
)
AND (:filter IS NULL OR shows.title LIKE :filter);
WHERE
(:onlyFollowed = 0 OR myshows_entries.id IS NOT NULL)
AND (:filter IS NULL OR shows.title LIKE :filter);
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ interface LibraryShowsDao {
fun pagedListLastWatched(
sort: SortOption,
filter: String?,
includeWatched: Boolean,
includeFollowed: Boolean,
onlyFollowed: Boolean,
): PagingSource<Int, LibraryShow>
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ class ObservePagedLibraryShows(
libraryShowsDao.pagedListLastWatched(
sort = params.sort,
filter = if (params.filter.isNullOrEmpty()) null else params.filter,
includeWatched = params.includeWatched,
includeFollowed = params.includeFollowed,
onlyFollowed = params.onlyFollowed,
)
}.flow

data class Parameters(
val sort: SortOption,
val filter: String? = null,
val includeWatched: Boolean = true,
val includeFollowed: Boolean = true,
val onlyFollowed: Boolean = true,
override val pagingConfig: PagingConfig,
) : PagingInteractor.Parameters<LibraryShow>
}
26 changes: 3 additions & 23 deletions ui/library/src/commonMain/kotlin/app/tivi/home/library/Library.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ internal fun Library(
openShowDetails = { eventSink(LibraryUiEvent.OpenShowDetails(it)) },
onMessageShown = { eventSink(LibraryUiEvent.ClearMessage(it)) },
onToggleIncludeFollowedShows = { eventSink(LibraryUiEvent.ToggleFollowedShowsIncluded) },
onToggleIncludeWatchedShows = { eventSink(LibraryUiEvent.ToggleWatchedShowsIncluded) },
openUser = {
scope.launch {
overlayHost.showInDialog(AccountScreen, navigator::goTo)
Expand All @@ -145,7 +144,6 @@ internal fun Library(
openShowDetails: (showId: Long) -> Unit,
onMessageShown: (id: Long) -> Unit,
onToggleIncludeFollowedShows: () -> Unit,
onToggleIncludeWatchedShows: () -> Unit,
refresh: () -> Unit,
openUser: () -> Unit,
onFilterChanged: (String) -> Unit,
Expand Down Expand Up @@ -217,7 +215,6 @@ internal fun Library(
paddingValues = paddingValues,
onFilterChanged = onFilterChanged,
onToggleIncludeFollowedShows = onToggleIncludeFollowedShows,
onToggleIncludeWatchedShows = onToggleIncludeWatchedShows,
onSortSelected = onSortSelected,
openShowDetails = openShowDetails,
modifier = Modifier
Expand Down Expand Up @@ -246,7 +243,6 @@ private fun LibraryGrid(
paddingValues: PaddingValues,
onFilterChanged: (String) -> Unit,
onToggleIncludeFollowedShows: () -> Unit,
onToggleIncludeWatchedShows: () -> Unit,
onSortSelected: (SortOption) -> Unit,
openShowDetails: (showId: Long) -> Unit,
modifier: Modifier = Modifier,
Expand Down Expand Up @@ -306,9 +302,9 @@ private fun LibraryGrid(
modifier = Modifier.padding(vertical = 8.dp),
) {
FilterChip(
selected = state.followedShowsIncluded,
selected = state.onlyFollowedShows,
leadingIcon = {
AnimatedVisibility(visible = state.followedShowsIncluded) {
AnimatedVisibility(visible = state.onlyFollowedShows) {
Icon(
imageVector = Icons.Default.Done,
contentDescription = null,
Expand All @@ -317,23 +313,7 @@ private fun LibraryGrid(
},
onClick = onToggleIncludeFollowedShows,
label = {
Text(text = LocalStrings.current.followingShowsTitle)
},
)

FilterChip(
selected = state.watchedShowsIncluded,
leadingIcon = {
AnimatedVisibility(visible = state.watchedShowsIncluded) {
Icon(
imageVector = Icons.Default.Done,
contentDescription = null,
)
}
},
onClick = onToggleIncludeWatchedShows,
label = {
Text(text = LocalStrings.current.watchedShowsTitle)
Text(text = LocalStrings.current.upnextFilterFollowedShowsOnlyTitle)
},
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ class LibraryPresenter(
val user by observeUserDetails.value.flow.collectAsRetainedState(null)
val authState by observeTraktAuthState.value.flow.collectAsRetainedState(TraktAuthState.LOGGED_OUT)

val includeWatchedShows by preferences.value.libraryWatchedActive.collectAsState()
val includeFollowedShows by preferences.value.libraryFollowedActive.collectAsState()
val onlyFollowed by preferences.value.libraryFollowedActive.collectAsState()

val coroutineScope = rememberCoroutineScope()

Expand Down Expand Up @@ -120,9 +119,6 @@ class LibraryPresenter(
LibraryUiEvent.ToggleFollowedShowsIncluded -> {
coroutineScope.launch { preferences.value.libraryFollowedActive.toggle() }
}
LibraryUiEvent.ToggleWatchedShowsIncluded -> {
coroutineScope.launch { preferences.value.libraryWatchedActive.toggle() }
}
LibraryUiEvent.OpenAccount -> navigator.goTo(AccountScreen)
is LibraryUiEvent.OpenShowDetails -> {
navigator.goTo(ShowDetailsScreen(event.showId))
Expand All @@ -143,14 +139,13 @@ class LibraryPresenter(
}
}

LaunchedEffect(filter, sort, includeFollowedShows, includeWatchedShows) {
LaunchedEffect(filter, sort, onlyFollowed) {
// When the filter and sort options change, update the data source
retainedObservePagedLibraryShows(
ObservePagedLibraryShows.Parameters(
sort = sort,
filter = filter,
includeFollowed = includeFollowedShows,
includeWatched = includeWatchedShows,
onlyFollowed = onlyFollowed,
pagingConfig = PAGING_CONFIG,
),
)
Expand All @@ -166,8 +161,7 @@ class LibraryPresenter(
availableSorts = AVAILABLE_SORT_OPTIONS,
sort = sort,
message = message,
watchedShowsIncluded = includeWatchedShows,
followedShowsIncluded = includeFollowedShows,
onlyFollowedShows = onlyFollowed,
eventSink = ::eventSink,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ data class LibraryUiState(
val availableSorts: List<SortOption> = emptyList(),
val sort: SortOption = SortOption.LAST_WATCHED,
val message: UiMessage? = null,
val followedShowsIncluded: Boolean = false,
val watchedShowsIncluded: Boolean = false,
val onlyFollowedShows: Boolean = false,
val eventSink: (LibraryUiEvent) -> Unit,
) : CircuitUiState

Expand All @@ -35,7 +34,6 @@ sealed interface LibraryUiEvent : CircuitUiEvent {
data class ChangeFilter(val filter: String?) : LibraryUiEvent
data class ChangeSort(val sort: SortOption) : LibraryUiEvent
data object ToggleFollowedShowsIncluded : LibraryUiEvent
data object ToggleWatchedShowsIncluded : LibraryUiEvent
data object OpenAccount : LibraryUiEvent
data class OpenShowDetails(val showId: Long) : LibraryUiEvent
}

0 comments on commit 1f04706

Please sign in to comment.