diff --git a/app/build.gradle b/app/build.gradle index 4c6002a3e405ccaea6fedf32684faf9da774c5f6..2ffa21fd6c4c64a312f5c1e6ff2389eb89c1af36 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -32,12 +32,12 @@ android { dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation 'com.android.support:appcompat-v7:25.3.1' - implementation 'com.android.support:design:25.3.1' - implementation 'com.android.support:cardview-v7:25.3.1' - implementation 'com.android.support:support-v4:25.3.1' + implementation 'com.android.support:appcompat-v7:25.4.0' + implementation 'com.android.support:design:25.4.0' + implementation 'com.android.support:cardview-v7:25.4.0' + implementation 'com.android.support:support-v4:25.4.0' testImplementation 'junit:junit:4.12' - implementation 'com.google.code.gson:gson:2.6.2' + implementation 'com.google.code.gson:gson:2.8.2' implementation 'com.github.PhilJay:MPAndroidChart:v3.0.0-beta1' - implementation 'com.android.support.constraint:constraint-layout:1.0.2' + implementation 'com.android.support.constraint:constraint-layout:1.1.3' } diff --git a/app/src/main/java/foundation/e/weather/MainActivity.java b/app/src/main/java/foundation/e/weather/MainActivity.java index fcdbe01fadcf1b284afb0363a0b0cc718e22ea03..a9959798be540f5fcd2651c9dd2bd6b475ec19ce 100644 --- a/app/src/main/java/foundation/e/weather/MainActivity.java +++ b/app/src/main/java/foundation/e/weather/MainActivity.java @@ -19,6 +19,7 @@ import android.location.LocationManager; import android.os.Bundle; import android.os.Handler; import android.os.Looper; +import android.os.Message; import android.provider.Settings; import android.support.annotation.NonNull; import android.support.design.widget.AppBarLayout; @@ -123,6 +124,7 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC private RecyclerView mRecyclerView; private List mWeatherForecastList; + private WeatherForecastAdapter mWeatherForecastAdapter; private static Handler mHandler; private ProgressDialog mGetWeatherProgress; @@ -166,9 +168,6 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC this.storedContext = this; fab.setOnClickListener(fabListener); - getWeather(); - - updateUI(); mHandler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { @@ -185,6 +184,7 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC setVisibleUpdating(false); break; case Constants.PARSE_RESULT_SUCCESS: + mWeatherForecastList = (List) msg.obj; setVisibleUpdating(false); updateUI(); if (!mWeatherForecastList.isEmpty()) { @@ -196,6 +196,7 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC } }; + getWeather(); } private void updateCurrentWeather() { @@ -451,7 +452,9 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC mRecyclerView = (RecyclerView) findViewById(R.id.forecast_recycler_view); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); - + mWeatherForecastAdapter = new WeatherForecastAdapter(this, + getSupportFragmentManager()); + mRecyclerView.setAdapter(mWeatherForecastAdapter); mIconWeatherView.setTypeface(weatherFontIcon); mTemperatureView.setTypeface(robotoThin); @@ -776,7 +779,7 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC JSONObject jsonObject = new JSONObject(data); JSONArray listArray = jsonObject.getJSONArray("list"); - + List weatherForecasts = new ArrayList<>(); int listArrayCount = listArray.length(); for (int i = 0; i < listArrayCount; i++) { WeatherForecast weatherForecast = new WeatherForecast(); @@ -815,9 +818,12 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC weatherForecast.setDescription(weatherObject.getString("description")); weatherForecast.setIcon(weatherObject.getString("icon")); - mWeatherForecastList.add(weatherForecast); - mHandler.sendEmptyMessage(Constants.PARSE_RESULT_SUCCESS); + weatherForecasts.add(weatherForecast); } + Message message = mHandler.obtainMessage(); + message.obj = weatherForecasts; + message.what = Constants.PARSE_RESULT_SUCCESS; + mHandler.sendMessage(message); } catch (JSONException e) { mHandler.sendEmptyMessage(Constants.TASK_RESULT_ERROR); e.printStackTrace(); @@ -833,6 +839,7 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC } private void updateUI() { + Log.i(TAG, "updateUI: "+Thread.currentThread().getName()); //ImageView android = (ImageView) findViewById(R.id.android); if (mWeatherForecastList.size() < 5) { mRecyclerView.setVisibility(View.INVISIBLE); @@ -841,10 +848,6 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC mRecyclerView.setVisibility(View.VISIBLE); //android.setVisibility(View.GONE); } - WeatherForecastAdapter adapter = new WeatherForecastAdapter(this, - mWeatherForecastList, - getSupportFragmentManager()); - - mRecyclerView.setAdapter(adapter); + mWeatherForecastAdapter.updateWeatherList(mWeatherForecastList); } } diff --git a/app/src/main/java/foundation/e/weather/WeatherForecastActivity.java b/app/src/main/java/foundation/e/weather/WeatherForecastActivity.java index dc534b910b832316d00911ccd111e3ea7f81e0c2..a4fcee73c50f4187e8ffe560b53dacd10ac34354 100644 --- a/app/src/main/java/foundation/e/weather/WeatherForecastActivity.java +++ b/app/src/main/java/foundation/e/weather/WeatherForecastActivity.java @@ -1,5 +1,7 @@ package foundation.e.weather; +import static foundation.e.weather.utils.Utils.getWeatherForecastUrl; + import android.app.ProgressDialog; import android.content.SharedPreferences; import android.os.Bundle; @@ -15,12 +17,6 @@ import android.view.View; import android.widget.ImageView; import android.widget.Toast; -import foundation.e.weather.adapter.WeatherForecastAdapter; -import foundation.e.weather.model.WeatherForecast; -import foundation.e.weather.utils.AppPreference; -import foundation.e.weather.utils.Constants; -import foundation.e.weather.utils.LanguageUtil; -import foundation.e.weather.utils.PreferenceUtil; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -33,7 +29,12 @@ import java.net.URL; import java.util.ArrayList; import java.util.List; -import static foundation.e.weather.utils.Utils.getWeatherForecastUrl; +import foundation.e.weather.adapter.WeatherForecastAdapter; +import foundation.e.weather.model.WeatherForecast; +import foundation.e.weather.utils.AppPreference; +import foundation.e.weather.utils.Constants; +import foundation.e.weather.utils.LanguageUtil; +import foundation.e.weather.utils.PreferenceUtil; public class WeatherForecastActivity extends BaseActivity { @@ -42,6 +43,7 @@ public class WeatherForecastActivity extends BaseActivity { private List mWeatherForecastList; private ConnectionDetector mConnectionDetector; private RecyclerView mRecyclerView; + private WeatherForecastAdapter mWeatherForecastAdapter; private static Handler mHandler; private ProgressDialog mGetWeatherProgress; @@ -57,6 +59,10 @@ public class WeatherForecastActivity extends BaseActivity { mRecyclerView = (RecyclerView) findViewById(R.id.forecast_recycler_view); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); + mWeatherForecastAdapter = new WeatherForecastAdapter(this, + getSupportFragmentManager()); + mRecyclerView.setAdapter(mWeatherForecastAdapter); + updateUI(); mHandler = new Handler() { @@ -64,14 +70,14 @@ public class WeatherForecastActivity extends BaseActivity { switch (msg.what) { case Constants.TASK_RESULT_ERROR: Toast.makeText(WeatherForecastActivity.this, - R.string.toast_parse_error, - Toast.LENGTH_SHORT).show(); + R.string.toast_parse_error, + Toast.LENGTH_SHORT).show(); setVisibleUpdating(false); break; case Constants.PARSE_RESULT_ERROR: Toast.makeText(WeatherForecastActivity.this, - R.string.toast_parse_error, - Toast.LENGTH_SHORT).show(); + R.string.toast_parse_error, + Toast.LENGTH_SHORT).show(); setVisibleUpdating(false); break; case Constants.PARSE_RESULT_SUCCESS: @@ -79,7 +85,7 @@ public class WeatherForecastActivity extends BaseActivity { updateUI(); if (!mWeatherForecastList.isEmpty()) { AppPreference.saveWeatherForecast(WeatherForecastActivity.this, - mWeatherForecastList); + mWeatherForecastList); } break; } @@ -96,10 +102,7 @@ public class WeatherForecastActivity extends BaseActivity { mRecyclerView.setVisibility(View.VISIBLE); android.setVisibility(View.GONE); } - WeatherForecastAdapter adapter = new WeatherForecastAdapter(this, - mWeatherForecastList, - getSupportFragmentManager()); - mRecyclerView.setAdapter(adapter); + mWeatherForecastAdapter.updateWeatherList(mWeatherForecastList); } @Override @@ -127,8 +130,8 @@ public class WeatherForecastActivity extends BaseActivity { setVisibleUpdating(true); } else { Toast.makeText(WeatherForecastActivity.this, - R.string.connection_not_found, - Toast.LENGTH_SHORT).show(); + R.string.connection_not_found, + Toast.LENGTH_SHORT).show(); } return true; case android.R.id.home: @@ -153,13 +156,15 @@ public class WeatherForecastActivity extends BaseActivity { SharedPreferences pref = getSharedPreferences(Constants.APP_SETTINGS_NAME, 0); String latitude = pref.getString(Constants.APP_SETTINGS_LATITUDE, "51.51"); String longitude = pref.getString(Constants.APP_SETTINGS_LONGITUDE, "-0.13"); - String locale = LanguageUtil.getLanguageName(PreferenceUtil.getLanguage(WeatherForecastActivity.this)); + String locale = LanguageUtil.getLanguageName( + PreferenceUtil.getLanguage(WeatherForecastActivity.this)); String units = AppPreference.getTemperatureUnit(WeatherForecastActivity.this); String requestResult = ""; HttpURLConnection connection = null; try { - URL url = getWeatherForecastUrl(Constants.WEATHER_FORECAST_ENDPOINT, latitude, longitude, units, locale); + URL url = getWeatherForecastUrl(Constants.WEATHER_FORECAST_ENDPOINT, latitude, + longitude, units, locale); connection = (HttpURLConnection) url.openConnection(); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { diff --git a/app/src/main/java/foundation/e/weather/adapter/WeatherForecastAdapter.java b/app/src/main/java/foundation/e/weather/adapter/WeatherForecastAdapter.java index 04d2554a011c057e1a87c69f8999ec595feeba88..de0de20cfe0500320acfef23969a716cb6fe6a24 100644 --- a/app/src/main/java/foundation/e/weather/adapter/WeatherForecastAdapter.java +++ b/app/src/main/java/foundation/e/weather/adapter/WeatherForecastAdapter.java @@ -3,27 +3,33 @@ package foundation.e.weather.adapter; import android.content.Context; import android.support.v4.app.FragmentManager; import android.support.v7.widget.RecyclerView; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import java.util.ArrayList; +import java.util.List; + import foundation.e.weather.R; import foundation.e.weather.model.WeatherForecast; -import java.util.List; - public class WeatherForecastAdapter extends RecyclerView.Adapter { private Context mContext; - private List mWeatherList; + private List mWeatherList = new ArrayList<>(); private FragmentManager mFragmentManager; - public WeatherForecastAdapter(Context context, List weather, FragmentManager fragmentManager) { + public WeatherForecastAdapter(Context context, FragmentManager fragmentManager) { mContext = context; - mWeatherList = weather; mFragmentManager = fragmentManager; } + public void updateWeatherList(List data) { + this.mWeatherList = new ArrayList<>(data); + notifyDataSetChanged(); + } + @Override public WeatherForecastViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater inflater = LayoutInflater.from(parent.getContext()); @@ -39,7 +45,7 @@ public class WeatherForecastAdapter extends RecyclerView.Adapter