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