mirror of https://github.com/iSoron/uhabits.git
parent
c8c4df6ef7
commit
8a60dda74e
@ -0,0 +1,130 @@
|
||||
package org.isoron.uhabits.fragments;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Typeface;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.isoron.helpers.DateHelper;
|
||||
import org.isoron.uhabits.R;
|
||||
import org.isoron.uhabits.helpers.ListHabitsHelper;
|
||||
import org.isoron.uhabits.loaders.HabitListLoader;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
class HabitListAdapter extends BaseAdapter
|
||||
{
|
||||
private final int buttonCount;
|
||||
private final int tvNameWidth;
|
||||
private LayoutInflater inflater;
|
||||
private Typeface fontawesome;
|
||||
private HabitListLoader loader;
|
||||
private ListHabitsHelper helper;
|
||||
private List selectedPositions;
|
||||
private View.OnLongClickListener onCheckmarkLongClickListener;
|
||||
private View.OnClickListener onCheckmarkClickListener;
|
||||
|
||||
public HabitListAdapter(Context context, HabitListLoader loader)
|
||||
{
|
||||
this.loader = loader;
|
||||
|
||||
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
fontawesome = Typeface.createFromAsset(context.getAssets(), "fontawesome-webfont.ttf");
|
||||
helper = new ListHabitsHelper(context, loader);
|
||||
|
||||
buttonCount = helper.getButtonCount();
|
||||
tvNameWidth = helper.getHabitNameWidth();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount()
|
||||
{
|
||||
return loader.habits.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getItem(int position)
|
||||
{
|
||||
return loader.habitsList.get(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position)
|
||||
{
|
||||
return ((Habit) getItem(position)).getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View view, ViewGroup parent)
|
||||
{
|
||||
final Habit habit = loader.habitsList.get(position);
|
||||
|
||||
if (view == null || (Long) view.getTag(R.id.timestamp_key) != DateHelper.getStartOfToday())
|
||||
{
|
||||
view = inflater.inflate(R.layout.list_habits_item, null);
|
||||
((TextView) view.findViewById(R.id.tvStar)).setTypeface(fontawesome);
|
||||
|
||||
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(tvNameWidth,
|
||||
LinearLayout.LayoutParams.WRAP_CONTENT, 1);
|
||||
view.findViewById(R.id.label).setLayoutParams(params);
|
||||
|
||||
inflateCheckmarkButtons(view);
|
||||
|
||||
view.setTag(R.id.timestamp_key, DateHelper.getStartOfToday());
|
||||
}
|
||||
|
||||
TextView tvStar = ((TextView) view.findViewById(R.id.tvStar));
|
||||
TextView tvName = (TextView) view.findViewById(R.id.label);
|
||||
LinearLayout llInner = (LinearLayout) view.findViewById(R.id.llInner);
|
||||
LinearLayout llButtons = (LinearLayout) view.findViewById(R.id.llButtons);
|
||||
|
||||
llInner.setTag(R.string.habit_key, habit.getId());
|
||||
|
||||
helper.updateNameAndIcon(habit, tvStar, tvName);
|
||||
helper.updateCheckmarkButtons(habit, llButtons);
|
||||
|
||||
boolean selected = selectedPositions.contains(position);
|
||||
if (selected) llInner.setBackgroundResource(R.drawable.selected_box);
|
||||
else
|
||||
{
|
||||
if (android.os.Build.VERSION.SDK_INT >= 21)
|
||||
llInner.setBackgroundResource(R.drawable.ripple_white);
|
||||
else llInner.setBackgroundResource(R.drawable.card_background);
|
||||
}
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
private void inflateCheckmarkButtons(View view)
|
||||
{
|
||||
for (int i = 0; i < buttonCount; i++)
|
||||
{
|
||||
View check = inflater.inflate(R.layout.list_habits_item_check, null);
|
||||
TextView btCheck = (TextView) check.findViewById(R.id.tvCheck);
|
||||
btCheck.setTypeface(fontawesome);
|
||||
btCheck.setOnLongClickListener(onCheckmarkLongClickListener);
|
||||
btCheck.setOnClickListener(onCheckmarkClickListener);
|
||||
((LinearLayout) view.findViewById(R.id.llButtons)).addView(check);
|
||||
}
|
||||
}
|
||||
|
||||
public void setSelectedPositions(List selectedPositions)
|
||||
{
|
||||
this.selectedPositions = selectedPositions;
|
||||
}
|
||||
|
||||
public void setOnCheckmarkLongClickListener(View.OnLongClickListener listener)
|
||||
{
|
||||
this.onCheckmarkLongClickListener = listener;
|
||||
}
|
||||
|
||||
public void setOnCheckmarkClickListener(View.OnClickListener listener)
|
||||
{
|
||||
this.onCheckmarkClickListener = listener;
|
||||
}
|
||||
}
|
@ -0,0 +1,127 @@
|
||||
package org.isoron.uhabits.helpers;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.isoron.uhabits.R;
|
||||
import org.isoron.uhabits.loaders.HabitListLoader;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
import org.isoron.uhabits.models.Score;
|
||||
|
||||
public class ListHabitsHelper
|
||||
{
|
||||
public static final int INACTIVE_COLOR = Color.rgb(200, 200, 200);
|
||||
public static final int INACTIVE_CHECKMARK_COLOR = Color.rgb(230, 230, 230);
|
||||
|
||||
private final Context context;
|
||||
private final HabitListLoader loader;
|
||||
|
||||
public ListHabitsHelper(Context context, HabitListLoader loader)
|
||||
{
|
||||
this.context = context;
|
||||
this.loader = loader;
|
||||
}
|
||||
|
||||
public int getButtonCount()
|
||||
{
|
||||
DisplayMetrics dm = context.getResources().getDisplayMetrics();
|
||||
int width = (int) (dm.widthPixels / dm.density);
|
||||
return Math.max(0, (int) ((width - 160) / 42.0));
|
||||
}
|
||||
|
||||
public int getHabitNameWidth()
|
||||
{
|
||||
DisplayMetrics dm = context.getResources().getDisplayMetrics();
|
||||
int width = (int) (dm.widthPixels / dm.density);
|
||||
return (int) ((width - 30 - getButtonCount() * 42) * dm.density);
|
||||
}
|
||||
|
||||
public void updateCheckmarkButtons(Habit habit, LinearLayout llButtons)
|
||||
{
|
||||
int activeColor = getActiveColor(habit);
|
||||
int m = llButtons.getChildCount();
|
||||
Long habitId = habit.getId();
|
||||
|
||||
int isChecked[] = loader.checkmarks.get(habitId);
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
|
||||
TextView tvCheck = (TextView) llButtons.getChildAt(i);
|
||||
tvCheck.setTag(R.string.habit_key, habitId);
|
||||
tvCheck.setTag(R.string.offset_key, i);
|
||||
if(isChecked.length > i)
|
||||
updateCheckmark(activeColor, tvCheck, isChecked[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public int getActiveColor(Habit habit)
|
||||
{
|
||||
int activeColor = habit.color;
|
||||
if(habit.isArchived()) activeColor = INACTIVE_COLOR;
|
||||
|
||||
return activeColor;
|
||||
}
|
||||
|
||||
public void updateNameAndIcon(Habit habit, TextView tvStar,
|
||||
TextView tvName)
|
||||
{
|
||||
int activeColor = getActiveColor(habit);
|
||||
|
||||
tvName.setText(habit.name);
|
||||
tvName.setTextColor(activeColor);
|
||||
|
||||
if (habit.isArchived())
|
||||
{
|
||||
tvStar.setText(context.getString(R.string.fa_archive));
|
||||
tvStar.setTextColor(activeColor);
|
||||
}
|
||||
else
|
||||
{
|
||||
int score = loader.scores.get(habit.getId());
|
||||
|
||||
if (score < Score.HALF_STAR_CUTOFF)
|
||||
{
|
||||
tvStar.setText(context.getString(R.string.fa_star_o));
|
||||
tvStar.setTextColor(INACTIVE_COLOR);
|
||||
}
|
||||
else if (score < Score.FULL_STAR_CUTOFF)
|
||||
{
|
||||
tvStar.setText(context.getString(R.string.fa_star_half_o));
|
||||
tvStar.setTextColor(INACTIVE_COLOR);
|
||||
}
|
||||
else
|
||||
{
|
||||
tvStar.setText(context.getString(R.string.fa_star));
|
||||
tvStar.setTextColor(activeColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void updateCheckmark(int activeColor, TextView tvCheck, int check)
|
||||
{
|
||||
switch (check)
|
||||
{
|
||||
case 2:
|
||||
tvCheck.setText(R.string.fa_check);
|
||||
tvCheck.setTextColor(activeColor);
|
||||
tvCheck.setTag(R.string.toggle_key, 2);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
tvCheck.setText(R.string.fa_check);
|
||||
tvCheck.setTextColor(INACTIVE_CHECKMARK_COLOR);
|
||||
tvCheck.setTag(R.string.toggle_key, 1);
|
||||
break;
|
||||
|
||||
case 0:
|
||||
tvCheck.setText(R.string.fa_times);
|
||||
tvCheck.setTextColor(INACTIVE_CHECKMARK_COLOR);
|
||||
tvCheck.setTag(R.string.toggle_key, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue