diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index fa7b2065..45d85d4a 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -98,7 +98,7 @@
-
diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/widgets/WidgetConfigActivity.java b/app/src/main/java/pl/szczodrzynski/edziennik/ui/widgets/WidgetConfigActivity.java
index 06014abf..bd92ee5a 100644
--- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/widgets/WidgetConfigActivity.java
+++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/widgets/WidgetConfigActivity.java
@@ -27,9 +27,9 @@ import pl.szczodrzynski.edziennik.App;
import pl.szczodrzynski.edziennik.R;
import pl.szczodrzynski.edziennik.data.db.entity.Profile;
import pl.szczodrzynski.edziennik.databinding.DialogWidgetConfigBinding;
+import pl.szczodrzynski.edziennik.ui.widgets.luckynumber.WidgetLuckyNumberProvider;
import pl.szczodrzynski.edziennik.ui.widgets.notifications.WidgetNotificationsProvider;
import pl.szczodrzynski.edziennik.ui.widgets.timetable.WidgetTimetableProvider;
-import pl.szczodrzynski.edziennik.widgets.luckynumber.WidgetLuckyNumber;
import static pl.szczodrzynski.edziennik.ExtensionsKt.filterOutArchived;
@@ -150,7 +150,7 @@ public class WidgetConfigActivity extends Activity {
refreshIntent = new Intent(app.getContext(), WidgetNotificationsProvider.class);
break;
case WIDGET_LUCKY_NUMBER:
- refreshIntent = new Intent(app.getContext(), WidgetLuckyNumber.class);
+ refreshIntent = new Intent(app.getContext(), WidgetLuckyNumberProvider.class);
break;
}
refreshIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/widgets/luckynumber/WidgetLuckyNumberProvider.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/widgets/luckynumber/WidgetLuckyNumberProvider.kt
new file mode 100644
index 00000000..014f5bbe
--- /dev/null
+++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/widgets/luckynumber/WidgetLuckyNumberProvider.kt
@@ -0,0 +1,139 @@
+/*
+ * Copyright (c) Kuba SzczodrzyĆski 2020-1-7.
+ */
+
+package pl.szczodrzynski.edziennik.ui.widgets.luckynumber
+
+import android.app.PendingIntent
+import android.appwidget.AppWidgetManager
+import android.appwidget.AppWidgetProvider
+import android.content.Context
+import android.content.Intent
+import android.os.Bundle
+import android.view.View
+import android.widget.RemoteViews
+import pl.szczodrzynski.edziennik.App
+import pl.szczodrzynski.edziennik.MainActivity
+import pl.szczodrzynski.edziennik.R
+import pl.szczodrzynski.edziennik.getJsonObject
+import pl.szczodrzynski.edziennik.ui.widgets.WidgetConfig
+import pl.szczodrzynski.edziennik.utils.Utils
+import pl.szczodrzynski.edziennik.utils.models.Date
+
+class WidgetLuckyNumberProvider : AppWidgetProvider() {
+ companion object {
+ private const val TAG = "WidgetLuckyNumberProvider"
+ }
+
+ private fun getRemoteViews(app: App, config: WidgetConfig): RemoteViews {
+ return if (config.bigStyle) {
+ RemoteViews(app.packageName, if (config.darkTheme) R.layout.widget_lucky_number_dark_big else R.layout.widget_lucky_number_big)
+ } else {
+ RemoteViews(app.packageName, if (config.darkTheme) R.layout.widget_lucky_number_dark else R.layout.widget_lucky_number)
+ }
+ }
+
+ override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
+ val app = context.applicationContext as App
+ val widgetConfigs = app.config.widgetConfigs
+ for (appWidgetId in appWidgetIds) {
+ val config = widgetConfigs.getJsonObject(appWidgetId.toString())?.let { app.gson.fromJson(it, WidgetConfig::class.java) } ?: continue
+
+ val views = getRemoteViews(app, config)
+
+ val today = Date.getToday()
+ val todayValue = today.value
+
+ val tomorrow = Date.getToday().stepForward(0, 0, 1)
+ val tomorrowValue = tomorrow.value
+
+ val profile = app.db.profileDao().getByIdNow(config.profileId)
+ val luckyNumber = app.db.luckyNumberDao().getNearestFutureNow(config.profileId, todayValue)
+ val isYours = luckyNumber?.number == profile?.studentNumber
+
+ var noNumberText = false
+
+ if (profile != null) {
+ views.setTextViewText(R.id.widgetLuckyNumberProfileRight, profile.name)
+ views.setTextViewText(R.id.widgetLuckyNumberProfileBottom, profile.name)
+ }
+
+ if (profile == null || luckyNumber == null || luckyNumber.number == -1) {
+ noNumberText = true
+ views.setTextViewText(R.id.widgetLuckyNumberTextRight, null)
+ views.setTextViewText(R.id.widgetLuckyNumberTextBottom, null)
+ }
+ else {
+ views.setTextViewText(R.id.widgetLuckyNumberTextRight, luckyNumber.number.toString())
+ views.setTextViewText(R.id.widgetLuckyNumberTextBottom, luckyNumber.number.toString())
+ }
+
+ val drawableRes = when {
+ luckyNumber == null || luckyNumber.number == -1 -> R.drawable.emoji_sad
+ isYours -> R.drawable.emoji_glasses
+ !isYours -> R.drawable.emoji_smiling
+ else -> R.drawable.emoji_no_face
+ }
+
+ views.setViewVisibility(R.id.widgetLuckyNumberTextRightLayout, if (noNumberText) View.GONE else View.VISIBLE)
+ views.setViewVisibility(R.id.widgetLuckyNumberTextBottomLayout, if (noNumberText) View.GONE else View.VISIBLE)
+ views.setImageViewResource(R.id.widgetLuckyNumberIcon, drawableRes)
+
+ updateLayout(config, views, appWidgetManager, appWidgetId)
+
+ val openIntent = Intent(context, MainActivity::class.java)
+ openIntent.action = Intent.ACTION_MAIN
+ openIntent.putExtra("fragmentId", MainActivity.DRAWER_ITEM_HOME)
+ val openPendingIntent = PendingIntent.getActivity(context, 0, openIntent, 0)
+ views.setOnClickPendingIntent(R.id.widgetLuckyNumberRoot, openPendingIntent)
+
+ appWidgetManager.updateAppWidget(appWidgetId, views)
+ }
+ }
+
+ private fun updateLayout(config: WidgetConfig, views: RemoteViews, appWidgetManager: AppWidgetManager, appWidgetId: Int) {
+ val options = appWidgetManager.getAppWidgetOptions(appWidgetId)
+ val minWidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH)
+ val minHeight = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT)
+ val width = Utils.getCellsForSize(minWidth)
+ val height = Utils.getCellsForSize(minHeight)
+ when (width) {
+ 1 -> {
+ views.setViewVisibility(R.id.widgetLuckyNumberProfileRight, View.GONE)
+ views.setViewVisibility(R.id.widgetLuckyNumberProfileBottom, View.GONE)
+ views.setViewVisibility(R.id.widgetLuckyNumberTextRight, View.GONE)
+ views.setViewVisibility(R.id.widgetLuckyNumberTextBottom, View.VISIBLE)
+ }
+ 2 -> {
+ views.setViewVisibility(R.id.widgetLuckyNumberProfileRight, View.GONE)
+ views.setViewVisibility(R.id.widgetLuckyNumberProfileBottom, View.VISIBLE)
+ views.setViewVisibility(R.id.widgetLuckyNumberTextRight, View.VISIBLE)
+ views.setViewVisibility(R.id.widgetLuckyNumberTextBottom, View.GONE)
+ }
+ else -> {
+ views.setViewVisibility(R.id.widgetLuckyNumberProfileRight, if (config.bigStyle) View.GONE else View.VISIBLE)
+ views.setViewVisibility(R.id.widgetLuckyNumberProfileBottom, if (config.bigStyle) View.VISIBLE else View.GONE)
+ views.setViewVisibility(R.id.widgetLuckyNumberTextRight, View.VISIBLE)
+ views.setViewVisibility(R.id.widgetLuckyNumberTextBottom, View.GONE)
+ }
+ }
+ }
+
+ override fun onAppWidgetOptionsChanged(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int, newOptions: Bundle?) {
+ val app = context.applicationContext as App
+ val widgetConfigs = app.config.widgetConfigs
+ val config = widgetConfigs.getJsonObject(appWidgetId.toString())?.let { app.gson.fromJson(it, WidgetConfig::class.java) } ?: return
+ val views: RemoteViews = getRemoteViews(app, config)
+ updateLayout(config, views, appWidgetManager, appWidgetId)
+ appWidgetManager.updateAppWidget(appWidgetId, views)
+ }
+
+ override fun onDeleted(context: Context, appWidgetIds: IntArray) {
+ val app = context.applicationContext as App
+ val widgetConfigs = app.config.widgetConfigs
+ appWidgetIds.forEach {
+ widgetConfigs.remove(it.toString())
+ }
+ app.config.widgetConfigs = widgetConfigs
+ }
+}
diff --git a/app/src/main/res/layout/widget_lucky_number.xml b/app/src/main/res/layout/widget_lucky_number.xml
index e554435d..a0da5179 100644
--- a/app/src/main/res/layout/widget_lucky_number.xml
+++ b/app/src/main/res/layout/widget_lucky_number.xml
@@ -6,7 +6,10 @@
android:layout_height="match_parent"
android:background="@drawable/widget_background"
android:gravity="center"
- android:orientation="vertical">
+ android:orientation="vertical"
+ tools:layout_width="200dp"
+ tools:layout_height="100dp"
+ tools:layout_gravity="center">
+ tools:srcCompat="@drawable/emoji_no_face" />