Loading app/src/main/java/foundation/e/weather/MainActivity.java +15 −12 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -123,6 +124,7 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC private RecyclerView mRecyclerView; private List<WeatherForecast> mWeatherForecastList; private WeatherForecastAdapter mWeatherForecastAdapter; private static Handler mHandler; private ProgressDialog mGetWeatherProgress; Loading Loading @@ -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) { Loading @@ -185,6 +184,7 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC setVisibleUpdating(false); break; case Constants.PARSE_RESULT_SUCCESS: mWeatherForecastList = (List<WeatherForecast>) msg.obj; setVisibleUpdating(false); updateUI(); if (!mWeatherForecastList.isEmpty()) { Loading @@ -196,6 +196,7 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC } }; getWeather(); } private void updateCurrentWeather() { Loading Loading @@ -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); Loading Loading @@ -776,7 +779,7 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC JSONObject jsonObject = new JSONObject(data); JSONArray listArray = jsonObject.getJSONArray("list"); List<WeatherForecast> weatherForecasts = new ArrayList<>(); int listArrayCount = listArray.length(); for (int i = 0; i < listArrayCount; i++) { WeatherForecast weatherForecast = new WeatherForecast(); Loading Loading @@ -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(); Loading @@ -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); Loading @@ -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); } } app/src/main/java/foundation/e/weather/WeatherForecastActivity.java +25 −20 Original line number Diff line number Diff line package foundation.e.weather; import static foundation.e.weather.utils.Utils.getWeatherForecastUrl; import android.app.ProgressDialog; import android.content.SharedPreferences; import android.os.Bundle; Loading @@ -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; Loading @@ -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 { Loading @@ -42,6 +43,7 @@ public class WeatherForecastActivity extends BaseActivity { private List<WeatherForecast> mWeatherForecastList; private ConnectionDetector mConnectionDetector; private RecyclerView mRecyclerView; private WeatherForecastAdapter mWeatherForecastAdapter; private static Handler mHandler; private ProgressDialog mGetWeatherProgress; Loading @@ -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() { Loading Loading @@ -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 Loading Loading @@ -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) { Loading app/src/main/java/foundation/e/weather/adapter/WeatherForecastAdapter.java +12 −6 Original line number Diff line number Diff line Loading @@ -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<WeatherForecastViewHolder> { private Context mContext; private List<WeatherForecast> mWeatherList; private List<WeatherForecast> mWeatherList = new ArrayList<>(); private FragmentManager mFragmentManager; public WeatherForecastAdapter(Context context, List<WeatherForecast> weather, FragmentManager fragmentManager) { public WeatherForecastAdapter(Context context, FragmentManager fragmentManager) { mContext = context; mWeatherList = weather; mFragmentManager = fragmentManager; } public void updateWeatherList(List<WeatherForecast> data) { this.mWeatherList = new ArrayList<>(data); notifyDataSetChanged(); } @Override public WeatherForecastViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater inflater = LayoutInflater.from(parent.getContext()); Loading @@ -39,7 +45,7 @@ public class WeatherForecastAdapter extends RecyclerView.Adapter<WeatherForecast @Override public int getItemCount() { return (mWeatherList != null ? mWeatherList.size() : 0); return mWeatherList != null ? mWeatherList.size() : 0; } } gradlew 100644 → 100755 +0 −0 File mode changed from 100644 to 100755. View file Loading
app/src/main/java/foundation/e/weather/MainActivity.java +15 −12 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -123,6 +124,7 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC private RecyclerView mRecyclerView; private List<WeatherForecast> mWeatherForecastList; private WeatherForecastAdapter mWeatherForecastAdapter; private static Handler mHandler; private ProgressDialog mGetWeatherProgress; Loading Loading @@ -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) { Loading @@ -185,6 +184,7 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC setVisibleUpdating(false); break; case Constants.PARSE_RESULT_SUCCESS: mWeatherForecastList = (List<WeatherForecast>) msg.obj; setVisibleUpdating(false); updateUI(); if (!mWeatherForecastList.isEmpty()) { Loading @@ -196,6 +196,7 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC } }; getWeather(); } private void updateCurrentWeather() { Loading Loading @@ -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); Loading Loading @@ -776,7 +779,7 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC JSONObject jsonObject = new JSONObject(data); JSONArray listArray = jsonObject.getJSONArray("list"); List<WeatherForecast> weatherForecasts = new ArrayList<>(); int listArrayCount = listArray.length(); for (int i = 0; i < listArrayCount; i++) { WeatherForecast weatherForecast = new WeatherForecast(); Loading Loading @@ -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(); Loading @@ -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); Loading @@ -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); } }
app/src/main/java/foundation/e/weather/WeatherForecastActivity.java +25 −20 Original line number Diff line number Diff line package foundation.e.weather; import static foundation.e.weather.utils.Utils.getWeatherForecastUrl; import android.app.ProgressDialog; import android.content.SharedPreferences; import android.os.Bundle; Loading @@ -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; Loading @@ -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 { Loading @@ -42,6 +43,7 @@ public class WeatherForecastActivity extends BaseActivity { private List<WeatherForecast> mWeatherForecastList; private ConnectionDetector mConnectionDetector; private RecyclerView mRecyclerView; private WeatherForecastAdapter mWeatherForecastAdapter; private static Handler mHandler; private ProgressDialog mGetWeatherProgress; Loading @@ -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() { Loading Loading @@ -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 Loading Loading @@ -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) { Loading
app/src/main/java/foundation/e/weather/adapter/WeatherForecastAdapter.java +12 −6 Original line number Diff line number Diff line Loading @@ -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<WeatherForecastViewHolder> { private Context mContext; private List<WeatherForecast> mWeatherList; private List<WeatherForecast> mWeatherList = new ArrayList<>(); private FragmentManager mFragmentManager; public WeatherForecastAdapter(Context context, List<WeatherForecast> weather, FragmentManager fragmentManager) { public WeatherForecastAdapter(Context context, FragmentManager fragmentManager) { mContext = context; mWeatherList = weather; mFragmentManager = fragmentManager; } public void updateWeatherList(List<WeatherForecast> data) { this.mWeatherList = new ArrayList<>(data); notifyDataSetChanged(); } @Override public WeatherForecastViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater inflater = LayoutInflater.from(parent.getContext()); Loading @@ -39,7 +45,7 @@ public class WeatherForecastAdapter extends RecyclerView.Adapter<WeatherForecast @Override public int getItemCount() { return (mWeatherList != null ? mWeatherList.size() : 0); return mWeatherList != null ? mWeatherList.size() : 0; } }