Processed code review

pull/499/head
Thomas S 5 years ago
parent fd03414607
commit 1d372a8fbb

@ -17,17 +17,9 @@
~ 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/>.
--> -->
<manifest xmlns:tools="http://schemas.android.com/tools" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.isoron.uhabits" package="org.isoron.uhabits"
xmlns:android="http://schemas.android.com/apk/res/android" >
android:versionCode="36"
android:versionName="1.7.9">
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

@ -38,7 +38,7 @@ class NumberPickerFactory
fun create(value: Double, fun create(value: Double,
unit: String, unit: String,
callback: ListHabitsBehavior.NumberPickerCallback): AlertDialog { callback: ListHabitsBehavior.NumberPickerCallback): AlertDialog {
val inflater : LayoutInflater = LayoutInflater.from(context) val inflater = LayoutInflater.from(context)
val view = inflater.inflate(R.layout.number_picker_dialog, null) val view = inflater.inflate(R.layout.number_picker_dialog, null)
val picker = view.findViewById<NumberPicker>(R.id.picker) val picker = view.findViewById<NumberPicker>(R.id.picker)

@ -272,7 +272,7 @@ public class BarChart extends ScrollableChart
float round = dpToPixels(getContext(), 2); float round = dpToPixels(getContext(), 2);
int color = textColor; int color = textColor;
if (Habit.checkMarkValueToDouble(value) >= target) color = primaryColor; if (Checkmark.checkMarkValueToDouble(value) >= target) color = primaryColor;
rect.inset(-margin, 0); rect.inset(-margin, 0);
setModeOrColor(pGraph, XFERMODE_CLEAR, backgroundColor); setModeOrColor(pGraph, XFERMODE_CLEAR, backgroundColor);
@ -365,10 +365,10 @@ public class BarChart extends ScrollableChart
if (value == 0) return; if (value == 0) return;
int activeColor = textColor; int activeColor = textColor;
if (Habit.checkMarkValueToDouble(value) >= target) if (Checkmark.checkMarkValueToDouble(value) >= target)
activeColor = primaryColor; activeColor = primaryColor;
String label = NumberButtonViewKt.toShortString(Habit.checkMarkValueToDouble(value)); String label = NumberButtonViewKt.toShortString(Checkmark.checkMarkValueToDouble(value));
Rect rText = new Rect(); Rect rText = new Rect();
pText.getTextBounds(label, 0, label.length(), rText); pText.getTextBounds(label, 0, label.length(), rText);

@ -52,13 +52,8 @@ class ListHabitsMenu @Inject constructor(
return true return true
} }
R.id.actionCreateBooleanHabit -> { R.id.actionCreateHabit -> {
behavior.onCreateBooleanHabit() behavior.onCreateHabit()
return true
}
R.id.actionCreateNumeralHabit -> {
behavior.onCreateNumericalHabit()
return true return true
} }

@ -135,21 +135,12 @@ class ListHabitsScreen
activity.startActivity(intent) activity.startActivity(intent)
} }
override fun showCreateBooleanHabitScreen() {
val dialog = HabitTypeDialog()
activity.showDialog(dialog, "editHabit")
}
override fun showSelectHabitTypeDialog() { override fun showSelectHabitTypeDialog() {
val dialog = HabitTypeDialog() val dialog = HabitTypeDialog()
activity.showDialog(dialog, "habitType") activity.showDialog(dialog, "habitType")
} }
override fun showCreateNumericalHabitScreen() {
val dialog = HabitTypeDialog()
activity.showDialog(dialog, "editHabit")
}
override fun showDeleteConfirmationScreen(callback: OnConfirmedCallback) { override fun showDeleteConfirmationScreen(callback: OnConfirmedCallback) {
activity.showDialog(confirmDeleteDialogFactory.create(callback)) activity.showDialog(confirmDeleteDialogFactory.create(callback))
} }

@ -86,7 +86,7 @@ class HabitCardView(
get() = checkmarkPanel.values get() = checkmarkPanel.values
set(values) { set(values) {
checkmarkPanel.values = values checkmarkPanel.values = values
numberPanel.values = values.map { Habit.checkMarkValueToDouble(it) }.toDoubleArray() numberPanel.values = values.map { Checkmark.checkMarkValueToDouble(it) }.toDoubleArray()
} }
var threshold: Double var threshold: Double

@ -36,17 +36,19 @@ import java.text.*
private val BOLD_TYPEFACE = Typeface.create("sans-serif-condensed", Typeface.BOLD) private val BOLD_TYPEFACE = Typeface.create("sans-serif-condensed", Typeface.BOLD)
private val NORMAL_TYPEFACE = Typeface.create("sans-serif-condensed", Typeface.NORMAL) private val NORMAL_TYPEFACE = Typeface.create("sans-serif-condensed", Typeface.NORMAL)
fun Double.toShortString(): String = when { fun Double.toShortString(): String = doubleToShortString(this);
this >= 1e9 -> String.format("%.1fG", this / 1e9)
this >= 1e8 -> String.format("%.0fM", this / 1e6) fun doubleToShortString(d : Double): String = when {
this >= 1e7 -> String.format("%.1fM", this / 1e6) d >= 1e9 -> String.format("%.1fG", d / 1e9)
this >= 1e6 -> String.format("%.1fM", this / 1e6) d >= 1e8 -> String.format("%.0fM", d / 1e6)
this >= 1e5 -> String.format("%.0fk", this / 1e3) d >= 1e7 -> String.format("%.1fM", d / 1e6)
this >= 1e4 -> String.format("%.1fk", this / 1e3) d >= 1e6 -> String.format("%.1fM", d / 1e6)
this >= 1e3 -> String.format("%.1fk", this / 1e3) d >= 1e5 -> String.format("%.0fk", d / 1e3)
this >= 1e2 -> DecimalFormat("#").format(this) d >= 1e4 -> String.format("%.1fk", d / 1e3)
this >= 1e1 -> DecimalFormat("#.#").format(this) d >= 1e3 -> String.format("%.1fk", d / 1e3)
else -> DecimalFormat("#.##").format(this) d >= 1e2 -> DecimalFormat("#").format(d)
d >= 1e1 -> DecimalFormat("#.#").format(d)
else -> DecimalFormat("#.##").format(d)
} }
@AutoFactory @AutoFactory

@ -132,7 +132,7 @@ public class HistoryCard extends HabitCard
chart.setColor(color); chart.setColor(color);
if(habit.isNumerical()) if(habit.isNumerical())
{ {
chart.setTarget(Habit.doubleToCheckMarkValue(habit.getTargetValue())); chart.setTarget(Checkmark.doubleToCheckMarkValue(habit.getTargetValue()));
chart.setNumerical(true); chart.setNumerical(true);
} }
} }

@ -36,19 +36,19 @@ class IntentParser
return CheckmarkIntentData(parseHabit(uri), parseTimestamp(intent)) return CheckmarkIntentData(parseHabit(uri), parseTimestamp(intent))
} }
fun copyIntentData(source: Intent, destination: Intent){ fun copyIntentData(source: Intent, destination: Intent) {
destination.data=source.data; destination.data = source.data;
destination.putExtra("timestamp",source.getLongExtra("timestamp",getToday())) destination.putExtra("timestamp", source.getLongExtra("timestamp", DateUtils.getToday().unixTime))
} }
private fun parseHabit(uri: Uri): Habit { private fun parseHabit(uri: Uri): Habit {
val habit = habits.getById(parseId(uri)) ?: val habit = habits.getById(parseId(uri))
throw IllegalArgumentException("habit not found") ?: throw IllegalArgumentException("habit not found")
return habit return habit
} }
private fun parseTimestamp(intent: Intent): Timestamp { private fun parseTimestamp(intent: Intent): Timestamp {
val today = getToday() val today = DateUtils.getToday().unixTime;
var timestamp = intent.getLongExtra("timestamp", today) var timestamp = intent.getLongExtra("timestamp", today)
timestamp = DateUtils.getStartOfDay(timestamp) timestamp = DateUtils.getStartOfDay(timestamp)
@ -58,9 +58,5 @@ class IntentParser
return Timestamp(timestamp) return Timestamp(timestamp)
} }
private fun getToday() : Long{
return DateUtils.getToday().unixTime
}
class CheckmarkIntentData(var habit: Habit, var timestamp: Timestamp) class CheckmarkIntentData(var habit: Habit, var timestamp: Timestamp)
} }

@ -104,13 +104,17 @@ class PendingIntentFactory
}, },
FLAG_UPDATE_CURRENT) FLAG_UPDATE_CURRENT)
fun setNumericalValue(widgetContext: Context, habit: Habit, numericalValue: Int, timestamp: Long?): PendingIntent = fun setNumericalValue(widgetContext: Context,
habit: Habit,
numericalValue: Int,
timestamp: Long?):
PendingIntent =
getBroadcast( getBroadcast(
widgetContext, 2, widgetContext, 2,
Intent(widgetContext, WidgetReceiver::class.java).apply { Intent(widgetContext, WidgetReceiver::class.java).apply {
data = Uri.parse(habit.uriString) data = Uri.parse(habit.uriString)
action = WidgetReceiver.ACTION_SET_NUMERICAL_VALUE action = WidgetReceiver.ACTION_SET_NUMERICAL_VALUE
putExtra("numericalValue",numericalValue); putExtra("numericalValue", numericalValue);
if (timestamp != null) putExtra("timestamp", timestamp) if (timestamp != null) putExtra("timestamp", timestamp)
}, },
FLAG_UPDATE_CURRENT) FLAG_UPDATE_CURRENT)

@ -27,6 +27,7 @@ import org.isoron.uhabits.core.preferences.*;
import org.isoron.uhabits.core.ui.widgets.*; import org.isoron.uhabits.core.ui.widgets.*;
import org.isoron.uhabits.intents.*; import org.isoron.uhabits.intents.*;
import org.isoron.uhabits.sync.*; import org.isoron.uhabits.sync.*;
import org.isoron.uhabits.widgets.activities.*;
import dagger.*; import dagger.*;
@ -49,6 +50,9 @@ public class WidgetReceiver extends BroadcastReceiver
public static final String ACTION_TOGGLE_REPETITION = public static final String ACTION_TOGGLE_REPETITION =
"org.isoron.uhabits.ACTION_TOGGLE_REPETITION"; "org.isoron.uhabits.ACTION_TOGGLE_REPETITION";
public static final String ACTION_SET_NUMERICAL_VALUE =
"org.isoron.uhabits.ACTION_SET_NUMERICAL_VALUE";
private static final String TAG = "WidgetReceiver"; private static final String TAG = "WidgetReceiver";
@Override @Override
@ -104,6 +108,14 @@ public class WidgetReceiver extends BroadcastReceiver
controller.onRemoveRepetition(data.getHabit(), controller.onRemoveRepetition(data.getHabit(),
data.getTimestamp()); data.getTimestamp());
break; break;
case ACTION_SET_NUMERICAL_VALUE:
Intent numberSelectorIntent = new Intent(context, NumericalCheckmarkWidgetActivity.class);
numberSelectorIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
numberSelectorIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
numberSelectorIntent.setAction(NumericalCheckmarkWidgetActivity.ACTION_SHOW_NUMERICAL_VALUE_ACTIVITY);
parser.copyIntentData(intent,numberSelectorIntent);
context.startActivity(numberSelectorIntent);
break;
} }
} }
catch (RuntimeException e) catch (RuntimeException e)

