diff --git a/android/src/androidTest/java/org/ligi/passandroid/TestModule.java b/android/src/androidTest/java/org/ligi/passandroid/TestModule.java index 97404261..f2e2a4d0 100644 --- a/android/src/androidTest/java/org/ligi/passandroid/TestModule.java +++ b/android/src/androidTest/java/org/ligi/passandroid/TestModule.java @@ -5,7 +5,7 @@ import com.squareup.otto.Bus; import org.ligi.passandroid.injections.FixedPassListPassStore; import org.ligi.passandroid.model.BarCode; -import org.ligi.passandroid.model.Pass; +import org.ligi.passandroid.model.FiledPass; import org.ligi.passandroid.model.PassImpl; import org.ligi.passandroid.model.PassSortOrder; import org.ligi.passandroid.model.PassStore; @@ -25,7 +25,7 @@ import static org.mockito.Mockito.when; @Module public class TestModule { - private final List passList; + private final List passList; public TestModule() { passList = new ArrayList<>(); @@ -35,7 +35,7 @@ public class TestModule { passList.add(object); } - public TestModule(List passList) { + public TestModule(List passList) { this.passList = passList; } diff --git a/android/src/androidTest/java/org/ligi/passandroid/TheEmptyPassList.java b/android/src/androidTest/java/org/ligi/passandroid/TheEmptyPassList.java index 5bf60205..685bf446 100644 --- a/android/src/androidTest/java/org/ligi/passandroid/TheEmptyPassList.java +++ b/android/src/androidTest/java/org/ligi/passandroid/TheEmptyPassList.java @@ -4,7 +4,7 @@ import android.test.suitebuilder.annotation.MediumTest; import com.squareup.spoon.Spoon; -import org.ligi.passandroid.model.Pass; +import org.ligi.passandroid.model.FiledPass; import org.ligi.passandroid.ui.PassListActivity; import java.util.ArrayList; @@ -25,7 +25,7 @@ public class TheEmptyPassList extends BaseIntegration { public void setUp() throws Exception { super.setUp(); - App.setComponent(DaggerTestComponent.builder().testModule(new TestModule(new ArrayList())).build()); + App.setComponent(DaggerTestComponent.builder().testModule(new TestModule(new ArrayList())).build()); getActivity(); } diff --git a/android/src/androidTest/java/org/ligi/passandroid/injections/FixedPassListPassStore.java b/android/src/androidTest/java/org/ligi/passandroid/injections/FixedPassListPassStore.java index 46a1dd36..1279124c 100644 --- a/android/src/androidTest/java/org/ligi/passandroid/injections/FixedPassListPassStore.java +++ b/android/src/androidTest/java/org/ligi/passandroid/injections/FixedPassListPassStore.java @@ -1,23 +1,26 @@ package org.ligi.passandroid.injections; -import org.ligi.passandroid.model.CountedType; +import org.ligi.passandroid.model.FiledPass; import org.ligi.passandroid.model.Pass; import org.ligi.passandroid.model.PassSortOrder; import org.ligi.passandroid.model.PassStore; import java.util.List; -import java.util.Set; -import java.util.TreeSet; public class FixedPassListPassStore implements PassStore { - private final List passes; + private final List passes; private Pass actPass; - public FixedPassListPassStore(List passes) { + public FixedPassListPassStore(List passes) { this.passes = passes; } + @Override + public List getPassList() { + return passes; + } + @Override public void preCachePassesList() { // no effect in this impl @@ -33,16 +36,6 @@ public class FixedPassListPassStore implements PassStore { // no effect in this impl } - @Override - public int passCount() { - return passes.size(); - } - - @Override - public Pass getPassbookAt(int pos) { - return passes.get(pos); - } - @Override public Pass getPassbookForId(String id) { for (Pass pass : passes) { @@ -57,11 +50,6 @@ public class FixedPassListPassStore implements PassStore { public void sort(PassSortOrder order) { } - @Override - public Set getCountedTypes() { - return new TreeSet<>(); - } - @Override public Pass getCurrentPass() { return actPass; diff --git a/android/src/main/java/org/ligi/passandroid/helper/PassTypeCounter.java b/android/src/main/java/org/ligi/passandroid/helper/PassTypeCounter.java new file mode 100644 index 00000000..2175bc42 --- /dev/null +++ b/android/src/main/java/org/ligi/passandroid/helper/PassTypeCounter.java @@ -0,0 +1,35 @@ +package org.ligi.passandroid.helper; + +import org.ligi.passandroid.model.CountedType; +import org.ligi.passandroid.model.FiledPass; +import org.ligi.passandroid.model.Pass; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + +public class PassTypeCounter { + + public static Set count(final List passList) { + final Map tempMap = new HashMap<>(); + + for (Pass info : passList) { + if (tempMap.containsKey(info.getTypeNotNull())) { + final Integer i = tempMap.get(info.getTypeNotNull()); + tempMap.put(info.getTypeNotNull(), i + 1); + } else { + tempMap.put(info.getTypeNotNull(), 1); + } + } + + final Set result = new TreeSet<>(); + + for (String type : tempMap.keySet()) { + result.add(new CountedType(type, tempMap.get(type))); + } + + return result; + } +} diff --git a/android/src/main/java/org/ligi/passandroid/model/AndroidFileSystemPassStore.java b/android/src/main/java/org/ligi/passandroid/model/AndroidFileSystemPassStore.java index 1b1dc1f4..1914b5e7 100644 --- a/android/src/main/java/org/ligi/passandroid/model/AndroidFileSystemPassStore.java +++ b/android/src/main/java/org/ligi/passandroid/model/AndroidFileSystemPassStore.java @@ -14,11 +14,7 @@ import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeSet; public class AndroidFileSystemPassStore implements PassStore { @@ -28,7 +24,7 @@ public class AndroidFileSystemPassStore implements PassStore { private List passList = new ArrayList<>(); private Pass actPass; - public AndroidFileSystemPassStore(final Context context,final Settings settings) { + public AndroidFileSystemPassStore(final Context context, final Settings settings) { this.context = context; path = settings.getPassesDir(); @@ -44,6 +40,11 @@ public class AndroidFileSystemPassStore implements PassStore { return new File(getPathForID(id) + "/base_cache.obj"); } + @Override + public List getPassList() { + return passList; + } + @Override public void preCachePassesList() { for (String key : getPassIDArray()) { @@ -113,16 +114,6 @@ public class AndroidFileSystemPassStore implements PassStore { return passes_dir; } - @Override - public int passCount() { - return passList.size(); - } - - @Override - public Pass getPassbookAt(final int pos) { - return passList.get(pos); - } - @Override public Pass getPassbookForId(final String id) { for (Pass pass : passList) { @@ -148,29 +139,6 @@ public class AndroidFileSystemPassStore implements PassStore { } - @Override - public Set getCountedTypes() { - // TODO - some sort of caching - final Map tempMap = new HashMap<>(); - - for (Pass info : passList) { - if (tempMap.containsKey(info.getTypeNotNull())) { - final Integer i = tempMap.get(info.getTypeNotNull()); - tempMap.put(info.getTypeNotNull(), i + 1); - } else { - tempMap.put(info.getTypeNotNull(), 1); - } - } - - final Set result = new TreeSet<>(); - - for (String type : tempMap.keySet()) { - result.add(new CountedType(type, tempMap.get(type))); - } - - return result; - } - @Override public Pass getCurrentPass() { return actPass; diff --git a/android/src/main/java/org/ligi/passandroid/model/CountedType.java b/android/src/main/java/org/ligi/passandroid/model/CountedType.java index 05cd0415..cb061afa 100644 --- a/android/src/main/java/org/ligi/passandroid/model/CountedType.java +++ b/android/src/main/java/org/ligi/passandroid/model/CountedType.java @@ -15,4 +15,15 @@ public class CountedType implements Comparable { public int compareTo(@NonNull CountedType another) { return another.count - count; } + + @Override + public boolean equals(Object o) { + if (!(o instanceof CountedType)) { + return false; + } + + CountedType other = (CountedType) o; + + return type.equals(other.type) && count.equals(other.count); + } } diff --git a/android/src/main/java/org/ligi/passandroid/model/FiledPass.java b/android/src/main/java/org/ligi/passandroid/model/FiledPass.java index 93df146c..a13a335e 100644 --- a/android/src/main/java/org/ligi/passandroid/model/FiledPass.java +++ b/android/src/main/java/org/ligi/passandroid/model/FiledPass.java @@ -1,9 +1,12 @@ package org.ligi.passandroid.model; import java.io.Serializable; -public interface FiledPass extends Pass,Serializable { + +public interface FiledPass extends Pass, Serializable { String getPath(); + void setPath(String Path); + void save(PassStore store); } diff --git a/android/src/main/java/org/ligi/passandroid/model/PassStore.java b/android/src/main/java/org/ligi/passandroid/model/PassStore.java index 87b0f9f2..21bc7219 100644 --- a/android/src/main/java/org/ligi/passandroid/model/PassStore.java +++ b/android/src/main/java/org/ligi/passandroid/model/PassStore.java @@ -2,26 +2,22 @@ package org.ligi.passandroid.model; import android.support.annotation.Nullable; -import java.util.Set; +import java.util.List; public interface PassStore { + List getPassList(); + void preCachePassesList(); void deleteCacheForId(String id); void refreshPassesList(); - int passCount(); - - Pass getPassbookAt(int pos); - Pass getPassbookForId(String id); void sort(PassSortOrder order); - Set getCountedTypes(); - @Nullable Pass getCurrentPass(); diff --git a/android/src/main/java/org/ligi/passandroid/ui/NavigationFragment.java b/android/src/main/java/org/ligi/passandroid/ui/NavigationFragment.java index 51426c47..0ced6311 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/NavigationFragment.java +++ b/android/src/main/java/org/ligi/passandroid/ui/NavigationFragment.java @@ -23,6 +23,7 @@ import org.ligi.passandroid.events.NavigationOpenedEvent; import org.ligi.passandroid.events.SortOrderChangeEvent; import org.ligi.passandroid.events.TypeFocusEvent; import org.ligi.passandroid.helper.CategoryHelper; +import org.ligi.passandroid.helper.PassTypeCounter; import org.ligi.passandroid.model.CountedType; import org.ligi.passandroid.model.PassSortOrder; import org.ligi.passandroid.model.PassStore; @@ -142,7 +143,7 @@ public class NavigationFragment extends Fragment { } private boolean shouldDisplaySort() { - return passStore.getCountedTypes().size() >= 2; + return PassTypeCounter.count(passStore.getPassList()).size() >= 2; } private void setCategoryNavVisibilityByCurrentConditions() { @@ -154,7 +155,7 @@ public class NavigationFragment extends Fragment { private void createCategoryJumpMarks(LayoutInflater inflater) { - final Set countedTypes = passStore.getCountedTypes(); + final Set countedTypes = PassTypeCounter.count(passStore.getPassList()); setCategoryNavVisibilityByCurrentConditions(); diff --git a/android/src/main/java/org/ligi/passandroid/ui/PassAdapter.java b/android/src/main/java/org/ligi/passandroid/ui/PassAdapter.java index b1b68276..c9377f04 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/PassAdapter.java +++ b/android/src/main/java/org/ligi/passandroid/ui/PassAdapter.java @@ -12,9 +12,12 @@ import android.view.ViewGroup; import org.ligi.axt.AXT; import org.ligi.passandroid.App; import org.ligi.passandroid.R; +import org.ligi.passandroid.model.FiledPass; import org.ligi.passandroid.model.Pass; import org.ligi.passandroid.model.PassStore; +import java.util.List; + import javax.inject.Inject; public class PassAdapter extends RecyclerView.Adapter { @@ -40,7 +43,7 @@ public class PassAdapter extends RecyclerView.Adapter { @Override public void onBindViewHolder(PassViewHolder viewHolder, final int longClickedCardPosition) { - final Pass pass = passStore.getPassbookAt(longClickedCardPosition); + final Pass pass = passStore.getPassList().get(longClickedCardPosition); viewHolder.apply(pass,passListActivity); @@ -59,7 +62,7 @@ public class PassAdapter extends RecyclerView.Adapter { @Override public boolean onLongClick(View v) { - final Pass pass = passStore.getPassbookAt(longClickedCardPosition); + final Pass pass = getList().get(longClickedCardPosition); if (actionMode != null) { final boolean clickedOnDifferentItem = actionMode.getTag() == null || !actionMode.getTag().equals(pass); @@ -85,7 +88,7 @@ public class PassAdapter extends RecyclerView.Adapter { @Override public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) { - if (new PassMenuOptions(passListActivity, passStore.getPassbookAt(longClickedCardPosition)).process(menuItem)) { + if (new PassMenuOptions(passListActivity, getList().get(longClickedCardPosition)).process(menuItem)) { actionMode.finish(); return true; } @@ -100,7 +103,7 @@ public class PassAdapter extends RecyclerView.Adapter { } }); - actionMode.setTag(passStore.getPassbookAt(longClickedCardPosition)); + actionMode.setTag(getList().get(longClickedCardPosition)); root.setCardElevation(v.getContext().getResources().getDimension(R.dimen.card_longclick_elevation)); @@ -116,9 +119,13 @@ public class PassAdapter extends RecyclerView.Adapter { return position; } + public List getList() { + return passStore.getPassList(); + } + @Override public int getItemCount() { - return passStore.passCount(); + return getList().size(); } } diff --git a/android/src/main/java/org/ligi/passandroid/ui/PassListActivity.java b/android/src/main/java/org/ligi/passandroid/ui/PassListActivity.java index d5364acc..b00ce815 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/PassListActivity.java +++ b/android/src/main/java/org/ligi/passandroid/ui/PassListActivity.java @@ -98,7 +98,7 @@ public class PassListActivity extends PassAndroidActivity { intent.setType("*/*"); // tried with octet stream - no use startActivityForResult(intent, OPEN_FILE_READ_REQUEST_CODE); } catch (ActivityNotFoundException e) { - Snackbar.make(floatingActionsMenu,"Unavailable",Snackbar.LENGTH_LONG).show(); + Snackbar.make(floatingActionsMenu, "Unavailable", Snackbar.LENGTH_LONG).show(); } } @@ -133,7 +133,7 @@ public class PassListActivity extends PassAndroidActivity { passStore.refreshPassesList(); passStore.sort(settings.getSortOrder()); - AXT.at(emptyView).setVisibility(passStore.passCount() == 0); + AXT.at(emptyView).setVisibility(passStore.getPassList().isEmpty()); passAdapter.notifyDataSetChanged(); @@ -190,8 +190,9 @@ public class PassListActivity extends PassAndroidActivity { } private void scrollToType(String type) { + for (int i = 0; i < passAdapter.getItemCount(); i++) { - if (passStore.getPassbookAt(i).getTypeNotNull().equals(type)) { + if (passStore.getPassList().get(i).getTypeNotNull().equals(type)) { recyclerView.scrollToPosition(i); return; // we are done } diff --git a/android/src/test/java/org/ligi/passandroid/unittest/ThePassTypeCounter.java b/android/src/test/java/org/ligi/passandroid/unittest/ThePassTypeCounter.java new file mode 100644 index 00000000..feca8aad --- /dev/null +++ b/android/src/test/java/org/ligi/passandroid/unittest/ThePassTypeCounter.java @@ -0,0 +1,45 @@ +package org.ligi.passandroid.unittest; + +import org.json.JSONException; +import org.junit.Test; +import org.ligi.passandroid.helper.PassTypeCounter; +import org.ligi.passandroid.model.CountedType; +import org.ligi.passandroid.model.FiledPass; +import org.ligi.passandroid.model.PassImpl; + +import java.util.ArrayList; +import java.util.Set; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ThePassTypeCounter { + + @Test + public void shouldReturn0ForEmptyPassList() throws JSONException { + final Set count = PassTypeCounter.count(new ArrayList()); + + assertThat(count.size()).isEqualTo(0); + } + + + @Test + public void shouldBeAbleToCount() throws JSONException { + + final Set count = PassTypeCounter.count(new ArrayList() { + { + add(getPassWithType("foo")); + add(getPassWithType("bar")); + add(getPassWithType("bar")); + } + }); + + assertThat(count).containsOnly(new CountedType("foo",1),new CountedType("bar",2)); + } + + private FiledPass getPassWithType(final String type) { + final PassImpl pass = new PassImpl(); + pass.setType(type); + return pass; + } + +}