Restore dynamic checkmark button count

pull/145/head
Alinson S. Xavier 9 years ago
parent 6484b96e5a
commit abe6b10964

@ -19,18 +19,14 @@
package org.isoron.uhabits; package org.isoron.uhabits;
import org.isoron.uhabits.commands.CommandRunner; import org.isoron.uhabits.commands.*;
import org.isoron.uhabits.models.HabitList; import org.isoron.uhabits.models.*;
import org.isoron.uhabits.models.ModelFactory; import org.isoron.uhabits.models.sqlite.*;
import org.isoron.uhabits.models.sqlite.SQLModelFactory; import org.isoron.uhabits.utils.*;
import org.isoron.uhabits.models.sqlite.SQLiteHabitList;
import org.isoron.uhabits.ui.habits.list.model.HabitCardListCache;
import org.isoron.uhabits.utils.Preferences;
import javax.inject.Singleton; import javax.inject.*;
import dagger.Module; import dagger.*;
import dagger.Provides;
/** /**
* Module that provides dependencies when the application is running on * Module that provides dependencies when the application is running on
@ -48,13 +44,6 @@ public class AndroidModule
return new CommandRunner(); return new CommandRunner();
} }
@Provides
@Singleton
HabitCardListCache provideHabitCardListCache()
{
return new HabitCardListCache();
}
@Provides @Provides
@Singleton @Singleton
HabitList provideHabitList() HabitList provideHabitList()

@ -19,31 +19,28 @@
package org.isoron.uhabits.ui.habits.list; package org.isoron.uhabits.ui.habits.list;
import android.content.Context; import android.content.*;
import android.support.annotation.NonNull; import android.content.res.*;
import android.support.annotation.Nullable; import android.support.annotation.*;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.View; import android.view.*;
import android.view.ViewGroup; import android.widget.*;
import android.widget.ProgressBar;
import android.widget.TextView; import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.R; import org.isoron.uhabits.ui.*;
import org.isoron.uhabits.models.ModelObservable; import org.isoron.uhabits.ui.habits.list.controllers.*;
import org.isoron.uhabits.ui.BaseRootView; import org.isoron.uhabits.ui.habits.list.model.*;
import org.isoron.uhabits.ui.habits.list.controllers.HabitCardListController; import org.isoron.uhabits.ui.habits.list.views.*;
import org.isoron.uhabits.ui.habits.list.model.HabitCardListAdapter; import org.isoron.uhabits.utils.*;
import org.isoron.uhabits.ui.habits.list.model.HintList;
import org.isoron.uhabits.ui.habits.list.views.HabitCardListView; import butterknife.*;
import org.isoron.uhabits.ui.habits.list.views.HintView;
import org.isoron.uhabits.utils.InterfaceUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
public class ListHabitsRootView extends BaseRootView public class ListHabitsRootView extends BaseRootView
implements ModelObservable.Listener implements ModelObservable.Listener
{ {
public static final int MAX_CHECKMARK_COUNT = 21;
@BindView(R.id.listView) @BindView(R.id.listView)
HabitCardListView listView; HabitCardListView listView;
@ -71,6 +68,15 @@ public class ListHabitsRootView extends BaseRootView
init(); init();
} }
public static int getCheckmarkCount(View v)
{
Resources res = v.getResources();
float labelWidth = res.getDimension(R.dimen.habitNameWidth);
float buttonWidth = res.getDimension(R.dimen.checkmarkWidth);
return Math.min(MAX_CHECKMARK_COUNT, Math.max(0,
(int) ((v.getMeasuredWidth() - labelWidth) / buttonWidth)));
}
@Override @Override
@NonNull @NonNull
public ProgressBar getProgressBar() public ProgressBar getProgressBar()
@ -80,10 +86,16 @@ public class ListHabitsRootView extends BaseRootView
public boolean getShowArchived() public boolean getShowArchived()
{ {
if(listAdapter == null) return false; if (listAdapter == null) return false;
return listAdapter.getIncludeArchived(); return listAdapter.getIncludeArchived();
} }
public void setShowArchived(boolean showArchived)
{
if (listAdapter == null) return;
listAdapter.setShowArchived(showArchived);
}
@NonNull @NonNull
@Override @Override
public Toolbar getToolbar() public Toolbar getToolbar()
@ -103,19 +115,6 @@ public class ListHabitsRootView extends BaseRootView
updateEmptyView(); updateEmptyView();
} }
public void setShowArchived(boolean showArchived)
{
if(listAdapter == null) return;
listAdapter.setShowArchived(showArchived);
}
private void updateEmptyView()
{
if (listAdapter == null) return;
llEmpty.setVisibility(
listAdapter.getCount() > 0 ? View.GONE : View.VISIBLE);
}
public void setController(@Nullable ListHabitsController controller, public void setController(@Nullable ListHabitsController controller,
@Nullable ListHabitsSelectionMenu menu) @Nullable ListHabitsSelectionMenu menu)
{ {
@ -139,20 +138,6 @@ public class ListHabitsRootView extends BaseRootView
listAdapter.setListView(listView); listAdapter.setListView(listView);
} }
private void init()
{
addView(inflate(getContext(), R.layout.list_habits, null));
ButterKnife.bind(this);
tvStarEmpty.setTypeface(InterfaceUtils.getFontAwesome(getContext()));
initToolbar();
String hints[] =
getContext().getResources().getStringArray(R.array.hints);
HintList hintList = new HintList(hints);
hintView.setHints(hintList);
}
@Override @Override
protected void onAttachedToWindow() protected void onAttachedToWindow()
{ {
@ -170,4 +155,25 @@ public class ListHabitsRootView extends BaseRootView
listAdapter.getObservable().removeListener(this); listAdapter.getObservable().removeListener(this);
super.onDetachedFromWindow(); super.onDetachedFromWindow();
} }
private void init()
{
addView(inflate(getContext(), R.layout.list_habits, null));
ButterKnife.bind(this);
tvStarEmpty.setTypeface(InterfaceUtils.getFontAwesome(getContext()));
initToolbar();
String hints[] =
getContext().getResources().getStringArray(R.array.hints);
HintList hintList = new HintList(hints);
hintView.setHints(hintList);
}
private void updateEmptyView()
{
if (listAdapter == null) return;
llEmpty.setVisibility(
listAdapter.getCount() > 0 ? View.GONE : View.VISIBLE);
}
} }

@ -51,6 +51,7 @@ import java.io.File;
public class ListHabitsScreen extends BaseScreen public class ListHabitsScreen extends BaseScreen
{ {
@Nullable @Nullable
ListHabitsController controller; ListHabitsController controller;
@ -71,7 +72,8 @@ public class ListHabitsScreen extends BaseScreen
setMenu(menu); setMenu(menu);
setSelectionMenu(selectionMenu); setSelectionMenu(selectionMenu);
HabitCardListAdapter adapter = new HabitCardListAdapter(); HabitCardListAdapter adapter = new HabitCardListAdapter(
ListHabitsRootView.MAX_CHECKMARK_COUNT);
rootView.setListAdapter(adapter); rootView.setListAdapter(adapter);
selectionMenu.setAdapter(adapter); selectionMenu.setAdapter(adapter);
} }

@ -19,22 +19,15 @@
package org.isoron.uhabits.ui.habits.list.model; package org.isoron.uhabits.ui.habits.list.model;
import android.support.annotation.NonNull; import android.support.annotation.*;
import android.support.annotation.Nullable; import android.view.*;
import android.view.View; import android.widget.*;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import org.isoron.uhabits.HabitsApplication; import org.isoron.uhabits.*;
import org.isoron.uhabits.models.Habit; import org.isoron.uhabits.models.*;
import org.isoron.uhabits.models.ModelObservable; import org.isoron.uhabits.ui.habits.list.views.*;
import org.isoron.uhabits.ui.habits.list.views.HabitCardListView;
import org.isoron.uhabits.ui.habits.list.views.HabitCardView;
import java.util.LinkedList; import java.util.*;
import java.util.List;
import javax.inject.Inject;
/** /**
* Provides data that backs a {@link HabitCardListView}. * Provides data that backs a {@link HabitCardListView}.
@ -48,25 +41,25 @@ public class HabitCardListAdapter extends BaseAdapter
@NonNull @NonNull
private ModelObservable observable; private ModelObservable observable;
@Inject
@NonNull
HabitCardListCache cache;
@Nullable @Nullable
private HabitCardListView listView; private HabitCardListView listView;
@NonNull @NonNull
private final LinkedList<Habit> selected; private final LinkedList<Habit> selected;
public HabitCardListAdapter() @NonNull
private final HabitCardListCache cache;
public HabitCardListAdapter(int checkmarkCount)
{ {
this.selected = new LinkedList<>(); this.selected = new LinkedList<>();
this.observable = new ModelObservable(); this.observable = new ModelObservable();
HabitsApplication.getComponent().inject(this); HabitsApplication.getComponent().inject(this);
cache = new HabitCardListCache();
cache.setListener(this); cache.setListener(this);
cache.setCheckmarkCount(5); // TODO: make this dynamic somehow cache.setCheckmarkCount(checkmarkCount);
} }
/** /**

@ -19,22 +19,18 @@
package org.isoron.uhabits.ui.habits.list.model; package org.isoron.uhabits.ui.habits.list.model;
import android.support.annotation.NonNull; import android.support.annotation.*;
import android.support.annotation.Nullable; import android.util.*;
import org.isoron.uhabits.HabitsApplication; import org.isoron.uhabits.*;
import org.isoron.uhabits.commands.Command; import org.isoron.uhabits.commands.*;
import org.isoron.uhabits.commands.CommandRunner; import org.isoron.uhabits.models.*;
import org.isoron.uhabits.models.Habit; import org.isoron.uhabits.tasks.*;
import org.isoron.uhabits.models.HabitList; import org.isoron.uhabits.utils.*;
import org.isoron.uhabits.tasks.BaseTask;
import org.isoron.uhabits.utils.DateUtils;
import java.util.HashMap; import java.util.*;
import java.util.LinkedList;
import java.util.List;
import javax.inject.Inject; import javax.inject.*;
/** /**
* A HabitCardListCache fetches and keeps a cache of all the data necessary to * A HabitCardListCache fetches and keeps a cache of all the data necessary to
@ -114,7 +110,7 @@ public class HabitCardListCache implements CommandRunner.Listener
public void onAttached() public void onAttached()
{ {
// refreshAllHabits(true); refreshAllHabits(true);
if (lastLoadTimestamp == null) refreshAllHabits(true); if (lastLoadTimestamp == null) refreshAllHabits(true);
commandRunner.addListener(this); commandRunner.addListener(this);
} }
@ -129,11 +125,12 @@ public class HabitCardListCache implements CommandRunner.Listener
public void onDetached() public void onDetached()
{ {
// commandRunner.removeListener(this); commandRunner.removeListener(this);
} }
public void refreshAllHabits(final boolean refreshScoresAndCheckmarks) public void refreshAllHabits(final boolean refreshScoresAndCheckmarks)
{ {
Log.d("HabitCardListCache", "Refreshing all habits");
if (currentFetchTask != null) currentFetchTask.cancel(true); if (currentFetchTask != null) currentFetchTask.cancel(true);
currentFetchTask = new RefreshAllHabitsTask(refreshScoresAndCheckmarks); currentFetchTask = new RefreshAllHabitsTask(refreshScoresAndCheckmarks);
currentFetchTask.execute(); currentFetchTask.execute();

@ -32,10 +32,11 @@ import android.widget.TextView;
import org.isoron.uhabits.R; import org.isoron.uhabits.R;
import org.isoron.uhabits.models.Habit; import org.isoron.uhabits.models.Habit;
import org.isoron.uhabits.models.Score; import org.isoron.uhabits.models.Score;
import org.isoron.uhabits.ui.habits.list.*;
import org.isoron.uhabits.ui.habits.show.views.RingView; import org.isoron.uhabits.ui.habits.show.views.RingView;
import org.isoron.uhabits.utils.ColorUtils; import org.isoron.uhabits.utils.ColorUtils;
import java.util.Random; import java.util.*;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
@ -81,7 +82,9 @@ public class HabitCardView extends FrameLayout
public void setCheckmarkValues(int checkmarks[]) public void setCheckmarkValues(int checkmarks[])
{ {
checkmarkPanel.setCheckmarkValues(checkmarks); int count = ListHabitsRootView.getCheckmarkCount(this);
int visibleCheckmarks[] = Arrays.copyOfRange(checkmarks, 0, count);
checkmarkPanel.setCheckmarkValues(visibleCheckmarks);
postInvalidate(); postInvalidate();
} }

@ -28,6 +28,7 @@ import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import org.isoron.uhabits.R; import org.isoron.uhabits.R;
import org.isoron.uhabits.ui.habits.list.*;
import org.isoron.uhabits.utils.DateUtils; import org.isoron.uhabits.utils.DateUtils;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
@ -50,8 +51,9 @@ public class HeaderView extends LinearLayout
{ {
removeAllViews(); removeAllViews();
GregorianCalendar day = DateUtils.getStartOfTodayCalendar(); GregorianCalendar day = DateUtils.getStartOfTodayCalendar();
double count = ListHabitsRootView.getCheckmarkCount(this);
for (int i = 0; i < getButtonCount(); i++) for (int i = 0; i < count; i++)
{ {
int position = 0; int position = 0;
@ -66,14 +68,6 @@ public class HeaderView extends LinearLayout
} }
} }
private int getButtonCount()
{
float labelWidth = getResources().getDimension(R.dimen.habitNameWidth);
float buttonWidth = getResources().getDimension(R.dimen.checkmarkWidth);
return Math.max(0,
(int) ((getMeasuredWidth() - labelWidth) / buttonWidth));
}
private int getCheckmarkOrder() private int getCheckmarkOrder()
{ {
if (isInEditMode()) return CHECKMARK_LEFT_TO_RIGHT; if (isInEditMode()) return CHECKMARK_LEFT_TO_RIGHT;

@ -19,21 +19,16 @@
package org.isoron.uhabits; package org.isoron.uhabits;
import org.isoron.uhabits.commands.CommandRunner; import org.isoron.uhabits.commands.*;
import org.isoron.uhabits.models.Habit; import org.isoron.uhabits.models.*;
import org.isoron.uhabits.models.HabitList; import org.isoron.uhabits.models.memory.*;
import org.isoron.uhabits.models.ModelFactory; import org.isoron.uhabits.utils.*;
import org.isoron.uhabits.models.memory.MemoryHabitList;
import org.isoron.uhabits.models.memory.MemoryModelFactory;
import org.isoron.uhabits.ui.habits.list.model.HabitCardListCache;
import org.isoron.uhabits.utils.Preferences;
import javax.inject.Singleton; import javax.inject.*;
import dagger.Module; import dagger.*;
import dagger.Provides;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.*;
@Module @Module
public class TestModule public class TestModule
@ -52,13 +47,6 @@ public class TestModule
return mock(CommandRunner.class); return mock(CommandRunner.class);
} }
@Singleton
@Provides
HabitCardListCache provideHabitCardListCache()
{
return mock(HabitCardListCache.class);
}
@Singleton @Singleton
@Provides @Provides
HabitList provideHabitList() HabitList provideHabitList()

Loading…
Cancel
Save