Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d9bbe1cc authored by Amit Kumar's avatar Amit Kumar
Browse files

Fix crash issue because of sharing variable in different threads.

Remove inconsistency in recycler view.
parent 2187a196
Loading
Loading
Loading
Loading
+15 −12
Original line number Diff line number Diff line
@@ -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<WeatherForecast> 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<WeatherForecast>) 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<WeatherForecast> 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);
    }
}
+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;
@@ -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<WeatherForecast> 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() {
@@ -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
@@ -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) {
+12 −6
Original line number Diff line number Diff line
@@ -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());
@@ -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.