Skip to content

Commit

Permalink
Provider filter: change provider filter on switch change.
Browse files Browse the repository at this point in the history
  • Loading branch information
UweTrottmann committed Feb 2, 2024
1 parent bfdc9d5 commit 23ddc94
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ShowsDistillationFragment : AppCompatDialogFragment() {
filterListener,
SortShowsView.ShowSortOrder.fromSettings(requireContext()),
sortOrderListener,
model.showsDistillationUiState
model
)
val viewPager = binding.viewPagerShowsDistillation
viewPager.adapter = tabsAdapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import androidx.compose.ui.platform.ComposeView
import androidx.viewpager.widget.PagerAdapter
import com.battlelancer.seriesguide.R
import com.battlelancer.seriesguide.settings.DisplaySettings
import kotlinx.coroutines.flow.StateFlow
import com.battlelancer.seriesguide.streaming.SgWatchProvider

class ShowsDistillationPageAdapter(
private val context: Context,
private val initialShowFilter: ShowsDistillationSettings.ShowFilter,
private val filterListener: FilterShowsView.FilterListener,
private val initialShowSortOrder: SortShowsView.ShowSortOrder,
private val sortOrderListener: SortShowsView.SortOrderListener,
private val showsDistillationUiState: StateFlow<ShowsDistillationUiState>
private val showsDistillationViewModel: ShowsDistillationViewModel
) : PagerAdapter() {

override fun instantiateItem(container: ViewGroup, position: Int): Any {
Expand All @@ -45,7 +45,14 @@ class ShowsDistillationPageAdapter(
ComposeView(context).apply {
this.layoutParams = layoutParams
setContent {
WatchProviderFilter(showsDistillationUiState)
WatchProviderFilter(
showsDistillationUiState = showsDistillationViewModel.showsDistillationUiState,
onProviderFilterChange = { provider: SgWatchProvider, checked: Boolean ->
showsDistillationViewModel.changeWatchProviderFilter(
provider,
checked
)
})
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.battlelancer.seriesguide.provider.SgRoomDatabase
import com.battlelancer.seriesguide.streaming.SgWatchProvider
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch

class ShowsDistillationViewModel(application: Application) : AndroidViewModel(application) {

Expand All @@ -25,6 +27,13 @@ class ShowsDistillationViewModel(application: Application) : AndroidViewModel(ap
initialValue = ShowsDistillationUiState()
)

fun changeWatchProviderFilter(watchProvider: SgWatchProvider, filter: Boolean) {
viewModelScope.launch(Dispatchers.IO) {
SgRoomDatabase.getInstance(getApplication()).sgWatchProviderHelper()
.setFilterLocal(watchProvider._id, filter)
}
}

}

data class ShowsDistillationUiState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,24 @@ import com.battlelancer.seriesguide.ui.theme.SeriesGuideTheme
import kotlinx.coroutines.flow.StateFlow

@Composable
fun WatchProviderFilter(showsDistillationUiState: StateFlow<ShowsDistillationUiState>) {
fun WatchProviderFilter(
showsDistillationUiState: StateFlow<ShowsDistillationUiState>,
onProviderFilterChange: (SgWatchProvider, Boolean) -> Unit
) {
val uiState by showsDistillationUiState.collectAsState()
SeriesGuideTheme {
WatchProviderList(watchProviders = uiState.watchProviders)
WatchProviderList(
watchProviders = uiState.watchProviders,
onProviderFilterChange
)
}
}

@Composable
fun WatchProviderList(watchProviders: List<SgWatchProvider>) {
fun WatchProviderList(
watchProviders: List<SgWatchProvider>,
onProviderFilterChange: (SgWatchProvider, Boolean) -> Unit
) {

LazyColumn(
modifier = Modifier
Expand All @@ -39,18 +48,21 @@ fun WatchProviderList(watchProviders: List<SgWatchProvider>) {
verticalArrangement = Arrangement.spacedBy(32.dp)
) {
items(items = watchProviders, key = { it._id }) {
WatchProviderFilterItem(it)
WatchProviderFilterItem(it, onProviderFilterChange)
}
}

}

@Composable
fun WatchProviderFilterItem(item: SgWatchProvider, modifier: Modifier = Modifier) {
Row(modifier.fillMaxWidth()) {
fun WatchProviderFilterItem(
item: SgWatchProvider,
onProviderFilterChange: (SgWatchProvider, Boolean) -> Unit
) {
Row(Modifier.fillMaxWidth()) {
Text(text = item.provider_name)
Switch(checked = item.enabled, onCheckedChange = {
// checked = it
Switch(checked = item.filter_local, onCheckedChange = {
onProviderFilterChange(item, it)
})
}
}
Expand All @@ -59,16 +71,18 @@ fun WatchProviderFilterItem(item: SgWatchProvider, modifier: Modifier = Modifier
@Composable
fun WatchProviderFilterPreview() {
SeriesGuideTheme {
WatchProviderList(watchProviders = List(20) {
SgWatchProvider(
_id = it,
provider_id = it,
provider_name = "Watch Provider $it",
display_priority = 0,
enabled = it.mod(2) == 0,
logo_path = "",
type = SgWatchProvider.Type.SHOWS.id
)
})
WatchProviderList(
watchProviders = List(20) {
SgWatchProvider(
_id = it,
provider_id = it,
provider_name = "Watch Provider $it",
display_priority = 0,
enabled = it.mod(2) == 0,
logo_path = "",
type = SgWatchProvider.Type.SHOWS.id
)
},
onProviderFilterChange = { _: SgWatchProvider, _: Boolean -> })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ interface SgWatchProviderHelper {
@Query("UPDATE sg_watch_provider SET enabled=:enabled WHERE _id=:id")
fun setEnabled(id: Int, enabled: Boolean)

@Query("UPDATE sg_watch_provider SET filter_local=:enabled WHERE _id=:id")
suspend fun setFilterLocal(id: Int, enabled: Boolean)

@Query("UPDATE sg_watch_provider SET enabled=0 WHERE type=:type")
fun setAllDisabled(type: Int)

Expand Down

0 comments on commit 23ddc94

Please sign in to comment.