refactored some code

This commit is contained in:
Chumva 2018-04-25 11:25:02 +03:00
parent 359049c7fe
commit db7cedbadc
3 changed files with 43 additions and 42 deletions

View file

@ -10,6 +10,7 @@ import android.widget.TextView;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.wikivoyage.data.TravelArticle;
import net.osmand.plus.wikivoyage.explore.travelcards.ArticleTravelCard;
import net.osmand.plus.wikivoyage.explore.travelcards.OpenBetaTravelCard;
import net.osmand.plus.wikivoyage.explore.travelcards.StartEditingTravelCard;
@ -35,27 +36,22 @@ public class ExploreRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = null;
int layoutId = 0;
if (viewType == OpenBetaTravelCard.TYPE) {
layoutId = R.layout.wikivoyage_open_beta_card;
itemView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.wikivoyage_open_beta_card, parent, false);
return new OpenBetaTravelCard.OpenBetaTravelVH(itemView);
}
if (viewType == StartEditingTravelCard.TYPE) {
layoutId = R.layout.wikivoyage_start_editing_card;
itemView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.wikivoyage_start_editing_card, parent, false);
return new StartEditingTravelCard.StartEditingTravelVH(itemView);
}
if (viewType == ArticleTravelCard.TYPE) {
layoutId = ArticleTravelCard.USE_ALTERNATIVE_CARD ? R.layout.wikivoyage_article_card_alternative : R.layout.wikivoyage_article_card;
itemView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
int layoutId = ArticleTravelCard.USE_ALTERNATIVE_CARD ? R.layout.wikivoyage_article_card_alternative : R.layout.wikivoyage_article_card;
View itemView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
return new ArticleTravelCard.ArticleTravelVH(itemView);
}
if (viewType == HEADER_TYPE) {
layoutId = R.layout.wikivoyage_list_header;
itemView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.wikivoyage_list_header, parent, false);
return new HeaderVH(itemView);
}
return null;
@ -63,23 +59,24 @@ public class ExploreRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
Object object = getItem(position);
if (viewHolder instanceof HeaderVH) {
final HeaderVH holder = (HeaderVH) viewHolder;
holder.title.setText((String) getItem(position));
holder.description.setText(String.valueOf(items.size() - 3));
holder.title.setText((String) object);
holder.description.setText(String.valueOf(getArticleItemCount()));
} else if (viewHolder instanceof ArticleTravelCard.ArticleTravelVH) {
if (getItem(position) instanceof ArticleTravelCard) {
ArticleTravelCard articleTravelCard = (ArticleTravelCard) getItem(position);
if (object instanceof ArticleTravelCard) {
ArticleTravelCard articleTravelCard = (ArticleTravelCard) object;
articleTravelCard.bindViewHolder(viewHolder);
}
} else if (viewHolder instanceof OpenBetaTravelCard.OpenBetaTravelVH) {
if (getItem(position) instanceof OpenBetaTravelCard) {
OpenBetaTravelCard openBetaTravelCard = (OpenBetaTravelCard) getItem(position);
if (object instanceof OpenBetaTravelCard) {
OpenBetaTravelCard openBetaTravelCard = (OpenBetaTravelCard) object;
openBetaTravelCard.bindViewHolder(viewHolder);
}
} else if (viewHolder instanceof StartEditingTravelCard.StartEditingTravelVH) {
if (getItem(position) instanceof StartEditingTravelCard) {
StartEditingTravelCard startEditingTravelCard = (StartEditingTravelCard) getItem(position);
if (object instanceof StartEditingTravelCard) {
StartEditingTravelCard startEditingTravelCard = (StartEditingTravelCard) object;
startEditingTravelCard.bindViewHolder(viewHolder);
}
}
@ -87,17 +84,18 @@ public class ExploreRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
@Override
public int getItemViewType(int position) {
if (getItem(position) instanceof String) {
Object object = getItem(position);
if (object instanceof String) {
return HEADER_TYPE;
}
if (getItem(position) instanceof OpenBetaTravelCard) {
return ((OpenBetaTravelCard) getItem(position)).getCardType();
if (object instanceof OpenBetaTravelCard) {
return ((OpenBetaTravelCard) object).getCardType();
}
if (getItem(position) instanceof StartEditingTravelCard) {
return ((StartEditingTravelCard) getItem(position)).getCardType();
if (object instanceof StartEditingTravelCard) {
return ((StartEditingTravelCard) object).getCardType();
}
if (getItem(position) instanceof ArticleTravelCard) {
return ((ArticleTravelCard) getItem(position)).getCardType();
if (object instanceof ArticleTravelCard) {
return ((ArticleTravelCard) object).getCardType();
}
return -1;
}
@ -107,6 +105,16 @@ public class ExploreRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
return items.size();
}
public int getArticleItemCount() {
int count = 0;
for (Object o : items) {
if (o instanceof TravelArticle) {
count++;
}
}
return count;
}
private Object getItem(int position) {
return items.get(position);
}

View file

@ -26,35 +26,31 @@ import java.util.List;
public class ExploreTabFragment extends BaseOsmAndFragment {
boolean nightMode;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final OsmandApplication app = getMyApplication();
nightMode = !getSettings().isLightContent();
final View mainView = inflater.inflate(R.layout.fragment_explore_tab, container, false);
ExploreRvAdapter adapter = new ExploreRvAdapter(app);
ExploreRvAdapter adapter = new ExploreRvAdapter(getMyApplication());
final RecyclerView rv = (RecyclerView) mainView.findViewById(R.id.recycler_view);
rv.setLayoutManager(new LinearLayoutManager(getContext()));
adapter.setItems(getItems());
rv.setAdapter(adapter);
return mainView;
}
private List<Object> getItems() {
List<Object> items = new LinkedList<>();
List<TravelArticle> savedArticles = getMyApplication().getTravelDbHelper().searchPopular();
final OsmandApplication app = getMyApplication();
boolean nightMode = !getSettings().isLightContent();
List<Object> items = new ArrayList<>();
List<TravelArticle> savedArticles = app.getTravelDbHelper().searchPopular();
items.add(new OpenBetaTravelCard(app, nightMode, getFragmentManager()));
items.add(new StartEditingTravelCard(app, nightMode));
if (!savedArticles.isEmpty()) {
items.add(new OpenBetaTravelCard(getMyApplication(), nightMode, getFragmentManager()));
items.add(new StartEditingTravelCard(getMyApplication(), nightMode));
items.add(getString(R.string.popular_destinations));
for (TravelArticle article : savedArticles) {
items.add(new ArticleTravelCard(getMyApplication(), nightMode, article, getFragmentManager()));
items.add(new ArticleTravelCard(app, nightMode, article, getFragmentManager()));
}
}
return items;

View file

@ -34,10 +34,7 @@ public class ArticleTravelCard extends BaseTravelCard {
public ArticleTravelCard(OsmandApplication app, boolean nightMode, TravelArticle article, FragmentManager fragmentManager) {
super(app, nightMode);
this.article = article;
int colorId = app.getSettings().isLightContent()
? R.color.wikivoyage_active_light : R.color.wikivoyage_active_dark;
IconsCache ic = app.getIconsCache();
readIcon = ic.getIcon(R.drawable.ic_action_read_article, colorId);
readIcon = getActiveIcon(R.drawable.ic_action_read_article);
this.fragmentManager = fragmentManager;
}