diff --git a/app/src/main/java/org/openlp/android2/fragments/SearchFragment.java b/app/src/main/java/org/openlp/android2/fragments/SearchFragment.java index 14833b1..1c50103 100644 --- a/app/src/main/java/org/openlp/android2/fragments/SearchFragment.java +++ b/app/src/main/java/org/openlp/android2/fragments/SearchFragment.java @@ -1,25 +1,53 @@ package org.openlp.android2.fragments; -import android.net.Uri; +import android.app.Fragment; +import android.content.Context; import android.os.Bundle; +import android.preference.PreferenceManager; +import android.text.Html; import android.util.Log; +import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.FrameLayout; +import android.widget.LinearLayout; +import android.widget.ListAdapter; +import android.widget.SimpleAdapter; +import android.widget.Spinner; +import android.widget.TextView; import android.widget.Toast; +import com.loopj.android.http.AsyncHttpClient; +import com.loopj.android.http.TextHttpResponseHandler; + +import org.apache.http.Header; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; import org.openlp.android2.R; import org.openlp.android2.api.Api; import org.openlp.android2.common.JsonHelpers; -import org.openlp.android2.common.OpenLPFragment; + import org.openlp.android2.common.OpenLPHttpClient; +import java.util.ArrayList; +import java.util.List; + /** */ -public class SearchFragment extends OpenLPFragment { +public class SearchFragment extends Fragment { private final String LOG_TAG = SearchFragment.class.getName(); + private Spinner spinner; + private static AsyncHttpClient client = new AsyncHttpClient(); + public Context context; + protected String calledURL; + protected OpenLPHttpClient httpClient; + protected String updateUrl; public SearchFragment() { Log.d(LOG_TAG, "Constructor"); @@ -33,21 +61,25 @@ public class SearchFragment extends OpenLPFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { context = getActivity(); - updateUrl = Api.SERVICE_LIST; + updateUrl = Api.SEARCHABLE_PLUGINS; httpClient = new OpenLPHttpClient(context); - return super.onCreateView(inflater, container, savedInstanceState); + View view = inflater.inflate(R.layout.fragment_search, container, false); + spinner = (Spinner)view.findViewById(R.id.search_spinner); + triggerTextRequest(Api.SEARCHABLE_PLUGINS); + //spinner.setOnItemSelectedListener(new MyOnItemSelectedListener()); + return view; } @Override public void onResume() { super.onResume(); Log.d(LOG_TAG, "Resuming..."); - triggerTextRequest(Api.SEARCHABLE_PLUGINS); + //triggerTextRequest(Api.SEARCHABLE_PLUGINS); Log.d(LOG_TAG, "Resumed..."); } @@ -58,16 +90,35 @@ public class SearchFragment extends OpenLPFragment { public void manageResponse(String response, boolean notInError) { if (calledURL.equals(updateUrl)) { - populateTabDisplay(response); - }else { - processUpdate(response, notInError); + populatePluginList(response); + } else { + //processUpdate(response, notInError); } } - private void populateTabDisplay(String response){ - int a = 1; + private void populatePluginList(String response) { + Log.i(LOG_TAG, "populatePluginList - entry"); + List categories = new ArrayList(); + + try { + JSONArray items = new JSONObject(response).getJSONObject("results").getJSONArray("items"); + for (int i = 0; i < items.length(); ++i) { + JSONArray item = items.getJSONArray(i); + categories.add(item.get(1).toString()); + } + } catch (JSONException e) { + Log.e(LOG_TAG, response); + e.printStackTrace(); + } + ArrayAdapter LTRadapter = new ArrayAdapter(getActivity(), + android.R.layout.simple_spinner_item, categories); + LTRadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + spinner.setAdapter(LTRadapter); + + Log.i(LOG_TAG, "populatePluginList - exit"); } + public void itemClicked(int position) { try { String request = JsonHelpers.createRequestJSON("id", Integer.toString(position)); @@ -79,5 +130,27 @@ public class SearchFragment extends OpenLPFragment { 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); + String callurl = String.format("%s%s", httpClient.getAbsoluteUrl(client), url ); + client.get(callurl, null, new TextHttpResponseHandler() { + @Override + public void onSuccess(int statusCode, Header[] headers, String responseString) { + // called when response HTTP status is "200 OK" + manageResponse(responseString, true); + } + @Override + public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { + // called when response HTTP status is "4XX" (eg. 401, 403, 404) + if (statusCode == 401) { + Toast.makeText(context, R.string.httpreturn_unauthorised, Toast.LENGTH_LONG).show(); + } else { + Toast.makeText(context, R.string.unable, Toast.LENGTH_LONG).show(); + } + manageResponse(responseString, false); + } + }); + } } diff --git a/app/src/main/res/layout/fragment_search.xml b/app/src/main/res/layout/fragment_search.xml index 143cf4f..e1324d3 100644 --- a/app/src/main/res/layout/fragment_search.xml +++ b/app/src/main/res/layout/fragment_search.xml @@ -1,85 +1,78 @@ - + + - - + android:layout_gravity="center_horizontal|top" + android:weightSum="1"> - + android:text="Select Plugin" + android:id="@+id/search_title" + android:textAppearance="?android:attr/textAppearanceMedium" /> - - - - - - - - - - - - - - - - - - - - + android:id="@+id/search_spinner" + android:layout_weight="0.58" /> + - + + + + + + + + + + + + + + + +