From 3b5bf64066645a87a644f8677e67d9a2af313808 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 11 Mar 2018 23:12:36 +0100 Subject: [PATCH] properly update widget date and time --- .../clock/helpers/MyWidgetDateTimeProvider.kt | 40 ++++++++++++++++++- .../res/layout/widget_config_date_time.xml | 4 +- app/src/main/res/layout/widget_date_time.xml | 10 +++-- app/src/main/res/values/dimens.xml | 1 + 4 files changed, 48 insertions(+), 7 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/MyWidgetDateTimeProvider.kt b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/MyWidgetDateTimeProvider.kt index f276592..2da3be4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/MyWidgetDateTimeProvider.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/MyWidgetDateTimeProvider.kt @@ -2,7 +2,16 @@ package com.simplemobiletools.clock.helpers import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetProvider +import android.content.ComponentName import android.content.Context +import android.widget.RemoteViews +import com.simplemobiletools.clock.R +import com.simplemobiletools.clock.extensions.config +import com.simplemobiletools.clock.extensions.getFormattedDate +import com.simplemobiletools.clock.extensions.getFormattedTime +import com.simplemobiletools.commons.extensions.setBackgroundColor +import com.simplemobiletools.commons.extensions.setText +import java.util.* class MyWidgetDateTimeProvider : AppWidgetProvider() { @@ -11,6 +20,35 @@ class MyWidgetDateTimeProvider : AppWidgetProvider() { } private fun performUpdate(context: Context) { - + val appWidgetManager = AppWidgetManager.getInstance(context) + appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach { + RemoteViews(context.packageName, R.layout.widget_date_time).apply { + updateTexts(context, this) + updateColors(context, this) + appWidgetManager.updateAppWidget(it, this) + } + } } + + private fun updateTexts(context: Context, views: RemoteViews) { + val calendar = Calendar.getInstance() + views.apply { + setText(R.id.widget_time, context.getFormattedTime(calendar, false)) + setText(R.id.widget_date, context.getFormattedDate(calendar)) + } + } + + private fun updateColors(context: Context, views: RemoteViews) { + val config = context.config + val widgetBgColor = config.widgetBgColor + val widgetTextColor = config.widgetTextColor + + views.apply { + setBackgroundColor(R.id.widget_date_time_holder, widgetBgColor) + setTextColor(R.id.widget_time, widgetTextColor) + setTextColor(R.id.widget_date, widgetTextColor) + } + } + + private fun getComponentName(context: Context) = ComponentName(context, this::class.java) } diff --git a/app/src/main/res/layout/widget_config_date_time.xml b/app/src/main/res/layout/widget_config_date_time.xml index 9099dc7..467f4d2 100644 --- a/app/src/main/res/layout/widget_config_date_time.xml +++ b/app/src/main/res/layout/widget_config_date_time.xml @@ -19,7 +19,7 @@ android:layout_height="wrap_content" android:gravity="center_horizontal" android:includeFontPadding="false" - android:textSize="@dimen/clock_text_size" + android:textSize="@dimen/widget_time_text_size" tools:text="00:00"/> diff --git a/app/src/main/res/layout/widget_date_time.xml b/app/src/main/res/layout/widget_date_time.xml index 4a7b954..eea0e5b 100644 --- a/app/src/main/res/layout/widget_date_time.xml +++ b/app/src/main/res/layout/widget_date_time.xml @@ -4,15 +4,15 @@ xmlns:tools="http://schemas.android.com/tools" android:id="@+id/widget_date_time_holder" android:layout_width="match_parent" - android:layout_height="match_parent"> + android:layout_height="wrap_content"> diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index e014abf..ea1281b 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -9,4 +9,5 @@ 70sp 46sp 60sp + 50sp