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.setHabit(habit);
view.setCheckmarkValues(values);
view.setValues(values);
view.setSelected(false);
view.setScore(habit.getScores().getTodayValue());
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
public void onInvalidToggle()

@ -21,8 +21,8 @@ package org.isoron.uhabits.activities.habits.list.controllers;
import android.support.annotation.*;
import org.isoron.uhabits.models.Habit;
import org.isoron.uhabits.activities.habits.list.views.HabitCardView;
import org.isoron.uhabits.activities.habits.list.views.*;
import org.isoron.uhabits.models.*;
public class HabitCardController implements HabitCardView.Controller
{
@ -32,6 +32,18 @@ public class HabitCardController implements HabitCardView.Controller
@Nullable
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
public void onInvalidToggle()
{
@ -55,7 +67,9 @@ public class HabitCardController implements HabitCardView.Controller
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 org.isoron.uhabits.models.*;
import org.isoron.uhabits.activities.habits.list.model.*;
import org.isoron.uhabits.activities.habits.list.views.*;
import org.isoron.uhabits.models.*;
/**
* 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);
}
@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
* rejected.
@ -172,7 +184,8 @@ public class HabitCardListController implements HabitCardListView.Controller
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.

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

@ -56,6 +56,9 @@ public class HabitCardView extends FrameLayout
@BindView(R.id.checkmarkPanel)
CheckmarkPanelView checkmarkPanel;
@BindView(R.id.numberPanel)
NumberPanelView numberPanel;
@BindView(R.id.innerFrame)
LinearLayout innerFrame;
@ -92,28 +95,38 @@ public class HabitCardView extends FrameLayout
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();
}
public void setController(Controller controller)
{
checkmarkPanel.setController(null);
numberPanel.setController(null);
if (controller == null) return;
checkmarkPanel.setController(controller);
numberPanel.setController(controller);
}
public void setDataOffset(int dataOffset)
{
this.dataOffset = dataOffset;
checkmarkPanel.setDataOffset(dataOffset);
numberPanel.setDataOffset(dataOffset);
}
public void setHabit(@NonNull Habit habit)
@ -122,6 +135,7 @@ public class HabitCardView extends FrameLayout
this.habit = habit;
checkmarkPanel.setHabit(habit);
numberPanel.setHabit(habit);
refresh();
attachToHabit();
@ -191,7 +205,8 @@ public class HabitCardView extends FrameLayout
inflate(context, R.layout.list_habits_card, this);
ButterKnife.bind(this);
innerFrame.setOnTouchListener((v, event) -> {
innerFrame.setOnTouchListener((v, event) ->
{
if (SDK_INT >= LOLLIPOP)
v.getBackground().setHotspot(event.getX(), event.getY());
return false;
@ -205,15 +220,12 @@ public class HabitCardView extends FrameLayout
{
Random rand = new Random();
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.setTextColor(color);
scoreRing.setColor(color);
scoreRing.setPercentage(rand.nextFloat());
checkmarkPanel.setColor(color);
checkmarkPanel.setValues(values);
numberPanel.setColor(color);
}
private void refresh()
@ -223,6 +235,12 @@ public class HabitCardView extends FrameLayout
label.setTextColor(color);
scoreRing.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();
}
@ -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
{
private static Typeface TYPEFACE =
Typeface.create("sans-serif-condensed", Typeface.BOLD);
Typeface.create("sans-serif-condensed", Typeface.NORMAL);
private int color;
@ -72,13 +72,13 @@ public class NumberButtonView extends TextView
private static String formatValue(int 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 >= 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 >= 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);
}

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

@ -46,6 +46,11 @@
android:id="@+id/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>
</merge>

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