Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
1d22563f0d
3 changed files with 122 additions and 9 deletions
|
@ -25,6 +25,12 @@ public class WikivoyageLocalDataHelper {
|
|||
private TLongObjectHashMap<WikivoyageSearchHistoryItem> historyMap;
|
||||
private List<WikivoyageArticle> savedArticles;
|
||||
|
||||
private Listener listener;
|
||||
|
||||
public void setListener(Listener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
private WikivoyageLocalDataHelper(OsmandApplication app) {
|
||||
dbHelper = new WikivoyageLocalDataDbHelper(app);
|
||||
historyMap = dbHelper.getAllHistoryMap();
|
||||
|
@ -99,6 +105,7 @@ public class WikivoyageLocalDataHelper {
|
|||
saved.content = article.getPartialContent();
|
||||
savedArticles.add(saved);
|
||||
dbHelper.addSavedArticle(saved);
|
||||
notifySavedUpdated();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -107,6 +114,7 @@ public class WikivoyageLocalDataHelper {
|
|||
if (savedArticle != null) {
|
||||
savedArticles.remove(savedArticle);
|
||||
dbHelper.removeSavedArticle(savedArticle);
|
||||
notifySavedUpdated();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -114,6 +122,12 @@ public class WikivoyageLocalDataHelper {
|
|||
return getArticle(article.cityId, article.lang) != null;
|
||||
}
|
||||
|
||||
private void notifySavedUpdated() {
|
||||
if (listener != null) {
|
||||
listener.savedArticlesUpdated();
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private WikivoyageArticle getArticle(long cityId, String lang) {
|
||||
for (WikivoyageArticle article : savedArticles) {
|
||||
|
@ -124,6 +138,11 @@ public class WikivoyageLocalDataHelper {
|
|||
return null;
|
||||
}
|
||||
|
||||
public interface Listener {
|
||||
|
||||
void savedArticlesUpdated();
|
||||
}
|
||||
|
||||
private static class WikivoyageLocalDataDbHelper {
|
||||
|
||||
private static final int DB_VERSION = 2;
|
||||
|
|
|
@ -34,11 +34,6 @@ public class SavedArticlesRvAdapter extends RecyclerView.Adapter<RecyclerView.Vi
|
|||
|
||||
SavedArticlesRvAdapter(OsmandApplication app) {
|
||||
this.app = app;
|
||||
List<WikivoyageArticle> savedArticles = WikivoyageLocalDataHelper.getInstance(app).getSavedArticles();
|
||||
if (!savedArticles.isEmpty()) {
|
||||
items.add(app.getString(R.string.saved_articles));
|
||||
items.addAll(savedArticles);
|
||||
}
|
||||
int colorId = app.getSettings().isLightContent()
|
||||
? R.color.wikivoyage_active_light : R.color.wikivoyage_active_dark;
|
||||
IconsCache ic = app.getIconsCache();
|
||||
|
@ -96,6 +91,16 @@ public class SavedArticlesRvAdapter extends RecyclerView.Adapter<RecyclerView.Vi
|
|||
return items.get(position);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public List<Object> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setItems(List<Object> items) {
|
||||
this.items.clear();
|
||||
this.items.addAll(items);
|
||||
}
|
||||
|
||||
static class HeaderVH extends RecyclerView.ViewHolder {
|
||||
|
||||
final TextView title;
|
||||
|
@ -149,8 +154,7 @@ public class SavedArticlesRvAdapter extends RecyclerView.Adapter<RecyclerView.Vi
|
|||
public void onClick(View view) {
|
||||
Object item = getItemByPosition();
|
||||
if (item != null && item instanceof WikivoyageArticle) {
|
||||
WikivoyageArticle article = (WikivoyageArticle) item;
|
||||
Toast.makeText(app, "delete: " + article.getTitle(), Toast.LENGTH_SHORT).show();
|
||||
WikivoyageLocalDataHelper.getInstance(app).removeArticleFromSaved((WikivoyageArticle) item);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -3,6 +3,7 @@ package net.osmand.plus.wikivoyage.explore;
|
|||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.util.DiffUtil;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
|
@ -11,18 +12,107 @@ import android.view.ViewGroup;
|
|||
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.base.BaseOsmAndFragment;
|
||||
import net.osmand.plus.wikivoyage.data.WikivoyageArticle;
|
||||
import net.osmand.plus.wikivoyage.data.WikivoyageLocalDataHelper;
|
||||
|
||||
public class SavedArticlesTabFragment extends BaseOsmAndFragment {
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class SavedArticlesTabFragment extends BaseOsmAndFragment implements WikivoyageLocalDataHelper.Listener {
|
||||
|
||||
private WikivoyageLocalDataHelper dataHelper;
|
||||
|
||||
private SavedArticlesRvAdapter adapter;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
dataHelper = WikivoyageLocalDataHelper.getInstance(getMyApplication());
|
||||
adapter = new SavedArticlesRvAdapter(getMyApplication());
|
||||
|
||||
final View mainView = inflater.inflate(R.layout.fragment_saved_articles_tab, container, false);
|
||||
|
||||
final RecyclerView rv = (RecyclerView) mainView.findViewById(R.id.recycler_view);
|
||||
rv.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
rv.setAdapter(new SavedArticlesRvAdapter(getMyApplication()));
|
||||
rv.setAdapter(adapter);
|
||||
|
||||
return mainView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
adapter.setItems(getItems());
|
||||
adapter.notifyDataSetChanged();
|
||||
dataHelper.setListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
dataHelper.setListener(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void savedArticlesUpdated() {
|
||||
List<Object> newItems = getItems();
|
||||
SavedArticlesDiffCallback diffCallback = new SavedArticlesDiffCallback(adapter.getItems(), newItems);
|
||||
adapter.setItems(newItems);
|
||||
DiffUtil.calculateDiff(diffCallback).dispatchUpdatesTo(adapter);
|
||||
}
|
||||
|
||||
private List<Object> getItems() {
|
||||
List<Object> items = new ArrayList<>();
|
||||
List<WikivoyageArticle> savedArticles = dataHelper.getSavedArticles();
|
||||
if (!savedArticles.isEmpty()) {
|
||||
Collections.reverse(savedArticles);
|
||||
items.add(getString(R.string.saved_articles));
|
||||
items.addAll(savedArticles);
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
private static class SavedArticlesDiffCallback extends DiffUtil.Callback {
|
||||
|
||||
private List<Object> oldItems;
|
||||
private List<Object> newItems;
|
||||
|
||||
SavedArticlesDiffCallback(List<Object> oldItems, List<Object> newItems) {
|
||||
this.oldItems = oldItems;
|
||||
this.newItems = newItems;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOldListSize() {
|
||||
return oldItems.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNewListSize() {
|
||||
return newItems.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
|
||||
Object oldItem = oldItems.get(oldItemPosition);
|
||||
Object newItem = newItems.get(newItemPosition);
|
||||
return (oldItem instanceof String && newItem instanceof String) || oldItem == newItem;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
|
||||
Object oldItem = oldItems.get(oldItemPosition);
|
||||
Object newItem = newItems.get(newItemPosition);
|
||||
if (oldItem instanceof String && newItem instanceof String) {
|
||||
return false;
|
||||
} else if (oldItem instanceof WikivoyageArticle && newItem instanceof WikivoyageArticle) {
|
||||
WikivoyageArticle oldArticle = (WikivoyageArticle) oldItem;
|
||||
WikivoyageArticle newArticle = (WikivoyageArticle) newItem;
|
||||
return oldArticle.getCityId() == newArticle.getCityId()
|
||||
&& oldArticle.getLang().equals(newArticle.getLang());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue