Fix Custom search

This commit is contained in:
Tim Bentley 2016-01-09 11:55:03 +00:00
parent 2cae45c903
commit 632e655c96
1 changed files with 11 additions and 28 deletions

View File

@ -3,14 +3,8 @@ package org.openlp.android2.fragments;
import android.app.DialogFragment;
import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Html;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
@ -20,11 +14,7 @@ import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
@ -46,6 +36,7 @@ import org.openlp.android2.dialogs.SearchSelectionDialog;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@ -59,7 +50,8 @@ public class SearchFragment extends Fragment {
protected String calledURL;
protected OpenLPHttpClient httpClient;
protected String updateUrl;
protected String seachedPlugin;
protected String searchedPlugin;
protected Map<String, String> pluginMap = new HashMap<String, String>();
public SearchFragment() {
Log.d(LOG_TAG, "Constructor");
@ -97,8 +89,8 @@ public class SearchFragment extends Fragment {
(InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
seachedPlugin = spinner.getSelectedItem().toString();
requestSearch(spinner.getSelectedItem().toString(), tv.getText().toString());
searchedPlugin = pluginMap.get(spinner.getSelectedItem().toString());
requestSearch(tv.getText().toString());
return true;
}
return false;
@ -123,6 +115,7 @@ public class SearchFragment extends Fragment {
private void populatePluginList(String response, Boolean notInError) {
Log.i(LOG_TAG, "populatePluginList - entry");
List<String> categories = new ArrayList<String>();
pluginMap.clear();
if (notInError) {
try {
@ -130,6 +123,7 @@ public class SearchFragment extends Fragment {
for (int i = 0; i < items.length(); ++i) {
JSONArray item = items.getJSONArray(i);
categories.add(item.get(1).toString());
pluginMap.put(item.get(1).toString(),item.get(0).toString());
}
} catch (JSONException e) {
Log.e(LOG_TAG, response);
@ -143,17 +137,6 @@ public class SearchFragment extends Fragment {
}
}
public void itemClicked(int position) {
try {
String request = JsonHelpers.createRequestJSON("id", Integer.toString(position));
triggerTextRequest(String.format("%s%s", Api.SERVICE_SET, request));
Log.d(LOG_TAG, String.format("Setting list data. apiBase(%s), position(%s)",
Api.SERVICE_SET, position));
} catch (JsonHelpers.JSONHandlerException e) {
e.printStackTrace();
Toast.makeText(context, "Request Failed", Toast.LENGTH_SHORT).show();
}
}
protected void triggerTextRequest(String url) {
calledURL = url;
Log.d(LOG_TAG, "Trigger Request for url " + url);
@ -178,13 +161,13 @@ public class SearchFragment extends Fragment {
});
}
public void requestSearch(String plugin, String text) {
public void requestSearch(String text) {
updateUrl = Api.SEARCH_PLUGIN_FORMATTED;
try {
String request = JsonHelpers.createRequestJSON("text", text);
String url = String.format(Api.SEARCH_PLUGIN_FORMATTED, plugin.toLowerCase());
String url = String.format(Api.SEARCH_PLUGIN_FORMATTED, searchedPlugin);
triggerTextRequest(String.format("%s%s", url, request));
Log.d(LOG_TAG, String.format("Search request. apiBase(%s), text(%s)", plugin, text));
Log.d(LOG_TAG, String.format("Search request. apiBase(%s), text(%s)", searchedPlugin, text));
} catch (JsonHelpers.JSONHandlerException e) {
e.printStackTrace();
Toast.makeText(context, "Search Request Failed", Toast.LENGTH_SHORT).show();
@ -227,7 +210,7 @@ public class SearchFragment extends Fragment {
e.printStackTrace();
}
Bundle args = new Bundle();
args.putString("plugin", seachedPlugin);
args.putString("plugin", searchedPlugin);
args.putString("text", it);
args.putString("key", Long.toString(id));
DialogFragment newFragment = new SearchSelectionDialog();