11package org.schabi.newpipe.ui.screens
22
3+ import android.content.res.Configuration
4+ import androidx.compose.foundation.layout.Row
35import androidx.compose.foundation.layout.padding
6+ import androidx.compose.material.icons.Icons
7+ import androidx.compose.material.icons.filled.ClearAll
48import androidx.compose.material3.DropdownMenuItem
59import androidx.compose.material3.ExperimentalMaterial3Api
610import androidx.compose.material3.ExposedDropdownMenuBox
711import androidx.compose.material3.ExposedDropdownMenuDefaults
12+ import androidx.compose.material3.Icon
13+ import androidx.compose.material3.IconButton
814import androidx.compose.material3.MaterialTheme
915import androidx.compose.material3.MenuAnchorType
16+ import androidx.compose.material3.PlainTooltip
17+ import androidx.compose.material3.Surface
1018import androidx.compose.material3.Text
1119import androidx.compose.material3.TextField
20+ import androidx.compose.material3.TooltipBox
21+ import androidx.compose.material3.TooltipDefaults
22+ import androidx.compose.material3.rememberTooltipState
1223import androidx.compose.runtime.Composable
1324import androidx.compose.runtime.getValue
1425import androidx.compose.runtime.mutableStateOf
1526import androidx.compose.runtime.remember
1627import androidx.compose.runtime.setValue
28+ import androidx.compose.ui.Alignment
1729import androidx.compose.ui.Modifier
1830import androidx.compose.ui.res.stringResource
31+ import androidx.compose.ui.tooling.preview.Preview
1932import androidx.compose.ui.unit.dp
2033import androidx.lifecycle.compose.collectAsStateWithLifecycle
2134import androidx.lifecycle.viewmodel.compose.viewModel
@@ -24,73 +37,103 @@ import org.schabi.newpipe.R
2437import org.schabi.newpipe.local.history.HistoryViewModel
2538import org.schabi.newpipe.local.history.SortKey
2639import org.schabi.newpipe.ui.components.items.ItemList
40+ import org.schabi.newpipe.ui.theme.AppTheme
2741
2842@Composable
2943fun HistoryScreen (viewModel : HistoryViewModel = viewModel()) {
3044 val sortKey by viewModel.sortKey.collectAsStateWithLifecycle()
3145 val historyItems = viewModel.historyItems.collectAsLazyPagingItems()
3246
3347 ItemList (historyItems, header = {
34- HistoryHeader (sortKey, viewModel::updateOrder)
48+ HistoryHeader (sortKey, viewModel::updateOrder, viewModel::deleteWatchHistory )
3549 })
3650}
3751
3852@OptIn(ExperimentalMaterial3Api ::class )
3953@Composable
4054private fun HistoryHeader (
4155 sortKey : SortKey ,
42- onSelectItem : (SortKey ) -> Unit ,
56+ onSelectSortKey : (SortKey ) -> Unit ,
57+ onClickClear : () -> Unit ,
4358) {
4459 var expanded by remember { mutableStateOf(false ) }
4560 val selected = when (sortKey) {
4661 SortKey .MOST_PLAYED -> R .string.title_most_played
4762 SortKey .LAST_PLAYED -> R .string.title_last_played
4863 }
4964
50- ExposedDropdownMenuBox (
51- modifier = Modifier .padding(top = 12 .dp, start = 12 .dp),
52- expanded = expanded,
53- onExpandedChange = { expanded = it },
54- ) {
55- TextField (
56- enabled = true ,
57- modifier = Modifier .menuAnchor(MenuAnchorType .PrimaryNotEditable ),
58- value = stringResource(selected),
59- readOnly = true ,
60- onValueChange = {},
61- trailingIcon = { ExposedDropdownMenuDefaults .TrailingIcon (expanded = expanded) },
62- colors = ExposedDropdownMenuDefaults .textFieldColors(),
63- label = { Text (text = stringResource(R .string.history_sort_label)) }
64- )
65-
66- ExposedDropdownMenu (
65+ Row (verticalAlignment = Alignment .CenterVertically ) {
66+ ExposedDropdownMenuBox (
67+ modifier = Modifier .padding(top = 12 .dp, start = 12 .dp),
6768 expanded = expanded,
68- onDismissRequest = { expanded = false },
69+ onExpandedChange = { expanded = it },
6970 ) {
70- DropdownMenuItem (
71- text = {
72- Text (
73- text = stringResource(R .string.title_most_played),
74- color = MaterialTheme .colorScheme.onBackground,
75- )
76- },
77- onClick = {
78- expanded = false
79- onSelectItem(SortKey .MOST_PLAYED )
80- }
81- )
82- DropdownMenuItem (
83- text = {
84- Text (
85- text = stringResource(R .string.title_last_played),
86- color = MaterialTheme .colorScheme.onBackground,
87- )
88- },
89- onClick = {
90- expanded = false
91- onSelectItem(SortKey .LAST_PLAYED )
92- }
71+ TextField (
72+ enabled = true ,
73+ modifier = Modifier .menuAnchor(MenuAnchorType .PrimaryNotEditable ),
74+ value = stringResource(selected),
75+ readOnly = true ,
76+ onValueChange = {},
77+ trailingIcon = { ExposedDropdownMenuDefaults .TrailingIcon (expanded = expanded) },
78+ colors = ExposedDropdownMenuDefaults .textFieldColors(),
79+ label = { Text (text = stringResource(R .string.history_sort_label)) }
9380 )
81+
82+ ExposedDropdownMenu (
83+ expanded = expanded,
84+ onDismissRequest = { expanded = false },
85+ ) {
86+ DropdownMenuItem (
87+ text = {
88+ Text (
89+ text = stringResource(R .string.title_most_played),
90+ color = MaterialTheme .colorScheme.onBackground,
91+ )
92+ },
93+ onClick = {
94+ expanded = false
95+ onSelectSortKey(SortKey .MOST_PLAYED )
96+ }
97+ )
98+ DropdownMenuItem (
99+ text = {
100+ Text (
101+ text = stringResource(R .string.title_last_played),
102+ color = MaterialTheme .colorScheme.onBackground,
103+ )
104+ },
105+ onClick = {
106+ expanded = false
107+ onSelectSortKey(SortKey .LAST_PLAYED )
108+ }
109+ )
110+ }
111+ }
112+
113+ TooltipBox (
114+ positionProvider = TooltipDefaults .rememberPlainTooltipPositionProvider(),
115+ tooltip = {
116+ PlainTooltip { Text (text = stringResource(R .string.clear_views_history_title)) }
117+ },
118+ state = rememberTooltipState(),
119+ ) {
120+ IconButton (onClick = onClickClear) {
121+ Icon (
122+ imageVector = Icons .Default .ClearAll ,
123+ contentDescription = stringResource(R .string.clear_history_description),
124+ )
125+ }
126+ }
127+ }
128+ }
129+
130+ @Preview(name = " Light mode" , uiMode = Configuration .UI_MODE_NIGHT_NO )
131+ @Preview(name = " Dark mode" , uiMode = Configuration .UI_MODE_NIGHT_YES )
132+ @Composable
133+ private fun HistoryHeaderPreview () {
134+ AppTheme {
135+ Surface (color = MaterialTheme .colorScheme.background) {
136+ HistoryHeader (SortKey .MOST_PLAYED , {}, {})
94137 }
95138 }
96139}
0 commit comments