-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refs/heads/refactor' into About-Compose
# Conflicts: # app/build.gradle # build.gradle
- Loading branch information
Showing
15 changed files
with
363 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.schabi.newpipe | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import androidx.preference.PreferenceManager | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
class AppModule { | ||
|
||
@Provides | ||
@Singleton | ||
fun providesSharedPreference(@ApplicationContext context: Context): SharedPreferences { | ||
return PreferenceManager.getDefaultSharedPreferences(context) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
app/src/main/java/org/schabi/newpipe/settings/DebugScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.schabi.newpipe.settings | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Modifier | ||
import org.schabi.newpipe.R | ||
import org.schabi.newpipe.settings.viewmodel.SettingsViewModel | ||
import org.schabi.newpipe.ui.SwitchPreference | ||
import org.schabi.newpipe.ui.theme.SizeTokens | ||
|
||
@Composable | ||
fun DebugScreen(viewModel: SettingsViewModel, modifier: Modifier = Modifier) { | ||
|
||
val settingsLayoutRedesign by viewModel.settingsLayoutRedesign.collectAsState() | ||
|
||
Column(modifier = modifier) { | ||
SwitchPreference( | ||
modifier = Modifier.padding(SizeTokens.SpacingExtraSmall), | ||
R.string.settings_layout_redesign, | ||
settingsLayoutRedesign, | ||
viewModel::toggleSettingsLayoutRedesign | ||
) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/org/schabi/newpipe/settings/SettingsScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.schabi.newpipe.settings | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.material3.HorizontalDivider | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import org.schabi.newpipe.R | ||
import org.schabi.newpipe.ui.TextPreference | ||
|
||
@Composable | ||
fun SettingsScreen( | ||
onSelectSettingOption: (SettingsScreenKey) -> Unit, | ||
modifier: Modifier = Modifier | ||
) { | ||
Column(modifier = modifier) { | ||
TextPreference( | ||
title = R.string.settings_category_debug_title, | ||
onClick = { onSelectSettingOption(SettingsScreenKey.DEBUG) } | ||
) | ||
HorizontalDivider(color = Color.Black) | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
app/src/main/java/org/schabi/newpipe/settings/SettingsV2Activity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package org.schabi.newpipe.settings | ||
|
||
import android.os.Bundle | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import androidx.activity.viewModels | ||
import androidx.annotation.StringRes | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableIntStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.navigation.compose.NavHost | ||
import androidx.navigation.compose.composable | ||
import androidx.navigation.compose.rememberNavController | ||
import androidx.navigation.navArgument | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import org.schabi.newpipe.R | ||
import org.schabi.newpipe.settings.viewmodel.SettingsViewModel | ||
import org.schabi.newpipe.ui.Toolbar | ||
import org.schabi.newpipe.ui.theme.AppTheme | ||
|
||
const val SCREEN_TITLE_KEY = "SCREEN_TITLE_KEY" | ||
|
||
@AndroidEntryPoint | ||
class SettingsV2Activity : ComponentActivity() { | ||
|
||
private val settingsViewModel: SettingsViewModel by viewModels() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
setContent { | ||
val navController = rememberNavController() | ||
var screenTitle by remember { mutableIntStateOf(SettingsScreenKey.ROOT.screenTitle) } | ||
navController.addOnDestinationChangedListener { _, _, arguments -> | ||
screenTitle = | ||
arguments?.getInt(SCREEN_TITLE_KEY) ?: SettingsScreenKey.ROOT.screenTitle | ||
} | ||
|
||
AppTheme { | ||
Scaffold(topBar = { | ||
Toolbar( | ||
title = stringResource(id = screenTitle), | ||
hasSearch = true, | ||
onSearchQueryChange = null // TODO: Add suggestions logic | ||
) | ||
}) { padding -> | ||
NavHost( | ||
navController = navController, | ||
startDestination = SettingsScreenKey.ROOT.name, | ||
modifier = Modifier.padding(padding) | ||
) { | ||
composable( | ||
SettingsScreenKey.ROOT.name, | ||
listOf(createScreenTitleArg(SettingsScreenKey.ROOT.screenTitle)) | ||
) { | ||
SettingsScreen(onSelectSettingOption = { screen -> | ||
navController.navigate(screen.name) | ||
}) | ||
} | ||
composable( | ||
SettingsScreenKey.DEBUG.name, | ||
listOf(createScreenTitleArg(SettingsScreenKey.DEBUG.screenTitle)) | ||
) { | ||
DebugScreen(settingsViewModel) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun createScreenTitleArg(@StringRes screenTitle: Int) = navArgument(SCREEN_TITLE_KEY) { | ||
defaultValue = screenTitle | ||
} | ||
|
||
enum class SettingsScreenKey(@StringRes val screenTitle: Int) { | ||
ROOT(R.string.settings), | ||
DEBUG(R.string.settings_category_debug_title) | ||
} |
39 changes: 39 additions & 0 deletions
39
app/src/main/java/org/schabi/newpipe/settings/viewmodel/SettingsViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.schabi.newpipe.settings.viewmodel | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import androidx.core.content.ContextCompat | ||
import androidx.lifecycle.AndroidViewModel | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import org.schabi.newpipe.R | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class SettingsViewModel @Inject constructor( | ||
@ApplicationContext context: Context, | ||
private val preferenceManager: SharedPreferences | ||
) : AndroidViewModel(context.applicationContext as Application) { | ||
|
||
private var _settingsLayoutRedesignPref: Boolean | ||
get() = preferenceManager.getBoolean( | ||
ContextCompat.getString(getApplication(), R.string.settings_layout_redesign_key), false | ||
) | ||
set(value) { | ||
preferenceManager.edit().putBoolean( | ||
ContextCompat.getString(getApplication(), R.string.settings_layout_redesign_key), | ||
value | ||
).apply() | ||
} | ||
private val _settingsLayoutRedesign: MutableStateFlow<Boolean> = | ||
MutableStateFlow(_settingsLayoutRedesignPref) | ||
val settingsLayoutRedesign = _settingsLayoutRedesign.asStateFlow() | ||
|
||
fun toggleSettingsLayoutRedesign(newState: Boolean) { | ||
_settingsLayoutRedesign.value = newState | ||
_settingsLayoutRedesignPref = newState | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
app/src/main/java/org/schabi/newpipe/ui/SwitchPreference.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.schabi.newpipe.ui | ||
|
||
import androidx.annotation.StringRes | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.width | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Switch | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.text.style.TextAlign | ||
import org.schabi.newpipe.ui.theme.SizeTokens | ||
|
||
@Composable | ||
fun SwitchPreference( | ||
modifier: Modifier = Modifier, | ||
@StringRes title: Int, | ||
isChecked: Boolean, | ||
onCheckedChange: (Boolean) -> Unit, | ||
@StringRes summary: Int? = null | ||
) { | ||
Row( | ||
verticalAlignment = Alignment.CenterVertically, | ||
horizontalArrangement = Arrangement.SpaceBetween, | ||
modifier = modifier.fillMaxWidth() | ||
) { | ||
Column { | ||
Text( | ||
text = stringResource(id = title), | ||
modifier = Modifier.padding(SizeTokens.SpacingExtraSmall), | ||
style = MaterialTheme.typography.titleSmall, | ||
textAlign = TextAlign.Start, | ||
) | ||
summary?.let { | ||
Text( | ||
text = stringResource(id = summary), | ||
modifier = Modifier.padding(SizeTokens.SpacingExtraSmall), | ||
style = MaterialTheme.typography.bodySmall, | ||
textAlign = TextAlign.Start, | ||
) | ||
} | ||
} | ||
Spacer(modifier = Modifier.width(SizeTokens.SpacingSmall)) | ||
Switch(checked = isChecked, onCheckedChange = onCheckedChange) | ||
} | ||
} |
Oops, something went wrong.