diff --git a/uhabits-android/src/androidTest/java/org/isoron/uhabits/widgets/ScoreWidgetTest.java b/uhabits-android/src/androidTest/java/org/isoron/uhabits/widgets/ScoreWidgetTest.java index 82f2c451f..bdf9f658f 100644 --- a/uhabits-android/src/androidTest/java/org/isoron/uhabits/widgets/ScoreWidgetTest.java +++ b/uhabits-android/src/androidTest/java/org/isoron/uhabits/widgets/ScoreWidgetTest.java @@ -45,7 +45,7 @@ public class ScoreWidgetTest extends BaseViewTest setTheme(R.style.TransparentWidgetTheme); habit = fixtures.createLongHabit(); - ScoreWidget widget = new ScoreWidget(targetContext, 0, habit); + ScoreWidget widget = new ScoreWidget(targetContext, 0, habit, prefs); view = convertToView(widget, 400, 400); } diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/CheckmarkWidget.java b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/CheckmarkWidget.java deleted file mode 100644 index 4ca00133b..000000000 --- a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/CheckmarkWidget.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (C) 2016 Álinson Santos Xavier - * - * This file is part of Loop Habit Tracker. - * - * Loop Habit Tracker is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Loop Habit Tracker is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -package org.isoron.uhabits.widgets; - -import android.app.*; -import android.content.*; -import android.support.annotation.*; -import android.view.*; - -import org.isoron.uhabits.core.models.*; -import org.isoron.uhabits.utils.*; -import org.isoron.uhabits.widgets.views.*; - -public class CheckmarkWidget extends BaseWidget -{ - @NonNull - private final Habit habit; - - public CheckmarkWidget(@NonNull Context context, - int widgetId, - @NonNull Habit habit) - { - super(context, widgetId); - this.habit = habit; - } - - @Override - public PendingIntent getOnClickPendingIntent(Context context) - { - return pendingIntentFactory.toggleCheckmark(habit, null); - } - - @Override - public void refreshData(View v) - { - CheckmarkWidgetView view = (CheckmarkWidgetView) v; - int color = PaletteUtils.getColor(getContext(), habit.getColor()); - double score = habit.getScores().getTodayValue(); - float percentage = (float) score; - int checkmark = habit.getCheckmarks().getTodayValue(); - - view.setPercentage(percentage); - view.setActiveColor(color); - view.setName(habit.getName()); - view.setCheckmarkValue(checkmark); - view.refresh(); - } - - @Override - protected View buildView() - { - return new CheckmarkWidgetView(getContext()); - } - - @Override - protected int getDefaultHeight() - { - return 125; - } - - @Override - protected int getDefaultWidth() - { - return 125; - } -} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/CheckmarkWidget.kt b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/CheckmarkWidget.kt new file mode 100644 index 000000000..53316ad9c --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/CheckmarkWidget.kt @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.uhabits.widgets + +import android.content.* +import android.view.* +import org.isoron.uhabits.core.models.* +import org.isoron.uhabits.utils.* +import org.isoron.uhabits.widgets.views.* + +class CheckmarkWidget( + context: Context, + widgetId: Int, + private val habit: Habit +) : BaseWidget(context, widgetId) { + + override fun getOnClickPendingIntent(context: Context) = + pendingIntentFactory.toggleCheckmark(habit, null) + + override fun refreshData(v: View) { + (v as CheckmarkWidgetView).apply { + setPercentage(habit.scores.todayValue.toFloat()) + setActiveColor(PaletteUtils.getColor(context, habit.color)) + setName(habit.name) + setCheckmarkValue(habit.checkmarks.todayValue) + refresh() + } + } + + override fun buildView() = CheckmarkWidgetView(context) + override fun getDefaultHeight() = 125 + override fun getDefaultWidth() = 125 +} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/CheckmarkWidgetProvider.java b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/CheckmarkWidgetProvider.java deleted file mode 100644 index 3b798bd72..000000000 --- a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/CheckmarkWidgetProvider.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2016 Álinson Santos Xavier - * - * This file is part of Loop Habit Tracker. - * - * Loop Habit Tracker is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Loop Habit Tracker is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ -package org.isoron.uhabits.widgets; - -import android.content.*; -import android.support.annotation.*; - -import org.isoron.uhabits.core.models.*; - -public class CheckmarkWidgetProvider extends BaseWidgetProvider -{ - @NonNull - @Override - protected CheckmarkWidget getWidgetFromId(@NonNull Context context, int id) - { - Habit habit = getHabitFromWidgetId(id); - return new CheckmarkWidget(context, id, habit); - } -} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/StreakWidgetProvider.java b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/CheckmarkWidgetProvider.kt similarity index 65% rename from uhabits-android/src/main/java/org/isoron/uhabits/widgets/StreakWidgetProvider.java rename to uhabits-android/src/main/java/org/isoron/uhabits/widgets/CheckmarkWidgetProvider.kt index a097f7bb6..9026887b8 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/StreakWidgetProvider.java +++ b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/CheckmarkWidgetProvider.kt @@ -16,20 +16,13 @@ * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ -package org.isoron.uhabits.widgets; +package org.isoron.uhabits.widgets -import android.content.*; -import android.support.annotation.*; +import android.content.* -import org.isoron.uhabits.core.models.*; - -public class StreakWidgetProvider extends BaseWidgetProvider -{ - @NonNull - @Override - protected BaseWidget getWidgetFromId(@NonNull Context context, int id) - { - Habit habit = getHabitFromWidgetId(id); - return new StreakWidget(context, id, habit); +class CheckmarkWidgetProvider : BaseWidgetProvider() { + override fun getWidgetFromId(context: Context, id: Int): CheckmarkWidget { + val habit = getHabitFromWidgetId(id) + return CheckmarkWidget(context, id, habit) } } diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/FrequencyWidget.java b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/FrequencyWidget.java deleted file mode 100644 index 779980ae7..000000000 --- a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/FrequencyWidget.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (C) 2016 Álinson Santos Xavier - * - * This file is part of Loop Habit Tracker. - * - * Loop Habit Tracker is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Loop Habit Tracker is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -package org.isoron.uhabits.widgets; - -import android.app.*; -import android.content.*; -import android.support.annotation.*; -import android.view.*; - -import org.isoron.uhabits.activities.common.views.*; -import org.isoron.uhabits.core.models.*; -import org.isoron.uhabits.utils.*; -import org.isoron.uhabits.widgets.views.*; - -public class FrequencyWidget extends BaseWidget -{ - @NonNull - private final Habit habit; - - public FrequencyWidget(@NonNull Context context, - int widgetId, - @NonNull Habit habit) - { - super(context, widgetId); - this.habit = habit; - } - - @Override - public PendingIntent getOnClickPendingIntent(Context context) - { - return pendingIntentFactory.showHabit(habit); - } - - @Override - public void refreshData(View v) - { - GraphWidgetView widgetView = (GraphWidgetView) v; - FrequencyChart chart = (FrequencyChart) widgetView.getDataView(); - - widgetView.setTitle(habit.getName()); - - int color = PaletteUtils.getColor(getContext(), habit.getColor()); - - chart.setColor(color); - chart.setFrequency(habit.getRepetitions().getWeekdayFrequency()); - } - - @Override - protected View buildView() - { - FrequencyChart chart = new FrequencyChart(getContext()); - return new GraphWidgetView(getContext(), chart); - } - - @Override - protected int getDefaultHeight() - { - return 200; - } - - @Override - protected int getDefaultWidth() - { - return 200; - } -} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/FrequencyWidget.kt b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/FrequencyWidget.kt new file mode 100644 index 000000000..5516f2bc9 --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/FrequencyWidget.kt @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.uhabits.widgets + +import android.content.* +import android.view.* +import org.isoron.uhabits.activities.common.views.* +import org.isoron.uhabits.core.models.* +import org.isoron.uhabits.utils.* +import org.isoron.uhabits.widgets.views.* + +class FrequencyWidget( + context: Context, + widgetId: Int, + private val habit: Habit +) : BaseWidget(context, widgetId) { + + override fun getOnClickPendingIntent(context: Context) = + pendingIntentFactory.showHabit(habit) + + override fun refreshData(v: View) { + val widgetView = v as GraphWidgetView + widgetView.setTitle(habit.name) + (widgetView.dataView as FrequencyChart).apply { + setColor(PaletteUtils.getColor(context, habit.color)) + setFrequency(habit.repetitions.weekdayFrequency) + } + } + + override fun buildView() = + GraphWidgetView(context, FrequencyChart(context)) + + override fun getDefaultHeight() = 200 + override fun getDefaultWidth() = 200 +} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/ScoreWidgetProvider.java b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/FrequencyWidgetProvider.kt similarity index 65% rename from uhabits-android/src/main/java/org/isoron/uhabits/widgets/ScoreWidgetProvider.java rename to uhabits-android/src/main/java/org/isoron/uhabits/widgets/FrequencyWidgetProvider.kt index 1ad3fb80b..f35320b41 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/ScoreWidgetProvider.java +++ b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/FrequencyWidgetProvider.kt @@ -16,20 +16,14 @@ * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ -package org.isoron.uhabits.widgets; -import android.content.*; -import android.support.annotation.*; +package org.isoron.uhabits.widgets -import org.isoron.uhabits.core.models.*; +import android.content.* -public class ScoreWidgetProvider extends BaseWidgetProvider -{ - @NonNull - @Override - protected BaseWidget getWidgetFromId(@NonNull Context context, int id) - { - Habit habit = getHabitFromWidgetId(id); - return new ScoreWidget(context, id, habit); +class FrequencyWidgetProvider : BaseWidgetProvider() { + override fun getWidgetFromId(context: Context, id: Int): BaseWidget { + val habit = getHabitFromWidgetId(id) + return FrequencyWidget(context, id, habit) } } diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/HabitPickerDialog.java b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/HabitPickerDialog.java deleted file mode 100644 index 7d8dae0f5..000000000 --- a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/HabitPickerDialog.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (C) 2016 Álinson Santos Xavier - * - * This file is part of Loop Habit Tracker. - * - * Loop Habit Tracker is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Loop Habit Tracker is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -package org.isoron.uhabits.widgets; - -import android.app.*; -import android.content.*; -import android.os.*; -import android.view.*; -import android.widget.*; - -import org.isoron.uhabits.*; -import org.isoron.uhabits.core.models.*; -import org.isoron.uhabits.core.preferences.*; - -import java.util.*; - -import static android.appwidget.AppWidgetManager.*; - -public class HabitPickerDialog extends Activity - implements AdapterView.OnItemClickListener -{ - private HabitList habitList; - - private WidgetPreferences preferences; - - private Integer widgetId; - - private ArrayList habitIds; - - @Override - public void onItemClick(AdapterView parent, - View view, - int position, - long id) - { - Long habitId = habitIds.get(position); - preferences.addWidget(widgetId, habitId); - - HabitsApplication app = (HabitsApplication) getApplicationContext(); - app.getComponent().getWidgetUpdater().updateWidgets(); - - Intent resultValue = new Intent(); - resultValue.putExtra(EXTRA_APPWIDGET_ID, widgetId); - setResult(RESULT_OK, resultValue); - finish(); - } - - @Override - protected void onCreate(Bundle savedInstanceState) - { - super.onCreate(savedInstanceState); - setContentView(R.layout.widget_configure_activity); - - HabitsApplication app = (HabitsApplication) getApplicationContext(); - HabitsApplicationComponent component = app.getComponent(); - habitList = component.getHabitList(); - preferences = component.getWidgetPreferences(); - - Intent intent = getIntent(); - Bundle extras = intent.getExtras(); - - if (extras != null) - widgetId = extras.getInt(EXTRA_APPWIDGET_ID, INVALID_APPWIDGET_ID); - - ListView listView = (ListView) findViewById(R.id.listView); - - habitIds = new ArrayList<>(); - ArrayList habitNames = new ArrayList<>(); - - for (Habit h : habitList) - { - if (h.isArchived()) continue; - habitIds.add(h.getId()); - habitNames.add(h.getName()); - } - - ArrayAdapter adapter = - new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, - habitNames); - listView.setAdapter(adapter); - listView.setOnItemClickListener(this); - } - -} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/HabitPickerDialog.kt b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/HabitPickerDialog.kt new file mode 100644 index 000000000..c1e8f8d9f --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/HabitPickerDialog.kt @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.uhabits.widgets + +import android.app.* +import android.appwidget.AppWidgetManager.* +import android.content.* +import android.os.* +import android.view.* +import android.widget.* +import org.isoron.uhabits.* +import org.isoron.uhabits.core.models.* +import org.isoron.uhabits.core.preferences.* +import java.util.* + +class HabitPickerDialog : Activity(), AdapterView.OnItemClickListener { + + private var widgetId = 0 + private lateinit var habitList: HabitList + private lateinit var preferences: WidgetPreferences + private lateinit var habitIds: ArrayList + private lateinit var widgetUpdater: WidgetUpdater + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + val component = (applicationContext as HabitsApplication).component + habitList = component.habitList + preferences = component.widgetPreferences + widgetUpdater = component.widgetUpdater + widgetId = intent.extras?.getInt(EXTRA_APPWIDGET_ID, + INVALID_APPWIDGET_ID) ?: 0 + + habitIds = ArrayList() + val habitNames = ArrayList() + for (h in habitList) { + if (h.isArchived) continue + habitIds.add(h.getId()!!) + habitNames.add(h.name) + } + + setContentView(R.layout.widget_configure_activity) + with(findViewById(R.id.listView) as ListView) { + adapter = ArrayAdapter(context, android.R.layout.simple_list_item_1, + habitNames) + onItemClickListener = this@HabitPickerDialog + } + } + + override fun onItemClick(parent: AdapterView<*>, + view: View, + position: Int, + id: Long) { + preferences.addWidget(widgetId, habitIds[position]) + widgetUpdater.updateWidgets() + setResult(Activity.RESULT_OK, Intent().apply { + putExtra(EXTRA_APPWIDGET_ID, widgetId) + }) + finish() + } +} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/HistoryWidget.java b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/HistoryWidget.java deleted file mode 100644 index 17b326adc..000000000 --- a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/HistoryWidget.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (C) 2016 Álinson Santos Xavier - * - * This file is part of Loop Habit Tracker. - * - * Loop Habit Tracker is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Loop Habit Tracker is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -package org.isoron.uhabits.widgets; - -import android.app.*; -import android.content.*; -import android.support.annotation.*; -import android.view.*; - -import org.isoron.uhabits.activities.common.views.*; -import org.isoron.uhabits.core.models.*; -import org.isoron.uhabits.utils.*; -import org.isoron.uhabits.widgets.views.*; - -public class HistoryWidget extends BaseWidget -{ - @NonNull - private Habit habit; - - public HistoryWidget(@NonNull Context context, int id, @NonNull Habit habit) - { - super(context, id); - this.habit = habit; - } - - @Override - public PendingIntent getOnClickPendingIntent(Context context) - { - return pendingIntentFactory.showHabit(habit); - } - - @Override - public void refreshData(View view) - { - GraphWidgetView widgetView = (GraphWidgetView) view; - HistoryChart chart = (HistoryChart) widgetView.getDataView(); - - int color = PaletteUtils.getColor(getContext(), habit.getColor()); - int[] values = habit.getCheckmarks().getAllValues(); - - chart.setColor(color); - chart.setCheckmarks(values); - } - - @Override - protected View buildView() - { - HistoryChart dataView = new HistoryChart(getContext()); - GraphWidgetView widgetView = - new GraphWidgetView(getContext(), dataView); - widgetView.setTitle(habit.getName()); - return widgetView; - } - - @Override - protected int getDefaultHeight() - { - return 250; - } - - @Override - protected int getDefaultWidth() - { - return 250; - } -} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/HistoryWidget.kt b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/HistoryWidget.kt new file mode 100644 index 000000000..b9f4df952 --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/HistoryWidget.kt @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.uhabits.widgets + +import android.app.* +import android.content.* +import android.view.* +import org.isoron.uhabits.activities.common.views.* +import org.isoron.uhabits.core.models.* +import org.isoron.uhabits.utils.* +import org.isoron.uhabits.widgets.views.* + +class HistoryWidget( + context: Context, + id: Int, + private val habit: Habit +) : BaseWidget(context, id) { + + override fun getOnClickPendingIntent(context: Context): PendingIntent { + return pendingIntentFactory.showHabit(habit) + } + + override fun refreshData(view: View) { + val widgetView = view as GraphWidgetView + (widgetView.dataView as HistoryChart).apply { + setColor(PaletteUtils.getColor(context, habit.color)) + setCheckmarks(habit.checkmarks.allValues) + } + } + + override fun buildView() = + GraphWidgetView(context, HistoryChart(context)).apply { + setTitle(habit.name) + } + + override fun getDefaultHeight() = 250 + override fun getDefaultWidth() = 250 +} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/HistoryWidgetProvider.java b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/HistoryWidgetProvider.kt similarity index 65% rename from uhabits-android/src/main/java/org/isoron/uhabits/widgets/HistoryWidgetProvider.java rename to uhabits-android/src/main/java/org/isoron/uhabits/widgets/HistoryWidgetProvider.kt index 4fad4b853..f7b5eb482 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/HistoryWidgetProvider.java +++ b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/HistoryWidgetProvider.kt @@ -16,20 +16,13 @@ * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ -package org.isoron.uhabits.widgets; +package org.isoron.uhabits.widgets -import android.content.*; -import android.support.annotation.*; +import android.content.* -import org.isoron.uhabits.core.models.*; - -public class HistoryWidgetProvider extends BaseWidgetProvider -{ - @NonNull - @Override - protected BaseWidget getWidgetFromId(@NonNull Context context, int id) - { - Habit habit = getHabitFromWidgetId(id); - return new HistoryWidget(context, id, habit); +class HistoryWidgetProvider : BaseWidgetProvider() { + override fun getWidgetFromId(context: Context, id: Int): BaseWidget { + val habit = getHabitFromWidgetId(id) + return HistoryWidget(context, id, habit) } } diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/ScoreWidget.java b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/ScoreWidget.java deleted file mode 100644 index fb19a2dce..000000000 --- a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/ScoreWidget.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (C) 2016 Álinson Santos Xavier - * - * This file is part of Loop Habit Tracker. - * - * Loop Habit Tracker is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Loop Habit Tracker is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -package org.isoron.uhabits.widgets; - -import android.app.*; -import android.content.*; -import android.support.annotation.*; -import android.view.*; - -import org.isoron.uhabits.*; -import org.isoron.uhabits.activities.common.views.*; -import org.isoron.uhabits.activities.habits.show.views.*; -import org.isoron.uhabits.core.models.*; -import org.isoron.uhabits.core.preferences.*; -import org.isoron.uhabits.utils.*; -import org.isoron.uhabits.widgets.views.*; - -import java.util.*; - -public class ScoreWidget extends BaseWidget -{ - @NonNull - private Habit habit; - - private final Preferences prefs; - - public ScoreWidget(@NonNull Context context, int id, @NonNull Habit habit) - { - super(context, id); - this.habit = habit; - - HabitsApplication app = - (HabitsApplication) context.getApplicationContext(); - prefs = app.getComponent().getPreferences(); - } - - @Override - public PendingIntent getOnClickPendingIntent(Context context) - { - return pendingIntentFactory.showHabit(habit); - } - - @Override - public void refreshData(View view) - { - int defaultScoreInterval = prefs.getDefaultScoreSpinnerPosition(); - int size = ScoreCard.BUCKET_SIZES[defaultScoreInterval]; - - GraphWidgetView widgetView = (GraphWidgetView) view; - ScoreChart chart = (ScoreChart) widgetView.getDataView(); - - List scores; - ScoreList scoreList = habit.getScores(); - - if (size == 1) scores = scoreList.toList(); - else scores = scoreList.groupBy(ScoreCard.getTruncateField(size)); - - int color = PaletteUtils.getColor(getContext(), habit.getColor()); - - chart.setIsTransparencyEnabled(true); - chart.setBucketSize(size); - chart.setColor(color); - chart.setScores(scores); - } - - @Override - protected View buildView() - { - ScoreChart dataView = new ScoreChart(getContext()); - GraphWidgetView view = new GraphWidgetView(getContext(), dataView); - view.setTitle(habit.getName()); - return view; - } - - @Override - protected int getDefaultHeight() - { - return 300; - } - - @Override - protected int getDefaultWidth() - { - return 300; - } -} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/ScoreWidget.kt b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/ScoreWidget.kt new file mode 100644 index 000000000..3ee5ed2ba --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/ScoreWidget.kt @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.uhabits.widgets + +import android.content.* +import android.view.* +import org.isoron.uhabits.activities.common.views.* +import org.isoron.uhabits.activities.habits.show.views.* +import org.isoron.uhabits.core.models.* +import org.isoron.uhabits.core.preferences.* +import org.isoron.uhabits.utils.* +import org.isoron.uhabits.widgets.views.* + +class ScoreWidget( + context: Context, + id: Int, + private val habit: Habit, + private val prefs: Preferences +) : BaseWidget(context, id) { + + override fun getOnClickPendingIntent(context: Context) = + pendingIntentFactory.showHabit(habit) + + override fun refreshData(view: View) { + val size = ScoreCard.BUCKET_SIZES[prefs.defaultScoreSpinnerPosition] + val scores = when(size) { + 1 -> habit.scores.toList() + else -> habit.scores.groupBy(ScoreCard.getTruncateField(size)) + } + + val widgetView = view as GraphWidgetView + (widgetView.dataView as ScoreChart).apply { + setIsTransparencyEnabled(true) + setBucketSize(size) + setColor(PaletteUtils.getColor(context, habit.color)) + setScores(scores) + } + } + + override fun buildView() = + GraphWidgetView(context, ScoreChart(context)).apply { + setTitle(habit.name) + } + + override fun getDefaultHeight() = 300 + override fun getDefaultWidth() = 300 +} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/FrequencyWidgetProvider.java b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/ScoreWidgetProvider.kt similarity index 65% rename from uhabits-android/src/main/java/org/isoron/uhabits/widgets/FrequencyWidgetProvider.java rename to uhabits-android/src/main/java/org/isoron/uhabits/widgets/ScoreWidgetProvider.kt index 423cf6ebc..fa31a63ac 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/FrequencyWidgetProvider.java +++ b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/ScoreWidgetProvider.kt @@ -16,21 +16,15 @@ * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ +package org.isoron.uhabits.widgets -package org.isoron.uhabits.widgets; +import android.content.* +import org.isoron.uhabits.* -import android.content.*; -import android.support.annotation.*; - -import org.isoron.uhabits.core.models.*; - -public class FrequencyWidgetProvider extends BaseWidgetProvider -{ - @NonNull - @Override - protected BaseWidget getWidgetFromId(@NonNull Context context, int id) - { - Habit habit = getHabitFromWidgetId(id); - return new FrequencyWidget(context, id, habit); +class ScoreWidgetProvider : BaseWidgetProvider() { + override fun getWidgetFromId(context: Context, id: Int): BaseWidget { + val component = (context.applicationContext as HabitsApplication).component + val habit = getHabitFromWidgetId(id) + return ScoreWidget(context, id, habit, component.preferences) } } diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/StreakWidget.java b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/StreakWidget.java deleted file mode 100644 index 75251a09c..000000000 --- a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/StreakWidget.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (C) 2016 Álinson Santos Xavier - * - * This file is part of Loop Habit Tracker. - * - * Loop Habit Tracker is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Loop Habit Tracker is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -package org.isoron.uhabits.widgets; - -import android.app.*; -import android.content.*; -import android.support.annotation.*; -import android.view.*; -import android.view.ViewGroup.*; - -import org.isoron.uhabits.activities.common.views.*; -import org.isoron.uhabits.core.models.*; -import org.isoron.uhabits.utils.*; -import org.isoron.uhabits.widgets.views.*; - -import java.util.*; - -import static android.view.ViewGroup.LayoutParams.*; - -public class StreakWidget extends BaseWidget -{ - @NonNull - private Habit habit; - - public StreakWidget(@NonNull Context context, int id, @NonNull Habit habit) - { - super(context, id); - this.habit = habit; - } - - @Override - public PendingIntent getOnClickPendingIntent(Context context) - { - return pendingIntentFactory.showHabit(habit); - } - - @Override - public void refreshData(View view) - { - GraphWidgetView widgetView = (GraphWidgetView) view; - StreakChart chart = (StreakChart) widgetView.getDataView(); - - int color = PaletteUtils.getColor(getContext(), habit.getColor()); - - int count = chart.getMaxStreakCount(); - List streaks = habit.getStreaks().getBest(count); - - chart.setColor(color); - chart.setStreaks(streaks); - } - - @Override - protected View buildView() - { - StreakChart dataView = new StreakChart(getContext()); - GraphWidgetView view = new GraphWidgetView(getContext(), dataView); - LayoutParams params = new LayoutParams(MATCH_PARENT, MATCH_PARENT); - view.setTitle(habit.getName()); - view.setLayoutParams(params); - return view; - } - - @Override - protected int getDefaultHeight() - { - return 200; - } - - @Override - protected int getDefaultWidth() - { - return 200; - } -} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/StreakWidget.kt b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/StreakWidget.kt new file mode 100644 index 000000000..f8795e297 --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/StreakWidget.kt @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.uhabits.widgets + +import android.content.* +import android.view.* +import android.view.ViewGroup.* +import android.view.ViewGroup.LayoutParams.* +import org.isoron.uhabits.activities.common.views.* +import org.isoron.uhabits.core.models.* +import org.isoron.uhabits.utils.* +import org.isoron.uhabits.widgets.views.* + +class StreakWidget( + context: Context, + id: Int, + private val habit: Habit +) : BaseWidget(context, id) { + + override fun getOnClickPendingIntent(context: Context) = + pendingIntentFactory.showHabit(habit) + + override fun refreshData(view: View) { + val widgetView = view as GraphWidgetView + (widgetView.dataView as StreakChart).apply { + setColor(PaletteUtils.getColor(context, habit.color)) + setStreaks(habit.streaks.getBest(maxStreakCount)) + } + } + + override fun buildView(): View { + return GraphWidgetView(context, StreakChart(context)).apply { + setTitle(habit.name) + layoutParams = LayoutParams(MATCH_PARENT, MATCH_PARENT) + } + } + + override fun getDefaultHeight() = 200 + override fun getDefaultWidth() = 200 +} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/StreakWidgetProvider.kt b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/StreakWidgetProvider.kt new file mode 100644 index 000000000..5284cb9e1 --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/StreakWidgetProvider.kt @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ +package org.isoron.uhabits.widgets + +import android.content.* + +class StreakWidgetProvider : BaseWidgetProvider() { + override fun getWidgetFromId(context: Context, id: Int): BaseWidget { + val habit = getHabitFromWidgetId(id) + return StreakWidget(context, id, habit) + } +} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/WidgetDimensions.java b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/WidgetDimensions.java deleted file mode 100644 index 5c88a2114..000000000 --- a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/WidgetDimensions.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (C) 2016 Álinson Santos Xavier - * - * This file is part of Loop Habit Tracker. - * - * Loop Habit Tracker is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Loop Habit Tracker is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -package org.isoron.uhabits.widgets; - -public class WidgetDimensions -{ - private final int portraitWidth; - - private final int portraitHeight; - - private final int landscapeWidth; - - private final int landscapeHeight; - - public WidgetDimensions(int portraitWidth, - int portraitHeight, - int landscapeWidth, - int landscapeHeight) - { - this.portraitWidth = portraitWidth; - this.portraitHeight = portraitHeight; - this.landscapeWidth = landscapeWidth; - this.landscapeHeight = landscapeHeight; - } - - public int getLandscapeHeight() - { - return landscapeHeight; - } - - public int getLandscapeWidth() - { - return landscapeWidth; - } - - public int getPortraitHeight() - { - return portraitHeight; - } - - public int getPortraitWidth() - { - return portraitWidth; - } -} \ No newline at end of file diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/WidgetDimensions.kt b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/WidgetDimensions.kt new file mode 100644 index 000000000..e017e2106 --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/WidgetDimensions.kt @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.uhabits.widgets + +class WidgetDimensions(val portraitWidth: Int, + val portraitHeight: Int, + val landscapeWidth: Int, + val landscapeHeight: Int) \ No newline at end of file diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/WidgetUpdater.java b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/WidgetUpdater.java deleted file mode 100644 index a10014212..000000000 --- a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/WidgetUpdater.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright (C) 2016 Álinson Santos Xavier - * - * This file is part of Loop Habit Tracker. - * - * Loop Habit Tracker is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Loop Habit Tracker is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -package org.isoron.uhabits.widgets; - -import android.appwidget.*; -import android.content.*; -import android.support.annotation.*; - -import org.isoron.androidbase.*; -import org.isoron.uhabits.core.commands.*; -import org.isoron.uhabits.core.tasks.*; - -import javax.inject.*; - -/** - * A WidgetUpdater listens to the commands being executed by the application and - * updates the home-screen widgets accordingly. - *

- * There should be only one instance of this class, created when the application - * starts. To access it, call HabitApplication.getWidgetUpdater(). - */ -public class WidgetUpdater implements CommandRunner.Listener -{ - @NonNull - private final CommandRunner commandRunner; - - private TaskRunner taskRunner; - - @NonNull - private final Context context; - - @Inject - public WidgetUpdater(@NonNull @AppContext Context context, - @NonNull CommandRunner commandRunner, - @NonNull TaskRunner taskRunner) - { - this.context = context; - this.commandRunner = commandRunner; - this.taskRunner = taskRunner; - } - - @Override - public void onCommandExecuted(@NonNull Command command, - @Nullable Long refreshKey) - { - updateWidgets(); - } - - /** - * Instructs the updater to start listening to commands. If any relevant - * commands are executed after this method is called, the corresponding - * widgets will get updated. - */ - public void startListening() - { - commandRunner.addListener(this); - } - - /** - * Instructs the updater to stop listening to commands. Every command - * executed after this method is called will be ignored by the updater. - */ - public void stopListening() - { - commandRunner.removeListener(this); - } - - public void updateWidgets() - { - taskRunner.execute(() -> - { - updateWidgets(CheckmarkWidgetProvider.class); - updateWidgets(HistoryWidgetProvider.class); - updateWidgets(ScoreWidgetProvider.class); - updateWidgets(StreakWidgetProvider.class); - updateWidgets(FrequencyWidgetProvider.class); - }); - } - - public void updateWidgets(Class providerClass) - { - ComponentName provider = new ComponentName(context, providerClass); - Intent intent = new Intent(context, providerClass); - intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); - int ids[] = - AppWidgetManager.getInstance(context).getAppWidgetIds(provider); - intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids); - context.sendBroadcast(intent); - } -} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/widgets/WidgetUpdater.kt b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/WidgetUpdater.kt new file mode 100644 index 000000000..80c159dad --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/widgets/WidgetUpdater.kt @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.uhabits.widgets + +import android.appwidget.* +import android.content.* +import org.isoron.androidbase.* +import org.isoron.uhabits.core.commands.* +import org.isoron.uhabits.core.tasks.* +import javax.inject.* + +/** + * A WidgetUpdater listens to the commands being executed by the application and + * updates the home-screen widgets accordingly. + */ +class WidgetUpdater +@Inject constructor( + @AppContext private val context: Context, + private val commandRunner: CommandRunner, + private val taskRunner: TaskRunner +) : CommandRunner.Listener { + + override fun onCommandExecuted(command: Command, refreshKey: Long?) { + updateWidgets() + } + + /** + * Instructs the updater to start listening to commands. If any relevant + * commands are executed after this method is called, the corresponding + * widgets will get updated. + */ + fun startListening() { + commandRunner.addListener(this) + } + + /** + * Instructs the updater to stop listening to commands. Every command + * executed after this method is called will be ignored by the updater. + */ + fun stopListening() { + commandRunner.removeListener(this) + } + + fun updateWidgets() { + taskRunner.execute { + updateWidgets(CheckmarkWidgetProvider::class.java) + updateWidgets(HistoryWidgetProvider::class.java) + updateWidgets(ScoreWidgetProvider::class.java) + updateWidgets(StreakWidgetProvider::class.java) + updateWidgets(FrequencyWidgetProvider::class.java) + } + } + + fun updateWidgets(providerClass: Class<*>) { + val ids = AppWidgetManager.getInstance(context).getAppWidgetIds( + ComponentName(context, providerClass)) + context.sendBroadcast(Intent(context, providerClass).apply { + action = AppWidgetManager.ACTION_APPWIDGET_UPDATE + putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids) + }) + } +}