Update controllers and HabitCardView

pull/157/merge
Alinson S. Xavier 9 years ago
parent 7bf9f88ee3
commit e2a8de3acf

@ -59,7 +59,7 @@ public class HabitCardViewTest extends BaseViewTest
view = new HabitCardView(targetContext); view = new HabitCardView(targetContext);
view.setHabit(habit); view.setHabit(habit);
view.setCheckmarkValues(values); view.setValues(values);
view.setSelected(false); view.setSelected(false);
view.setScore(habit.getScores().getTodayValue()); view.setScore(habit.getScores().getTodayValue());
view.setController(controller); view.setController(controller);

@ -157,6 +157,18 @@ public class ListHabitsController
})); }));
} }
@Override
public void onInvalidEdit()
{
screen.showMessage(R.string.long_press_to_edit);
}
@Override
public void onEdit(@NonNull Habit habit, long timestamp)
{
}
@Override @Override
public void onInvalidToggle() public void onInvalidToggle()

@ -21,8 +21,8 @@ package org.isoron.uhabits.activities.habits.list.controllers;
import android.support.annotation.*; import android.support.annotation.*;
import org.isoron.uhabits.models.Habit; import org.isoron.uhabits.activities.habits.list.views.*;
import org.isoron.uhabits.activities.habits.list.views.HabitCardView; import org.isoron.uhabits.models.*;
public class HabitCardController implements HabitCardView.Controller public class HabitCardController implements HabitCardView.Controller
{ {
@ -32,6 +32,18 @@ public class HabitCardController implements HabitCardView.Controller
@Nullable @Nullable
private Listener listener; private Listener listener;
@Override
public void onEdit(@NonNull Habit habit, long timestamp)
{
if(listener != null) listener.onEdit(habit, timestamp);
}
@Override
public void onInvalidEdit()
{
if(listener != null) listener.onInvalidEdit();
}
@Override @Override
public void onInvalidToggle() public void onInvalidToggle()
{ {
@ -55,7 +67,9 @@ public class HabitCardController implements HabitCardView.Controller
this.view = view; this.view = view;
} }
public interface Listener extends CheckmarkButtonController.Listener public interface Listener extends CheckmarkButtonController.Listener,
NumberButtonController.Listener
{ {
} }
} }

