package com.example.android.sunshine;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.example.android.sunshine.data.SunshinePreferences;
import com.example.android.sunshine.utilities.NetworkUtils;
import com.example.android.sunshine.utilities.OpenWeatherJsonUtils;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private TextView mWeatherTextView;
// TODO (6) Add a TextView variable for the error message display private TextView errorTextView;
// TODO (16) Add a ProgressBar variable to show and hide the progress bar private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forecast);
/*
* Using findViewById, we get a reference to our TextView from xml. This allows us to
* do things like set the text of the TextView. */
mWeatherTextView = (TextView) findViewById(R.id.tv_weather_data);
// TODO (7) Find the TextView for the error message using findViewById
errorTextView = (TextView) findViewById(R.id.errorTextView);
// TODO (17) Find the ProgressBar using findViewById
progressBar = (ProgressBar) findViewById(R.id.loadingProgressBar);
/* Once all of our views are setup, we can load the weather data. */
loadWeatherData();
}
/**
* This method will get the user's preferred location for weather, and then tell some
* background method to get the weather data in the background. */
private void loadWeatherData() {
// TODO (20) Call showWeatherDataView before executing the AsyncTask
showWeatherDataView();
String location = SunshinePreferences.getPreferredWeatherLocation(this);
new FetchWeatherTask().execute(location);
}
// TODO (8) Create a method called showWeatherDataView that will hide the error message and show the weather data
private void showWeatherDataView(){
errorTextView.setVisibility(View.INVISIBLE);
mWeatherTextView.setVisibility(View.VISIBLE);
}
// TODO (9) Create a method called showErrorMessage that will hide the weather data and show the error message
private void showErrorMessage(){
mWeatherTextView.setVisibility(View.INVISIBLE);
errorTextView.setVisibility(View.VISIBLE);
}
public class FetchWeatherTask extends AsyncTask<String, Void, String[]> {
// TODO (18) Within your AsyncTask, override the method onPreExecute and show the loading indicator
@Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected String[] doInBackground(String... params) {
/* If there's no zip code, there's nothing to look up. */
if (params.length == 0) {
return null;
}
String location = params[0];
URL weatherRequestUrl = NetworkUtils.buildUrl(location);
try {
String jsonWeatherResponse = NetworkUtils
.getResponseFromHttpUrl(weatherRequestUrl);
String[] simpleJsonWeatherData = OpenWeatherJsonUtils
.getSimpleWeatherStringsFromJson(MainActivity.this, jsonWeatherResponse);
return simpleJsonWeatherData;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String[] weatherData) {
// TODO (19) As soon as the data is finished loading, hide the loading indicator
progressBar.setVisibility(View.INVISIBLE);
if (weatherData != null) {
// TODO (11) If the weather data was not null, make sure the data view is visible
showWeatherDataView();
/*
* Iterate through the array and append the Strings to the TextView. The reason why we add
* the "\n\n\n" after the String is to give visual separation between each String in the
* TextView. Later, we'll learn about a better way to display lists of data.*/
for (String weatherString : weatherData) {
mWeatherTextView.append((weatherString) + "\n\n\n");
}
}
// TODO (10) If the weather data was null, show the error message
if(weatherData == null){
showErrorMessage();
}
}
}
@Override public boolean onCreateOptionsMenu(Menu menu) {
/* Use AppCompatActivity's method getMenuInflater to get a handle on the menu inflater */
MenuInflater inflater = getMenuInflater();
/* Use the inflater's inflate method to inflate our menu layout to this menu */
inflater.inflate(R.menu.forecast, menu);
/* Return true so that the menu is displayed in the Toolbar */
return true;
}
@Override public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_refresh) {
mWeatherTextView.setText("");
loadWeatherData();
return true;
}
return super.onOptionsItemSelected(item);
}
}