Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.68 KB | None | 0 0
  1. package com.emanuelenberg.stormy;
  2.  
  3. import android.content.Context;
  4. import android.graphics.drawable.Drawable;
  5. import android.net.ConnectivityManager;
  6. import android.net.NetworkInfo;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.ImageView;
  12. import android.widget.ProgressBar;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15.  
  16. import org.json.JSONException;
  17. import org.json.JSONObject;
  18.  
  19. import java.io.IOException;
  20.  
  21. import butterknife.BindView;
  22. import butterknife.ButterKnife;
  23. import okhttp3.Call;
  24. import okhttp3.Callback;
  25. import okhttp3.OkHttpClient;
  26. import okhttp3.Request;
  27. import okhttp3.Response;
  28.  
  29. public class MainActivity extends AppCompatActivity {
  30. public static final String TAG = MainActivity.class.getSimpleName();
  31.  
  32. private CurrentWeather mCurrentWeather;
  33.  
  34. @BindView(R.id.timeLabel) TextView mTimeLabel;
  35. @BindView(R.id.temperatureValue) TextView mTemperatureValue;
  36. @BindView(R.id.humidityValue) TextView mHumidityValue;
  37. @BindView(R.id.precipValue) TextView mPrecipValue;
  38. @BindView(R.id.summaryLabel) TextView mSummaryLabel;
  39. @BindView(R.id.iconImageView) ImageView mIconImageView;
  40. @BindView(R.id.refreshImageView) ImageView mRefreshImageView;
  41. @BindView(R.id.progressBar) ProgressBar mProgressBar;
  42.  
  43.  
  44. @Override
  45. protected void onCreate(Bundle savedInstanceState) {
  46. super.onCreate(savedInstanceState);
  47. setContentView(R.layout.activity_main);
  48. ButterKnife.bind(this);
  49.  
  50. mProgressBar.setVisibility(View.INVISIBLE);
  51.  
  52. final double latitude = 37.8267;
  53. final double longitude = -122.423;
  54.  
  55. mRefreshImageView.setOnClickListener(new View.OnClickListener() {
  56. @Override
  57. public void onClick(View v) {
  58. getForecast(latitude, longitude);
  59. }
  60. });
  61.  
  62. getForecast(latitude, longitude);
  63. Log.d(TAG, "Main UI code is running");
  64. }
  65.  
  66. private void getForecast(double latitude, double longitude) {
  67. String apiKey = "487df07849a9112c9454e8fc8e35787d";
  68. String forecastURL = ("https://api.forecast.io/forecast/" + apiKey + "/" + latitude + "," + longitude);
  69. if (isNetworkAvailable()) {
  70. toggleRefresh();
  71. OkHttpClient client = new OkHttpClient();
  72. Request request = new Request.Builder()
  73. .url(forecastURL)
  74. .build();
  75. Call call = client.newCall(request);
  76. call.enqueue(new Callback() {
  77. @Override
  78. public void onFailure(Call call, IOException e) {
  79. runOnUiThread(new Runnable() {
  80. @Override
  81. public void run() {
  82. toggleRefresh();
  83. }
  84. });
  85. alertUserAboutError();
  86. }
  87.  
  88. @Override
  89. public void onResponse(Call call, Response response) throws IOException {
  90. runOnUiThread(new Runnable() {
  91. @Override
  92. public void run() {
  93. toggleRefresh();
  94. }
  95. });
  96. try {
  97. String jsonData = response.body().string();
  98. Log.v(TAG, jsonData);
  99. if (response.isSuccessful()) {
  100. mCurrentWeather = getCurrentDetails(jsonData);
  101. runOnUiThread(new Runnable() {
  102. @Override
  103. public void run() {
  104. updateDisplay();
  105. }
  106. });
  107. }else{
  108. alertUserAboutError();
  109. }
  110. } catch (IOException e) {
  111. Log.e(TAG, "Exception caught: ", e);
  112. } catch (JSONException e){
  113. Log.e(TAG, "Exception caught: ", e);
  114. }
  115. }
  116. });
  117. }else{
  118. Toast.makeText(this, R.string.network_unavailable_message, Toast.LENGTH_LONG).show();
  119. }
  120. }
  121.  
  122. private void toggleRefresh() {
  123. if (mProgressBar.getVisibility() == View.INVISIBLE){
  124. mProgressBar.setVisibility(View.VISIBLE);
  125. mRefreshImageView.setVisibility(View.INVISIBLE);
  126. }else{
  127. mProgressBar.setVisibility(View.INVISIBLE);
  128. mRefreshImageView.setVisibility(View.VISIBLE);
  129. }
  130.  
  131. }
  132.  
  133. private void updateDisplay() {
  134. mTemperatureValue.setText(mCurrentWeather.getTemperatureInCelsius()+"");
  135. mTimeLabel.setText("At " + mCurrentWeather.getFormattedTime() + " it will be");
  136. mHumidityValue.setText(mCurrentWeather.getHumidity() +"");
  137. mPrecipValue.setText(mCurrentWeather.getPrecipChance() + "%");
  138. mSummaryLabel.setText(mCurrentWeather.getSummary());
  139.  
  140. Drawable drawable = getResources().getDrawable(mCurrentWeather.getIconId());
  141. mIconImageView.setImageDrawable(drawable);
  142.  
  143. }
  144.  
  145. private CurrentWeather getCurrentDetails(String jsonData) throws JSONException {
  146. JSONObject forecast = new JSONObject(jsonData);
  147. JSONObject currently = forecast.getJSONObject("currently");
  148.  
  149.  
  150. CurrentWeather currentWeather = new CurrentWeather();
  151. currentWeather.setIcon(currently.getString("icon"));
  152. currentWeather.setTime(currently.getLong("time"));
  153. currentWeather.setTemperature(currently.getDouble("temperature"));
  154. currentWeather.setHumidity(currently.getDouble("humidity"));
  155. currentWeather.setPrecipChance(currently.getDouble("precipProbability"));
  156. currentWeather.setSummary(currently.getString("summary"));
  157. currentWeather.setTimezone(forecast.getString("timezone"));
  158.  
  159. Log.d(TAG, currentWeather.getFormattedTime());
  160.  
  161. return currentWeather;
  162. }
  163.  
  164. private boolean isNetworkAvailable() {
  165. ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  166. NetworkInfo networkInfo = manager.getActiveNetworkInfo();
  167. boolean isAvailable = false;
  168. if(networkInfo != null && networkInfo.isConnected()){
  169. isAvailable = true;
  170. }
  171. return isAvailable;
  172. }
  173.  
  174. private void alertUserAboutError() {
  175. AlertDialogFragment dialog = new AlertDialogFragment();
  176. dialog.show(getFragmentManager(), "error_dialog");
  177. }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement