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

Commit

Permalink
Fixed list adapter (#148)
Browse files Browse the repository at this point in the history
Fixed scroll issue when opening arrow details
Fixed #147
Made all list items implement Comparable
Fixed bug that led to a failing ui test
Refactored intent system
Fixed state persistence in edit training activity
  • Loading branch information
DreierF authored Sep 15, 2016
1 parent 8706e53 commit 72b27da
Show file tree
Hide file tree
Showing 38 changed files with 323 additions and 197 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package de.dreier.mytargets.activities;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.print.PrintHelper;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -34,10 +34,10 @@ public class DispersionPatternActivity extends ChildActivityBase {
private ArrowStatistic statistic;

@NonNull
public static IntentWrapper getIntent(Activity activity, ArrowStatistic statistics) {
Intent intent = new Intent(activity, DispersionPatternActivity.class);
public static IntentWrapper getIntent(Fragment fragment, ArrowStatistic statistics) {
Intent intent = new Intent(fragment.getContext(), DispersionPatternActivity.class);
intent.putExtra(ITEM, Parcels.wrap(statistics));
return new IntentWrapper(activity, intent);
return new IntentWrapper(fragment, intent);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

package de.dreier.mytargets.activities;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.transition.Transition;
import android.view.Menu;
import android.view.MenuItem;
Expand Down Expand Up @@ -81,15 +81,15 @@ public class InputActivity extends ChildActivityBase implements OnTargetSetListe
private boolean transitionFinished = true;

@NonNull
public static IntentWrapper newEndIntent(Activity activity, long roundId) {
return getEndIntent(activity, roundId, 0);
public static IntentWrapper createIntent(Fragment fragment, Round round) {
return getIntent(fragment, round, 0);
}

public static IntentWrapper getEndIntent(Activity activity, long roundId, int passeIndex) {
Intent i = new Intent(activity, InputActivity.class);
i.putExtra(ROUND_ID, roundId);
public static IntentWrapper getIntent(Fragment fragment, Round round, int passeIndex) {
Intent i = new Intent(fragment.getContext(), InputActivity.class);
i.putExtra(ROUND_ID, round.getId());
i.putExtra(PASSE_IND, passeIndex);
return new IntentWrapper(activity, i);
return new IntentWrapper(fragment, i);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package de.dreier.mytargets.activities;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.AsyncTask;
Expand All @@ -19,6 +18,7 @@
import android.print.PrintManager;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.view.Menu;
Expand Down Expand Up @@ -57,16 +57,16 @@ public class ScoreboardActivity extends AppCompatActivity {
private ActivityScoreboardBinding binding;

@NonNull
public static IntentWrapper getIntent(Activity activity, long trainingId) {
return getIntent(activity, trainingId, -1);
public static IntentWrapper getIntent(Fragment fragment, long trainingId) {
return getIntent(fragment, trainingId, -1);
}

@NonNull
public static IntentWrapper getIntent(Activity activity, long trainingId, long roundId) {
Intent intent = new Intent(activity, ScoreboardActivity.class);
public static IntentWrapper getIntent(Fragment fragment, long trainingId, long roundId) {
Intent intent = new Intent(fragment.getContext(), ScoreboardActivity.class);
intent.putExtra(TRAINING_ID, trainingId);
intent.putExtra(ROUND_ID, roundId);
return new IntentWrapper(activity, intent);
return new IntentWrapper(fragment, intent);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

package de.dreier.mytargets.activities;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
Expand Down Expand Up @@ -57,10 +56,10 @@ public class StatisticsActivity extends ChildActivityBase {
private List<Round> rounds;

@NonNull
public static IntentWrapper getIntent(Activity context, List<Long> roundIds) {
Intent i = new Intent(context, StatisticsActivity.class);
public static IntentWrapper getIntent(Fragment fragment, List<Long> roundIds) {
Intent i = new Intent(fragment.getContext(), StatisticsActivity.class);
i.putExtra(ROUND_IDS, Utils.toArray(roundIds));
return new IntentWrapper(context, i);
return new IntentWrapper(fragment, i);
}

@Override
Expand Down
23 changes: 10 additions & 13 deletions app/src/main/java/de/dreier/mytargets/adapters/ListAdapterBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,24 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import de.dreier.mytargets.interfaces.ItemAdapter;
import de.dreier.mytargets.shared.models.IIdProvider;
import de.dreier.mytargets.utils.multiselector.SelectableViewHolder;


public abstract class ListAdapterBase<T extends IIdProvider>
/**
*
* The list is automatically sorted in natural order.
* */
public abstract class ListAdapterBase<T extends IIdProvider & Comparable<T>>
extends RecyclerView.Adapter<SelectableViewHolder<T>> implements ItemAdapter<T> {

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

public ListAdapterBase(Context context) {
this(context, (l, r) -> (int) (l.getId() - r.getId()));
}

public ListAdapterBase(Context context, Comparator<T> comparator) {
inflater = LayoutInflater.from(context);
this.comparator = comparator;
setHasStableIds(true);
}

Expand Down Expand Up @@ -67,6 +63,7 @@ public final void onBindViewHolder(SelectableViewHolder<T> viewHolder, int posit
}

public void setList(List<T> list) {
Collections.sort(list);
this.list = list;
notifyDataSetChanged();
}
Expand All @@ -77,7 +74,7 @@ public T getItem(int pos) {

@Override
public void addItem(T item) {
int pos = Collections.binarySearch(list, item, comparator);
int pos = Collections.binarySearch(list, item);
if (pos < 0) {
list.add(-pos - 1, item);
notifyItemInserted(-pos - 1);
Expand All @@ -88,9 +85,9 @@ public void addItem(T item) {

@Override
public void removeItem(T item) {
int pos = Collections.binarySearch(list, item, comparator);
int pos = Collections.binarySearch(list, item);
if (pos < 0) {
throw new IllegalArgumentException("Item must not be inserted twice!");
throw new IllegalArgumentException("Item has already been removed!");
}
list.remove(pos);
notifyItemRemoved(pos);
Expand All @@ -105,4 +102,4 @@ public T getItemById(long id) {
}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public ArrowListFragment() {
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
binding.fab.setOnClickListener(
view1 -> EditArrowFragment.createArrowIntent(getActivity())
view1 -> EditArrowFragment.createIntent(this)
.fromFab(binding.fab)
.start());
}
Expand Down Expand Up @@ -77,17 +77,19 @@ public void onLoadFinished(Loader<List<Arrow>> loader, List<Arrow> data) {

@Override
protected void onEdit(Arrow item) {
EditArrowFragment.editArrowIntent(getActivity(), item.getId()).start();
EditArrowFragment.editIntent(this, item)
.start();
}

@Override
protected void onItemSelected(Arrow item) {
EditArrowFragment.editArrowIntent(getActivity(), item.getId()).start();
EditArrowFragment.editIntent(this, item)
.start();
}

private class ArrowAdapter extends ListAdapterBase<Arrow> {
ArrowAdapter(Context context) {
super(context, (l, r) -> l.getName().compareTo(r.getName()));
super(context);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
binding.recyclerView.addItemDecoration(
new DividerItemDecoration(getContext(), R.drawable.inset_divider));
binding.fab.setOnClickListener(
view1 -> EditBowFragment.createBowIntent(getActivity())
view1 -> EditBowFragment.createIntent(this)
.fromFab(binding.fab)
.start());
mAdapter = new BowAdapter(getContext());
Expand All @@ -74,17 +74,17 @@ public void onLoadFinished(Loader<List<Bow>> loader, List<Bow> data) {

@Override
protected void onEdit(Bow item) {
EditBowFragment.editBowIntent(getActivity(), item.getId()).start();
EditBowFragment.editIntent(this, item).start();
}

@Override
protected void onItemSelected(Bow item) {
EditBowFragment.editBowIntent(getActivity(), item.getId()).start();
EditBowFragment.editIntent(this, item).start();
}

private class BowAdapter extends ListAdapterBase<Bow> {
public BowAdapter(Context context) {
super(context, (l, r) -> l.getName().compareTo(r.getName()));
super(context);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void onLongClick(SelectableViewHolder holder) {

private class DistanceAdapter extends ListAdapterBase<Dimension> {
DistanceAdapter(Context context) {
super(context, Dimension::compareTo);
super(context);
}

@Override
Expand All @@ -120,7 +120,7 @@ class ViewHolder extends SelectableViewHolder<Dimension> {

public ViewHolder(View itemView) {
super(itemView, mSelector, DistanceGridFragment.this);
binding = ItemDistanceBinding.bind(itemView);
binding = DataBindingUtil.bind(itemView);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -59,15 +58,15 @@ public EditArrowFragment() {
}

@NonNull
protected static IntentWrapper createArrowIntent(FragmentActivity activity) {
return new IntentWrapper(activity, SimpleFragmentActivityBase.EditArrowActivity.class);
protected static IntentWrapper createIntent(Fragment fragment) {
return new IntentWrapper(fragment, SimpleFragmentActivityBase.EditArrowActivity.class);
}

@NonNull
static IntentWrapper editArrowIntent(Activity activity, long arrowId) {
Intent i = new Intent(activity, SimpleFragmentActivityBase.EditArrowActivity.class);
i.putExtra(ARROW_ID, arrowId);
return new IntentWrapper(activity, i);
static IntentWrapper editIntent(Fragment fragment, Arrow arrow) {
Intent i = new Intent(fragment.getContext(), SimpleFragmentActivityBase.EditArrowActivity.class);
i.putExtra(ARROW_ID, arrow.getId());
return new IntentWrapper(fragment, i);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -63,15 +62,15 @@ public EditBowFragment() {
}

@NonNull
protected static IntentWrapper createBowIntent(FragmentActivity activity) {
return new IntentWrapper(activity, SimpleFragmentActivityBase.EditBowActivity.class);
protected static IntentWrapper createIntent(Fragment fragment) {
return new IntentWrapper(fragment, SimpleFragmentActivityBase.EditBowActivity.class);
}

@NonNull
static IntentWrapper editBowIntent(Activity activity, long bowId) {
Intent i = new Intent(activity, SimpleFragmentActivityBase.EditBowActivity.class);
i.putExtra(BOW_ID, bowId);
return new IntentWrapper(activity, i);
static IntentWrapper editIntent(Fragment fragment, Bow bow) {
Intent i = new Intent(fragment.getContext(), SimpleFragmentActivityBase.EditBowActivity.class);
i.putExtra(BOW_ID, bow.getId());
return new IntentWrapper(fragment, i);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
*/
package de.dreier.mytargets.fragments;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -45,18 +45,18 @@ public class EditRoundFragment extends EditFragmentBase {
private FragmentEditRoundBinding binding;

@NonNull
protected static IntentWrapper createRoundIntent(Activity activity, long trainingId) {
Intent i = new Intent(activity, SimpleFragmentActivityBase.EditRoundActivity.class);
protected static IntentWrapper createIntent(Fragment fragment, long trainingId) {
Intent i = new Intent(fragment.getContext(), SimpleFragmentActivityBase.EditRoundActivity.class);
i.putExtra(ITEM_ID, trainingId);
return new IntentWrapper(activity, i);
return new IntentWrapper(fragment, i);
}

@NonNull
protected static IntentWrapper editRoundIntent(Activity activity, long trainingId, long roundId) {
Intent i = new Intent(activity, SimpleFragmentActivityBase.EditRoundActivity.class);
protected static IntentWrapper editIntent(Fragment fragment, long trainingId, Round round) {
Intent i = new Intent(fragment.getContext(), SimpleFragmentActivityBase.EditRoundActivity.class);
i.putExtra(ITEM_ID, trainingId);
i.putExtra(ROUND_ID, roundId);
return new IntentWrapper(activity, i);
i.putExtra(ROUND_ID, round.getId());
return new IntentWrapper(fragment, i);
}

@Nullable
Expand Down Expand Up @@ -133,9 +133,8 @@ protected void onSave() {
finish();
if (roundId == -1) {
Round round = onSaveRound();
Activity activity = getActivity();
RoundFragment.getRoundIntent(activity, round.getId()).startWithoutAnimation();
InputActivity.newEndIntent(activity, round.getId()).start();
RoundFragment.getIntent(this, round).startWithoutAnimation();
InputActivity.createIntent(this, round).start();
} else {
onSaveRound();
getActivity().overridePendingTransition(R.anim.left_in, R.anim.right_out);
Expand Down
Loading

0 comments on commit 72b27da

Please sign in to comment.