Skip to content
This repository has been archived by the owner on Apr 26, 2020. It is now read-only.

Commit

Permalink
Fixes for Beta 4 (#234)
Browse files Browse the repository at this point in the history
* Fixed Marshall/Unmarshall issue

* Some refactorings in input activity

* Fixed loosing selection when app gets killed in background

* Moved adapter two hierarchies upwards

* Closed #230

* Closed #232

* Fixed null distance

* Fixed another NPE and added activity level logging

* Added missing generic
  • Loading branch information
DreierF authored Jan 24, 2017
1 parent 79fddf7 commit 8065398
Show file tree
Hide file tree
Showing 48 changed files with 550 additions and 358 deletions.
4 changes: 4 additions & 0 deletions app/src/main/assets/migrations/database/19.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
UPDATE Round SET distance="-1 m" WHERE distance IS NULL;
UPDATE Round SET targetDiameter="-1 cm" WHERE targetDiameter IS NULL;
UPDATE RoundTemplate SET distance="-1 m" WHERE distance IS NULL;
UPDATE RoundTemplate SET targetDiameter="-1 cm" WHERE targetDiameter IS NULL;
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@

package de.dreier.mytargets.base.activities;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.view.MenuItem;

import com.google.firebase.analytics.FirebaseAnalytics;

import de.dreier.mytargets.R;
import de.dreier.mytargets.utils.Utils;

Expand All @@ -28,6 +32,16 @@ public abstract class ChildActivityBase extends AppCompatActivity {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
logEvent(getClass().getSimpleName());
}

protected void logEvent(String event) {
FirebaseAnalytics.getInstance(this).logEvent(event, null);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

package de.dreier.mytargets.base.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.ArrayList;
Expand All @@ -32,11 +30,9 @@
public abstract class SimpleListAdapterBase<T extends IIdProvider & Comparable<T>>
extends ListAdapterBase<SelectableViewHolder<T>, T> {

protected final LayoutInflater inflater;
private List<T> list = new ArrayList<>();

public SimpleListAdapterBase(Context context) {
inflater = LayoutInflater.from(context);
public SimpleListAdapterBase() {
setHasStableIds(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ public int getItemPosition(C item) {
return -1;
}

public void ensureItemIsExpanded(C item) {
ExpandableHeaderHolder<P, C> parentHolder = getHeaderHolderForChild(item);
int pos = getHeaderIndex(parentHolder);
if (pos >= 0 && !headersList.get(pos).expanded) {
expandOrCollapse(headersList.get(pos));
}
}

private void expandOrCollapse(ExpandableHeaderHolder<P, C> header) {
int childLength = header.children.size();
if (!header.expanded) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public abstract class HeaderListAdapter<C extends IIdProvider>
extends HeaderListAdapterBase<HeaderListAdapter.SimpleHeader, C, HeaderListAdapterBase.HeaderHolder<HeaderListAdapter.SimpleHeader, C>> {

public HeaderListAdapter(PartitionDelegate<SimpleHeader, C> parentPartition, Comparator<C> childComparator) {
super(parentPartition, (h1, h2) -> h1.index.compareTo(h2.index), childComparator);
super(parentPartition, SimpleHeader::compareTo, childComparator);
}

@NonNull
Expand Down Expand Up @@ -80,7 +80,7 @@ public void bindItem() {
}
}

public static class SimpleHeader implements IIdProvider {
public static class SimpleHeader implements IIdProvider, Comparable<SimpleHeader> {
Long index;
String title;

Expand All @@ -93,5 +93,15 @@ public SimpleHeader(Long index, String title) {
public Long getId() {
return index;
}

@Override
public int compareTo(@NonNull SimpleHeader other) {
return index.compareTo(other.index);
}

@Override
public String toString() {
return title;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public void removeItem(C item) {
header.remove(item);
}

@Override
public void setList(List<C> children) {
fillChildMap(children);
notifyDataSetChanged();
Expand All @@ -163,8 +164,7 @@ protected void fillChildMap(List<C> children) {
}

private void addChildToMap(C child) {
P parent = partitionDelegate.getParent(child);
H parentHolder = getHeaderHolder(parent, childComparator);
H parentHolder = getHeaderHolderForChild(child);
int pos = getHeaderIndex(parentHolder);
if (pos < 0) {
parentHolder.add(child);
Expand All @@ -174,6 +174,11 @@ private void addChildToMap(C child) {
}
}

protected H getHeaderHolderForChild(C child) {
P parent = partitionDelegate.getParent(child);
return getHeaderHolder(parent, childComparator);
}

@NonNull
protected abstract H getHeaderHolder(P parent, Comparator<C> childComparator);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@

import org.parceler.Parcels;

import de.dreier.mytargets.base.adapters.ListAdapterBase;
import de.dreier.mytargets.base.adapters.SimpleListAdapterBase;
import de.dreier.mytargets.shared.models.IIdSettable;

public abstract class EditableListFragment<T extends IIdSettable & Model & Comparable<T>> extends EditableListFragmentBase<T> {

protected SimpleListAdapterBase<T> adapter;
public abstract class EditableListFragment<T extends IIdSettable & Model & Comparable<T>> extends EditableListFragmentBase<T, SimpleListAdapterBase<T>> {

protected final void onSelected(T item) {
if (listener == null) {
Expand All @@ -37,9 +34,4 @@ protected final void onSelected(T item) {
}

protected abstract void onItemSelected(T item);

@Override
protected ListAdapterBase<?, T> getAdapter() {
return adapter;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,23 @@
import de.dreier.mytargets.R;
import de.dreier.mytargets.base.adapters.ListAdapterBase;
import de.dreier.mytargets.shared.models.IIdSettable;
import de.dreier.mytargets.utils.SelectorBundler;
import de.dreier.mytargets.utils.MultiSelectorBundler;
import de.dreier.mytargets.utils.multiselector.MultiSelector;
import de.dreier.mytargets.utils.multiselector.SelectableViewHolder;
import icepick.State;

/**
* @param <T> Model of the item which is managed within the fragment.
*/
public abstract class EditableListFragmentBase<T extends IIdSettable & Model> extends ListFragmentBase<T> {
public abstract class EditableListFragmentBase<T extends IIdSettable & Model,
U extends ListAdapterBase<?, T>> extends ListFragmentBase<T, U> {

public static final String ITEM_ID = "id";

protected boolean supportsStatistics = false;
protected boolean supportsDeletion = true;
@State(SelectorBundler.class)
protected MultiSelector mSelector = new MultiSelector();
@State(MultiSelectorBundler.class)
protected MultiSelector selector = new MultiSelector();

/**
* Resource describing FAB action
Expand All @@ -72,20 +73,20 @@ public abstract class EditableListFragmentBase<T extends IIdSettable & Model> ex
*/
ActionMode actionMode = null;

private final ActionMode.Callback mDeleteMode = new ActionMode.Callback() {
private final ActionMode.Callback actionModeCallback = new ActionMode.Callback() {

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
MenuItem edit = menu.findItem(R.id.action_edit);
edit.setVisible(mSelector.getSelectedIds().size() == 1);
edit.setVisible(selector.getSelectedIds().size() == 1);
menu.findItem(R.id.action_statistics).setVisible(supportsStatistics);
menu.findItem(R.id.action_delete).setVisible(supportsDeletion);
return false;
}

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mSelector.setSelectable(true);
selector.setSelectable(true);
actionMode = mode;
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_menu_edit_delete, menu);
Expand Down Expand Up @@ -113,17 +114,17 @@ public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
}

private List<T> getSelectedItems() {
List<Long> ids = mSelector.getSelectedIds();
List<Long> ids = selector.getSelectedIds();
return Stream.of(ids)
.map(id -> getAdapter().getItemById(id))
.map(id -> adapter.getItemById(id))
.filter(item -> item != null)
.collect(Collectors.toList());
}

@Override
public void onDestroyActionMode(ActionMode mode) {
mSelector.setSelectable(false);
mSelector.clearSelections();
selector.setSelectable(false);
selector.clearSelections();
actionMode = null;
}
};
Expand All @@ -136,15 +137,15 @@ public void onResume() {

private void remove(List<T> deleted) {
for (T item : deleted) {
getAdapter().removeItem(item);
adapter.removeItem(item);
}
getAdapter().notifyDataSetChanged();
adapter.notifyDataSetChanged();
String message = getResources()
.getQuantityString(itemTypeDelRes, deleted.size(), deleted.size());
Snackbar.make(getView().findViewById(R.id.coordinatorLayout), message, Snackbar.LENGTH_LONG)
.setAction(R.string.undo, v -> {
for (T item : deleted) {
getAdapter().addItem(item);
adapter.addItem(item);
}
deleted.clear();
})
Expand All @@ -166,13 +167,11 @@ public void onShown(Snackbar snackbar) {
}).show();
}

protected abstract ListAdapterBase<?, T> getAdapter();

private void updateTitle() {
if (actionMode == null) {
return;
}
int count = mSelector.getSelectedIds().size();
int count = selector.getSelectedIds().size();
if (count == 0) {
actionMode.finish();
} else {
Expand All @@ -182,25 +181,26 @@ private void updateTitle() {
}
}

public void onClick(SelectableViewHolder holder, T mItem) {
@Override
public void onClick(SelectableViewHolder<T> holder, T mItem) {
if (mItem == null) {
return;
}
if (!mSelector.tapSelection(holder)) {
if (!selector.tapSelection(holder)) {
onSelected(mItem);
} else {
updateTitle();
}
}

@Override
public void onLongClick(SelectableViewHolder holder) {
public void onLongClick(SelectableViewHolder<T> holder) {
if (actionMode == null) {
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.startSupportActionMode(mDeleteMode);
mSelector.setSelectable(true);
activity.startSupportActionMode(actionModeCallback);
selector.setSelectable(true);
}
mSelector.setSelected(holder, true);
selector.setSelected(holder, true);
updateTitle();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;

import com.google.firebase.analytics.FirebaseAnalytics;

import de.dreier.mytargets.R;
import de.dreier.mytargets.utils.Utils;
import icepick.Icepick;
Expand All @@ -41,15 +39,10 @@ public abstract class FragmentBase extends Fragment implements LoaderManager.Loa

private static final int LOADER_ID = 0;

public void logEvent(String event) {
FirebaseAnalytics.getInstance(getContext()).logEvent(event, null);
}

@CallSuper
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
logEvent(getClass().getSimpleName());
Icepick.restoreInstanceState(this, savedInstanceState);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@
import android.content.Context;
import android.os.Parcelable;

import de.dreier.mytargets.base.adapters.ListAdapterBase;
import de.dreier.mytargets.utils.multiselector.OnItemClickListener;

public abstract class ListFragmentBase<T> extends FragmentBase implements OnItemClickListener<T> {
public abstract class ListFragmentBase<T, U extends ListAdapterBase<?, T>> extends FragmentBase implements OnItemClickListener<T> {

/**
* Listener which gets called when item gets selected
*/
protected OnItemSelectedListener listener;

/**
* Adapter for the fragment's RecyclerView
*/
protected U adapter;

@Override
public void onAttach(Context activity) {
super.onAttach(activity);
Expand Down
Loading

0 comments on commit 8065398

Please sign in to comment.