diff --git a/app/src/main/java/com/battlelancer/seriesguide/shows/ShowsDistillationFragment.kt b/app/src/main/java/com/battlelancer/seriesguide/shows/ShowsDistillationFragment.kt index 3b6e94bdf3..14795d14b6 100644 --- a/app/src/main/java/com/battlelancer/seriesguide/shows/ShowsDistillationFragment.kt +++ b/app/src/main/java/com/battlelancer/seriesguide/shows/ShowsDistillationFragment.kt @@ -12,6 +12,7 @@ import androidx.appcompat.app.AppCompatDialogFragment import androidx.core.content.edit import androidx.fragment.app.DialogFragment import androidx.fragment.app.FragmentManager +import androidx.fragment.app.viewModels import androidx.preference.PreferenceManager import com.battlelancer.seriesguide.R import com.battlelancer.seriesguide.appwidget.ListWidgetProvider @@ -26,6 +27,7 @@ import com.battlelancer.seriesguide.util.safeShow class ShowsDistillationFragment : AppCompatDialogFragment() { + private val model: ShowsDistillationViewModel by viewModels() private var binding: DialogShowsDistillationBinding? = null override fun onCreate(savedInstanceState: Bundle?) { @@ -48,7 +50,8 @@ class ShowsDistillationFragment : AppCompatDialogFragment() { ShowFilter.fromSettings(requireContext()), filterListener, SortShowsView.ShowSortOrder.fromSettings(requireContext()), - sortOrderListener + sortOrderListener, + model.showsDistillationUiState ) val viewPager = binding.viewPagerShowsDistillation viewPager.adapter = tabsAdapter diff --git a/app/src/main/java/com/battlelancer/seriesguide/shows/ShowsDistillationPageAdapter.kt b/app/src/main/java/com/battlelancer/seriesguide/shows/ShowsDistillationPageAdapter.kt index b32c7471b5..c2bd332341 100644 --- a/app/src/main/java/com/battlelancer/seriesguide/shows/ShowsDistillationPageAdapter.kt +++ b/app/src/main/java/com/battlelancer/seriesguide/shows/ShowsDistillationPageAdapter.kt @@ -8,16 +8,19 @@ import android.view.View import android.view.ViewGroup import android.widget.LinearLayout import androidx.annotation.StringRes +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 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 sortOrderListener: SortShowsView.SortOrderListener, + private val showsDistillationUiState: StateFlow ) : PagerAdapter() { override fun instantiateItem(container: ViewGroup, position: Int): Any { @@ -37,6 +40,16 @@ class ShowsDistillationPageAdapter( setFilterListener(filterListener) } } + + DistillationPages.FILTER_WATCH_PROVIDERS -> { + ComposeView(context).apply { + this.layoutParams = layoutParams + setContent { + WatchProviderFilter(showsDistillationUiState) + } + } + } + DistillationPages.SORT -> { SortShowsView(context).apply { this.layoutParams = layoutParams @@ -68,6 +81,7 @@ class ShowsDistillationPageAdapter( enum class DistillationPages(@StringRes val titleRes: Int) { FILTER(R.string.action_shows_filter), + FILTER_WATCH_PROVIDERS(R.string.action_stream), SORT(R.string.action_shows_sort) } } \ No newline at end of file diff --git a/app/src/main/java/com/battlelancer/seriesguide/shows/ShowsDistillationViewModel.kt b/app/src/main/java/com/battlelancer/seriesguide/shows/ShowsDistillationViewModel.kt new file mode 100644 index 0000000000..a11ac49f2d --- /dev/null +++ b/app/src/main/java/com/battlelancer/seriesguide/shows/ShowsDistillationViewModel.kt @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2024 Uwe Trottmann + +package com.battlelancer.seriesguide.shows + +import android.app.Application +import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.viewModelScope +import com.battlelancer.seriesguide.provider.SgRoomDatabase +import com.battlelancer.seriesguide.streaming.SgWatchProvider +import kotlinx.coroutines.flow.SharingStarted +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.stateIn + +class ShowsDistillationViewModel(application: Application) : AndroidViewModel(application) { + + val showsDistillationUiState: StateFlow = + SgRoomDatabase.getInstance(application).sgWatchProviderHelper() + .allWatchProvidersFlow(SgWatchProvider.Type.SHOWS.id) + .map { ShowsDistillationUiState(it) } + .stateIn( + scope = viewModelScope, + started = SharingStarted.WhileSubscribed(), + initialValue = ShowsDistillationUiState() + ) + +} + +data class ShowsDistillationUiState( + val watchProviders: List = listOf() +) \ No newline at end of file diff --git a/app/src/main/java/com/battlelancer/seriesguide/shows/WatchProviderFilterView.kt b/app/src/main/java/com/battlelancer/seriesguide/shows/WatchProviderFilterView.kt index f55314057d..92158dd0a8 100644 --- a/app/src/main/java/com/battlelancer/seriesguide/shows/WatchProviderFilterView.kt +++ b/app/src/main/java/com/battlelancer/seriesguide/shows/WatchProviderFilterView.kt @@ -3,38 +3,72 @@ package com.battlelancer.seriesguide.shows -import android.content.Context -import android.util.AttributeSet -import android.view.LayoutInflater -import android.widget.LinearLayout -import androidx.recyclerview.widget.LinearLayoutManager -import com.battlelancer.seriesguide.databinding.ViewFilterWatchProvidersBinding +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.material3.Switch +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp import com.battlelancer.seriesguide.streaming.SgWatchProvider +import com.battlelancer.seriesguide.ui.theme.SeriesGuideTheme +import kotlinx.coroutines.flow.StateFlow -class WatchProviderFilterView @JvmOverloads constructor( - context: Context, - attrs: AttributeSet? = null, - defStyleAttr: Int = 0 -) : LinearLayout(context, attrs, defStyleAttr) { - - private val binding: ViewFilterWatchProvidersBinding - private val adapter: WatchProviderFilterAdapter - - init { - orientation = VERTICAL +@Composable +fun WatchProviderFilter(showsDistillationUiState: StateFlow) { + val uiState by showsDistillationUiState.collectAsState() + SeriesGuideTheme { + WatchProviderList(watchProviders = uiState.watchProviders) + } +} - // can't do in onFinishInflate as that is only called when inflating from XML - binding = ViewFilterWatchProvidersBinding.inflate(LayoutInflater.from(context), this) +@Composable +fun WatchProviderList(watchProviders: List) { - adapter = WatchProviderFilterAdapter() - binding.recyclerViewProviderFilter.also { - it.layoutManager = LinearLayoutManager(context) - it.adapter = adapter + LazyColumn( + modifier = Modifier + .fillMaxWidth() + .height(400.dp), + verticalArrangement = Arrangement.spacedBy(32.dp) + ) { + items(items = watchProviders, key = { it._id }) { + WatchProviderFilterItem(it) } } - fun setWatchProviders(watchProviders: List) { - adapter.submitList(watchProviders) +} + +@Composable +fun WatchProviderFilterItem(item: SgWatchProvider, modifier: Modifier = Modifier) { + Row(modifier.fillMaxWidth()) { + Text(text = item.provider_name) + Switch(checked = item.enabled, onCheckedChange = { +// checked = it + }) } +} +@Preview() +@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 + ) + }) + } } \ No newline at end of file diff --git a/app/src/main/java/com/battlelancer/seriesguide/streaming/SgWatchProviderHelper.kt b/app/src/main/java/com/battlelancer/seriesguide/streaming/SgWatchProviderHelper.kt index c5bc90ecef..f7b91735cb 100644 --- a/app/src/main/java/com/battlelancer/seriesguide/streaming/SgWatchProviderHelper.kt +++ b/app/src/main/java/com/battlelancer/seriesguide/streaming/SgWatchProviderHelper.kt @@ -43,6 +43,9 @@ interface SgWatchProviderHelper { @Query("SELECT * FROM sg_watch_provider WHERE type=:type ORDER BY display_priority ASC, provider_name ASC") fun allWatchProvidersPagingSource(type: Int): PagingSource + @Query("SELECT * FROM sg_watch_provider WHERE type=:type ORDER BY display_priority ASC, provider_name ASC") + fun allWatchProvidersFlow(type: Int): Flow> + @Query("SELECT provider_id FROM sg_watch_provider WHERE type=:type AND enabled=1") fun getEnabledWatchProviderIds(type: Int): LiveData> @@ -55,6 +58,7 @@ interface SgWatchProviderHelper { @Query("UPDATE sg_watch_provider SET enabled=0 WHERE type=:type") fun setAllDisabled(type: Int) + // TODO remove if unused // @Query("SELECT _id FROM sg_watch_provider WHERE provider_id=:providerId AND type=:type") // fun getByExternalProviderId(providerId: Int, type: Int): Long? diff --git a/app/src/main/res/layout/view_filter_watch_providers.xml b/app/src/main/res/layout/view_filter_watch_providers.xml deleted file mode 100644 index 5ab2955113..0000000000 --- a/app/src/main/res/layout/view_filter_watch_providers.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - \ No newline at end of file