Skip to content

Commit

Permalink
feat: more widget config options (closes #301 closes #262)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Apr 17, 2024
1 parent 6d0ce07 commit 539193e
Show file tree
Hide file tree
Showing 6 changed files with 380 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ fun TimeZoneSelectDialog(
onDismissRequest = onDismissRequest,
properties = remember { DialogProperties(usePlatformDefaultWidth = false) }) {
Surface(color = MaterialTheme.colorScheme.surface) {
var searchQuery by remember {
mutableStateOf("")
}
Column(
modifier = Modifier
.fillMaxSize()
.padding(horizontal = 8.dp)
) {
var filteredZones by remember { mutableStateOf(clockModel.timeZones) }
var searchQuery by remember {
mutableStateOf("")
}
TextField(
modifier = Modifier
.fillMaxWidth()
Expand Down Expand Up @@ -138,11 +138,78 @@ fun TimeZoneSelectDialog(
}
}

@Composable
fun TimeZonePickerDialog(
clockModel: ClockModel, onDismissRequest: () -> Unit, onSelectTimeZone: (TimeZone) -> Unit
) {
Dialog(
onDismissRequest = onDismissRequest,
properties = remember { DialogProperties(usePlatformDefaultWidth = false) }) {
Surface(color = MaterialTheme.colorScheme.surface) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(horizontal = 8.dp)
) {
var filteredZones by remember { mutableStateOf(clockModel.timeZones) }
var searchQuery by remember {
mutableStateOf("")
}
TextField(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 10.dp),
value = searchQuery,
onValueChange = {
searchQuery = it
val lowerQuery = searchQuery.lowercase()
filteredZones = clockModel.timeZones.filter {
it.countryName.lowercase()
.contains(lowerQuery) || it.zoneName.lowercase()
.contains(lowerQuery)
}
},
placeholder = { Text(stringResource(R.string.search_country_timezone)) },
colors = TextFieldDefaults.colors(
focusedContainerColor = MaterialTheme.colorScheme.secondaryContainer,
unfocusedContainerColor = MaterialTheme.colorScheme.secondaryContainer,
focusedTextColor = MaterialTheme.colorScheme.onSecondaryContainer,
unfocusedTextColor = MaterialTheme.colorScheme.onSecondaryContainer,
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent
),
shape = RoundedCornerShape(50),
leadingIcon = {
IconButton(onClick = onDismissRequest) {
Icon(
imageVector = Icons.AutoMirrored.Rounded.ArrowBackIos,
contentDescription = null
)
}
}
)

LazyColumn(Modifier.weight(1f), contentPadding = PaddingValues(horizontal = 8.dp)) {
items(filteredZones) {
TimeZoneCard(
it,
allowSelection = false,
onClick = { _ ->
onSelectTimeZone.invoke(it)
})
}
}
}
}
}
}

@Composable
private fun TimeZoneCard(
timeZone: TimeZone,
selected: Boolean,
onClick: (Boolean) -> Unit
allowSelection: Boolean = true,
selected: Boolean = false,
onClick: (Boolean) -> Unit = {}
) {
Box(
modifier = Modifier
Expand All @@ -161,21 +228,23 @@ private fun TimeZoneCard(
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically
) {
IconButton(onClick = {
onClick.invoke(!selected)
}) {
if (selected) {
Icon(
imageVector = Icons.Rounded.CheckCircle,
contentDescription = null,
tint = MaterialTheme.colorScheme.primary
)
} else {
Icon(
imageVector = Icons.Outlined.Circle,
contentDescription = null,
tint = MaterialTheme.colorScheme.primary
)
if (allowSelection) {
IconButton(onClick = {
onClick.invoke(!selected)
}) {
if (selected) {
Icon(
imageVector = Icons.Rounded.CheckCircle,
contentDescription = null,
tint = MaterialTheme.colorScheme.primary
)
} else {
Icon(
imageVector = Icons.Outlined.Circle,
contentDescription = null,
tint = MaterialTheme.colorScheme.primary
)
}
}
}
Column(Modifier.weight(1f)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import android.appwidget.AppWidgetProvider
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.TypedValue
import android.view.View
import android.widget.RemoteViews
import com.bnyro.clock.R
import com.bnyro.clock.presentation.widgets.DigitalClockWidgetConfig.Companion.InitialTextSize
import com.bnyro.clock.presentation.widgets.DigitalClockWidgetConfig.Companion.PREF_FILE
import com.bnyro.clock.ui.MainActivity
import com.bnyro.clock.util.applyDigitalClockWidgetOptions
import com.bnyro.clock.util.loadDigitalClockWidgetSettings

class DigitalClockWidget : AppWidgetProvider() {

Expand All @@ -21,7 +19,6 @@ class DigitalClockWidget : AppWidgetProvider() {
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
val sharedPreferences = context.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE)
appWidgetIds.forEach { appWidgetId ->
val pendingIntent: PendingIntent = PendingIntent.getActivity(
context,
Expand All @@ -39,19 +36,8 @@ class DigitalClockWidget : AppWidgetProvider() {
} else {
viewMapping
}
val showDate = sharedPreferences.getBoolean(
DigitalClockWidgetConfig.PREF_SHOW_DATE + appWidgetId,
true
)
val textSize = sharedPreferences.getFloat(
DigitalClockWidgetConfig.PREF_DATE_TEXT_SIZE + appWidgetId,
InitialTextSize
)
views.setTextViewTextSize(R.id.textClock, TypedValue.COMPLEX_UNIT_SP, textSize)

val visibility = if (showDate) View.VISIBLE else View.GONE
views.setViewVisibility(R.id.textClock, visibility)

val options = context.loadDigitalClockWidgetSettings(appWidgetId)
views.applyDigitalClockWidgetOptions(options)
appWidgetManager.updateAppWidget(appWidgetId, views)
}
}
Expand Down
Loading

0 comments on commit 539193e

Please sign in to comment.