mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-08 01:58:52 -06:00
Can make either the normal checkmark widget or stackview checkmark widget
This commit is contained in:
@@ -26,7 +26,7 @@ import android.view.View
|
||||
import android.widget.RemoteViews
|
||||
import org.isoron.uhabits.R
|
||||
|
||||
class StackGroupWidget(
|
||||
class CheckmarkStackWidget(
|
||||
context: Context,
|
||||
widgetId: Int,
|
||||
private val habitIds: List<Long>
|
||||
@@ -39,10 +39,10 @@ class StackGroupWidget(
|
||||
}
|
||||
|
||||
override fun getRemoteViews(width: Int, height: Int): RemoteViews {
|
||||
val remoteViews = RemoteViews(context.packageName, R.layout.widget_stackview)
|
||||
val serviceIntent = Intent(context, StackWidgetService::class.java)
|
||||
val remoteViews = RemoteViews(context.packageName, R.layout.stackview_widget)
|
||||
val serviceIntent = Intent(context, CheckmarkStackWidgetService::class.java)
|
||||
serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id)
|
||||
serviceIntent.putExtra(StackWidgetService.HABIT_IDS_SELECTED, habitIds.toLongArray())
|
||||
serviceIntent.putExtra(CheckmarkStackWidgetService.HABIT_IDS_SELECTED, habitIds.toLongArray())
|
||||
remoteViews.setRemoteAdapter(R.id.stackWidgetView, serviceIntent)
|
||||
AppWidgetManager.getInstance(context).notifyAppWidgetViewDataChanged(id, R.id.stackWidgetView)
|
||||
// TODO what should the empty view look like?
|
||||
@@ -3,11 +3,11 @@ package org.isoron.uhabits.widgets
|
||||
import android.content.Context
|
||||
import org.isoron.uhabits.HabitsApplication
|
||||
|
||||
class StackWidgetProvider : BaseWidgetProvider() {
|
||||
class CheckmarkStackWidgetProvider : BaseWidgetProvider() {
|
||||
|
||||
override fun getWidgetFromId(context: Context, id: Int): StackGroupWidget {
|
||||
override fun getWidgetFromId(context: Context, id: Int): CheckmarkStackWidget {
|
||||
val habitIds = getHabitGroupFromWidget(context, id)
|
||||
return StackGroupWidget(context, id, habitIds)
|
||||
return CheckmarkStackWidget(context, id, habitIds)
|
||||
}
|
||||
|
||||
private fun getHabitGroupFromWidget(context: Context, widgetId: Int) : List<Long> {
|
||||
@@ -19,25 +19,25 @@ import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH;
|
||||
import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT;
|
||||
import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH;
|
||||
import static org.isoron.androidbase.utils.InterfaceUtils.dpToPixels;
|
||||
import static org.isoron.uhabits.widgets.StackWidgetService.HABIT_IDS_SELECTED;
|
||||
import static org.isoron.uhabits.widgets.CheckmarkStackWidgetService.HABIT_IDS_SELECTED;
|
||||
|
||||
public class StackWidgetService extends RemoteViewsService {
|
||||
public class CheckmarkStackWidgetService extends RemoteViewsService {
|
||||
|
||||
public static final String HABIT_IDS_SELECTED = "HABIT_IDS_SELECTED";
|
||||
|
||||
@Override
|
||||
public RemoteViewsFactory onGetViewFactory(Intent intent) {
|
||||
return new StackRemoteViewsFactory(this.getApplicationContext(), intent);
|
||||
return new CheckmarkStackRemoteViewsFactory(this.getApplicationContext(), intent);
|
||||
}
|
||||
}
|
||||
|
||||
class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
|
||||
class CheckmarkStackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
|
||||
private Context mContext;
|
||||
private int mAppWidgetId;
|
||||
private ArrayList<Habit> mHabitList;
|
||||
private List<Long> mHabitsSelected;
|
||||
|
||||
public StackRemoteViewsFactory(Context context, Intent intent) {
|
||||
public CheckmarkStackRemoteViewsFactory(Context context, Intent intent) {
|
||||
mContext = context;
|
||||
mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
|
||||
mHabitsSelected = new ArrayList<>();
|
||||
@@ -19,10 +19,23 @@
|
||||
package org.isoron.uhabits.widgets
|
||||
|
||||
import android.content.*
|
||||
import org.isoron.uhabits.HabitsApplication
|
||||
|
||||
class CheckmarkWidgetProvider : BaseWidgetProvider() {
|
||||
override fun getWidgetFromId(context: Context, id: Int): CheckmarkWidget {
|
||||
val habit = getHabitFromWidgetId(id)
|
||||
return CheckmarkWidget(context, id, habit)
|
||||
override fun getWidgetFromId(context: Context, id: Int): BaseWidget {
|
||||
try {
|
||||
val habit = getHabitFromWidgetId(id)
|
||||
return CheckmarkWidget(context, id, habit)
|
||||
} catch (e: Exception) {
|
||||
val habitIds = getHabitIdsGroupFromWidget(context, id)
|
||||
return CheckmarkStackWidget(context, id, habitIds)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getHabitIdsGroupFromWidget(context: Context, widgetId: Int) : List<Long> {
|
||||
val app = context.getApplicationContext() as HabitsApplication
|
||||
val widgetPrefs = app.component.widgetPreferences
|
||||
val habitIds = widgetPrefs.getHabitIdsGroupFromWidgetId(widgetId)
|
||||
return habitIds
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,7 +71,14 @@ class HabitGroupPickerDialog : Activity(), AdapterView.OnItemClickListener {
|
||||
}
|
||||
with(findViewById(R.id.doneConfigureButton) as Button) {
|
||||
setOnClickListener {
|
||||
if (!habitIdsSelected.isEmpty()) {
|
||||
if (habitIdsSelected.size == 1) {
|
||||
preferences.addWidget(widgetId, habitIdsSelected.first())
|
||||
widgetUpdater.updateWidgets()
|
||||
setResult(Activity.RESULT_OK, Intent().apply {
|
||||
putExtra(EXTRA_APPWIDGET_ID, widgetId)
|
||||
})
|
||||
finish()
|
||||
} else if (!habitIdsSelected.isEmpty()) {
|
||||
preferences.addWidget(widgetId, habitIdsSelected.toString())
|
||||
widgetUpdater.updateWidgets()
|
||||
setResult(Activity.RESULT_OK, Intent().apply {
|
||||
|
||||
@@ -61,7 +61,6 @@ class WidgetUpdater
|
||||
fun updateWidgets() {
|
||||
taskRunner.execute {
|
||||
updateWidgets(CheckmarkWidgetProvider::class.java)
|
||||
updateWidgets(StackWidgetProvider::class.java)
|
||||
updateWidgets(HistoryWidgetProvider::class.java)
|
||||
updateWidgets(ScoreWidgetProvider::class.java)
|
||||
updateWidgets(StreakWidgetProvider::class.java)
|
||||
|
||||
Reference in New Issue
Block a user