mirror of
https://github.com/whyorean/AuroraStore.git
synced 2026-06-20 21:50:19 -04:00
Improve search fragment
- Now get your recent apps instead of package names..
This commit is contained in:
@@ -23,7 +23,7 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class SearchHistoryAdapter extends RecyclerView.Adapter<SearchHistoryAdapter.ViewHolder> {
|
||||
|
||||
private ArrayList<String> queryHistory;
|
||||
public ArrayList<String> queryHistory;
|
||||
private Context c;
|
||||
|
||||
public SearchHistoryAdapter(ArrayList<String> queryHistory, Context c) {
|
||||
@@ -31,6 +31,21 @@ public class SearchHistoryAdapter extends RecyclerView.Adapter<SearchHistoryAdap
|
||||
this.c = c;
|
||||
}
|
||||
|
||||
public void add(int position, String query) {
|
||||
queryHistory.add(position, query);
|
||||
notifyItemInserted(position);
|
||||
}
|
||||
|
||||
public void remove(int position) {
|
||||
queryHistory.remove(position);
|
||||
notifyItemRemoved(position);
|
||||
}
|
||||
|
||||
public void remove(String query) {
|
||||
queryHistory.remove(query);
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public SearchHistoryAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
package com.dragons.aurora.fragment;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.SearchManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.widget.DefaultItemAnimator;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
@@ -21,36 +18,54 @@ import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.dragons.aurora.HistoryItemTouchHelper;
|
||||
import com.dragons.aurora.PlayStoreApiAuthenticator;
|
||||
import com.dragons.aurora.R;
|
||||
import com.dragons.aurora.activities.SearchActivity;
|
||||
import com.dragons.aurora.adapters.SearchHistoryAdapter;
|
||||
import com.dragons.aurora.task.playstore.SearchHistoryTask;
|
||||
import com.dragons.aurora.view.ClusterAppsCard;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
public class SearchFragment extends UtilFragment implements HistoryItemTouchHelper.RecyclerItemTouchHelperListener {
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
ArrayList<String> listHistory = new ArrayList<>();
|
||||
Set<String> setHistory = new HashSet<>();
|
||||
public class SearchFragment extends SearchHistoryTask implements HistoryItemTouchHelper.RecyclerItemTouchHelperListener {
|
||||
@BindView(R.id.searchClusterApp)
|
||||
ClusterAppsCard clusterAppsCard;
|
||||
@BindView(R.id.m_apps_recycler)
|
||||
RecyclerView clusterRecycler;
|
||||
@BindView(R.id.searchHistory)
|
||||
RecyclerView recyclerView;
|
||||
@BindView(R.id.search_layout)
|
||||
RelativeLayout search_layout;
|
||||
@BindView(R.id.emptyView)
|
||||
TextView emptyView;
|
||||
@BindView(R.id.clearAll)
|
||||
TextView clearAll;
|
||||
|
||||
private View view;
|
||||
private ArrayList<String> currList;
|
||||
private SearchHistoryAdapter searchHistoryAdapter;
|
||||
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.fragment_search, container, false);
|
||||
if (view != null) {
|
||||
if ((ViewGroup) view.getParent() != null)
|
||||
((ViewGroup) view.getParent()).removeView(view);
|
||||
return view;
|
||||
}
|
||||
|
||||
view = inflater.inflate(R.layout.fragment_search, container, false);
|
||||
SearchView searchToolbar = view.findViewById(R.id.search_apps);
|
||||
ButterKnife.bind(this, view);
|
||||
|
||||
recyclerView = view.findViewById(R.id.searchHistory);
|
||||
emptyView = view.findViewById(R.id.emptyView);
|
||||
|
||||
TextView clearAll = view.findViewById(R.id.clearAll);
|
||||
clearAll.setOnClickListener(v -> clearAll());
|
||||
|
||||
RelativeLayout search_layout = view.findViewById(R.id.search_layout);
|
||||
search_layout.setOnClickListener(v -> {
|
||||
searchToolbar.setFocusable(true);
|
||||
searchToolbar.setIconified(false);
|
||||
@@ -59,23 +74,23 @@ public class SearchFragment extends UtilFragment implements HistoryItemTouchHelp
|
||||
});
|
||||
|
||||
addQueryTextListener(searchToolbar);
|
||||
setupSearchHistory();
|
||||
getHistoryApps(getRecentAppsList());
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
setupSearchHistory();
|
||||
updateSearchHistory();
|
||||
}
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
||||
protected void addQueryTextListener(SearchView searchView) {
|
||||
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
|
||||
if (null != searchManager) {
|
||||
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
|
||||
}
|
||||
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
|
||||
|
||||
@Override
|
||||
public boolean onQueryTextChange(String query) {
|
||||
return true;
|
||||
@@ -108,8 +123,12 @@ public class SearchFragment extends UtilFragment implements HistoryItemTouchHelp
|
||||
});
|
||||
}
|
||||
|
||||
protected void setQuery(String query) {
|
||||
addHistory(query);
|
||||
private void setQuery(String query) {
|
||||
if (looksLikeAPackageId(query))
|
||||
addRecentApps(query);
|
||||
else
|
||||
addHistory(query);
|
||||
|
||||
Intent i = new Intent(getContext(), SearchActivity.class);
|
||||
i.setAction(Intent.ACTION_SEARCH);
|
||||
i.putExtra(SearchManager.QUERY, query);
|
||||
@@ -117,74 +136,50 @@ public class SearchFragment extends UtilFragment implements HistoryItemTouchHelp
|
||||
}
|
||||
|
||||
private void setupSearchHistory() {
|
||||
listHistory = getSharedValue();
|
||||
currList = getHistoryList();
|
||||
toggleEmptyRecycle(currList);
|
||||
searchHistoryAdapter = new SearchHistoryAdapter(currList, getContext());
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
|
||||
recyclerView.setItemAnimator(new DefaultItemAnimator());
|
||||
recyclerView.setNestedScrollingEnabled(false);
|
||||
recyclerView.setAdapter(searchHistoryAdapter);
|
||||
new ItemTouchHelper(
|
||||
new HistoryItemTouchHelper(0, ItemTouchHelper.LEFT, this))
|
||||
.attachToRecyclerView(recyclerView);
|
||||
}
|
||||
|
||||
if (listHistory.isEmpty())
|
||||
toggleEmptyRecycle(true);
|
||||
else {
|
||||
toggleEmptyRecycle(false);
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
|
||||
recyclerView.setItemAnimator(new DefaultItemAnimator());
|
||||
recyclerView.setNestedScrollingEnabled(false);
|
||||
recyclerView.setAdapter(new SearchHistoryAdapter(listHistory, getActivity()));
|
||||
new ItemTouchHelper(
|
||||
new HistoryItemTouchHelper(0, ItemTouchHelper.LEFT, this))
|
||||
.attachToRecyclerView(recyclerView);
|
||||
private void updateSearchHistory() {
|
||||
if (searchHistoryAdapter != null) {
|
||||
currList = getHistoryList();
|
||||
toggleEmptyRecycle(currList);
|
||||
searchHistoryAdapter.queryHistory = currList;
|
||||
searchHistoryAdapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public void addHistory(String query) {
|
||||
String date = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault()).format(new Date());
|
||||
String datedQuery = query + ":" + date;
|
||||
|
||||
listHistory.add(datedQuery);
|
||||
setHistory.clear();
|
||||
setHistory.addAll(listHistory);
|
||||
putSharedValue(setHistory);
|
||||
}
|
||||
|
||||
public void updateHistory() {
|
||||
setHistory.clear();
|
||||
setHistory.addAll(listHistory);
|
||||
putSharedValue(setHistory);
|
||||
|
||||
recyclerView.getAdapter().notifyDataSetChanged();
|
||||
private void updateHistoryPref() {
|
||||
Set<String> updatedSet = new HashSet<>();
|
||||
updatedSet.addAll(currList);
|
||||
writeToPref("SEARCH_HISTORY", updatedSet);
|
||||
if (recyclerView.getAdapter().getItemCount() == 0) {
|
||||
recyclerView.setVisibility(View.GONE);
|
||||
emptyView.setVisibility(View.VISIBLE);
|
||||
toggleEmptyRecycle(currList);
|
||||
}
|
||||
}
|
||||
|
||||
private ArrayList<String> getSharedValue() {
|
||||
Set<String> set = PreferenceManager
|
||||
.getDefaultSharedPreferences(getActivity())
|
||||
.getStringSet("SEARCH_HISTORY", null);
|
||||
|
||||
if (set != null) {
|
||||
listHistory.clear();
|
||||
listHistory.addAll(set);
|
||||
}
|
||||
return listHistory;
|
||||
}
|
||||
|
||||
private void putSharedValue(Set<String> setHistory) {
|
||||
PreferenceManager.getDefaultSharedPreferences(getActivity())
|
||||
.edit()
|
||||
.putStringSet("SEARCH_HISTORY", setHistory)
|
||||
.apply();
|
||||
}
|
||||
|
||||
private void clearAll() {
|
||||
setHistory.clear();
|
||||
listHistory.clear();
|
||||
if (recyclerView.getAdapter() != null)
|
||||
recyclerView.getAdapter().notifyDataSetChanged();
|
||||
putSharedValue(setHistory);
|
||||
toggleEmptyRecycle(true);
|
||||
if (searchHistoryAdapter != null) {
|
||||
currList = new ArrayList<>();
|
||||
searchHistoryAdapter.queryHistory = currList;
|
||||
searchHistoryAdapter.notifyDataSetChanged();
|
||||
}
|
||||
writeToPref("SEARCH_HISTORY", new HashSet<>());
|
||||
writeToPref("APP_HISTORY", new HashSet<>());
|
||||
clusterAppsCard.setVisibility(View.GONE);
|
||||
toggleEmptyRecycle(currList);
|
||||
}
|
||||
|
||||
private void toggleEmptyRecycle(boolean toggle) {
|
||||
if (toggle) {
|
||||
private void toggleEmptyRecycle(ArrayList<String> currList) {
|
||||
if (currList.isEmpty()) {
|
||||
recyclerView.setVisibility(View.GONE);
|
||||
emptyView.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
@@ -193,17 +188,28 @@ public class SearchFragment extends UtilFragment implements HistoryItemTouchHelp
|
||||
}
|
||||
}
|
||||
|
||||
public void getHistoryApps(ArrayList<String> appList) {
|
||||
if (!appList.isEmpty()) {
|
||||
TextView clusterTitle = clusterAppsCard.findViewById(R.id.m_apps_title);
|
||||
clusterTitle.setText(R.string.action_search_history_apps);
|
||||
clusterTitle.setTextSize(18);
|
||||
Observable.fromCallable(() -> getHistoryApps(new PlayStoreApiAuthenticator(this.getActivity()).getApi(), appList))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe((appToAdd) -> {
|
||||
if (view != null) {
|
||||
setupRecyclerView(clusterRecycler, appToAdd);
|
||||
}
|
||||
}, this::processException);
|
||||
} else clusterAppsCard.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position) {
|
||||
if (viewHolder instanceof SearchHistoryAdapter.ViewHolder) {
|
||||
String query = listHistory.get(viewHolder.getAdapterPosition());
|
||||
for (int j = listHistory.size() - 1; j >= 0; j--) {
|
||||
if (listHistory.get(j).contains(query)) {
|
||||
listHistory.remove(j);
|
||||
updateHistory();
|
||||
}
|
||||
}
|
||||
searchHistoryAdapter.remove(position);
|
||||
currList = searchHistoryAdapter.queryHistory;
|
||||
updateHistoryPref();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.dragons.aurora.task.playstore;
|
||||
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.text.TextUtils;
|
||||
import android.view.animation.AnimationUtils;
|
||||
|
||||
import com.dragons.aurora.R;
|
||||
import com.dragons.aurora.adapters.RecyclerAppsAdapter;
|
||||
import com.dragons.aurora.model.App;
|
||||
import com.dragons.aurora.model.AppBuilder;
|
||||
import com.dragons.aurora.playstoreapiv2.DetailsResponse;
|
||||
import com.dragons.aurora.playstoreapiv2.GooglePlayAPI;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class SearchHistoryTask extends ExceptionTask {
|
||||
|
||||
public Set<String> readFromPref(String Key) {
|
||||
Set<String> set = PreferenceManager
|
||||
.getDefaultSharedPreferences(getContext())
|
||||
.getStringSet(Key, null);
|
||||
if (set != null)
|
||||
return set;
|
||||
else
|
||||
return new HashSet<>();
|
||||
}
|
||||
|
||||
public void writeToPref(String Key, Set<String> newAppSet) {
|
||||
PreferenceManager
|
||||
.getDefaultSharedPreferences(getContext())
|
||||
.edit()
|
||||
.putStringSet(Key, newAppSet)
|
||||
.apply();
|
||||
}
|
||||
|
||||
public void addHistory(String query) {
|
||||
String date = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault()).format(new Date());
|
||||
String datedQuery = query + ":" + date;
|
||||
ArrayList<String> oldList = getHistoryList();
|
||||
oldList.add(datedQuery);
|
||||
Set<String> newSet = new HashSet<>();
|
||||
newSet.addAll(oldList);
|
||||
writeToPref("SEARCH_HISTORY", newSet);
|
||||
}
|
||||
|
||||
public void addRecentApps(String packageName) {
|
||||
Set<String> set = readFromPref("APP_HISTORY");
|
||||
|
||||
ArrayList<String> currList = new ArrayList<>();
|
||||
currList.addAll(set);
|
||||
currList.add(packageName);
|
||||
|
||||
set.clear();
|
||||
set.addAll(currList);
|
||||
writeToPref("APP_HISTORY", set);
|
||||
}
|
||||
|
||||
public ArrayList<String> getHistoryList() {
|
||||
Set<String> oldSet = readFromPref("SEARCH_HISTORY");
|
||||
ArrayList<String> oldList = new ArrayList<>();
|
||||
oldList.addAll(oldSet);
|
||||
return oldList;
|
||||
}
|
||||
|
||||
public ArrayList<String> getRecentAppsList() {
|
||||
ArrayList<String> currList = new ArrayList<>();
|
||||
Set<String> savedAppSet = readFromPref("APP_HISTORY");
|
||||
currList.clear();
|
||||
currList.addAll(savedAppSet);
|
||||
return currList;
|
||||
}
|
||||
|
||||
public List<App> getHistoryApps(GooglePlayAPI api, ArrayList<String> currList) throws IOException {
|
||||
List<App> apps = new ArrayList<>();
|
||||
for (String packageName : currList) {
|
||||
DetailsResponse response = api.details(packageName);
|
||||
App app = AppBuilder.build(response);
|
||||
apps.add(app);
|
||||
}
|
||||
return apps;
|
||||
}
|
||||
|
||||
public void setupRecyclerView(RecyclerView recyclerView, List<App> appsToAdd) {
|
||||
recyclerView.setHasFixedSize(true);
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
|
||||
recyclerView.setLayoutAnimation(AnimationUtils.loadLayoutAnimation(getContext(), R.anim.layout_anim));
|
||||
recyclerView.setAdapter(new RecyclerAppsAdapter(getContext(), appsToAdd));
|
||||
}
|
||||
|
||||
public boolean looksLikeAPackageId(String query) {
|
||||
if (TextUtils.isEmpty(query)) {
|
||||
return false;
|
||||
}
|
||||
String pattern = "([\\p{L}_$][\\p{L}\\p{N}_$]*\\.)+[\\p{L}_$][\\p{L}\\p{N}_$]*";
|
||||
Pattern r = Pattern.compile(pattern);
|
||||
return r.matcher(query).matches();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user