@ -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.receivers
import android.content.*
import android.util.*
import org.isoron.uhabits.*
import org.isoron.uhabits.core.preferences.*
import org.isoron.uhabits.core.ui.widgets.*
import org.isoron.uhabits.intents.*
import org.isoron.uhabits.sync.*
import dagger.*
import org.isoron.uhabits.activities.common.dialogs.NumberPickerFactory
import org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsBehavior
import org.isoron.uhabits.widgets.activities.NumericalCheckmarkWidgetActivity
import android.content.Intent
/**
* The Android BroadcastReceiver for Loop Habit Tracker.
*
*
* All broadcast messages are received and processed by this class.
*/
class WidgetReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val app = context.applicationContext as HabitsApplication
val component = DaggerWidgetReceiver_WidgetComponent
.builder()
.habitsApplicationComponent(app.component)
.build()
val parser = app.component.intentParser
val controller = component.widgetController
val prefs = app.component.preferences
if (prefs.isSyncEnabled)
context.startService(Intent(context, SyncService::class.java))
try {
val data: IntentParser.CheckmarkIntentData = parser.parseCheckmarkIntent(intent)
when (intent.action) {
ACTION_ADD_REPETITION -> controller.onAddRepetition(data.habit, data.timestamp)
ACTION_TOGGLE_REPETITION -> controller.onToggleRepetition(data.habit, data.timestamp)
ACTION_REMOVE_REPETITION -> controller.onRemoveRepetition(data.habit, data.timestamp)
ACTION_SET_NUMERICAL_VALUE -> {
val numberSelectorIntent = Intent(context, NumericalCheckmarkWidgetActivity::class.java)
numberSelectorIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
numberSelectorIntent.action = NumericalCheckmarkWidgetActivity.ACTION_SHOW_NUMERICAL_VALUE_ACTIVITY
parser.copyIntentData(intent,numberSelectorIntent)//give the habit and timestamp data to the numericalCheckmarkWidgetActivity
context.startActivity(numberSelectorIntent)
}
}
} catch (e: RuntimeException) {
Log.e("WidgetReceiver", "could not process intent", e)
}
}
@ReceiverScope
@Component(dependencies = [HabitsApplicationComponent::class])
internal interface WidgetComponent {
val widgetController: WidgetBehavior
}
companion object {
val ACTION_ADD_REPETITION = "org.isoron.uhabits.ACTION_ADD_REPETITION"
val ACTION_DISMISS_REMINDER = "org.isoron.uhabits.ACTION_DISMISS_REMINDER"
val ACTION_REMOVE_REPETITION = "org.isoron.uhabits.ACTION_REMOVE_REPETITION"
val ACTION_TOGGLE_REPETITION = "org.isoron.uhabits.ACTION_TOGGLE_REPETITION"
val ACTION_SET_NUMERICAL_VALUE = "org.isoron.uhabits.ACTION_SET_NUMERICAL_VALUE"
}
}

