Search history and recent searches displayed under search bar

This commit is contained in:
Clayton Wilson 2020-11-30 18:28:35 -05:00
parent 578d360319
commit cab6bffac5
6 changed files with 139 additions and 18 deletions

View File

@ -26,6 +26,7 @@ import org.json.JSONException;
import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Properties;
import static com.example.listify.SplashActivity.showSplash;
@ -33,6 +34,7 @@ import static com.example.listify.SplashActivity.showSplash;
public class MainActivity extends AppCompatActivity implements CreateListDialogFragment.OnNewListListener {
private AppBarConfiguration mAppBarConfiguration;
public static AuthManager am = new AuthManager();
public static ArrayList<String> searchHistory;
@Override
public void onBackPressed() {

View File

@ -1,4 +1,5 @@
package com.example.listify;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
@ -7,12 +8,14 @@ import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SearchView;
//import android.widget.SearchView;
import androidx.appcompat.widget.SearchView;
import android.widget.TextView;
import com.example.listify.adapter.SearchResultsListAdapter;
@ -28,12 +31,16 @@ import java.util.List;
import java.util.Properties;
import static com.example.listify.MainActivity.am;
import static com.example.listify.MainActivity.searchHistory;
public class SearchResults extends AppCompatActivity implements FilterDialogFragment.OnFilterListener, SortDialogFragment.OnSortListener, Requestor.Receiver {
private ListView resultsListView;
private MenuItem filterItem;
private ProgressBar loadingSearch;
private TextView tvNoResults;
private SearchView.SearchAutoComplete searchAutoComplete;
private ArrayList<String> orderedHistory = new ArrayList<>();
private ArrayAdapter<String> searchHistoryAdapter;
private SearchResultsListAdapter searchResultsListAdapter;
private List<Product> resultsProductList = new ArrayList<>();
private List<Product> resultsProductListSorted = new ArrayList<>();
@ -85,19 +92,19 @@ public class SearchResults extends AppCompatActivity implements FilterDialogFrag
// There's no easy way to find the close button on the search bar, so this is the way I'm
// doing it
int searchCloseButtonId = searchView.getContext().getResources().getIdentifier("android:id/search_close_btn", null, null);
ImageView closeButton = (ImageView) searchView.findViewById(searchCloseButtonId);
closeButton.setOnClickListener(new View.OnClickListener() {
// Override default close behavior to only clear the search text and the query
@Override
public void onClick(View v) {
// Finding the edit text of the search bar. Same as the method above
int searchTextId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchText = (EditText) searchView.findViewById(searchTextId);
searchText.setText("");
searchView.setQuery("", false);
}
});
// int searchCloseButtonId = searchView.getContext().getResources().getIdentifier("android:id/search_close_btn", null, null);
// ImageView closeButton = (ImageView) searchView.findViewById(searchCloseButtonId);
// closeButton.setOnClickListener(new View.OnClickListener() {
// // Override default close behavior to only clear the search text and the query
// @Override
// public void onClick(View v) {
// // Finding the edit text of the search bar. Same as the method above
// int searchTextId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
// EditText searchText = (EditText) searchView.findViewById(searchTextId);
// searchText.setText("");
// searchView.setQuery("", false);
// }
// });
resultsListView = (ListView) findViewById(R.id.search_results_list);
searchResultsListAdapter = new SearchResultsListAdapter(this, resultsProductListSorted);
@ -113,7 +120,7 @@ public class SearchResults extends AppCompatActivity implements FilterDialogFrag
}
});
// Handle searches
// Handle searches
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
@ -135,6 +142,49 @@ public class SearchResults extends AppCompatActivity implements FilterDialogFrag
return false;
}
});
searchAutoComplete = (SearchView.SearchAutoComplete) searchView.findViewById(androidx.appcompat.R.id.search_src_text);
SearchManager searchManager = (SearchManager) getSystemService(this.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
orderedHistory.clear();
orderedHistory.addAll(searchHistory);
// Reverse the history so it's displayed in the correct ordered
for (int i = 0; i < orderedHistory.size() / 2; i++) {
String temp = orderedHistory.get(i);
orderedHistory.set(i, orderedHistory.get(orderedHistory.size() - i - 1));
orderedHistory.set(orderedHistory.size() - i - 1, temp);
}
searchHistoryAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.search_suggestion, orderedHistory);
System.out.println(orderedHistory);
searchAutoComplete.setAdapter(searchHistoryAdapter);
searchAutoComplete.setThreshold(0);
searchAutoComplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String query = ((TextView) view).getText().toString();
searchView.setQuery(query, false);
// Show progress bar
loadingSearch.setVisibility(View.VISIBLE);
// Clear the old search results
resultsProductList.clear();
// Clear old search results from the view
resultsProductListSorted.clear();
searchResultsListAdapter.notifyDataSetChanged();
doSearch(query);
}
});
}
@Override
@ -188,7 +238,6 @@ public class SearchResults extends AppCompatActivity implements FilterDialogFrag
if (resultsProductList.isEmpty()) {
filterItem.setEnabled(false);
}
return true;
}
@ -200,6 +249,25 @@ public class SearchResults extends AppCompatActivity implements FilterDialogFrag
}
private void doSearch(String query) {
// Add this search to the history
searchHistory.add(query);
orderedHistory.clear();
orderedHistory.addAll(searchHistory);
ArrayList<String> t = new ArrayList<>();
t.addAll(searchHistory);
// Reverse the history so it's displayed in the correct ordered
for (int i = 0; i < t.size() / 2; i++) {
String temp = t.get(i);
t.set(i, t.get(t.size() - i - 1));
t.set(t.size() - i - 1, temp);
}
// Have to make a new adapter here because calling notifyDatasetChanged() would not update the search suggestions for some reason
searchHistoryAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.search_suggestion, t);
searchAutoComplete.setAdapter(searchHistoryAdapter);
Properties configs = new Properties();
try {
configs = AuthManager.loadProperties(this, "android.resource://" + getPackageName() + "/raw/auths.json");

View File

@ -22,6 +22,8 @@ import com.example.listify.Requestor;
import com.example.listify.SynchronousReceiver;
import com.example.listify.adapter.ShoppingListsSwipeableAdapter;
import com.example.listify.data.List;
import com.example.listify.data.SearchHistory;
import com.example.listify.model.Product;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import org.json.JSONException;
@ -32,6 +34,7 @@ import java.util.ArrayList;
import java.util.Properties;
import static com.example.listify.MainActivity.am;
import static com.example.listify.MainActivity.searchHistory;
public class HomeFragment extends Fragment implements CreateListDialogFragment.OnNewListListener, Requestor.Receiver {
ArrayList<List> shoppingLists = new ArrayList<>();
@ -156,6 +159,7 @@ public class HomeFragment extends Fragment implements CreateListDialogFragment.O
shoppingLists.clear();
Integer[] listIds = (Integer[]) delivered;
// Create threads and add them to a list
Thread[] threads = new Thread[listIds.length];
List[] results = new List[listIds.length];
@ -177,6 +181,30 @@ public class HomeFragment extends Fragment implements CreateListDialogFragment.O
t.start();
}
// Request search history
final SearchHistory[] history = new SearchHistory[1]; // Needs to be an array because of anonymous class weirdness
SynchronousReceiver<SearchHistory> historyReceiver = new SynchronousReceiver<>();
Properties configs = new Properties();
try {
configs = AuthManager.loadProperties(getContext(), "android.resource://" + getActivity().getPackageName() + "/raw/auths.json");
} catch (IOException | JSONException e) {
e.printStackTrace();
}
Requestor historyRequestor = new Requestor(am, configs.getProperty("apiKey"));
historyRequestor.getObject("0", SearchHistory.class, historyReceiver);
Thread historyThread = new Thread(new Runnable() {
@Override
public void run() {
try {
history[0] = historyReceiver.await();
} catch (Exception e) {
e.printStackTrace();
}
}
});
historyThread.start();
// Wait for each thread to finish and add results to shoppingLists
for (int i = 0; i < threads.length; i++) {
try {
@ -187,6 +215,15 @@ public class HomeFragment extends Fragment implements CreateListDialogFragment.O
shoppingLists.add(results[i]);
}
// Wait for search History response
try {
historyThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
searchHistory = history[0].getSearches();
// Set adapter and display this users lists
shoppingListsSwipeableAdapter = new ShoppingListsSwipeableAdapter(getActivity(), shoppingLists);

View File

@ -30,13 +30,13 @@
android:contentDescription="@string/backButton"
android:foreground="?android:attr/selectableItemBackgroundBorderless"/>
<SearchView
<androidx.appcompat.widget.SearchView
android:id="@+id/searchBar"
android:layout_width="300dp"
android:layout_height="50dp"
android:queryHint="Search Here"
>
</SearchView>
</androidx.appcompat.widget.SearchView>
</androidx.appcompat.widget.Toolbar>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/dropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="@color/colorAccent"
android:textAppearance="?android:attr/textAppearanceLargePopupMenu"/>

View File

@ -1,6 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- <item-->
<!-- android:id="@+id/action_search"-->
<!-- android:icon="@drawable/ic_baseline_search_28"-->
<!-- android:title="seeerch"-->
<!-- app:actionViewClass="androidx.appcompat.widget.SearchView"-->
<!-- app:showAsAction="always|collapseActionView"/>-->
<item
android:id="@+id/action_more"
android:icon="@drawable/ic_baseline_more_vert_24"