Slim down PassStore

This commit is contained in:
ligi 2016-01-04 17:11:35 +01:00
parent 3d2083be87
commit d23bfdd816
12 changed files with 136 additions and 81 deletions

View file

@ -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<Pass> passList;
private final List<FiledPass> passList;
public TestModule() {
passList = new ArrayList<>();
@ -35,7 +35,7 @@ public class TestModule {
passList.add(object);
}
public TestModule(List<Pass> passList) {
public TestModule(List<FiledPass> passList) {
this.passList = passList;
}

View file

@ -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<PassListActivity> {
public void setUp() throws Exception {
super.setUp();
App.setComponent(DaggerTestComponent.builder().testModule(new TestModule(new ArrayList<Pass>())).build());
App.setComponent(DaggerTestComponent.builder().testModule(new TestModule(new ArrayList<FiledPass>())).build());
getActivity();
}

View file

@ -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<Pass> passes;
private final List<FiledPass> passes;
private Pass actPass;
public FixedPassListPassStore(List<Pass> passes) {
public FixedPassListPassStore(List<FiledPass> passes) {
this.passes = passes;
}
@Override
public List<FiledPass> 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<CountedType> getCountedTypes() {
return new TreeSet<>();
}
@Override
public Pass getCurrentPass() {
return actPass;

View file

@ -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<CountedType> count(final List<FiledPass> passList) {
final Map<String, Integer> 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<CountedType> result = new TreeSet<>();
for (String type : tempMap.keySet()) {
result.add(new CountedType(type, tempMap.get(type)));
}
return result;
}
}

View file

@ -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<FiledPass> 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<FiledPass> 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<CountedType> getCountedTypes() {
// TODO - some sort of caching
final Map<String, Integer> 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<CountedType> result = new TreeSet<>();
for (String type : tempMap.keySet()) {
result.add(new CountedType(type, tempMap.get(type)));
}
return result;
}
@Override
public Pass getCurrentPass() {
return actPass;

View file

@ -15,4 +15,15 @@ public class CountedType implements Comparable<CountedType> {
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);
}
}

View file

@ -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);
}

View file

@ -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<FiledPass> getPassList();
void preCachePassesList();
void deleteCacheForId(String id);
void refreshPassesList();
int passCount();
Pass getPassbookAt(int pos);
Pass getPassbookForId(String id);
void sort(PassSortOrder order);
Set<CountedType> getCountedTypes();
@Nullable
Pass getCurrentPass();

View file

@ -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<CountedType> countedTypes = passStore.getCountedTypes();
final Set<CountedType> countedTypes = PassTypeCounter.count(passStore.getPassList());
setCategoryNavVisibilityByCurrentConditions();

View file

@ -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<PassViewHolder> {
@ -40,7 +43,7 @@ public class PassAdapter extends RecyclerView.Adapter<PassViewHolder> {
@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<PassViewHolder> {
@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<PassViewHolder> {
@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<PassViewHolder> {
}
});
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<PassViewHolder> {
return position;
}
public List<FiledPass> getList() {
return passStore.getPassList();
}
@Override
public int getItemCount() {
return passStore.passCount();
return getList().size();
}
}

View file

@ -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
}

View file

@ -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<CountedType> count = PassTypeCounter.count(new ArrayList<FiledPass>());
assertThat(count.size()).isEqualTo(0);
}
@Test
public void shouldBeAbleToCount() throws JSONException {
final Set<CountedType> count = PassTypeCounter.count(new ArrayList<FiledPass>() {
{
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;
}
}