@ -44,7 +44,7 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
private WidgetPreferences widgetPrefs; private WidgetPreferences widgetPrefs;
public static void updateAppWidget(@NonNull Context context, @NonNull AppWidgetManager manager, public static void updateAppWidget(@NonNull AppWidgetManager manager,
@NonNull BaseWidget widget) @NonNull BaseWidget widget)
{ {
RemoteViews landscape = widget.getLandscapeRemoteViews(); RemoteViews landscape = widget.getLandscapeRemoteViews();
@ -86,7 +86,7 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
BaseWidget widget = getWidgetFromId(context, widgetId); BaseWidget widget = getWidgetFromId(context, widgetId);
WidgetDimensions dims = getDimensionsFromOptions(context, options); WidgetDimensions dims = getDimensionsFromOptions(context, options);
widget.setDimensions(dims); widget.setDimensions(dims);
updateAppWidget(context, manager, widget); updateAppWidget(manager, widget);
} }
catch (RuntimeException e) catch (RuntimeException e)
{ {
@ -181,7 +181,7 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
Bundle options = manager.getAppWidgetOptions(widgetId); Bundle options = manager.getAppWidgetOptions(widgetId);
widget.setDimensions(getDimensionsFromOptions(context, options)); widget.setDimensions(getDimensionsFromOptions(context, options));
updateAppWidget(context, manager, widget); updateAppWidget(manager, widget);
} }
catch (RuntimeException e) catch (RuntimeException e)
{ {

@ -19,8 +19,9 @@
package org.isoron.uhabits.widgets package org.isoron.uhabits.widgets
import android.app.PendingIntent import android.app.*
import android.content.* import android.content.*
import android.util.Log
import android.view.* import android.view.*
import org.isoron.uhabits.core.models.* import org.isoron.uhabits.core.models.*
import org.isoron.uhabits.utils.* import org.isoron.uhabits.utils.*
@ -32,22 +33,46 @@ open class CheckmarkWidget(
protected val habit: Habit protected val habit: Habit
) : BaseWidget(context, widgetId) { ) : BaseWidget(context, widgetId) {
override fun getOnClickPendingIntent(context: Context): PendingIntent{ override fun getOnClickPendingIntent(context: Context): PendingIntent {
return pendingIntentFactory.toggleCheckmark(habit, null) return if (habit.isNumerical) {
pendingIntentFactory.setNumericalValue(context, habit, 10, null)
} else {
pendingIntentFactory.toggleCheckmark(habit, null)
}
} }
override fun refreshData(v: View) { override fun refreshData(v: View) {
(v as CheckmarkWidgetView).apply { (v as CheckmarkWidgetView).apply {
setBackgroundAlpha(preferedBackgroundAlpha) setBackgroundAlpha(preferedBackgroundAlpha)
setPercentage(habit.scores.todayValue.toFloat())
setActiveColor(PaletteUtils.getColor(context, habit.color)) setActiveColor(PaletteUtils.getColor(context, habit.color))
setName(habit.name) setName(habit.name)
setCheckmarkValue(habit.checkmarks.todayValue) setCheckmarkValue(habit.checkmarks.todayValue)
if (habit.isNumerical) {
isNumerical = true;
checkmarkState = getNumericalCheckmarkState();
setPercentage((Checkmark.checkMarkValueToDouble(habit.checkmarks.todayValue) / habit.data.targetValue).toFloat())
} else {
checkmarkState = habit.checkmarks.todayValue;
setPercentage(habit.scores.todayValue.toFloat())
}
refresh() refresh()
} }
} }
override fun buildView(): View = CheckmarkWidgetView(context) override fun buildView(): View {
return CheckmarkWidgetView(context)
}
override fun getDefaultHeight() = 125 override fun getDefaultHeight() = 125
override fun getDefaultWidth() = 125 override fun getDefaultWidth() = 125
private fun getNumericalCheckmarkState(): Int {
return if (habit.isCompletedToday) {
Checkmark.CHECKED_EXPLICITLY
} else {
Checkmark.UNCHECKED
}
}
} }

@ -24,11 +24,7 @@ class CheckmarkWidgetProvider : BaseWidgetProvider() {
override fun getWidgetFromId(context: Context, id: Int): BaseWidget { override fun getWidgetFromId(context: Context, id: Int): BaseWidget {
val habits = getHabitsFromWidgetId(id) val habits = getHabitsFromWidgetId(id)
return if (habits.size == 1) { return if (habits.size == 1) {
if (habits[0].isNumerical){ CheckmarkWidget(context, id, habits[0])
NumericalCheckmarkWidget(context, id, habits[0])
}else {
CheckmarkWidget(context, id, habits[0])
}
} }
else StackWidget(context, id, StackWidgetType.CHECKMARK, habits) else StackWidget(context, id, StackWidgetType.CHECKMARK, habits)
} }

@ -1,49 +0,0 @@
package org.isoron.uhabits.widgets
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.View
import org.isoron.uhabits.core.models.Checkmark
import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.receivers.WidgetReceiver
import org.isoron.uhabits.receivers.WidgetReceiver.Companion.ACTION_SET_NUMERICAL_VALUE
import org.isoron.uhabits.utils.PaletteUtils
import org.isoron.uhabits.widgets.activities.NumericalCheckmarkWidgetActivity
import org.isoron.uhabits.widgets.views.CheckmarkWidgetView
import org.isoron.uhabits.widgets.views.NumericalCheckmarkWidgetView
class NumericalCheckmarkWidget(context: Context, widgetId: Int, habit: Habit) : CheckmarkWidget(context, widgetId, habit) {
private lateinit var view: NumericalCheckmarkWidgetView
override fun getOnClickPendingIntent(context: Context): PendingIntent {
return pendingIntentFactory.setNumericalValue(context, habit, 10,null)
}
override fun buildView(): View {
view = NumericalCheckmarkWidgetView(context)
return view;
}
override fun refreshData(v: View) {
(v as NumericalCheckmarkWidgetView).apply {
setPercentage(habit.scores.todayValue.toFloat())
setActiveColor(PaletteUtils.getColor(context, habit.color))
setName(habit.name)
setCheckmarkValue(habit.checkmarks.todayValue)
setCheckmarkState(getCheckmarkState())
refresh()
}
}
private fun getCheckmarkState():Int{
return if(habit.isCompletedToday){
Checkmark.CHECKED_EXPLICITLY
}else{
Checkmark.UNCHECKED
}
}
}

@ -33,9 +33,9 @@ import org.isoron.uhabits.activities.common.views.*;
import org.isoron.uhabits.utils.*; import org.isoron.uhabits.utils.*;
import static org.isoron.androidbase.utils.InterfaceUtils.getDimension; import static org.isoron.androidbase.utils.InterfaceUtils.getDimension;
import static org.isoron.uhabits.activities.habits.list.views.NumberButtonViewKt.*;
public class CheckmarkWidgetView extends HabitWidgetView public class CheckmarkWidgetView extends HabitWidgetView {
{
protected int activeColor; protected int activeColor;
protected float percentage; protected float percentage;
@ -49,20 +49,29 @@ public class CheckmarkWidgetView extends HabitWidgetView
protected int checkmarkValue; protected int checkmarkValue;
public CheckmarkWidgetView(Context context) protected int checkmarkState;
{
protected boolean isNumerical;
public boolean isNumerical() {
return isNumerical;
}
public void setNumerical(boolean numerical) {
isNumerical = numerical;
}
public CheckmarkWidgetView(Context context) {
super(context); super(context);
init(); init();
} }
public CheckmarkWidgetView(Context context, AttributeSet attrs) public CheckmarkWidgetView(Context context, AttributeSet attrs) {
{
super(context, attrs); super(context, attrs);
init(); init();
} }
public void refresh() public void refresh() {
{
if (backgroundPaint == null || frame == null || ring == null) return; if (backgroundPaint == null || frame == null || ring == null) return;
StyledResources res = new StyledResources(getContext()); StyledResources res = new StyledResources(getContext());
@ -71,8 +80,7 @@ public class CheckmarkWidgetView extends HabitWidgetView
int bgColor; int bgColor;
int fgColor; int fgColor;
switch (getCheckmarkState()) switch (getCheckmarkState()) {
{
case Checkmark.CHECKED_EXPLICITLY: case Checkmark.CHECKED_EXPLICITLY:
bgColor = activeColor; bgColor = activeColor;
fgColor = res.getColor(R.attr.highContrastReverseTextColor); fgColor = res.getColor(R.attr.highContrastReverseTextColor);
@ -115,58 +123,66 @@ public class CheckmarkWidgetView extends HabitWidgetView
* - Checkmark.CHECKED_IMPLICITLY * - Checkmark.CHECKED_IMPLICITLY
* - Checkmark.UNCHECKED * - Checkmark.UNCHECKED
*/ */
protected int getCheckmarkState(){ public int getCheckmarkState() {
return checkmarkValue; return checkmarkState;
} }
/** /**
* @Return the text that should be displayed in the middle of the widget * @brief set the state of the checkmark to either:
* - Checkmark.CHECKED_EXPLICITLY
* - Checkmark.CHECKED_IMPLICITLY
* - Checkmark.UNCHECKED
*/ */
protected String getText(){ public void setCheckmarkState(int checkmarkState) {
switch (getCheckmarkState()) this.checkmarkState = checkmarkState;
{ }
case Checkmark.CHECKED_EXPLICITLY:
return getResources().getString(R.string.fa_check);
case Checkmark.CHECKED_IMPLICITLY:
return getResources().getString(R.string.fa_check);
case Checkmark.UNCHECKED: /**
default: * @Return the text that should be displayed in the middle of the widget
return getResources().getString(R.string.fa_times); */
protected String getText() {
if (isNumerical) {
return doubleToShortString(Checkmark.checkMarkValueToDouble(checkmarkValue));
} else {
switch (getCheckmarkState()) {
case Checkmark.CHECKED_EXPLICITLY:
return getResources().getString(R.string.fa_check);
case Checkmark.CHECKED_IMPLICITLY:
return getResources().getString(R.string.fa_check);
case Checkmark.UNCHECKED:
default:
return getResources().getString(R.string.fa_times);
}
} }
} }
public void setActiveColor(int activeColor) public void setActiveColor(int activeColor) {
{
this.activeColor = activeColor; this.activeColor = activeColor;
} }
public void setCheckmarkValue(int checkmarkValue) public void setCheckmarkValue(int checkmarkValue) {
{
this.checkmarkValue = checkmarkValue; this.checkmarkValue = checkmarkValue;
} }
public void setName(@NonNull String name) public void setName(@NonNull String name) {
{
this.name = name; this.name = name;
} }
public void setPercentage(float percentage) public void setPercentage(float percentage) {
{
this.percentage = percentage; this.percentage = percentage;
} }
@Override @Override
@NonNull @NonNull
protected Integer getInnerLayoutId() protected Integer getInnerLayoutId() {
{
return R.layout.widget_checkmark; return R.layout.widget_checkmark;
} }
@Override @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
{
int width = MeasureSpec.getSize(widthMeasureSpec); int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec);
@ -183,9 +199,9 @@ public class CheckmarkWidgetView extends HabitWidgetView
ring.setVisibility(VISIBLE); ring.setVisibility(VISIBLE);
widthMeasureSpec = widthMeasureSpec =
MeasureSpec.makeMeasureSpec((int) w, MeasureSpec.EXACTLY); MeasureSpec.makeMeasureSpec((int) w, MeasureSpec.EXACTLY);
heightMeasureSpec = heightMeasureSpec =
MeasureSpec.makeMeasureSpec((int) h, MeasureSpec.EXACTLY); MeasureSpec.makeMeasureSpec((int) h, MeasureSpec.EXACTLY);
float textSize = 0.15f * h; float textSize = 0.15f * h;
float maxTextSize = getDimension(getContext(), R.dimen.smallerTextSize); float maxTextSize = getDimension(getContext(), R.dimen.smallerTextSize);
@ -198,15 +214,13 @@ public class CheckmarkWidgetView extends HabitWidgetView
super.onMeasure(widthMeasureSpec, heightMeasureSpec); super.onMeasure(widthMeasureSpec, heightMeasureSpec);
} }
private void init() private void init() {
{
ring = (RingView) findViewById(R.id.scoreRing); ring = (RingView) findViewById(R.id.scoreRing);
label = (TextView) findViewById(R.id.label); label = (TextView) findViewById(R.id.label);
if (ring != null) ring.setIsTransparencyEnabled(true); if (ring != null) ring.setIsTransparencyEnabled(true);
if (isInEditMode()) if (isInEditMode()) {
{
percentage = 0.75f; percentage = 0.75f;
name = "Wake up early"; name = "Wake up early";
activeColor = PaletteUtils.getAndroidTestColor(6); activeColor = PaletteUtils.getAndroidTestColor(6);

@ -1,42 +0,0 @@
package org.isoron.uhabits.widgets.views
import android.content.Context
import android.util.AttributeSet
import org.isoron.androidbase.utils.StyledResources
import org.isoron.uhabits.R
import org.isoron.uhabits.activities.common.dialogs.NumberPickerFactory
import org.isoron.uhabits.activities.habits.list.views.toShortString
import org.isoron.uhabits.core.models.Checkmark
import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsBehavior
class NumericalCheckmarkWidgetView : CheckmarkWidgetView {
private var checkmarkState : Int = Checkmark.UNCHECKED
constructor(context: Context) : super(context) {}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}
/**
* @param state the new state/style of the widget, either:
* - Checkmark.CHECKED_EXPLICITLY
* - Checkmark.CHECKED_IMPLICITLY
* - Checkmark.UNCHECKED
*/
fun setCheckmarkState(state : Int) {
checkmarkState = state
}
override fun getCheckmarkState(): Int {
return checkmarkState
}
override fun getText(): String {
val numberValue : Double = Habit.checkMarkValueToDouble(checkmarkValue)
return numberValue.toShortString()
}
}

@ -27,20 +27,7 @@
android:enabled="true" android:enabled="true"
android:icon="?iconAdd" android:icon="?iconAdd"
android:title="@string/add_habit" android:title="@string/add_habit"
app:showAsAction="always"> app:showAsAction="always"/>
<menu android:id="@+id/createHabitMenu">
<item
android:id="@+id/actionCreateBooleanHabit"
android:enabled="true"
android:title="@string/yes_or_no"/>
<item
android:id="@+id/actionCreateNumeralHabit"
android:enabled="true"
android:title="@string/number"/>
</menu>
</item>
<item <item

@ -115,4 +115,19 @@ public final class Checkmark
.append("value", value) .append("value", value)
.toString(); .toString();
} }
public static double checkMarkValueToDouble(int a)
{
return ((double) a)/1000.0;
}
public static double checkMarkValueToDouble(double a)
{
return a/1000.0;
}
public static int doubleToCheckMarkValue(double a)
{
return (int)Math.round(a*1000);
}
} }

@ -94,21 +94,6 @@ public class Habit
observable = new ModelObservable(); observable = new ModelObservable();
} }
public static double checkMarkValueToDouble(int a)
{
return ((double) a)/1000.0;
}
public static double checkMarkValueToDouble(double a)
{
return a/1000.0;
}
public static int doubleToCheckMarkValue(double a)
{
return (int)Math.round(a*1000);
}
/** /**
* Clears the reminder for a habit. * Clears the reminder for a habit.
*/ */

@ -278,7 +278,7 @@ public abstract class ScoreList implements Iterable<Score>
if (habit.isNumerical()) if (habit.isNumerical())
{ {
value = Habit.checkMarkValueToDouble(value); value = Checkmark.checkMarkValueToDouble(value);
value /= habit.getTargetValue(); value /= habit.getTargetValue();
value = Math.min(1, value); value = Math.min(1, value);
} }

@ -83,9 +83,9 @@ public class ListHabitsBehavior
CheckmarkList checkmarks = habit.getCheckmarks(); CheckmarkList checkmarks = habit.getCheckmarks();
double oldValue = checkmarks.getValues(timestamp, timestamp)[0]; double oldValue = checkmarks.getValues(timestamp, timestamp)[0];
screen.showNumberPicker(Habit.checkMarkValueToDouble(oldValue), habit.getUnit(), newValue -> screen.showNumberPicker(Checkmark.checkMarkValueToDouble(oldValue), habit.getUnit(), newValue ->
{ {
int intValue = Habit.doubleToCheckMarkValue(newValue); int intValue = Checkmark.doubleToCheckMarkValue(newValue);
commandRunner.execute( commandRunner.execute(
new CreateRepetitionCommand(habit, timestamp, intValue), new CreateRepetitionCommand(habit, timestamp, intValue),
habit.getId()); habit.getId());

@ -61,15 +61,7 @@ public class ListHabitsMenuBehavior
updateAdapterFilter(); updateAdapterFilter();
} }
public void onCreateBooleanHabit() public void onCreateHabit() { screen.showSelectHabitTypeDialog(); }
{
screen.showCreateBooleanHabitScreen();
}
public void onCreateNumericalHabit()
{
screen.showCreateNumericalHabitScreen();
}
public void onViewFAQ() public void onViewFAQ()
{ {
@ -150,10 +142,6 @@ public class ListHabitsMenuBehavior
void showAboutScreen(); void showAboutScreen();
void showCreateBooleanHabitScreen();
void showCreateNumericalHabitScreen();
void showFAQScreen(); void showFAQScreen();
void showSettingsScreen(); void showSettingsScreen();

Loading…
Cancel
Save