Skip to content

Commit

Permalink
Provider filter: use compose to create provider filter view.
Browse files Browse the repository at this point in the history
  • Loading branch information
UweTrottmann committed Feb 1, 2024
1 parent 1cd3de3 commit 9da4b43
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ShowsDistillationUiState>
) : PagerAdapter() {

override fun instantiateItem(container: ViewGroup, position: Int): Any {
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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<ShowsDistillationUiState> =
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<SgWatchProvider> = listOf()
)
Original file line number Diff line number Diff line change
Expand Up @@ -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<ShowsDistillationUiState>) {
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<SgWatchProvider>) {

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<SgWatchProvider>) {
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
)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int, SgWatchProvider>

@Query("SELECT * FROM sg_watch_provider WHERE type=:type ORDER BY display_priority ASC, provider_name ASC")
fun allWatchProvidersFlow(type: Int): Flow<List<SgWatchProvider>>

@Query("SELECT provider_id FROM sg_watch_provider WHERE type=:type AND enabled=1")
fun getEnabledWatchProviderIds(type: Int): LiveData<List<Int>>

Expand All @@ -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?

Expand Down
16 changes: 0 additions & 16 deletions app/src/main/res/layout/view_filter_watch_providers.xml

This file was deleted.

0 comments on commit 9da4b43

Please sign in to comment.