@@ -13,8 +13,13 @@ import androidx.compose.foundation.shape.CircleShape
1313import androidx.compose.material3.*
1414import androidx.compose.runtime.Composable
1515import androidx.compose.runtime.Stable
16+ import androidx.compose.runtime.collectAsState
17+ import androidx.compose.runtime.getValue
18+ import androidx.compose.runtime.mutableStateOf
1619import androidx.compose.runtime.remember
1720import androidx.compose.runtime.rememberCoroutineScope
21+ import androidx.compose.runtime.saveable.rememberSaveable
22+ import androidx.compose.runtime.setValue
1823import androidx.compose.ui.Alignment
1924import androidx.compose.ui.Modifier
2025import androidx.compose.ui.draw.clip
@@ -32,7 +37,10 @@ import com.w2sv.androidutils.extensions.playStoreUrl
3237import com.w2sv.androidutils.extensions.showToast
3338import com.w2sv.wifiwidget.BuildConfig
3439import com.w2sv.wifiwidget.R
40+ import com.w2sv.wifiwidget.activities.HomeActivity
41+ import com.w2sv.wifiwidget.ui.DialogButton
3542import com.w2sv.wifiwidget.ui.JostText
43+ import com.w2sv.wifiwidget.ui.ThemeSelectionRow
3644import com.w2sv.wifiwidget.ui.WifiWidgetTheme
3745import kotlinx.coroutines.launch
3846
@@ -48,9 +56,39 @@ private fun NavigationDrawerPrev() {
4856
4957@OptIn(ExperimentalMaterial3Api ::class )
5058@Composable
51- fun NavigationDrawer (modifier : Modifier = Modifier , initialValue : DrawerValue = DrawerValue .Closed , content : @Composable (DrawerState ) -> Unit ) {
59+ fun NavigationDrawer (
60+ modifier : Modifier = Modifier ,
61+ initialValue : DrawerValue = DrawerValue .Closed ,
62+ viewModel : HomeActivity .ViewModel = androidx.lifecycle.viewmodel.compose.viewModel(),
63+ content : @Composable (DrawerState ) -> Unit
64+ ) {
5265 val drawerState = rememberDrawerState(initialValue = initialValue)
5366 val scope = rememberCoroutineScope()
67+ val context = LocalContext .current
68+
69+ var showThemeDialog by rememberSaveable {
70+ mutableStateOf(false )
71+ }
72+
73+ val theme by viewModel.theme.collectAsState()
74+ val themeRequiringUpdate by viewModel.themeRequiringUpdate.collectAsState()
75+
76+ if (showThemeDialog) {
77+ ThemeDialog (
78+ onDismissRequest = {
79+ viewModel.resetTheme()
80+ showThemeDialog = false
81+ },
82+ theme = { theme },
83+ onThemeSelected = { viewModel.theme.value = it },
84+ applyButtonEnabled = { themeRequiringUpdate },
85+ onApplyButtonPress = {
86+ viewModel.updateTheme()
87+ showThemeDialog = false
88+ context.showToast(" Updated Theme" )
89+ }
90+ )
91+ }
5492
5593 val closeDrawer: () -> Unit = {
5694 scope.launch {
@@ -63,7 +101,10 @@ fun NavigationDrawer(modifier: Modifier = Modifier, initialValue: DrawerValue =
63101 ModalNavigationDrawer (
64102 modifier = modifier,
65103 drawerContent = {
66- NavigationDrawerContent (closeDrawer)
104+ NavigationDrawerContent (
105+ closeDrawer = closeDrawer,
106+ onItemThemePressed = { showThemeDialog = true }
107+ )
67108 },
68109 drawerState = drawerState
69110 ) {
@@ -73,7 +114,7 @@ fun NavigationDrawer(modifier: Modifier = Modifier, initialValue: DrawerValue =
73114
74115@OptIn(ExperimentalMaterial3Api ::class )
75116@Composable
76- private fun NavigationDrawerContent (closeDrawer : () -> Unit ) {
117+ private fun NavigationDrawerContent (closeDrawer : () -> Unit , onItemThemePressed : () -> Unit ) {
77118 ModalDrawerSheet {
78119 Column (
79120 modifier = Modifier
@@ -97,7 +138,47 @@ private fun NavigationDrawerContent(closeDrawer: () -> Unit) {
97138 .padding(bottom = 12 .dp)
98139 )
99140 remember {
100- NavigationDrawerItemProperties .get()
141+ listOf (
142+ NavigationDrawerItem (
143+ R .drawable.ic_nightlight_24,
144+ R .string.theme
145+ ) {
146+ onItemThemePressed()
147+ },
148+ NavigationDrawerItem (
149+ R .drawable.ic_share_24,
150+ R .string.share
151+ ) {
152+ ShareCompat .IntentBuilder (it)
153+ .setType(" text/plain" )
154+ .setText(" Check out WiFi Widget!\n ${it.playStoreUrl} " )
155+ .setChooserTitle(" Choose an app" )
156+ .startChooser()
157+ },
158+ NavigationDrawerItem (
159+ R .drawable.ic_star_rate_24,
160+ R .string.rate
161+ ) {
162+ try {
163+ it.startActivity(
164+ Intent (
165+ Intent .ACTION_VIEW ,
166+ Uri .parse(it.playStoreUrl)
167+ )
168+ .setPackage(" com.android.vending" )
169+ )
170+ } catch (e: ActivityNotFoundException ) {
171+ it.showToast(" You're not signed into the Play Store \uD83E\uDD14 " )
172+ }
173+ },
174+ NavigationDrawerItem (
175+ R .drawable.ic_github_24,
176+ R .string.code
177+ ) {
178+ it
179+ .openUrl(" https://github.com/w2sv/WiFi-Widget" )
180+ }
181+ )
101182 }
102183 .forEach {
103184 NavigationDrawerItem (properties = it, closeDrawer = closeDrawer)
@@ -115,45 +196,15 @@ fun VersionText(modifier: Modifier = Modifier) {
115196}
116197
117198@Stable
118- private data class NavigationDrawerItemProperties (
199+ private data class NavigationDrawerItem (
119200 @DrawableRes val icon : Int ,
120201 @StringRes val label : Int ,
121202 val callback : (Context ) -> Unit
122- ) {
123- companion object {
124- fun get (): List <NavigationDrawerItemProperties > =
125- listOf (
126- NavigationDrawerItemProperties (R .drawable.ic_share_24, R .string.share) {
127- ShareCompat .IntentBuilder (it)
128- .setType(" text/plain" )
129- .setText(" Check out WiFi Widget!\n ${it.playStoreUrl} " )
130- .setChooserTitle(" Choose an app" )
131- .startChooser()
132- },
133- NavigationDrawerItemProperties (R .drawable.ic_star_rate_24, R .string.rate) {
134- try {
135- it.startActivity(
136- Intent (
137- Intent .ACTION_VIEW ,
138- Uri .parse(it.playStoreUrl)
139- )
140- .setPackage(" com.android.vending" )
141- )
142- } catch (e: ActivityNotFoundException ) {
143- it.showToast(" You're not signed into the Play Store \uD83E\uDD14 " )
144- }
145- },
146- NavigationDrawerItemProperties (R .drawable.ic_github_24, R .string.code) {
147- it
148- .openUrl(" https://github.com/w2sv/WiFi-Widget" )
149- }
150- )
151- }
152- }
203+ )
153204
154205@Composable
155206private fun NavigationDrawerItem (
156- properties : NavigationDrawerItemProperties ,
207+ properties : NavigationDrawerItem ,
157208 closeDrawer : () -> Unit
158209) {
159210 val context = LocalContext .current
@@ -183,4 +234,46 @@ private fun NavigationDrawerItem(
183234 fontWeight = FontWeight .Medium
184235 )
185236 }
237+ }
238+
239+ @Preview
240+ @Composable
241+ fun ThemeDialogPrev () {
242+ WifiWidgetTheme {
243+ ThemeDialog (
244+ onDismissRequest = { /* TODO*/ },
245+ theme = { 1 },
246+ onThemeSelected = {},
247+ applyButtonEnabled = { true },
248+ onApplyButtonPress = {}
249+ )
250+ }
251+ }
252+
253+ @Composable
254+ private fun ThemeDialog (
255+ onDismissRequest : () -> Unit ,
256+ theme : () -> Int ,
257+ onThemeSelected : (Int ) -> Unit ,
258+ applyButtonEnabled : () -> Boolean ,
259+ onApplyButtonPress : () -> Unit
260+ ) {
261+ AlertDialog (
262+ onDismissRequest = onDismissRequest,
263+ title = { JostText (text = stringResource(id = R .string.theme)) },
264+ icon = { Icon (painter = painterResource(id = R .drawable.ic_nightlight_24), contentDescription = " @null" , tint = MaterialTheme .colorScheme.primary)},
265+ confirmButton = {
266+ DialogButton (onClick = { onApplyButtonPress() }, enabled = applyButtonEnabled()) {
267+ JostText (text = stringResource(id = R .string.apply))
268+ }
269+ },
270+ dismissButton = {
271+ DialogButton (onClick = onDismissRequest) {
272+ JostText (text = stringResource(id = R .string.cancel))
273+ }
274+ },
275+ text = {
276+ ThemeSelectionRow (selected = theme, onSelected = onThemeSelected)
277+ }
278+ )
186279}
0 commit comments