forked from organicmaps/organicmaps
[android] Review fixes
This commit is contained in:
parent
0d509db028
commit
017bcdff9c
4 changed files with 50 additions and 34 deletions
|
@ -3,5 +3,4 @@
|
|||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/recycler"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:divider="@null"/>
|
||||
android:layout_height="match_parent"/>
|
||||
|
|
|
@ -17,7 +17,10 @@ import com.mapswithme.maps.widget.recycler.RecyclerLongClickListener;
|
|||
|
||||
import static com.mapswithme.maps.bookmarks.Holders.BaseBookmarkHolder.SECTION_BMKS;
|
||||
import static com.mapswithme.maps.bookmarks.Holders.BaseBookmarkHolder.SECTION_TRACKS;
|
||||
import static com.mapswithme.maps.bookmarks.Holders.BaseBookmarkHolder.getBookmarksSectionPosition;
|
||||
import static com.mapswithme.maps.bookmarks.Holders.BaseBookmarkHolder.getTracksSectionPosition;
|
||||
import static com.mapswithme.maps.bookmarks.Holders.BaseBookmarkHolder.isSectionEmpty;
|
||||
import static com.mapswithme.maps.bookmarks.Holders.BookmarkViewHolder.calculateBookmarkPosition;
|
||||
|
||||
public class BookmarkListAdapter extends RecyclerView.Adapter<Holders.BaseBookmarkHolder>
|
||||
{
|
||||
|
@ -77,16 +80,16 @@ public class BookmarkListAdapter extends RecyclerView.Adapter<Holders.BaseBookma
|
|||
switch (viewType)
|
||||
{
|
||||
case TYPE_TRACK:
|
||||
holder = new Holders.TrackViewHolder(inflater.inflate(R.layout.item_track,
|
||||
parent, false), mCategoryId);
|
||||
holder = new Holders.TrackViewHolder(inflater.inflate(R.layout.item_track, parent,
|
||||
false), mCategoryId);
|
||||
break;
|
||||
case TYPE_BOOKMARK:
|
||||
holder = new Holders.BookmarkViewHolder(inflater.inflate(R.layout.item_bookmark,
|
||||
parent,false), mCategoryId);
|
||||
holder = new Holders.BookmarkViewHolder(inflater.inflate(R.layout.item_bookmark, parent,
|
||||
false), mCategoryId);
|
||||
break;
|
||||
case TYPE_SECTION:
|
||||
holder = new Holders.SectionViewHolder((TextView) inflater.inflate(R.layout.item_category_title,
|
||||
parent, false), mCategoryId);
|
||||
TextView tv = (TextView) inflater.inflate(R.layout.item_category_title, parent, false);
|
||||
holder = new Holders.SectionViewHolder(tv, mCategoryId);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -107,15 +110,15 @@ public class BookmarkListAdapter extends RecyclerView.Adapter<Holders.BaseBookma
|
|||
@Override
|
||||
public int getItemViewType(int position)
|
||||
{
|
||||
final int bmkPos = Holders.BaseBookmarkHolder.getBookmarksSectionPosition(mCategoryId);
|
||||
final int bmkPos = getBookmarksSectionPosition(mCategoryId);
|
||||
final int trackPos = getTracksSectionPosition(mCategoryId);
|
||||
|
||||
if (position == bmkPos || position == trackPos)
|
||||
return TYPE_SECTION;
|
||||
|
||||
if (position > bmkPos && !Holders.BaseBookmarkHolder.isSectionEmpty(mCategoryId, SECTION_BMKS))
|
||||
if (position > bmkPos && !isSectionEmpty(mCategoryId, SECTION_BMKS))
|
||||
return TYPE_BOOKMARK;
|
||||
else if (position > trackPos && !Holders.BaseBookmarkHolder.isSectionEmpty(mCategoryId, SECTION_TRACKS))
|
||||
else if (position > trackPos && !isSectionEmpty(mCategoryId, SECTION_TRACKS))
|
||||
return TYPE_TRACK;
|
||||
|
||||
throw new IllegalArgumentException("Position not found: " + position);
|
||||
|
@ -131,8 +134,8 @@ public class BookmarkListAdapter extends RecyclerView.Adapter<Holders.BaseBookma
|
|||
public int getItemCount()
|
||||
{
|
||||
return BookmarkManager.INSTANCE.getCategorySize(mCategoryId)
|
||||
+ (Holders.BaseBookmarkHolder.isSectionEmpty(mCategoryId, SECTION_TRACKS) ? 0 : 1)
|
||||
+ (Holders.BaseBookmarkHolder.isSectionEmpty(mCategoryId, SECTION_BMKS) ? 0 : 1);
|
||||
+ (isSectionEmpty(mCategoryId, SECTION_TRACKS) ? 0 : 1)
|
||||
+ (isSectionEmpty(mCategoryId, SECTION_BMKS) ? 0 : 1);
|
||||
}
|
||||
|
||||
// FIXME: remove this heavy method and use BoomarkInfo class instead.
|
||||
|
@ -145,9 +148,7 @@ public class BookmarkListAdapter extends RecyclerView.Adapter<Holders.BaseBookma
|
|||
}
|
||||
else
|
||||
{
|
||||
final int pos = position - 1
|
||||
- (Holders.BaseBookmarkHolder.isSectionEmpty(mCategoryId, SECTION_TRACKS)
|
||||
? 0 : BookmarkManager.INSTANCE.getTracksCount(mCategoryId) + 1);
|
||||
final int pos = calculateBookmarkPosition(mCategoryId, position);
|
||||
final long bookmarkId = BookmarkManager.INSTANCE.getBookmarkIdByPosition(mCategoryId, pos);
|
||||
return BookmarkManager.INSTANCE.getBookmark(bookmarkId);
|
||||
}
|
||||
|
|
|
@ -67,7 +67,7 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment
|
|||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
|
||||
{
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
initList();
|
||||
configureAdapter();
|
||||
setHasOptionsMenu(true);
|
||||
ActionBar bar = ((AppCompatActivity) getActivity()).getSupportActionBar();
|
||||
if (bar != null)
|
||||
|
@ -110,15 +110,15 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment
|
|||
BookmarkManager.INSTANCE.removeSharingListener(this);
|
||||
}
|
||||
|
||||
private void initList()
|
||||
private void configureAdapter()
|
||||
{
|
||||
BookmarkListAdapter adapter = (BookmarkListAdapter) getAdapter();
|
||||
if (adapter != null)
|
||||
{
|
||||
adapter.startLocationUpdate();
|
||||
adapter.setOnClickListener(this);
|
||||
adapter.setOnLongClickListener(this);
|
||||
}
|
||||
if (adapter == null)
|
||||
return;
|
||||
|
||||
adapter.startLocationUpdate();
|
||||
adapter.setOnClickListener(this);
|
||||
adapter.setOnLongClickListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -198,7 +198,6 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onPreparedFileForSharing(@NonNull BookmarkSharingResult result)
|
||||
{
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.mapswithme.maps.bookmarks;
|
|||
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.location.Location;
|
||||
import android.support.annotation.IntDef;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
|
@ -21,6 +22,8 @@ import com.mapswithme.maps.widget.recycler.RecyclerLongClickListener;
|
|||
import com.mapswithme.util.Graphics;
|
||||
import com.mapswithme.util.UiUtils;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -123,6 +126,10 @@ public class Holders
|
|||
{
|
||||
static final int SECTION_TRACKS = 0;
|
||||
static final int SECTION_BMKS = 1;
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@IntDef({ SECTION_TRACKS, SECTION_BMKS })
|
||||
public @interface Section {}
|
||||
|
||||
final long mCategoryId;
|
||||
@NonNull
|
||||
private final View mView;
|
||||
|
@ -136,14 +143,17 @@ public class Holders
|
|||
|
||||
abstract void bind(int position);
|
||||
|
||||
static boolean isSectionEmpty(long categoryId, int section)
|
||||
static boolean isSectionEmpty(long categoryId, @Section int section)
|
||||
{
|
||||
if (section == SECTION_TRACKS)
|
||||
return BookmarkManager.INSTANCE.getTracksCount(categoryId) == 0;
|
||||
if (section == SECTION_BMKS)
|
||||
return BookmarkManager.INSTANCE.getBookmarksCount(categoryId) == 0;
|
||||
|
||||
throw new IllegalArgumentException("There is no section with index " + section);
|
||||
switch (section)
|
||||
{
|
||||
case SECTION_TRACKS:
|
||||
return BookmarkManager.INSTANCE.getTracksCount(categoryId) == 0;
|
||||
case SECTION_BMKS:
|
||||
return BookmarkManager.INSTANCE.getBookmarksCount(categoryId) == 0;
|
||||
default:
|
||||
throw new IllegalArgumentException("There is no section with index " + section);
|
||||
}
|
||||
}
|
||||
|
||||
static int getSectionForPosition(long categoryId, int position)
|
||||
|
@ -211,8 +221,7 @@ public class Holders
|
|||
@Override
|
||||
void bind(int position)
|
||||
{
|
||||
int pos = position - 1 - (isSectionEmpty(mCategoryId, SECTION_TRACKS)
|
||||
? 0 : BookmarkManager.INSTANCE.getTracksCount(mCategoryId) + 1);
|
||||
int pos = calculateBookmarkPosition(mCategoryId, position);
|
||||
final long bookmarkId = BookmarkManager.INSTANCE.getBookmarkIdByPosition(mCategoryId, pos);
|
||||
BookmarkInfo bookmark = new BookmarkInfo(mCategoryId, bookmarkId);
|
||||
mName.setText(bookmark.getTitle());
|
||||
|
@ -227,6 +236,14 @@ public class Holders
|
|||
mDistance.setText(null);
|
||||
mIcon.setImageResource(bookmark.getIcon().getSelectedResId());
|
||||
}
|
||||
|
||||
static int calculateBookmarkPosition(long categoryId, int position)
|
||||
{
|
||||
// Since bookmarks are always below tracks and header we should take it into account
|
||||
// during the bookmark's position calculation.
|
||||
return position - 1 - (isSectionEmpty(categoryId, SECTION_TRACKS)
|
||||
? 0 : BookmarkManager.INSTANCE.getTracksCount(categoryId) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
static class TrackViewHolder extends BaseBookmarkHolder
|
||||
|
|
Loading…
Add table
Reference in a new issue