mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Assign habits to widgets; refresh on database change
This commit is contained in:
@@ -16,6 +16,9 @@
|
||||
|
||||
package org.isoron.uhabits;
|
||||
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.net.Uri;
|
||||
@@ -108,5 +111,18 @@ public class MainActivity extends ReplayableActivity
|
||||
public void onPostExecuteCommand(Long refreshKey)
|
||||
{
|
||||
listHabitsFragment.onPostExecuteCommand(refreshKey);
|
||||
updateWidgets(this);
|
||||
}
|
||||
|
||||
public static void updateWidgets(Context context)
|
||||
{
|
||||
ComponentName provider = new ComponentName(context, SmallWidgetProvider.class);
|
||||
|
||||
Intent intent = new Intent(context, SmallWidgetProvider.class);
|
||||
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
|
||||
int ids[] = AppWidgetManager.getInstance(context).getAppWidgetIds(provider);
|
||||
|
||||
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
|
||||
context.sendBroadcast(intent);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,6 +103,8 @@ public class ReminderAlarmReceiver extends BroadcastReceiver
|
||||
habit.toggleRepetition(timestamp);
|
||||
habit.save();
|
||||
dismissNotification(context, habit);
|
||||
|
||||
MainActivity.updateWidgets(context);
|
||||
}
|
||||
|
||||
private void dismissAllHabits()
|
||||
|
||||
@@ -20,9 +20,9 @@ import android.appwidget.AppWidgetManager;
|
||||
import android.appwidget.AppWidgetProvider;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.util.Log;
|
||||
import android.widget.RemoteViews;
|
||||
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
import org.isoron.uhabits.views.SmallWidgetView;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
@@ -41,11 +41,14 @@ public class SmallWidgetProvider extends AppWidgetProvider
|
||||
{
|
||||
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.small_widget);
|
||||
|
||||
Habit habit = Habit.get((long) widgetId);
|
||||
|
||||
SmallWidgetView widgetView = new SmallWidgetView(context);
|
||||
widgetView.setDrawingCacheEnabled(true);
|
||||
widgetView.measure(200, 200);
|
||||
widgetView.layout(0, 0, 200, 200);
|
||||
widgetView.buildDrawingCache(true);
|
||||
widgetView.setHabit(habit);
|
||||
|
||||
Bitmap drawingCache = widgetView.getDrawingCache();
|
||||
|
||||
@@ -58,8 +61,8 @@ public class SmallWidgetProvider extends AppWidgetProvider
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Log.d("SmallWidgetProvider", drawingCache.toString());
|
||||
;
|
||||
remoteViews.setTextViewText(R.id.tvName, habit.name);
|
||||
remoteViews.setImageViewBitmap(R.id.imageView, drawingCache);
|
||||
manager.updateAppWidget(widgetId, remoteViews);
|
||||
}
|
||||
|
||||
@@ -381,6 +381,24 @@ public class Habit extends Model
|
||||
.executeSingle();
|
||||
}
|
||||
|
||||
public int getCurrentCheckmarkStatus()
|
||||
{
|
||||
updateCheckmarks();
|
||||
Checkmark c = getNewestCheckmark();
|
||||
|
||||
if(c != null) return c.value;
|
||||
else return 0;
|
||||
}
|
||||
|
||||
public int getCurrentStarStatus()
|
||||
{
|
||||
int score = getScore();
|
||||
|
||||
if(score >= FULL_STAR_CUTOFF) return 2;
|
||||
else if(score >= HALF_STAR_CUTOFF) return 1;
|
||||
else return 0;
|
||||
}
|
||||
|
||||
public int getRepsCount(int days)
|
||||
{
|
||||
long timeTo = DateHelper.getStartOfToday();
|
||||
|
||||
@@ -31,6 +31,7 @@ import android.view.View;
|
||||
|
||||
import org.isoron.helpers.ColorHelper;
|
||||
import org.isoron.uhabits.R;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
|
||||
public class SmallWidgetView extends View
|
||||
{
|
||||
@@ -84,12 +85,18 @@ public class SmallWidgetView extends View
|
||||
fa_full_star = context.getString(R.string.fa_star);
|
||||
|
||||
primaryColor = ColorHelper.palette[10];
|
||||
grey = Color.rgb(150, 150, 150);
|
||||
grey = Color.rgb(175, 175, 175);
|
||||
|
||||
textBounds = new Rect();
|
||||
check_status = 0;
|
||||
star_status = 0;
|
||||
}
|
||||
|
||||
check_status = 2;
|
||||
star_status = 2;
|
||||
public void setHabit(Habit habit)
|
||||
{
|
||||
this.check_status = habit.getCurrentCheckmarkStatus();
|
||||
this.star_status = habit.getCurrentStarStatus();
|
||||
this.primaryColor = habit.color;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvName"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textColor="@color/white"
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
android:initialLayout="@layout/small_widget"
|
||||
android:previewImage="@mipmap/ic_small_widget_preview"
|
||||
android:resizeMode="none"
|
||||
android:updatePeriodMillis="3600000"
|
||||
android:updatePeriodMillis="60000"
|
||||
android:widgetCategory="home_screen">
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user