diff --git a/app/src/main/java/com/bnyro/clock/domain/model/DigitalClockWidgetOptions.kt b/app/src/main/java/com/bnyro/clock/domain/model/DigitalClockWidgetOptions.kt new file mode 100644 index 00000000..ae1ccd69 --- /dev/null +++ b/app/src/main/java/com/bnyro/clock/domain/model/DigitalClockWidgetOptions.kt @@ -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 + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/bnyro/clock/presentation/widgets/DigitalClockWidget.kt b/app/src/main/java/com/bnyro/clock/presentation/widgets/DigitalClockWidget.kt index caf07e38..1ed50bac 100644 --- a/app/src/main/java/com/bnyro/clock/presentation/widgets/DigitalClockWidget.kt +++ b/app/src/main/java/com/bnyro/clock/presentation/widgets/DigitalClockWidget.kt @@ -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() { @@ -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, @@ -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) + } } diff --git a/app/src/main/java/com/bnyro/clock/presentation/widgets/DigitalClockWidgetConfig.kt b/app/src/main/java/com/bnyro/clock/presentation/widgets/DigitalClockWidgetConfig.kt index d0bf52d0..98d36eae 100644 --- a/app/src/main/java/com/bnyro/clock/presentation/widgets/DigitalClockWidgetConfig.kt +++ b/app/src/main/java/com/bnyro/clock/presentation/widgets/DigitalClockWidgetConfig.kt @@ -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 @@ -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 @@ -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) @@ -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 + ) + } + } + } } @@ -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) { diff --git a/app/src/main/java/com/bnyro/clock/util/WidgetUtils.kt b/app/src/main/java/com/bnyro/clock/util/widgets/DigitalClockWIdget.kt similarity index 72% rename from app/src/main/java/com/bnyro/clock/util/WidgetUtils.kt rename to app/src/main/java/com/bnyro/clock/util/widgets/DigitalClockWIdget.kt index 7e1e9f34..0b7c6241 100644 --- a/app/src/main/java/com/bnyro/clock/util/WidgetUtils.kt +++ b/app/src/main/java/com/bnyro/clock/util/widgets/DigitalClockWIdget.kt @@ -1,4 +1,4 @@ -package com.bnyro.clock.util +package com.bnyro.clock.util.widgets import android.appwidget.AppWidgetManager import android.content.Context @@ -7,58 +7,7 @@ import android.view.View import android.widget.RemoteViews import androidx.core.content.edit import com.bnyro.clock.R - - -private const val PREF_FILE = "WidgetConfig" -private const val PREF_SHOW_DATE = "showDate:" -private const val PREF_SHOW_TIME = "showTime:" -private const val PREF_SHOW_BACKGROUND = "showBackground:" -private const val PREF_DATE_TEXT_SIZE = "dateTextSize:" -private const val PREF_TIME_TEXT_SIZE = "timeTextSize:" -private const val PREF_TIME_ZONE = "timeZone:" -private const val PREF_TIME_ZONE_NAME = "timeZoneName:" - -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 - ) - } -} - -private val Context.widgetPreferences - get() = getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE) +import com.bnyro.clock.domain.model.DigitalClockWidgetOptions fun Context.saveDigitalClockWidgetSettings( appWidgetId: Int, @@ -121,6 +70,17 @@ fun Context.loadDigitalClockWidgetSettings( ) } +fun Context.deleteDigitalClockWidgetPref(appWidgetId: Int) = + widgetPreferences.edit { + remove(PREF_SHOW_DATE + appWidgetId) + remove(PREF_SHOW_TIME + appWidgetId) + remove(PREF_SHOW_BACKGROUND + appWidgetId) + remove(PREF_DATE_TEXT_SIZE + appWidgetId) + remove(PREF_TIME_TEXT_SIZE + appWidgetId) + remove(PREF_TIME_ZONE + appWidgetId) + remove(PREF_TIME_ZONE_NAME + appWidgetId) + } + fun Context.updateDigitalClockWidget( appWidgetId: Int, diff --git a/app/src/main/java/com/bnyro/clock/util/widgets/WidgetPreferences.kt b/app/src/main/java/com/bnyro/clock/util/widgets/WidgetPreferences.kt new file mode 100644 index 00000000..951c9abf --- /dev/null +++ b/app/src/main/java/com/bnyro/clock/util/widgets/WidgetPreferences.kt @@ -0,0 +1,17 @@ +package com.bnyro.clock.util.widgets + +import android.content.Context + +internal val PREF_FILE = "WidgetConfig" + +internal val Context.widgetPreferences + get() = getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE) + +// Digital Clock widget +internal const val PREF_SHOW_DATE = "showDate:" +internal const val PREF_SHOW_TIME = "showTime:" +internal const val PREF_SHOW_BACKGROUND = "showBackground:" +internal const val PREF_DATE_TEXT_SIZE = "dateTextSize:" +internal const val PREF_TIME_TEXT_SIZE = "timeTextSize:" +internal const val PREF_TIME_ZONE = "timeZone:" +internal const val PREF_TIME_ZONE_NAME = "timeZoneName:" \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ffdbb8f0..6542401f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -120,4 +120,5 @@ Use a different time zone for the widget Show Widget background Show Time + Digital Clock Widget \ No newline at end of file