Skip to content

Commit

Permalink
feat: Digital clock widget config improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Apr 19, 2024
1 parent ee5e444 commit 87a5a52
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 151 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.bnyro.clock.domain.model

data class DigitalClockWidgetOptions(
var showDate: Boolean = true,
var showTime: Boolean = true,
var dateTextSize: Float = DEFAULT_DATE_TEXT_SIZE,
var timeTextSize: Float = DEFAULT_TIME_TEXT_SIZE,
var timeZone: String? = null,
var timeZoneName: String = "",
var showBackground: Boolean = true
) {
companion object {
const val DEFAULT_DATE_TEXT_SIZE = 16f
const val DEFAULT_TIME_TEXT_SIZE = 52f

val dateSizeOptions = listOf(
12f,
16f,
20f,
24f,
28f,
32f
)

val timeSizeOptions = listOf(
36f,
40f,
44f,
48f,
52f,
56f,
60f,
64f,
68f,
72f,
76f,
80f
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import android.os.Build
import android.widget.RemoteViews
import com.bnyro.clock.R
import com.bnyro.clock.ui.MainActivity
import com.bnyro.clock.util.applyDigitalClockWidgetOptions
import com.bnyro.clock.util.loadDigitalClockWidgetSettings
import com.bnyro.clock.util.widgets.applyDigitalClockWidgetOptions
import com.bnyro.clock.util.widgets.deleteDigitalClockWidgetPref
import com.bnyro.clock.util.widgets.loadDigitalClockWidgetSettings

class DigitalClockWidget : AppWidgetProvider() {

Expand All @@ -19,7 +20,7 @@ class DigitalClockWidget : AppWidgetProvider() {
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
appWidgetIds.forEach { appWidgetId ->
for (appWidgetId in appWidgetIds) {
val pendingIntent: PendingIntent = PendingIntent.getActivity(
context,
0,
Expand All @@ -41,4 +42,11 @@ class DigitalClockWidget : AppWidgetProvider() {
appWidgetManager.updateAppWidget(appWidgetId, views)
}
}

override fun onDeleted(context: Context, appWidgetIds: IntArray) {
for (appWidgetId in appWidgetIds) {
context.deleteDigitalClockWidgetPref(appWidgetId)
}
super.onDeleted(context, appWidgetIds)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -23,10 +24,13 @@ import androidx.compose.material.icons.rounded.ExpandMore
import androidx.compose.material.icons.rounded.FormatSize
import androidx.compose.material.icons.rounded.Language
import androidx.compose.material3.Button
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand All @@ -42,22 +46,24 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.bnyro.clock.R
import com.bnyro.clock.domain.model.DigitalClockWidgetOptions
import com.bnyro.clock.presentation.components.SwitchItem
import com.bnyro.clock.presentation.components.SwitchWithDivider
import com.bnyro.clock.presentation.screens.clock.components.TimeZonePickerDialog
import com.bnyro.clock.presentation.screens.clock.model.ClockModel
import com.bnyro.clock.presentation.screens.settings.model.SettingsModel
import com.bnyro.clock.ui.theme.ClockYouTheme
import com.bnyro.clock.util.DigitalClockWidgetOptions
import com.bnyro.clock.util.ThemeUtil
import com.bnyro.clock.util.loadDigitalClockWidgetSettings
import com.bnyro.clock.util.saveDigitalClockWidgetSettings
import com.bnyro.clock.util.updateDigitalClockWidget
import com.bnyro.clock.util.widgets.loadDigitalClockWidgetSettings
import com.bnyro.clock.util.widgets.saveDigitalClockWidgetSettings
import com.bnyro.clock.util.widgets.updateDigitalClockWidget


class DigitalClockWidgetConfig : ComponentActivity() {

private var appWidgetId: Int = AppWidgetManager.INVALID_APPWIDGET_ID

@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -75,9 +81,33 @@ class DigitalClockWidgetConfig : ComponentActivity() {
val options = loadDigitalClockWidgetSettings(appWidgetId)
enableEdgeToEdge()
setContent {
DigitalClockWidgetSettings(
options = options, onComplete = this::complete
)
val settingsModel: SettingsModel = viewModel()
val darkTheme = when (settingsModel.themeMode) {
SettingsModel.Theme.SYSTEM -> isSystemInDarkTheme()
SettingsModel.Theme.DARK, SettingsModel.Theme.AMOLED -> true
else -> false
}
ClockYouTheme(
darkTheme = darkTheme,
customColorScheme = ThemeUtil.getSchemeFromSeed(
settingsModel.customColor,
true
)
) {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Scaffold(topBar = {
CenterAlignedTopAppBar(title = { Text(text = stringResource(R.string.digital_clock_widget)) })
}) { pV ->
DigitalClockWidgetSettings(
modifier = Modifier.padding(pV),
options = options, onComplete = this::complete
)
}
}
}
}


Expand All @@ -95,108 +125,96 @@ class DigitalClockWidgetConfig : ComponentActivity() {

@Composable
fun DigitalClockWidgetSettings(
modifier: Modifier = Modifier,
options: DigitalClockWidgetOptions,
onComplete: (DigitalClockWidgetOptions) -> Unit
) {
val settingsModel: SettingsModel = viewModel()

val clockModel: ClockModel = viewModel(factory = ClockModel.Factory)
var showTimeZoneDialog by remember { mutableStateOf(false) }

var customTimeZone by remember { mutableStateOf(options.timeZone) }
var customTimeZoneName by remember { mutableStateOf(options.timeZoneName) }

ClockYouTheme(
darkTheme = true,
customColorScheme = ThemeUtil.getSchemeFromSeed(
settingsModel.customColor,
true
)

var showDateOption by remember { mutableStateOf(options.showDate) }
var showTimeOption by remember { mutableStateOf(options.showTime) }
var showBackgroundOption by remember { mutableStateOf(options.showBackground) }
var selectedDateSize by remember { mutableStateOf(options.dateTextSize) }
var selectedTimeSize by remember { mutableStateOf(options.timeTextSize) }
Column(
modifier = modifier.padding(8.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.SpaceBetween
) {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
var showDateOption by remember { mutableStateOf(options.showDate) }
var showTimeOption by remember { mutableStateOf(options.showTime) }
var showBackgroundOption by remember { mutableStateOf(options.showBackground) }
var selectedDateSize by remember { mutableStateOf(options.dateTextSize) }
var selectedTimeSize by remember { mutableStateOf(options.timeTextSize) }
Column(
modifier = Modifier.padding(8.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.SpaceBetween
Column {
SwitchItem(
title = stringResource(R.string.show_date),
isChecked = showDateOption,
icon = Icons.Rounded.CalendarToday
) {
Column {
SwitchItem(
title = stringResource(R.string.show_date),
isChecked = showDateOption,
icon = Icons.Rounded.CalendarToday
) {
showDateOption = it
}
SwitchItem(
title = stringResource(R.string.show_time),
isChecked = showTimeOption,
icon = Icons.Rounded.CalendarToday
) {
showTimeOption = it
}
SwitchItem(
title = stringResource(R.string.show_widget_background),
isChecked = showBackgroundOption,
icon = Icons.Rounded.CalendarToday
) {
showBackgroundOption = it
}
TextSizeSelectSetting(
sizeOptions = DigitalClockWidgetOptions.dateSizeOptions,
title = stringResource(R.string.date_text_size),
currentSize = selectedDateSize
) {
selectedDateSize = it
}
TextSizeSelectSetting(
sizeOptions = DigitalClockWidgetOptions.timeSizeOptions,
title = stringResource(R.string.time_text_size),
currentSize = selectedTimeSize
) {
selectedTimeSize = it
showDateOption = it
}
SwitchItem(
title = stringResource(R.string.show_time),
isChecked = showTimeOption,
icon = Icons.Rounded.CalendarToday
) {
showTimeOption = it
}
SwitchItem(
title = stringResource(R.string.show_widget_background),
isChecked = showBackgroundOption,
icon = Icons.Rounded.CalendarToday
) {
showBackgroundOption = it
}
TextSizeSelectSetting(
sizeOptions = DigitalClockWidgetOptions.dateSizeOptions,
title = stringResource(R.string.date_text_size),
currentSize = selectedDateSize
) {
selectedDateSize = it
}
TextSizeSelectSetting(
sizeOptions = DigitalClockWidgetOptions.timeSizeOptions,
title = stringResource(R.string.time_text_size),
currentSize = selectedTimeSize
) {
selectedTimeSize = it
}
SwitchWithDivider(
title = stringResource(R.string.timezone),
description = stringResource(R.string.use_a_different_time_zone_for_the_widget),
icon = Icons.Rounded.Language,
isChecked = customTimeZone != null,
onChecked = {
if (it) {
showTimeZoneDialog = true
} else {
customTimeZone = null
}
SwitchWithDivider(
title = stringResource(R.string.timezone),
description = stringResource(R.string.use_a_different_time_zone_for_the_widget),
icon = Icons.Rounded.Language,
isChecked = customTimeZone != null,
onChecked = {
if (it) {
showTimeZoneDialog = true
} else {
customTimeZone = null
}
},
onClick = {
showTimeZoneDialog = true
}
)
},
onClick = {
showTimeZoneDialog = true
}
Button(
modifier = Modifier.padding(bottom = 16.dp),
onClick = {
options.apply {
showDate = showDateOption
showTime = showTimeOption
dateTextSize = selectedDateSize
timeTextSize = selectedTimeSize
timeZone = customTimeZone
timeZoneName = customTimeZoneName
showBackground = showBackgroundOption
}
onComplete.invoke(options)
}) {
Text(stringResource(R.string.save))
)
}
Button(
modifier = Modifier.padding(bottom = 16.dp),
onClick = {
options.apply {
showDate = showDateOption
showTime = showTimeOption
dateTextSize = selectedDateSize
timeTextSize = selectedTimeSize
timeZone = customTimeZone
timeZoneName = customTimeZoneName
showBackground = showBackgroundOption
}
}

onComplete.invoke(options)
}) {
Text(stringResource(R.string.save))
}
}
if (showTimeZoneDialog) {
Expand Down
Loading

0 comments on commit 87a5a52

Please sign in to comment.