Last active
July 25, 2016 22:05
-
-
Save kalelc/fa1de45a9627c7807a198b081f3e225d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.beelink.truckid; | |
import android.app.Activity; | |
import android.app.SearchManager; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.res.Resources; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.support.v4.view.MenuItemCompat; | |
import android.support.v7.app.ActionBar; | |
import android.support.v7.widget.SearchView; | |
import android.text.TextUtils; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.widget.ArrayAdapter; | |
import android.widget.ListView; | |
import android.widget.Spinner; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonObject; | |
import com.google.gson.reflect.TypeToken; | |
import org.greenrobot.eventbus.EventBus; | |
import org.greenrobot.eventbus.Subscribe; | |
import org.greenrobot.eventbus.ThreadMode; | |
import java.util.List; | |
import me.beelink.truckid.adapters.ControllableUnitAdapter; | |
import me.beelink.truckid.dialogs.ProgressDialogFragment; | |
import me.beelink.truckid.events.SearchDoneEvent; | |
import me.beelink.truckid.events.SearchQueryEvent; | |
import me.beelink.truckid.helpers.ApiManager; | |
import me.beelink.truckid.helpers.ObjectHelper; | |
import me.beelink.truckid.models.ControllableUnit; | |
public class SearchActivity extends BaseActivity { | |
public static final String TAG = SearchActivity.class.getSimpleName(); | |
public static final String KEY_RESULTS = "key_results"; | |
String searchQuery = null; | |
List<ControllableUnit> results = null; | |
SearchView searchView = null; | |
Spinner spinner ; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_search); | |
handleIntent(getIntent()); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
setupSearchInterface(menu); | |
return super.onCreateOptionsMenu(menu); | |
} | |
public static void start(Activity ctx) { | |
Intent intent = new Intent(ctx, SearchActivity.class); | |
intent.putExtra(SearchManager.QUERY, ""); | |
ctx.startActivity(intent); | |
} | |
protected void performQuery(@NonNull String searchQuery) { | |
Log.d(TAG, "Performing query: " + searchQuery); | |
if (TextUtils.isEmpty(searchQuery)) { | |
return; | |
} | |
updateProgressDialog(true); | |
EventBus.getDefault().post(new SearchQueryEvent(searchQuery)); | |
} | |
@SuppressWarnings("unused") | |
@Subscribe(threadMode = ThreadMode.ASYNC) | |
protected void handleSearchEvent(SearchQueryEvent event) { | |
String query = event.getQuery(); | |
performSearch(query); | |
} | |
protected void performSearch(@NonNull String query) { | |
new ApiManager(this.getBaseContext()).search(query, new ApiManager.ResponseHandler() { | |
@Override | |
public void onSuccess(JsonObject json) { | |
onSearchSuccess(json); | |
} | |
@Override | |
public void onFailure(Exception exc) { | |
onSearchFailed(); | |
} | |
}); | |
} | |
protected void onSearchFailed() { | |
EventBus.getDefault().post(new SearchDoneEvent(null)); | |
} | |
protected void onSearchSuccess(JsonElement json) { | |
SearchDoneEvent sde = new SearchDoneEvent(json); | |
sde.processResults(); | |
EventBus.getDefault().post(sde); | |
} | |
@SuppressWarnings("unused") | |
@Subscribe(threadMode = ThreadMode.MAIN) | |
public void processSearchResult(SearchDoneEvent event) { | |
if (!event.didReceiveResponse()) { | |
Log.d(TAG, "No response"); | |
} | |
updateProgressDialog(false); | |
showResults(event.getResults()); | |
} | |
public void showResults(@NonNull List<ControllableUnit> objects) { | |
results = objects; | |
Log.d(TAG, "Got " + objects.size() + " search results"); | |
ListView listView = (ListView) this.findViewById(R.id.list); | |
if (listView == null) { | |
return; | |
} | |
listView.setAdapter(new ControllableUnitAdapter(this, objects)); | |
} | |
@Override | |
protected void onNewIntent(Intent intent) { | |
super.onNewIntent(intent); | |
handleIntent(intent); | |
} | |
private void handleIntent(@Nullable Intent intent) { | |
if (intent != null) { | |
searchQuery = intent.getStringExtra(SearchManager.QUERY); | |
performQuery(searchQuery); | |
} else { | |
searchQuery = null; | |
} | |
} | |
public void setupSearchInterface(Menu menu) { | |
ActionBar bar = getSupportActionBar(); | |
if (bar == null) { | |
return; | |
} | |
bar.setDisplayHomeAsUpEnabled(true); | |
//create menu | |
getMenuInflater().inflate(R.menu.search_bar, menu); | |
MenuItem item = menu.findItem(R.id.element_filter); | |
spinner = (Spinner) MenuItemCompat.getActionView(item); | |
Resources res = getResources(); | |
String[] spinnerArray = res.getStringArray(R.array.advanced_search_options); | |
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray); //selected item will look like a spinner set from XML | |
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); | |
spinner.setAdapter(spinnerArrayAdapter); | |
//end | |
searchView = new SearchView(bar.getThemedContext()); | |
final MenuItem menuItem = menu.add(getString(R.string.advanced_search_hint)) | |
.setActionView(searchView) | |
.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS) | |
.setIcon(R.drawable.ic_search); | |
if (menuItem == null) { | |
return; | |
} | |
// Associate searchable configuration with the SearchView | |
SearchManager searchManager = | |
(SearchManager) getSystemService(Context.SEARCH_SERVICE); | |
searchView.setSearchableInfo( | |
searchManager.getSearchableInfo(getComponentName())); | |
searchView.setIconifiedByDefault(false); | |
searchView.requestFocus(); | |
} | |
ProgressDialogFragment dialog; | |
protected void updateProgressDialog(boolean show) { | |
if (dialog == null && !show) { | |
return; | |
} | |
if (dialog == null) { | |
createProgressDialog(); | |
} | |
if (show) { | |
dialog.show(getFragmentManager(), "searching_dialog"); | |
} else { | |
dismissProgressDialog(); | |
} | |
} | |
protected void createProgressDialog() { | |
dialog = ProgressDialogFragment.newInstance(getString(R.string.searching)); | |
} | |
protected void dismissProgressDialog() { | |
dialog.dismissAllowingStateLoss(); | |
} | |
@Override | |
public void onStart() { | |
super.onStart(); | |
EventBus.getDefault().register(this); | |
} | |
@Override | |
public void onStop() { | |
EventBus.getDefault().unregister(this); | |
super.onStop(); | |
} | |
@Override | |
public void onSaveInstanceState(Bundle outState) { | |
outState.putString(KEY_RESULTS, ObjectHelper.getGsonInstance().toJson(results)); | |
super.onSaveInstanceState(outState); | |
} | |
@Override | |
public void onRestoreInstanceState(Bundle inState) { | |
super.onRestoreInstanceState(inState); | |
if (results == null && inState.containsKey(KEY_RESULTS)) { | |
List<ControllableUnit> results = ObjectHelper.getGsonInstance().fromJson( | |
inState.getString(KEY_RESULTS), new TypeToken<List<ControllableUnit>>(){}.getType()); | |
if (results != null) { | |
showResults(results); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment