[android] Fixed update category instance when user deleted track

This commit is contained in:
Dmitry Donskoy 2018-07-09 18:31:38 +03:00 committed by Vlad Mihaylenko
parent e99ed6780d
commit c64ced99a8
3 changed files with 38 additions and 3 deletions

View file

@ -31,7 +31,7 @@ import static com.mapswithme.maps.bookmarks.Holders.BaseBookmarkHolder.calculate
public class BookmarkListAdapter extends RecyclerView.Adapter<Holders.BaseBookmarkHolder>
{
@NonNull
private final BookmarkCategory mCategory;
private BookmarkCategory mCategory;
// view types
static final int TYPE_TRACK = 0;
@ -114,6 +114,7 @@ public class BookmarkListAdapter extends RecyclerView.Adapter<Holders.BaseBookma
@Override
public void onBindViewHolder(Holders.BaseBookmarkHolder holder, int position)
{
holder.mCategory = mCategory;
holder.bind(position);
}
@ -171,4 +172,9 @@ public class BookmarkListAdapter extends RecyclerView.Adapter<Holders.BaseBookma
return BookmarkManager.INSTANCE.getBookmark(bookmarkId);
}
}
public void updateCategory(@NonNull BookmarkCategory category)
{
mCategory = category;
}
}

View file

@ -19,6 +19,7 @@ import android.view.ViewGroup;
import com.mapswithme.maps.MwmActivity;
import com.mapswithme.maps.R;
import com.mapswithme.maps.base.BaseMwmRecyclerFragment;
import com.mapswithme.maps.bookmarks.data.AbstractCategoriesSnapshot;
import com.mapswithme.maps.bookmarks.data.Bookmark;
import com.mapswithme.maps.bookmarks.data.BookmarkCategory;
import com.mapswithme.maps.bookmarks.data.BookmarkManager;
@ -42,6 +43,8 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment<BookmarkListA
public static final String TAG = BookmarksListFragment.class.getSimpleName();
public static final String EXTRA_CATEGORY = "bookmark_category";
@SuppressWarnings("NullableProblems")
@NonNull
private BookmarkCategory mCategory;
private int mSelectedPosition;
@ -50,7 +53,18 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment<BookmarkListA
public void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mCategory = getArguments().getParcelable(EXTRA_CATEGORY);
mCategory = getCategoryOrThrow();
}
@NonNull
private BookmarkCategory getCategoryOrThrow()
{
Bundle args = getArguments();
BookmarkCategory category;
if (args == null || ((category = args.getParcelable(EXTRA_CATEGORY))) == null)
throw new IllegalArgumentException("Category not exist in bundle");
return category;
}
@NonNull
@ -126,6 +140,7 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment<BookmarkListA
{
BookmarkListAdapter adapter = getAdapter();
adapter.registerAdapterDataObserver(new CategoryDataObserver());
adapter.startLocationUpdate();
adapter.setOnClickListener(this);
adapter.setOnLongClickListener(isCatalogCategory() ? null : this);
@ -264,4 +279,18 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment<BookmarkListA
return super.onOptionsItemSelected(item);
}
private class CategoryDataObserver extends RecyclerView.AdapterDataObserver
{
@Override
public void onChanged()
{
super.onChanged();
AbstractCategoriesSnapshot.Default snapshot =
BookmarkManager.INSTANCE.getCategoriesSnapshot(mCategory.getType().getFilterStrategy());
int index = snapshot.indexOfOrThrow(mCategory);
getAdapter().updateCategory(snapshot.getItems().get(index));
}
}
}

View file

@ -210,7 +210,7 @@ public class Holders
public @interface Section {}
@NonNull
final BookmarkCategory mCategory;
BookmarkCategory mCategory;
@NonNull
private final View mView;