Skip to content

Commit 91af8b0

Browse files
committed
Added the new compose screen with its components and events
1 parent e1169dc commit 91af8b0

8 files changed

Lines changed: 609 additions & 0 deletions

File tree

app/src/main/java/org/schabi/newpipe/MainActivity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@
9898
import java.util.List;
9999
import java.util.Objects;
100100

101+
import dagger.hilt.android.AndroidEntryPoint;
102+
103+
@AndroidEntryPoint
101104
public class MainActivity extends AppCompatActivity {
102105
private static final String TAG = "MainActivity";
103106
@SuppressWarnings("ConstantConditions")
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.schabi.newpipe.settings.components.irreversible_preference
2+
3+
import androidx.compose.foundation.clickable
4+
import androidx.compose.foundation.layout.Column
5+
import androidx.compose.foundation.layout.Row
6+
import androidx.compose.foundation.layout.Spacer
7+
import androidx.compose.foundation.layout.fillMaxWidth
8+
import androidx.compose.foundation.layout.padding
9+
import androidx.compose.material3.MaterialTheme
10+
import androidx.compose.material3.Text
11+
import androidx.compose.runtime.Composable
12+
import androidx.compose.runtime.derivedStateOf
13+
import androidx.compose.runtime.getValue
14+
import androidx.compose.runtime.remember
15+
import androidx.compose.ui.Alignment
16+
import androidx.compose.ui.Modifier
17+
import androidx.compose.ui.draw.alpha
18+
import androidx.compose.ui.tooling.preview.Preview
19+
import org.schabi.newpipe.ui.theme.AppTheme
20+
import org.schabi.newpipe.ui.theme.SizeTokens.SpacingExtraSmall
21+
import org.schabi.newpipe.ui.theme.SizeTokens.SpacingMedium
22+
23+
@Composable
24+
fun IrreversiblePreferenceComponent(
25+
title: String,
26+
summary: String,
27+
onClick: () -> Unit,
28+
modifier: Modifier = Modifier,
29+
enabled: Boolean = true,
30+
) {
31+
Row(
32+
modifier = Modifier
33+
.clickable {
34+
if (enabled) {
35+
onClick()
36+
}
37+
}
38+
.then(modifier),
39+
verticalAlignment = Alignment.CenterVertically,
40+
) {
41+
val alpha by remember {
42+
derivedStateOf {
43+
if (enabled) 1f else 0.38f
44+
}
45+
}
46+
Column(
47+
modifier = Modifier.padding(SpacingMedium)
48+
) {
49+
Text(
50+
text = title,
51+
modifier = Modifier.alpha(alpha),
52+
)
53+
Spacer(modifier = Modifier.padding(SpacingExtraSmall))
54+
Text(
55+
text = summary,
56+
style = MaterialTheme.typography.labelSmall,
57+
modifier = Modifier.alpha(alpha * 0.6f),
58+
)
59+
}
60+
}
61+
}
62+
63+
@Preview(showBackground = true, backgroundColor = 0xFFFFFFFF)
64+
@Composable
65+
private fun IrreversiblePreferenceComponentPreview() {
66+
val title = "Wipe cached metadata"
67+
val summary = "Remove all cached webpage data"
68+
AppTheme {
69+
Column {
70+
71+
IrreversiblePreferenceComponent(
72+
title = title,
73+
summary = summary,
74+
onClick = {},
75+
modifier = Modifier.fillMaxWidth()
76+
)
77+
IrreversiblePreferenceComponent(
78+
title = title,
79+
summary = summary,
80+
onClick = {},
81+
modifier = Modifier.fillMaxWidth(),
82+
enabled = false
83+
)
84+
}
85+
}
86+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.schabi.newpipe.settings.components.switch_preference
2+
3+
import androidx.compose.foundation.layout.Arrangement
4+
import androidx.compose.foundation.layout.Column
5+
import androidx.compose.foundation.layout.Row
6+
import androidx.compose.foundation.layout.Spacer
7+
import androidx.compose.foundation.layout.fillMaxWidth
8+
import androidx.compose.foundation.layout.padding
9+
import androidx.compose.material3.MaterialTheme
10+
import androidx.compose.material3.Switch
11+
import androidx.compose.material3.Text
12+
import androidx.compose.runtime.Composable
13+
import androidx.compose.ui.Alignment
14+
import androidx.compose.ui.Modifier
15+
import androidx.compose.ui.draw.alpha
16+
import androidx.compose.ui.tooling.preview.Preview
17+
import org.schabi.newpipe.ui.theme.AppTheme
18+
import org.schabi.newpipe.ui.theme.SizeTokens.SpacingExtraSmall
19+
import org.schabi.newpipe.ui.theme.SizeTokens.SpacingMedium
20+
21+
@Composable
22+
fun SwitchPreferenceComponent(
23+
title: String,
24+
summary: String,
25+
isChecked: Boolean,
26+
onCheckedChange: (Boolean) -> Unit,
27+
modifier: Modifier = Modifier,
28+
) {
29+
Row(
30+
modifier = modifier,
31+
horizontalArrangement = Arrangement.SpaceBetween,
32+
verticalAlignment = Alignment.CenterVertically
33+
) {
34+
35+
Column(
36+
horizontalAlignment = Alignment.Start,
37+
verticalArrangement = Arrangement.Center,
38+
modifier = Modifier.padding(SpacingMedium)
39+
) {
40+
Text(text = title)
41+
Spacer(modifier = Modifier.padding(SpacingExtraSmall))
42+
Text(
43+
text = summary,
44+
style = MaterialTheme.typography.labelSmall,
45+
modifier = Modifier.alpha(0.6f)
46+
)
47+
}
48+
49+
Switch(
50+
checked = isChecked,
51+
onCheckedChange = onCheckedChange,
52+
modifier = Modifier.padding(SpacingMedium)
53+
)
54+
}
55+
}
56+
57+
@Preview(showBackground = true, backgroundColor = 0xFFFFFFFF)
58+
@Composable
59+
private fun SwitchPreferenceComponentPreview() {
60+
val title = "Watch history"
61+
val subtitle = "Keep track of watched videos"
62+
var isChecked = false
63+
AppTheme {
64+
SwitchPreferenceComponent(
65+
title = title,
66+
summary = subtitle,
67+
isChecked = isChecked,
68+
onCheckedChange = {
69+
isChecked = it
70+
},
71+
modifier = Modifier.fillMaxWidth()
72+
)
73+
}
74+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.schabi.newpipe.settings.presentation.history_cache
2+
3+
sealed class HistoryCacheEvent {
4+
data class OnUpdateBooleanPreference(val key: String, val isEnabled: Boolean) :
5+
HistoryCacheEvent()
6+
7+
data class OnClickWipeCachedMetadata(val key: String) : HistoryCacheEvent()
8+
data class OnClickClearWatchHistory(val key: String) : HistoryCacheEvent()
9+
data class OnClickDeletePlaybackPositions(val key: String) : HistoryCacheEvent()
10+
data class OnClickClearSearchHistory(val key: String) : HistoryCacheEvent()
11+
data class OnClickReCaptchaCookies(val key: String) : HistoryCacheEvent()
12+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package org.schabi.newpipe.settings.presentation.history_cache
2+
3+
import androidx.compose.foundation.layout.Arrangement
4+
import androidx.compose.foundation.layout.Column
5+
import androidx.compose.foundation.layout.fillMaxSize
6+
import androidx.compose.foundation.layout.fillMaxWidth
7+
import androidx.compose.foundation.layout.padding
8+
import androidx.compose.foundation.rememberScrollState
9+
import androidx.compose.foundation.verticalScroll
10+
import androidx.compose.material3.Scaffold
11+
import androidx.compose.material3.SnackbarHost
12+
import androidx.compose.material3.SnackbarHostState
13+
import androidx.compose.material3.Surface
14+
import androidx.compose.runtime.Composable
15+
import androidx.compose.runtime.collectAsState
16+
import androidx.compose.runtime.getValue
17+
import androidx.compose.runtime.mutableStateOf
18+
import androidx.compose.runtime.remember
19+
import androidx.compose.runtime.rememberCoroutineScope
20+
import androidx.compose.ui.Alignment
21+
import androidx.compose.ui.Modifier
22+
import androidx.compose.ui.tooling.preview.Preview
23+
import androidx.hilt.navigation.compose.hiltViewModel
24+
import kotlinx.coroutines.launch
25+
import org.schabi.newpipe.settings.presentation.history_cache.components.CachePreferencesComponent
26+
import org.schabi.newpipe.settings.presentation.history_cache.components.HistoryPreferencesComponent
27+
import org.schabi.newpipe.ui.theme.AppTheme
28+
29+
@Composable
30+
fun HistoryCacheScreen(
31+
modifier: Modifier = Modifier,
32+
viewModel: HistoryCacheSettingsViewModel = hiltViewModel(),
33+
) {
34+
val state by viewModel.state.collectAsState()
35+
HistoryCacheComponent(
36+
state = state,
37+
onEvent = viewModel::onEvent,
38+
modifier = modifier
39+
)
40+
}
41+
42+
@Composable
43+
fun HistoryCacheComponent(
44+
state: HistoryCacheUiState,
45+
onEvent: (HistoryCacheEvent) -> Unit,
46+
modifier: Modifier = Modifier,
47+
) {
48+
val snackBarHostState = remember { SnackbarHostState() }
49+
Scaffold(
50+
modifier = modifier,
51+
snackbarHost = {
52+
SnackbarHost(snackBarHostState)
53+
}
54+
) { padding ->
55+
val scrollState = rememberScrollState()
56+
Column(
57+
modifier = Modifier
58+
.padding(padding)
59+
.verticalScroll(scrollState),
60+
horizontalAlignment = Alignment.CenterHorizontally,
61+
verticalArrangement = Arrangement.Center,
62+
) {
63+
HistoryPreferencesComponent(
64+
state = state.switchPreferencesUiState,
65+
onEvent = onEvent,
66+
modifier = Modifier.fillMaxWidth()
67+
)
68+
val coroutineScope = rememberCoroutineScope()
69+
CachePreferencesComponent(
70+
onEvent = onEvent,
71+
onShowSnackbar = {
72+
coroutineScope.launch {
73+
snackBarHostState.showSnackbar(it)
74+
}
75+
},
76+
modifier = Modifier.fillMaxWidth()
77+
)
78+
}
79+
}
80+
}
81+
82+
@Preview(showBackground = true)
83+
@Composable
84+
private fun HistoryCacheComponentPreview() {
85+
val state by remember {
86+
mutableStateOf(
87+
HistoryCacheUiState()
88+
)
89+
}
90+
AppTheme(
91+
useDarkTheme = false
92+
) {
93+
Surface {
94+
HistoryCacheComponent(
95+
state = state,
96+
onEvent = {
97+
},
98+
modifier = Modifier.fillMaxSize()
99+
)
100+
}
101+
}
102+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.schabi.newpipe.settings.presentation.history_cache
2+
3+
data class HistoryCacheUiState(
4+
val switchPreferencesUiState: SwitchPreferencesUiState = SwitchPreferencesUiState()
5+
)
6+
7+
data class SwitchPreferencesUiState(
8+
val watchHistoryEnabled: Boolean = false,
9+
val resumePlaybackEnabled: Boolean = false,
10+
val positionsInListsEnabled: Boolean = false,
11+
val searchHistoryEnabled: Boolean = false,
12+
)

0 commit comments

Comments
 (0)