Convert widget providers to Kotlin

pull/316/head^2
Alinson S. Xavier 8 years ago
parent efcb5710c0
commit c7aaa98935

@ -45,7 +45,7 @@ public class ScoreWidgetTest extends BaseViewTest
setTheme(R.style.TransparentWidgetTheme); setTheme(R.style.TransparentWidgetTheme);
habit = fixtures.createLongHabit(); habit = fixtures.createLongHabit();
ScoreWidget widget = new ScoreWidget(targetContext, 0, habit); ScoreWidget widget = new ScoreWidget(targetContext, 0, habit, prefs);
view = convertToView(widget, 400, 400); view = convertToView(widget, 400, 400);
} }

@ -1,83 +0,0 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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;
}
}

@ -0,0 +1,50 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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
}

@ -1,35 +0,0 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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);
}
}

@ -16,20 +16,13 @@
* You should have received a copy of the GNU General Public License along * You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.isoron.uhabits.widgets; package org.isoron.uhabits.widgets
import android.content.*; import android.content.*
import android.support.annotation.*;
import org.isoron.uhabits.core.models.*; class CheckmarkWidgetProvider : BaseWidgetProvider() {
override fun getWidgetFromId(context: Context, id: Int): CheckmarkWidget {
public class StreakWidgetProvider extends BaseWidgetProvider val habit = getHabitFromWidgetId(id)
{ return CheckmarkWidget(context, id, habit)
@NonNull
@Override
protected BaseWidget getWidgetFromId(@NonNull Context context, int id)
{
Habit habit = getHabitFromWidgetId(id);
return new StreakWidget(context, id, habit);
} }
} }

@ -1,83 +0,0 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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;
}
}

@ -0,0 +1,52 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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
}

@ -16,20 +16,14 @@
* You should have received a copy of the GNU General Public License along * You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.isoron.uhabits.widgets;
import android.content.*; package org.isoron.uhabits.widgets
import android.support.annotation.*;
import org.isoron.uhabits.core.models.*; import android.content.*
public class ScoreWidgetProvider extends BaseWidgetProvider class FrequencyWidgetProvider : BaseWidgetProvider() {
{ override fun getWidgetFromId(context: Context, id: Int): BaseWidget {
@NonNull val habit = getHabitFromWidgetId(id)
@Override return FrequencyWidget(context, id, habit)
protected BaseWidget getWidgetFromId(@NonNull Context context, int id)
{
Habit habit = getHabitFromWidgetId(id);
return new ScoreWidget(context, id, habit);
} }
} }

@ -1,101 +0,0 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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<Long> 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<String> habitNames = new ArrayList<>();
for (Habit h : habitList)
{
if (h.isArchived()) continue;
habitIds.add(h.getId());
habitNames.add(h.getName());
}
ArrayAdapter<String> adapter =
new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
habitNames);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
}

@ -0,0 +1,77 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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<Long>
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<Long>()
val habitNames = ArrayList<String>()
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()
}
}

@ -1,83 +0,0 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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;
}
}

@ -0,0 +1,55 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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
}

@ -16,20 +16,13 @@
* You should have received a copy of the GNU General Public License along * You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.isoron.uhabits.widgets; package org.isoron.uhabits.widgets
import android.content.*; import android.content.*
import android.support.annotation.*;
import org.isoron.uhabits.core.models.*; class HistoryWidgetProvider : BaseWidgetProvider() {
override fun getWidgetFromId(context: Context, id: Int): BaseWidget {
public class HistoryWidgetProvider extends BaseWidgetProvider val habit = getHabitFromWidgetId(id)
{ return HistoryWidget(context, id, habit)
@NonNull
@Override
protected BaseWidget getWidgetFromId(@NonNull Context context, int id)
{
Habit habit = getHabitFromWidgetId(id);
return new HistoryWidget(context, id, habit);
} }
} }

@ -1,103 +0,0 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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<Score> 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;
}
}

@ -0,0 +1,64 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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
}

@ -16,21 +16,15 @@
* You should have received a copy of the GNU General Public License along * You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.isoron.uhabits.widgets
package org.isoron.uhabits.widgets; import android.content.*
import org.isoron.uhabits.*
import android.content.*; class ScoreWidgetProvider : BaseWidgetProvider() {
import android.support.annotation.*; override fun getWidgetFromId(context: Context, id: Int): BaseWidget {
val component = (context.applicationContext as HabitsApplication).component
import org.isoron.uhabits.core.models.*; val habit = getHabitFromWidgetId(id)
return ScoreWidget(context, id, habit, component.preferences)
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);
} }
} }

@ -1,91 +0,0 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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<Streak> 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;
}
}

@ -0,0 +1,57 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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
}

@ -0,0 +1,28 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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)
}
}

@ -1,62 +0,0 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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;
}
}

@ -0,0 +1,25 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.widgets
class WidgetDimensions(val portraitWidth: Int,
val portraitHeight: Int,
val landscapeWidth: Int,
val landscapeHeight: Int)

@ -1,107 +0,0 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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.
* <p>
* 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);
}
}

@ -0,0 +1,79 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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)
})
}
}
Loading…
Cancel
Save