Search in sources :

Example 1 with CounterChampionAdapter

use of fr.neamar.lolgamedata.adapter.CounterChampionAdapter in project teamward-client by Neamar.

the class CounterChampionsFragment method loadCounters.

private void loadCounters(final RecyclerView recyclerView, final Account account, final String role) {
    // Instantiate the RequestQueue.
    final RequestQueue queue = VolleyQueue.newRequestQueue(getActivity());
    final String summonerName = account.summonerName;
    String region = account.region;
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
    final int requiredChampionMastery = Integer.parseInt(prefs.getString("counter_required_mastery", "3"));
    try {
        String url = ((LolApplication) getActivity().getApplication()).getApiUrl() + "/summoner/counter?summoner=" + URLEncoder.encode(summonerName, "UTF-8") + "&region=" + region.toLowerCase(Locale.ROOT) + "&role=" + role.toLowerCase(Locale.ROOT) + "&level=" + requiredChampionMastery;
        if (BuildConfig.DEBUG && summonerName.equalsIgnoreCase("MOCK")) {
            url = "https://gist.githubusercontent.com/Neamar/eb278b4d5f188546f56028c3a0310507/raw/counters.json";
        }
        NoCacheRetryJsonRequest jsonRequest = new NoCacheRetryJsonRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                loadIndicator.setVisibility(View.GONE);
                try {
                    Counters counters = new Counters(account, role, response);
                    adapter = new CounterChampionAdapter(counters);
                    recyclerView.setAdapter(adapter);
                    Log.i(TAG, "Loaded counters!");
                    Activity activity = getActivity();
                    if (activity != null) {
                        Tracker.trackViewCounters(activity, account, role, requiredChampionMastery);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                queue.stop();
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                loadIndicator.setVisibility(View.GONE);
                Log.e(TAG, error.toString());
                queue.stop();
                error.printStackTrace();
                if (error instanceof NoConnectionError) {
                    if (getActivity() != null) {
                        displaySnack(getString(R.string.no_internet_connection));
                    }
                }
                try {
                    String responseBody = new String(error.networkResponse.data, "utf-8");
                    Log.i(TAG, responseBody);
                    displaySnack(responseBody);
                    Activity activity = getActivity();
                    if (activity != null) {
                        Tracker.trackErrorViewingCounters(activity, account, responseBody.replace("Error:", ""));
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (NullPointerException e) {
                // Do nothing, no text content in the HTTP reply.
                }
            }
        });
        queue.add(jsonRequest);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}
Also used : LolApplication(fr.neamar.lolgamedata.LolApplication) VolleyError(com.android.volley.VolleyError) SharedPreferences(android.content.SharedPreferences) NoCacheRetryJsonRequest(fr.neamar.lolgamedata.volley.NoCacheRetryJsonRequest) CounterChampionAdapter(fr.neamar.lolgamedata.adapter.CounterChampionAdapter) SnackBarActivity(fr.neamar.lolgamedata.SnackBarActivity) Activity(android.app.Activity) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoConnectionError(com.android.volley.NoConnectionError) Response(com.android.volley.Response) JSONObject(org.json.JSONObject) RequestQueue(com.android.volley.RequestQueue) Counters(fr.neamar.lolgamedata.pojo.Counters)

Aggregations

Activity (android.app.Activity)1 SharedPreferences (android.content.SharedPreferences)1 NoConnectionError (com.android.volley.NoConnectionError)1 RequestQueue (com.android.volley.RequestQueue)1 Response (com.android.volley.Response)1 VolleyError (com.android.volley.VolleyError)1 LolApplication (fr.neamar.lolgamedata.LolApplication)1 SnackBarActivity (fr.neamar.lolgamedata.SnackBarActivity)1 CounterChampionAdapter (fr.neamar.lolgamedata.adapter.CounterChampionAdapter)1 Counters (fr.neamar.lolgamedata.pojo.Counters)1 NoCacheRetryJsonRequest (fr.neamar.lolgamedata.volley.NoCacheRetryJsonRequest)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1