@ -21,9 +21,9 @@ package org.isoron.uhabits.activities.habits.list.controllers;
import android.support.annotation.*; import android.support.annotation.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.activities.habits.list.model.*; import org.isoron.uhabits.activities.habits.list.model.*;
import org.isoron.uhabits.activities.habits.list.views.*; import org.isoron.uhabits.activities.habits.list.views.*;
import org.isoron.uhabits.models.*;
/** /**
* Controller responsible for receiving and processing the events generated by a * Controller responsible for receiving and processing the events generated by a
@ -75,6 +75,18 @@ public class HabitCardListController implements HabitCardListView.Controller
habitListener.onHabitReorder(habitFrom, habitTo); habitListener.onHabitReorder(habitFrom, habitTo);
} }
@Override
public void onEdit(@NonNull Habit habit, long timestamp)
{
if (habitListener != null) habitListener.onEdit(habit, timestamp);
}
@Override
public void onInvalidEdit()
{
if (habitListener != null) habitListener.onInvalidEdit();
}
/** /**
* Called when the user attempts to perform a toggle, but attempt is * Called when the user attempts to perform a toggle, but attempt is
* rejected. * rejected.
@ -172,7 +184,8 @@ public class HabitCardListController implements HabitCardListView.Controller
if (selectionListener != null) selectionListener.onSelectionFinish(); if (selectionListener != null) selectionListener.onSelectionFinish();
} }
public interface HabitListener extends CheckmarkButtonController.Listener public interface HabitListener extends CheckmarkButtonController.Listener,
NumberButtonController.Listener
{ {
/** /**
* Called when the user clicks a habit. * Called when the user clicks a habit.

@ -91,8 +91,8 @@ public class HabitCardListView extends RecyclerView
HabitCardView cardView = (HabitCardView) holder.itemView; HabitCardView cardView = (HabitCardView) holder.itemView;
cardView.setHabit(habit); cardView.setHabit(habit);
cardView.setSelected(selected); cardView.setSelected(selected);
cardView.setCheckmarkValues(checkmarks); cardView.setValues(checkmarks);
cardView.setCheckmarkCount(checkmarkCount); cardView.setButtonCount(checkmarkCount);
cardView.setDataOffset(dataOffset); cardView.setDataOffset(dataOffset);
cardView.setScore(score); cardView.setScore(score);
if (controller != null) setupCardViewController(holder); if (controller != null) setupCardViewController(holder);

@ -56,6 +56,9 @@ public class HabitCardView extends FrameLayout
@BindView(R.id.checkmarkPanel) @BindView(R.id.checkmarkPanel)
CheckmarkPanelView checkmarkPanel; CheckmarkPanelView checkmarkPanel;
@BindView(R.id.numberPanel)
NumberPanelView numberPanel;
@BindView(R.id.innerFrame) @BindView(R.id.innerFrame)
LinearLayout innerFrame; LinearLayout innerFrame;
@ -92,28 +95,38 @@ public class HabitCardView extends FrameLayout
new Handler(Looper.getMainLooper()).post(() -> refresh()); new Handler(Looper.getMainLooper()).post(() -> refresh());
} }
public void setCheckmarkCount(int checkmarkCount) public void setButtonCount(int buttonCount)
{ {
checkmarkPanel.setButtonCount(checkmarkCount); checkmarkPanel.setButtonCount(buttonCount);
numberPanel.setButtonCount(buttonCount);
} }
public void setCheckmarkValues(int checkmarks[]) public void setValues(int values[])
{ {
checkmarkPanel.setValues(checkmarks); checkmarkPanel.setValues(values);
int[] magnitudes = new int[]{10, 100, 1000, 10000};
int threshold = magnitudes[new Random().nextInt(4)];
numberPanel.setThreshold(threshold);
numberPanel.initEditMode();
postInvalidate(); postInvalidate();
} }
public void setController(Controller controller) public void setController(Controller controller)
{ {
checkmarkPanel.setController(null); checkmarkPanel.setController(null);
numberPanel.setController(null);
if (controller == null) return; if (controller == null) return;
checkmarkPanel.setController(controller); checkmarkPanel.setController(controller);
numberPanel.setController(controller);
} }
public void setDataOffset(int dataOffset) public void setDataOffset(int dataOffset)
{ {
this.dataOffset = dataOffset; this.dataOffset = dataOffset;
checkmarkPanel.setDataOffset(dataOffset); checkmarkPanel.setDataOffset(dataOffset);
numberPanel.setDataOffset(dataOffset);
} }
public void setHabit(@NonNull Habit habit) public void setHabit(@NonNull Habit habit)
@ -122,6 +135,7 @@ public class HabitCardView extends FrameLayout
this.habit = habit; this.habit = habit;
checkmarkPanel.setHabit(habit); checkmarkPanel.setHabit(habit);
numberPanel.setHabit(habit);
refresh(); refresh();
attachToHabit(); attachToHabit();
@ -191,7 +205,8 @@ public class HabitCardView extends FrameLayout
inflate(context, R.layout.list_habits_card, this); inflate(context, R.layout.list_habits_card, this);
ButterKnife.bind(this); ButterKnife.bind(this);
innerFrame.setOnTouchListener((v, event) -> { innerFrame.setOnTouchListener((v, event) ->
{
if (SDK_INT >= LOLLIPOP) if (SDK_INT >= LOLLIPOP)
v.getBackground().setHotspot(event.getX(), event.getY()); v.getBackground().setHotspot(event.getX(), event.getY());
return false; return false;
@ -205,15 +220,12 @@ public class HabitCardView extends FrameLayout
{ {
Random rand = new Random(); Random rand = new Random();
int color = ColorUtils.getAndroidTestColor(rand.nextInt(10)); int color = ColorUtils.getAndroidTestColor(rand.nextInt(10));
int[] values = new int[5];
for (int i = 0; i < 5; i++) values[i] = rand.nextInt(3);
label.setText(EDIT_MODE_HABITS[rand.nextInt(EDIT_MODE_HABITS.length)]); label.setText(EDIT_MODE_HABITS[rand.nextInt(EDIT_MODE_HABITS.length)]);
label.setTextColor(color); label.setTextColor(color);
scoreRing.setColor(color); scoreRing.setColor(color);
scoreRing.setPercentage(rand.nextFloat()); scoreRing.setPercentage(rand.nextFloat());
checkmarkPanel.setColor(color); checkmarkPanel.setColor(color);
checkmarkPanel.setValues(values); numberPanel.setColor(color);
} }
private void refresh() private void refresh()
@ -223,6 +235,12 @@ public class HabitCardView extends FrameLayout
label.setTextColor(color); label.setTextColor(color);
scoreRing.setColor(color); scoreRing.setColor(color);
checkmarkPanel.setColor(color); checkmarkPanel.setColor(color);
numberPanel.setColor(color);
boolean isNumberHabit = false; //(new Random().nextInt(3) == 0);
checkmarkPanel.setVisibility(isNumberHabit ? GONE : VISIBLE);
numberPanel.setVisibility(isNumberHabit ? VISIBLE : GONE);
postInvalidate(); postInvalidate();
} }
@ -256,5 +274,9 @@ public class HabitCardView extends FrameLayout
} }
} }
public interface Controller extends CheckmarkPanelView.Controller {} public interface Controller
extends CheckmarkPanelView.Controller, NumberPanelView.Controller
{
}
} }

@ -36,7 +36,7 @@ import static org.isoron.uhabits.utils.ColorUtils.*;
public class NumberButtonView extends TextView public class NumberButtonView extends TextView
{ {
private static Typeface TYPEFACE = private static Typeface TYPEFACE =
Typeface.create("sans-serif-condensed", Typeface.BOLD); Typeface.create("sans-serif-condensed", Typeface.NORMAL);
private int color; private int color;
@ -72,13 +72,13 @@ public class NumberButtonView extends TextView
private static String formatValue(int v) private static String formatValue(int v)
{ {
double fv = (double) v; double fv = (double) v;
if(v >= 1e9) return String.format("%.2fG", fv / 1e9); if(v >= 1e9) return String.format("%.1fG", fv / 1e9);
if(v >= 1e8) return String.format("%.0fM", fv / 1e6); if(v >= 1e8) return String.format("%.0fM", fv / 1e6);
if(v >= 1e7) return String.format("%.1fM", fv / 1e6); if(v >= 1e7) return String.format("%.1fM", fv / 1e6);
if(v >= 1e6) return String.format("%.2fM", fv / 1e6); if(v >= 1e6) return String.format("%.1fM", fv / 1e6);
if(v >= 1e5) return String.format("%.0fk", fv / 1e3); if(v >= 1e5) return String.format("%.0fk", fv / 1e3);
if(v >= 1e4) return String.format("%.1fk", fv / 1e3); if(v >= 1e4) return String.format("%.1fk", fv / 1e3);
if(v >= 1e3) return String.format("%.2fk", fv / 1e3); if(v >= 1e3) return String.format("%.1fk", fv / 1e3);
return String.format("%d", v); return String.format("%d", v);
} }

@ -84,7 +84,7 @@ public class NumberPanelView extends LinearLayout
if(isInEditMode()) initEditMode(); if(isInEditMode()) initEditMode();
} }
private void initEditMode() public void initEditMode()
{ {
int values[] = new int[nButtons]; int values[] = new int[nButtons];

@ -46,6 +46,11 @@
android:id="@+id/checkmarkPanel" android:id="@+id/checkmarkPanel"
style="@style/ListHabits.CheckmarkPanel"/> style="@style/ListHabits.CheckmarkPanel"/>
<org.isoron.uhabits.activities.habits.list.views.NumberPanelView
android:id="@+id/numberPanel"
android:visibility="gone"
style="@style/ListHabits.CheckmarkPanel"/>
</LinearLayout> </LinearLayout>
</merge> </merge>

@ -204,4 +204,6 @@
<string name="by_score">By score</string> <string name="by_score">By score</string>
<string name="download">Download</string> <string name="download">Download</string>
<string name="export">Export</string> <string name="export">Export</string>
<string name="long_press_to_edit">Press-and-hold to change the
value</string>
</resources> </resources>
Loading…
Cancel
Save