mirror of https://github.com/iSoron/uhabits.git
parent
19e79a8559
commit
7bf9f88ee3
@ -0,0 +1,102 @@
|
||||
/*
|
||||
* 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.activities.habits.list.controllers;
|
||||
|
||||
import android.support.annotation.*;
|
||||
|
||||
import com.google.auto.factory.*;
|
||||
|
||||
import org.isoron.uhabits.activities.habits.list.views.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.preferences.*;
|
||||
|
||||
@AutoFactory
|
||||
public class NumberButtonController
|
||||
{
|
||||
@Nullable
|
||||
private NumberButtonView view;
|
||||
|
||||
@Nullable
|
||||
private Listener listener;
|
||||
|
||||
@NonNull
|
||||
private final Preferences prefs;
|
||||
|
||||
@NonNull
|
||||
private Habit habit;
|
||||
|
||||
private long timestamp;
|
||||
|
||||
public NumberButtonController(@Provided @NonNull Preferences prefs,
|
||||
@NonNull Habit habit,
|
||||
long timestamp)
|
||||
{
|
||||
this.habit = habit;
|
||||
this.timestamp = timestamp;
|
||||
this.prefs = prefs;
|
||||
}
|
||||
|
||||
public void onClick()
|
||||
{
|
||||
if (prefs.isShortToggleEnabled()) performEdit();
|
||||
else performInvalidToggle();
|
||||
}
|
||||
|
||||
public boolean onLongClick()
|
||||
{
|
||||
performEdit();
|
||||
return true;
|
||||
}
|
||||
|
||||
public void performInvalidToggle()
|
||||
{
|
||||
if (listener != null) listener.onInvalidEdit();
|
||||
}
|
||||
|
||||
public void performEdit()
|
||||
{
|
||||
if (listener != null) listener.onEdit(habit, timestamp);
|
||||
}
|
||||
|
||||
public void setListener(@Nullable Listener listener)
|
||||
{
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
public void setView(@Nullable NumberButtonView view)
|
||||
{
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
public interface Listener
|
||||
{
|
||||
/**
|
||||
* Called when the user's attempt to edit the value is rejected.
|
||||
*/
|
||||
void onInvalidEdit();
|
||||
|
||||
/**
|
||||
* Called when a the user's attempt to edit the value has been accepted.
|
||||
* @param habit the habit being edited
|
||||
* @param timestamp the timestamp being edited
|
||||
*/
|
||||
void onEdit(@NonNull Habit habit, long timestamp);
|
||||
}
|
||||
}
|
@ -0,0 +1,131 @@
|
||||
/*
|
||||
* 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.activities.habits.list.views;
|
||||
|
||||
import android.content.*;
|
||||
import android.graphics.*;
|
||||
import android.support.annotation.*;
|
||||
import android.util.*;
|
||||
import android.view.*;
|
||||
import android.widget.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.activities.habits.list.controllers.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
|
||||
import static org.isoron.uhabits.utils.AttributeSetUtils.*;
|
||||
import static org.isoron.uhabits.utils.ColorUtils.*;
|
||||
|
||||
public class NumberButtonView extends TextView
|
||||
{
|
||||
private static Typeface TYPEFACE =
|
||||
Typeface.create("sans-serif-condensed", Typeface.BOLD);
|
||||
|
||||
private int color;
|
||||
|
||||
private int value;
|
||||
|
||||
private int threshold;
|
||||
|
||||
private StyledResources res;
|
||||
|
||||
public NumberButtonView(@Nullable Context context)
|
||||
{
|
||||
super(context);
|
||||
init();
|
||||
}
|
||||
|
||||
public NumberButtonView(@Nullable Context context,
|
||||
@Nullable AttributeSet attrs)
|
||||
{
|
||||
super(context, attrs);
|
||||
init();
|
||||
|
||||
if (context != null && attrs != null)
|
||||
{
|
||||
int color = getIntAttribute(context, attrs, "color", 0);
|
||||
int value = getIntAttribute(context, attrs, "value", 0);
|
||||
int threshold = getIntAttribute(context, attrs, "threshold", 1);
|
||||
setColor(getAndroidTestColor(color));
|
||||
setThreshold(threshold);
|
||||
setValue(value);
|
||||
}
|
||||
}
|
||||
|
||||
private static String formatValue(int v)
|
||||
{
|
||||
double fv = (double) v;
|
||||
if(v >= 1e9) return String.format("%.2fG", fv / 1e9);
|
||||
if(v >= 1e8) return String.format("%.0fM", fv / 1e6);
|
||||
if(v >= 1e7) return String.format("%.1fM", fv / 1e6);
|
||||
if(v >= 1e6) return String.format("%.2fM", fv / 1e6);
|
||||
if(v >= 1e5) return String.format("%.0fk", fv / 1e3);
|
||||
if(v >= 1e4) return String.format("%.1fk", fv / 1e3);
|
||||
if(v >= 1e3) return String.format("%.2fk", fv / 1e3);
|
||||
return String.format("%d", v);
|
||||
}
|
||||
|
||||
public void setColor(int color)
|
||||
{
|
||||
this.color = color;
|
||||
postInvalidate();
|
||||
}
|
||||
|
||||
public void setController(final NumberButtonController controller)
|
||||
{
|
||||
setOnClickListener(v -> controller.onClick());
|
||||
setOnLongClickListener(v -> controller.onLongClick());
|
||||
}
|
||||
|
||||
public void setThreshold(int threshold)
|
||||
{
|
||||
this.threshold = threshold;
|
||||
updateText();
|
||||
}
|
||||
|
||||
public void setValue(int value)
|
||||
{
|
||||
this.value = value;
|
||||
updateText();
|
||||
}
|
||||
|
||||
private void init()
|
||||
{
|
||||
res = new StyledResources(getContext());
|
||||
|
||||
setWillNotDraw(false);
|
||||
|
||||
setMinHeight(
|
||||
getResources().getDimensionPixelSize(R.dimen.checkmarkHeight));
|
||||
setMinWidth(
|
||||
getResources().getDimensionPixelSize(R.dimen.checkmarkWidth));
|
||||
|
||||
setFocusable(false);
|
||||
setGravity(Gravity.CENTER);
|
||||
setTypeface(TYPEFACE);
|
||||
}
|
||||
|
||||
private void updateText()
|
||||
{
|
||||
int lowColor = res.getColor(R.attr.lowContrastTextColor);
|
||||
setTextColor(value >= threshold ? color : lowColor);
|
||||
setText(formatValue(value));
|
||||
}
|
||||
}
|
@ -0,0 +1,255 @@
|
||||
/*
|
||||
* 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.activities.habits.list.views;
|
||||
|
||||
import android.content.*;
|
||||
import android.support.annotation.*;
|
||||
import android.util.*;
|
||||
import android.widget.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.activities.habits.list.*;
|
||||
import org.isoron.uhabits.activities.habits.list.controllers.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.preferences.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static android.view.View.MeasureSpec.*;
|
||||
import static org.isoron.uhabits.utils.AttributeSetUtils.*;
|
||||
import static org.isoron.uhabits.utils.ColorUtils.*;
|
||||
|
||||
public class NumberPanelView extends LinearLayout
|
||||
implements Preferences.Listener
|
||||
{
|
||||
private static final int LEFT_TO_RIGHT = 0;
|
||||
|
||||
private static final int RIGHT_TO_LEFT = 1;
|
||||
|
||||
@Nullable
|
||||
private Preferences prefs;
|
||||
|
||||
private int values[];
|
||||
|
||||
private int threshold;
|
||||
|
||||
private int nButtons;
|
||||
|
||||
private int color;
|
||||
|
||||
private Controller controller;
|
||||
|
||||
@NonNull
|
||||
private Habit habit;
|
||||
|
||||
private int dataOffset;
|
||||
|
||||
public NumberPanelView(Context context)
|
||||
{
|
||||
super(context);
|
||||
init();
|
||||
}
|
||||
|
||||
public NumberPanelView(Context ctx, AttributeSet attrs)
|
||||
{
|
||||
super(ctx, attrs);
|
||||
init();
|
||||
|
||||
if (ctx != null && attrs != null)
|
||||
{
|
||||
int paletteColor = getIntAttribute(ctx, attrs, "color", 0);
|
||||
setColor(getAndroidTestColor(paletteColor));
|
||||
setButtonCount(getIntAttribute(ctx, attrs, "button_count", 5));
|
||||
setThreshold(getIntAttribute(ctx, attrs, "threshold", 1));
|
||||
}
|
||||
|
||||
if(isInEditMode()) initEditMode();
|
||||
}
|
||||
|
||||
private void initEditMode()
|
||||
{
|
||||
int values[] = new int[nButtons];
|
||||
|
||||
for(int i = 0; i < nButtons; i++)
|
||||
values[i] = new Random().nextInt(threshold * 3);
|
||||
|
||||
setValues(values);
|
||||
}
|
||||
|
||||
public NumberButtonView indexToButton(int i)
|
||||
{
|
||||
int position = i;
|
||||
|
||||
if (getCheckmarkOrder() == RIGHT_TO_LEFT) position = nButtons - i - 1;
|
||||
|
||||
return (NumberButtonView) getChildAt(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCheckmarkOrderChanged()
|
||||
{
|
||||
setupButtons();
|
||||
}
|
||||
|
||||
public void setButtonCount(int newButtonCount)
|
||||
{
|
||||
if (nButtons != newButtonCount)
|
||||
{
|
||||
nButtons = newButtonCount;
|
||||
addButtons();
|
||||
}
|
||||
|
||||
setupButtons();
|
||||
}
|
||||
|
||||
public void setColor(int color)
|
||||
{
|
||||
this.color = color;
|
||||
setupButtons();
|
||||
}
|
||||
|
||||
public void setController(Controller controller)
|
||||
{
|
||||
this.controller = controller;
|
||||
setupButtons();
|
||||
}
|
||||
|
||||
public void setDataOffset(int dataOffset)
|
||||
{
|
||||
this.dataOffset = dataOffset;
|
||||
setupButtons();
|
||||
}
|
||||
|
||||
public void setHabit(@NonNull Habit habit)
|
||||
{
|
||||
this.habit = habit;
|
||||
setupButtons();
|
||||
}
|
||||
|
||||
public void setThreshold(int threshold)
|
||||
{
|
||||
this.threshold = threshold;
|
||||
setupButtons();
|
||||
}
|
||||
|
||||
public void setValues(int[] values)
|
||||
{
|
||||
this.values = values;
|
||||
setupButtons();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onAttachedToWindow()
|
||||
{
|
||||
super.onAttachedToWindow();
|
||||
if (prefs != null) prefs.addListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDetachedFromWindow()
|
||||
{
|
||||
if (prefs != null) prefs.removeListener(this);
|
||||
super.onDetachedFromWindow();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthSpec, int heightSpec)
|
||||
{
|
||||
float buttonWidth = getResources().getDimension(R.dimen.checkmarkWidth);
|
||||
float buttonHeight =
|
||||
getResources().getDimension(R.dimen.checkmarkHeight);
|
||||
|
||||
float width = buttonWidth * nButtons;
|
||||
|
||||
widthSpec = makeMeasureSpec((int) width, EXACTLY);
|
||||
heightSpec = makeMeasureSpec((int) buttonHeight, EXACTLY);
|
||||
|
||||
super.onMeasure(widthSpec, heightSpec);
|
||||
}
|
||||
|
||||
private void addButtons()
|
||||
{
|
||||
removeAllViews();
|
||||
|
||||
for (int i = 0; i < nButtons; i++)
|
||||
addView(new NumberButtonView(getContext()));
|
||||
}
|
||||
|
||||
private int getCheckmarkOrder()
|
||||
{
|
||||
if (prefs == null) return LEFT_TO_RIGHT;
|
||||
return prefs.shouldReverseCheckmarks() ? RIGHT_TO_LEFT : LEFT_TO_RIGHT;
|
||||
}
|
||||
|
||||
private void init()
|
||||
{
|
||||
Context appContext = getContext().getApplicationContext();
|
||||
if (appContext instanceof HabitsApplication)
|
||||
{
|
||||
HabitsApplication app = (HabitsApplication) appContext;
|
||||
prefs = app.getComponent().getPreferences();
|
||||
}
|
||||
|
||||
setWillNotDraw(false);
|
||||
values = new int[0];
|
||||
}
|
||||
|
||||
private void setupButtonControllers(long timestamp,
|
||||
NumberButtonView buttonView)
|
||||
{
|
||||
if (controller == null) return;
|
||||
if (!(getContext() instanceof ListHabitsActivity)) return;
|
||||
|
||||
ListHabitsActivity activity = (ListHabitsActivity) getContext();
|
||||
NumberButtonControllerFactory buttonControllerFactory = activity
|
||||
.getListHabitsComponent()
|
||||
.getNumberButtonControllerFactory();
|
||||
|
||||
NumberButtonController buttonController =
|
||||
buttonControllerFactory.create(habit, timestamp);
|
||||
buttonController.setListener(controller);
|
||||
buttonController.setView(buttonView);
|
||||
buttonView.setController(buttonController);
|
||||
}
|
||||
|
||||
private void setupButtons()
|
||||
{
|
||||
long timestamp = DateUtils.getStartOfToday();
|
||||
long day = DateUtils.millisecondsInOneDay;
|
||||
timestamp -= day * dataOffset;
|
||||
|
||||
for (int i = 0; i < nButtons; i++)
|
||||
{
|
||||
NumberButtonView buttonView = indexToButton(i);
|
||||
if (i + dataOffset >= values.length) break;
|
||||
buttonView.setValue(values[i + dataOffset]);
|
||||
buttonView.setColor(color);
|
||||
buttonView.setThreshold(threshold);
|
||||
setupButtonControllers(timestamp, buttonView);
|
||||
timestamp -= day;
|
||||
}
|
||||
}
|
||||
|
||||
public interface Controller extends NumberButtonController.Listener
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
~ 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/>.
|
||||
-->
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://isoron.org/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="?windowBackgroundColor">
|
||||
|
||||
<org.isoron.uhabits.activities.habits.list.views.CheckmarkButtonView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:color="0"
|
||||
app:value="2"/>
|
||||
|
||||
<org.isoron.uhabits.activities.habits.list.views.CheckmarkButtonView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:color="0"
|
||||
app:value="0"/>
|
||||
|
||||
|
||||
<org.isoron.uhabits.activities.habits.list.views.CheckmarkButtonView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:color="0"
|
||||
app:value="1"/>
|
||||
|
||||
<org.isoron.uhabits.activities.habits.list.views.NumberButtonView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:threshold="10"
|
||||
app:color="1"
|
||||
app:value="5"/>
|
||||
|
||||
<org.isoron.uhabits.activities.habits.list.views.NumberButtonView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:threshold="10"
|
||||
app:color="1"
|
||||
app:value="50"/>
|
||||
|
||||
<org.isoron.uhabits.activities.habits.list.views.NumberButtonView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:threshold="10"
|
||||
app:color="1"
|
||||
app:value="25304"/>
|
||||
|
||||
</LinearLayout>
|
@ -0,0 +1,80 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
~ 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/>.
|
||||
-->
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://isoron.org/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="?windowBackgroundColor"
|
||||
android:orientation="vertical">
|
||||
|
||||
<org.isoron.uhabits.activities.habits.list.views.NumberPanelView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:button_count="8"
|
||||
app:color="1"
|
||||
app:threshold="10"
|
||||
/>
|
||||
|
||||
<org.isoron.uhabits.activities.habits.list.views.NumberPanelView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:button_count="8"
|
||||
app:color="2"
|
||||
app:threshold="5000"
|
||||
/>
|
||||
|
||||
<org.isoron.uhabits.activities.habits.list.views.NumberPanelView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:button_count="8"
|
||||
app:color="6"
|
||||
app:threshold="10"
|
||||
/>
|
||||
|
||||
<org.isoron.uhabits.activities.habits.list.views.CheckmarkPanelView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:button_count="8"
|
||||
app:color="6"
|
||||
/>
|
||||
<org.isoron.uhabits.activities.habits.list.views.CheckmarkPanelView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:button_count="8"
|
||||
app:color="4"
|
||||
/>
|
||||
|
||||
<org.isoron.uhabits.activities.habits.list.views.NumberPanelView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:button_count="8"
|
||||
app:color="10"
|
||||
app:threshold="5"
|
||||
/>
|
||||
|
||||
<org.isoron.uhabits.activities.habits.list.views.CheckmarkPanelView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:button_count="8"
|
||||
app:color="10"
|
||||
/>
|
||||
|
||||
</LinearLayout>
|
Loading…
Reference in new issue