properly update widget date and time

This commit is contained in:
tibbi 2018-03-11 23:12:36 +01:00
parent 9bb9caf7c5
commit 3b5bf64066
4 changed files with 48 additions and 7 deletions

View File

@ -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)
}

View File

@ -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"/>
<TextView
@ -30,7 +30,7 @@
android:gravity="center_horizontal"
android:includeFontPadding="false"
android:paddingBottom="@dimen/small_margin"
android:textSize="@dimen/big_text_size"
android:textSize="@dimen/normal_text_size"
tools:text="Mon, 1 January"/>
</RelativeLayout>

View File

@ -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">
<TextView
android:id="@+id/widget_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/big_margin"
android:gravity="center_horizontal"
android:textSize="@dimen/clock_text_size"
android:includeFontPadding="false"
android:textSize="@dimen/widget_time_text_size"
tools:text="00:00"/>
<TextView
@ -21,7 +21,9 @@
android:layout_height="wrap_content"
android:layout_below="@+id/widget_time"
android:gravity="center_horizontal"
android:textSize="@dimen/big_text_size"
android:includeFontPadding="false"
android:paddingBottom="@dimen/normal_margin"
android:textSize="@dimen/normal_text_size"
tools:text="Mon, 1 January"/>
</RelativeLayout>

View File

@ -9,4 +9,5 @@
<dimen name="clock_text_size">70sp</dimen>
<dimen name="alarm_text_size">46sp</dimen>
<dimen name="stopwatch_text_size">60sp</dimen>
<dimen name="widget_time_text_size">50sp</dimen>
</resources>