Skip to content

Commit

Permalink
Refactor HeaderView; update list on resume
Browse files Browse the repository at this point in the history
  • Loading branch information
iSoron committed Jul 17, 2016
1 parent 28eb615 commit fa9f90a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/org/isoron/uhabits/BaseComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,6 @@ public interface BaseComponent
void inject(ListHabitsMenu listHabitsMenu);

void inject(PebbleReceiver receiver);

void inject(HeaderView headerView);
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ protected void onPause()
@Override
protected void onResume()
{
adapter.refresh();
rootView.postInvalidate();
super.onResume();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package org.isoron.uhabits.ui.habits.list.views;

import android.content.*;
import android.preference.*;
import android.graphics.*;
import android.util.*;
import android.view.*;
import android.widget.*;
Expand All @@ -30,20 +30,29 @@

import java.util.*;

import javax.inject.*;

public class HeaderView extends LinearLayout
{
private static final int CHECKMARK_LEFT_TO_RIGHT = 0;

private static final int CHECKMARK_RIGHT_TO_LEFT = 1;

private final Context context;

private int buttonCount;

@Inject
Preferences prefs;

public HeaderView(Context context, AttributeSet attrs)
{
super(context, attrs);
this.context = context;

if (isInEditMode())
{
setButtonCount(5);
return;
}

HabitsApplication.getComponent().inject(this);
}

public void setButtonCount(int buttonCount)
Expand All @@ -52,6 +61,25 @@ public void setButtonCount(int buttonCount)
createButtons();
}

@Override
protected void onDraw(Canvas canvas)
{
GregorianCalendar day = DateUtils.getStartOfTodayCalendar();

for (int i = 0; i < getChildCount(); i++)
{
int position = i;
if (shouldReverseCheckmarks()) position = getChildCount() - i - 1;

View button = getChildAt(position);
TextView label = (TextView) button.findViewById(R.id.tvCheck);
label.setText(DateUtils.formatHeaderDate(day));
day.add(GregorianCalendar.DAY_OF_MONTH, -1);
}

super.onDraw(canvas);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
Expand All @@ -61,32 +89,16 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

private void createButtons()
{
removeAllViews();
GregorianCalendar day = DateUtils.getStartOfTodayCalendar();
int layout = R.layout.list_habits_header_checkmark;

removeAllViews();
for (int i = 0; i < buttonCount; i++)
{
int position = 0;

if (getCheckmarkOrder() == CHECKMARK_LEFT_TO_RIGHT) position = i;

View tvDay =
inflate(context, R.layout.list_habits_header_checkmark, null);
TextView btCheck = (TextView) tvDay.findViewById(R.id.tvCheck);
btCheck.setText(DateUtils.formatHeaderDate(day));
addView(tvDay, position);
day.add(GregorianCalendar.DAY_OF_MONTH, -1);
}
addView(inflate(context, layout, null));
}

private int getCheckmarkOrder()
private boolean shouldReverseCheckmarks()
{
if (isInEditMode()) return CHECKMARK_LEFT_TO_RIGHT;

SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(getContext());
boolean reverse =
prefs.getBoolean("pref_checkmark_reverse_order", false);
return reverse ? CHECKMARK_RIGHT_TO_LEFT : CHECKMARK_LEFT_TO_RIGHT;
if (isInEditMode()) return false;
return prefs.shouldReverseCheckmarks();
}
}

0 comments on commit fa9f90a

Please sign in to comment.