mirror of
https://github.com/ClaytonWWilson/Listify.git
synced 2025-12-16 10:48:46 +00:00
Create new thread on search and show progress bar
This commit is contained in:
parent
4e7b180fe0
commit
4757ba2ddf
@ -9,6 +9,7 @@ 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 com.example.listify.adapter.SearchResultsListAdapter;
|
||||
import com.example.listify.data.ItemSearch;
|
||||
@ -23,7 +24,7 @@ import java.util.Properties;
|
||||
import static com.example.listify.MainActivity.am;
|
||||
|
||||
public class SearchResults extends AppCompatActivity implements SortDialogFragment.OnSortingListener, Requestor.Receiver {
|
||||
private ListView listView;
|
||||
private ProgressBar loadingSearch;
|
||||
private SearchResultsListAdapter searchResultsListAdapter;
|
||||
private List<Product> resultsProductList = new ArrayList<>();
|
||||
private List<Product> resultsProductListSorted = new ArrayList<>();
|
||||
@ -31,6 +32,7 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
|
||||
private int storeSelection;
|
||||
private int sortMode;
|
||||
private boolean descending;
|
||||
boolean doneLoading = false;
|
||||
|
||||
@Override
|
||||
public void sendSort(int storeSelection, int sortMode, boolean descending) {
|
||||
@ -47,6 +49,8 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
|
||||
Toolbar toolbar = findViewById(R.id.toolbar);
|
||||
setSupportActionBar(toolbar);
|
||||
|
||||
loadingSearch = (ProgressBar) findViewById(R.id.progress_loading_search);
|
||||
|
||||
// Back button closes this activity and returns to previous activity (MainActivity)
|
||||
ImageButton backButton = (ImageButton) findViewById(R.id.backToHomeButton);
|
||||
backButton.setOnClickListener(new View.OnClickListener() {
|
||||
@ -77,7 +81,7 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
|
||||
}
|
||||
});
|
||||
|
||||
listView = (ListView) findViewById(R.id.search_results_list);
|
||||
ListView listView = (ListView) findViewById(R.id.search_results_list);
|
||||
searchResultsListAdapter = new SearchResultsListAdapter(this, resultsProductListSorted);
|
||||
listView.setAdapter(searchResultsListAdapter);
|
||||
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
@ -96,7 +100,15 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
|
||||
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
|
||||
@Override
|
||||
public boolean onQueryTextSubmit(String query) {
|
||||
doSearch(query);
|
||||
// Show progress bar
|
||||
loadingSearch.setVisibility(View.VISIBLE);
|
||||
Thread t = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
doSearch(query);
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -122,12 +134,8 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
|
||||
sortDialog.show(getSupportFragmentManager(), "Sort");
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Override default phone back button to add animation
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
@ -136,10 +144,13 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
|
||||
}
|
||||
|
||||
private void doSearch(String query) {
|
||||
|
||||
// Clear the old search results
|
||||
resultsProductList.clear();
|
||||
|
||||
// Clear old search results from the view
|
||||
resultsProductListSorted.clear();
|
||||
searchResultsListAdapter.notifyDataSetChanged();
|
||||
|
||||
Properties configs = new Properties();
|
||||
try {
|
||||
configs = AuthManager.loadProperties(this, "android.resource://" + getPackageName() + "/raw/auths.json");
|
||||
@ -178,7 +189,6 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
|
||||
});
|
||||
break;
|
||||
|
||||
// TODO: May need to change this depending on if price is stored as a string or a double
|
||||
case 2:
|
||||
resultsProductListSorted.sort(new Comparator<Product>() {
|
||||
@Override
|
||||
@ -238,7 +248,13 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
searchResultsListAdapter.notifyDataSetChanged();
|
||||
if (doneLoading) {
|
||||
doneLoading = false;
|
||||
searchResultsListAdapter.notifyDataSetChanged();
|
||||
|
||||
// Hide progress bar
|
||||
loadingSearch.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -280,5 +296,7 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
|
||||
|
||||
// Apply selected sorting to the list
|
||||
sortResults();
|
||||
|
||||
doneLoading = true;
|
||||
}
|
||||
}
|
||||
@ -12,6 +12,16 @@
|
||||
tools:context=".SearchResults"
|
||||
tools:showIn="@layout/activity_search_results">
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/progress_loading_search"
|
||||
style="?android:attr/progressBarStyleLarge"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:layout_centerVertical="true"
|
||||
android:indeterminate="true"
|
||||
android:visibility="gone"/>
|
||||
|
||||
<ListView
|
||||
android:id="@+id/search_results_list"
|
||||
android:layout_width="fill_parent"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user