/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.android.datafrominternet;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

import com.example.android.datafrominternet.utilities.NetworkUtils;

import java.io.IOException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private EditText mSearchBoxEditText;

    private TextView mUrlDisplayTextView;

    private TextView mSearchResultsTextView;

    // TODO (12) Create a variable to store a reference to the error message TextView

    // TODO (24) Create a ProgressBar variable to store a reference to the ProgressBar

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSearchBoxEditText = (EditText) findViewById(R.id.et_search_box);

        mUrlDisplayTextView = (TextView) findViewById(R.id.tv_url_display);
        mSearchResultsTextView = (TextView) findViewById(R.id.tv_github_search_results_json);

        // TODO (13) Get a reference to the error TextView using findViewById

        // TODO (25) Get a reference to the ProgressBar using findViewById
    }

    /**
     * This method retrieves the search text from the EditText, constructs the
     * URL (using {@link NetworkUtils}) for the github repository you'd like to find, displays
     * that URL in a TextView, and finally fires off an AsyncTask to perform the GET request using
     * our {@link GithubQueryTask}
     */
    private void makeGithubSearchQuery() {
        String githubQuery = mSearchBoxEditText.getText().toString();
        URL githubSearchUrl = NetworkUtils.buildUrl(githubQuery);
        mUrlDisplayTextView.setText(githubSearchUrl.toString());
        new GithubQueryTask().execute(githubSearchUrl);
    }

    // TODO (14) Create a method called showJsonDataView to show the data and hide the error

    // TODO (15) Create a method called showErrorMessage to show the error and hide the data

    public class GithubQueryTask extends AsyncTask<URL, Void, String> {

        // TODO (26) Override onPreExecute to set the loading indicator to visible

        @Override
        protected String doInBackground(URL... params) {
            URL searchUrl = params[0];
            String githubSearchResults = null;
            try {
                githubSearchResults = NetworkUtils.getResponseFromHttpUrl(searchUrl);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return githubSearchResults;
        }

        @Override
        protected void onPostExecute(String githubSearchResults) {
            // TODO (27) As soon as the loading is complete, hide the loading indicator
            if (githubSearchResults != null && !githubSearchResults.equals("")) {
                // TODO (17) Call showJsonDataView if we have valid, non-null results
                mSearchResultsTextView.setText(githubSearchResults);
            }
            // TODO (16) Call showErrorMessage if the result is null in onPostExecute
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemThatWasClickedId = item.getItemId();
        if (itemThatWasClickedId == R.id.action_search) {
            makeGithubSearchQuery();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

@@ -1204,17 +1204,17 @@
  TODO (1
-2
+8
 ) Create
@@ -1291,17 +1291,17 @@
  TODO (2
-4
+0
 ) Create
@@ -1797,9 +1797,9 @@
 O (1
-3
+9
 ) Ge
@@ -1872,17 +1872,17 @@
  TODO (2
-5
+1
 ) Get a 
@@ -2584,10 +2584,10 @@
 DO (
-14
+22
 ) Cr
@@ -2640,20 +2640,17 @@
 the data
- and
+,
  hide th
@@ -2656,16 +2656,33 @@
 he error
+ and progress bar
 %0A%0A    //
@@ -2692,10 +2692,10 @@
 DO (
-15
+23
 ) Cr
@@ -2745,24 +2745,25 @@
 ow the error
+,
  and hide th
@@ -2768,16 +2768,147 @@
 the data
+ and progress bar%0A    %0A    // TODO (24) Create a method called showProgressBar to show the progress bar, and hide the data or error
 %0A%0A    pu
@@ -2992,17 +2992,17 @@
  TODO (2
-6
+5
 ) Overri
@@ -3021,14 +3021,43 @@
 ute 
-to set
+and call showProgressBar to display
  the
@@ -3078,19 +3078,8 @@
 ator
- to visible
 %0A%0A  
@@ -3570,96 +3570,8 @@
 ) %7B%0A
-            // TODO (27) As soon as the loading is complete, hide the loading indicator%0A
     
@@ -3677,10 +3677,10 @@
 DO (
-17
+26
 ) Ca
@@ -3838,18 +3838,18 @@
 / TODO (
-16
+27
 ) Call s

Update MainActivity.java