From 38dc1a8c1ec2d9a946fe66ea669d0551cc77a9fa Mon Sep 17 00:00:00 2001 From: "Mr. Dragon" Date: Sat, 19 May 2018 23:48:21 +0530 Subject: [PATCH] Improve search fragment - Now get your recent apps instead of package names.. --- .../aurora/adapters/SearchHistoryAdapter.java | 17 +- .../aurora/fragment/SearchFragment.java | 178 +++++++++--------- .../task/playstore/SearchHistoryTask.java | 109 +++++++++++ app/src/main/res/layout/fragment_search.xml | 18 +- app/src/main/res/values/strings.xml | 1 + 5 files changed, 235 insertions(+), 88 deletions(-) create mode 100644 app/src/main/java/com/dragons/aurora/task/playstore/SearchHistoryTask.java diff --git a/app/src/main/java/com/dragons/aurora/adapters/SearchHistoryAdapter.java b/app/src/main/java/com/dragons/aurora/adapters/SearchHistoryAdapter.java index a4ab70c88..fc2f0c232 100644 --- a/app/src/main/java/com/dragons/aurora/adapters/SearchHistoryAdapter.java +++ b/app/src/main/java/com/dragons/aurora/adapters/SearchHistoryAdapter.java @@ -23,7 +23,7 @@ import java.util.concurrent.TimeUnit; public class SearchHistoryAdapter extends RecyclerView.Adapter { - private ArrayList queryHistory; + public ArrayList queryHistory; private Context c; public SearchHistoryAdapter(ArrayList queryHistory, Context c) { @@ -31,6 +31,21 @@ public class SearchHistoryAdapter extends RecyclerView.Adapter listHistory = new ArrayList<>(); - Set 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 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 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 getSharedValue() { - Set set = PreferenceManager - .getDefaultSharedPreferences(getActivity()) - .getStringSet("SEARCH_HISTORY", null); - - if (set != null) { - listHistory.clear(); - listHistory.addAll(set); - } - return listHistory; - } - - private void putSharedValue(Set 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 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 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(); } } - } diff --git a/app/src/main/java/com/dragons/aurora/task/playstore/SearchHistoryTask.java b/app/src/main/java/com/dragons/aurora/task/playstore/SearchHistoryTask.java new file mode 100644 index 000000000..2b9482eab --- /dev/null +++ b/app/src/main/java/com/dragons/aurora/task/playstore/SearchHistoryTask.java @@ -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 readFromPref(String Key) { + Set set = PreferenceManager + .getDefaultSharedPreferences(getContext()) + .getStringSet(Key, null); + if (set != null) + return set; + else + return new HashSet<>(); + } + + public void writeToPref(String Key, Set 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 oldList = getHistoryList(); + oldList.add(datedQuery); + Set newSet = new HashSet<>(); + newSet.addAll(oldList); + writeToPref("SEARCH_HISTORY", newSet); + } + + public void addRecentApps(String packageName) { + Set set = readFromPref("APP_HISTORY"); + + ArrayList currList = new ArrayList<>(); + currList.addAll(set); + currList.add(packageName); + + set.clear(); + set.addAll(currList); + writeToPref("APP_HISTORY", set); + } + + public ArrayList getHistoryList() { + Set oldSet = readFromPref("SEARCH_HISTORY"); + ArrayList oldList = new ArrayList<>(); + oldList.addAll(oldSet); + return oldList; + } + + public ArrayList getRecentAppsList() { + ArrayList currList = new ArrayList<>(); + Set savedAppSet = readFromPref("APP_HISTORY"); + currList.clear(); + currList.addAll(savedAppSet); + return currList; + } + + public List getHistoryApps(GooglePlayAPI api, ArrayList currList) throws IOException { + List 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 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(); + } + +} diff --git a/app/src/main/res/layout/fragment_search.xml b/app/src/main/res/layout/fragment_search.xml index 9510bd4c2..af8b4f4e9 100644 --- a/app/src/main/res/layout/fragment_search.xml +++ b/app/src/main/res/layout/fragment_search.xml @@ -81,7 +81,6 @@ android:id="@+id/searchcard" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginBottom="@dimen/appbar_bar_height_margin" android:layout_marginEnd="12dp" android:layout_marginStart="12dp" android:layout_marginTop="12dp" @@ -107,4 +106,21 @@ android:textSize="18sp" /> + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 61f5e9011..ab1391c31 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -41,6 +41,7 @@ Refresh Token Remove Your Search History + Your Recent Apps Clear all Search Apps & Games Select an account to login