Add wikivoyage search history UI
This commit is contained in:
parent
21f2406dcc
commit
d9b641c420
5 changed files with 91 additions and 18 deletions
|
@ -0,0 +1,23 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item>
|
||||
<shape android:shape="oval">
|
||||
<solid android:color="@color/color_transparent"/>
|
||||
<size
|
||||
android:width="28dp"
|
||||
android:height="28dp"/>
|
||||
</shape>
|
||||
</item>
|
||||
<item
|
||||
android:id="@+id/placeholder_icon"
|
||||
android:bottom="2dp"
|
||||
android:left="2dp"
|
||||
android:right="2dp"
|
||||
android:top="2dp">
|
||||
|
||||
<bitmap
|
||||
android:gravity="center"
|
||||
android:src="@drawable/ic_action_history"
|
||||
android:tint="@color/icon_color"/>
|
||||
</item>
|
||||
</layer-list>
|
|
@ -1,6 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
>
|
||||
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item>
|
||||
<shape android:shape="oval">
|
||||
<solid android:color="@color/color_transparent"/>
|
||||
|
|
|
@ -22,6 +22,7 @@ import net.osmand.AndroidUtils;
|
|||
import net.osmand.IndexConstants;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.wikivoyage.data.WikivoyageArticle;
|
||||
import net.osmand.plus.wikivoyage.data.WikivoyageLocalDataHelper;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
|
@ -146,6 +147,8 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
|
|||
return;
|
||||
}
|
||||
|
||||
WikivoyageLocalDataHelper.getInstance(getMyApplication()).addToHistory(article);
|
||||
|
||||
contentWebView.loadDataWithBaseURL(getBaseUrl(), createHtmlContent(article), "text/html", "UTF-8", null);
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,9 @@ import net.osmand.plus.OsmandApplication;
|
|||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.widgets.tools.CropCircleTransformation;
|
||||
import net.osmand.plus.wikivoyage.data.WikivoyageArticle;
|
||||
import net.osmand.plus.wikivoyage.data.WikivoyageSearchHistoryItem;
|
||||
import net.osmand.plus.wikivoyage.data.WikivoyageSearchResult;
|
||||
import net.osmand.util.Algorithms;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -29,7 +31,9 @@ public class SearchRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView
|
|||
private static final int ITEM_TYPE = 1;
|
||||
|
||||
private OsmandApplication app;
|
||||
|
||||
private LayerDrawable placeholder;
|
||||
private LayerDrawable historyPlaceholder;
|
||||
|
||||
private List<Object> items = new ArrayList<>();
|
||||
|
||||
|
@ -41,11 +45,8 @@ public class SearchRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView
|
|||
|
||||
SearchRecyclerViewAdapter(OsmandApplication app) {
|
||||
this.app = app;
|
||||
placeholder = (LayerDrawable) ContextCompat.getDrawable(app, R.drawable.wikivoyage_search_placeholder);
|
||||
if (Build.VERSION.SDK_INT < 21 && placeholder != null) {
|
||||
placeholder.setDrawableByLayerId(R.id.placeholder_icon,
|
||||
app.getIconsCache().getIcon(R.drawable.ic_action_placeholder_city, R.color.icon_color));
|
||||
}
|
||||
placeholder = getPlaceholder(false);
|
||||
historyPlaceholder = getPlaceholder(true);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
|
@ -69,15 +70,24 @@ public class SearchRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView
|
|||
ItemVH holder = (ItemVH) viewHolder;
|
||||
boolean lastItem = pos == getItemCount() - 1;
|
||||
|
||||
WikivoyageSearchResult item = (WikivoyageSearchResult) getItem(pos);
|
||||
Picasso.get()
|
||||
.load(WikivoyageArticle.getThumbImageUrl(item.getImageTitle()))
|
||||
.transform(new CropCircleTransformation())
|
||||
.placeholder(placeholder)
|
||||
.into(holder.icon);
|
||||
holder.title.setText(item.getArticleTitles().get(0));
|
||||
holder.leftDescr.setText(item.getIsPartOf());
|
||||
holder.rightDescr.setText(item.getFirstLangsString());
|
||||
Object item = getItem(pos);
|
||||
if (item instanceof WikivoyageSearchResult) {
|
||||
WikivoyageSearchResult searchRes = (WikivoyageSearchResult) item;
|
||||
Picasso.get()
|
||||
.load(WikivoyageArticle.getThumbImageUrl(searchRes.getImageTitle()))
|
||||
.transform(new CropCircleTransformation())
|
||||
.placeholder(placeholder)
|
||||
.into(holder.icon);
|
||||
holder.title.setText(searchRes.getArticleTitles().get(0));
|
||||
holder.leftDescr.setText(searchRes.getIsPartOf());
|
||||
holder.rightDescr.setText(searchRes.getFirstLangsString());
|
||||
} else {
|
||||
WikivoyageSearchHistoryItem historyItem = (WikivoyageSearchHistoryItem) item;
|
||||
holder.icon.setImageDrawable(historyPlaceholder);
|
||||
holder.title.setText(historyItem.getArticleTitle());
|
||||
holder.leftDescr.setText(historyItem.getIsPartOf());
|
||||
holder.rightDescr.setText(Algorithms.capitalizeFirstLetter(historyItem.getLang()));
|
||||
}
|
||||
holder.divider.setVisibility(lastItem ? View.GONE : View.VISIBLE);
|
||||
holder.shadow.setVisibility(lastItem ? View.VISIBLE : View.GONE);
|
||||
}
|
||||
|
@ -101,6 +111,15 @@ public class SearchRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView
|
|||
return items.get(pos);
|
||||
}
|
||||
|
||||
public void setHistoryItems(@Nullable List<WikivoyageSearchHistoryItem> historyItems) {
|
||||
this.items.clear();
|
||||
if (historyItems != null && !historyItems.isEmpty()) {
|
||||
this.items.add(app.getString(R.string.shared_string_history));
|
||||
this.items.addAll(historyItems);
|
||||
}
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public void setItems(@Nullable List<WikivoyageSearchResult> items) {
|
||||
this.items.clear();
|
||||
if (items != null && !items.isEmpty()) {
|
||||
|
@ -110,6 +129,21 @@ public class SearchRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView
|
|||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
private LayerDrawable getPlaceholder(boolean history) {
|
||||
LayerDrawable res = (LayerDrawable) ContextCompat.getDrawable(
|
||||
app, history
|
||||
? R.drawable.wikivoyage_search_history_placeholder
|
||||
: R.drawable.wikivoyage_search_placeholder
|
||||
);
|
||||
if (Build.VERSION.SDK_INT < 21 && res != null) {
|
||||
res.setDrawableByLayerId(R.id.placeholder_icon, app.getIconsCache().getIcon(
|
||||
history ? R.drawable.ic_action_history : R.drawable.ic_action_placeholder_city,
|
||||
R.color.icon_color
|
||||
));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static class HeaderVH extends RecyclerView.ViewHolder {
|
||||
|
||||
final TextView title;
|
||||
|
|
|
@ -8,6 +8,7 @@ import android.support.v7.widget.LinearLayoutManager;
|
|||
import android.support.v7.widget.RecyclerView;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.text.Editable;
|
||||
import android.text.TextUtils;
|
||||
import android.text.TextWatcher;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
|
@ -20,6 +21,7 @@ import net.osmand.ResultMatcher;
|
|||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.wikivoyage.WikivoyageArticleDialogFragment;
|
||||
import net.osmand.plus.wikivoyage.WikivoyageBaseDialogFragment;
|
||||
import net.osmand.plus.wikivoyage.data.WikivoyageLocalDataHelper;
|
||||
import net.osmand.plus.wikivoyage.data.WikivoyageSearchResult;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -71,7 +73,7 @@ public class WikivoyageSearchDialogFragment extends WikivoyageBaseDialogFragment
|
|||
searchQuery = newQuery;
|
||||
if (searchQuery.isEmpty()) {
|
||||
cancelSearch();
|
||||
adapter.setItems(null);
|
||||
setAdapterItems(null);
|
||||
} else {
|
||||
runSearch();
|
||||
}
|
||||
|
@ -117,6 +119,9 @@ public class WikivoyageSearchDialogFragment extends WikivoyageBaseDialogFragment
|
|||
super.onResume();
|
||||
paused = false;
|
||||
searchEt.requestFocus();
|
||||
if (TextUtils.isEmpty(searchQuery)) {
|
||||
setAdapterItems(null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -132,6 +137,15 @@ public class WikivoyageSearchDialogFragment extends WikivoyageBaseDialogFragment
|
|||
}
|
||||
}
|
||||
|
||||
private void setAdapterItems(@Nullable List<WikivoyageSearchResult> items) {
|
||||
if (items == null || items.isEmpty()) {
|
||||
adapter.setHistoryItems(WikivoyageLocalDataHelper
|
||||
.getInstance(getMyApplication()).getAllHistory());
|
||||
} else {
|
||||
adapter.setItems(items);
|
||||
}
|
||||
}
|
||||
|
||||
private void runSearch() {
|
||||
switchProgressBarVisibility(true);
|
||||
cancelled = false;
|
||||
|
@ -141,7 +155,7 @@ public class WikivoyageSearchDialogFragment extends WikivoyageBaseDialogFragment
|
|||
getMyApplication().runInUIThread(new Runnable() {
|
||||
public void run() {
|
||||
if (!isCancelled()) {
|
||||
adapter.setItems(results);
|
||||
setAdapterItems(results);
|
||||
switchProgressBarVisibility(false);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue