Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static MainFragment newInstance() {
- return new MainFragment();
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.fragment_main, container, false);
- weatherList = new ArrayList<>();
- recyclerView = (RecyclerView) rootView.findViewById(R.id.list);
- swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout);
- recyclerView.setHasFixedSize(true);
- recyclerView.setLayoutManager(
- new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
- cityListAdapter = new CityListAdapter(getActivity(), weatherList);
- recyclerView.setAdapter(cityListAdapter);
- swipeRefreshLayout.setOnRefreshListener(this);
- swipeRefreshLayout.post(new Runnable() {
- @Override
- public void run() {
- swipeRefreshLayout.setRefreshing(true);
- getCityTemp();
- }
- });
- return rootView;
- }
- @Override
- public void onRefresh() {
- swipeRefreshLayout.setRefreshing(true);
- getCityTemp();
- }
- private void getCityTemp() {
- weatherList.clear();
- swipeRefreshLayout.setRefreshing(true);
- MyApplication.getInstance().getDb().getAllCityAsync(new DataBaseHelper.DatabaseHand<List<Weather>>() {
- @Override
- public void onComplete(boolean success, final List<Weather> result) {
- if (success) {
- for (final Weather cn : result) {
- String url = CITY_TEMP_URL + cn.location.getCity() + LANG + METRIC + API_KEY;
- JsonObjectRequest request = new JsonObjectRequest(url, null,
- new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject jsonObject) {
- try {
- MyApplication.getInstance().setWeather(
- JSONWeatherParser.getWeather(jsonObject.toString(), cn.getCityID()));
- weatherList.add(MyApplication.getInstance().getWeather());
- cityListAdapter.notifyDataSetChanged();
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError volleyError) {
- }
- });
- MyApplication.getInstance().addToRequestQueue(request);
- }
- }
- MyApplication.getInstance().getDb().close();
- }
- });
- swipeRefreshLayout.setRefreshing(false);
- }
- @Override
- public void onPause() {
- super.onPause();
- saveData();
- }
- private void read() {
- MyApplication.getInstance().getDb().getAllCityAsync(new DataBaseHelper.DatabaseHand<List<Weather>>() {
- @Override
- public void onComplete(boolean success, List<Weather> result) {
- if (success) {
- for (Weather cn : result) {
- String log =
- "Id: " + cn.getCityID() + "n" +
- " , name_city: " + cn.location.getCity() + "n" +
- " , lon: " + cn.location.getLongitude() + "n" +
- " , lat: " + cn.location.getLatitude() + "n" +
- " , country: " + cn.location.getCountry() + "n" +
- " , sunrise: " + cn.location.getSunrise() + "n" +
- " , sunset: " + cn.location.getSunset() + "n" +
- " , weather_id: " + cn.currentCondition.getWeatherId() + "n" +
- " , description: " + cn.currentCondition.getDescr() + "n" +
- " , main: " + cn.currentCondition.getCondition() + "n" +
- " , icon: " + cn.currentCondition.getIcon() + "n" +
- " , humidity: " + cn.currentCondition.getHumidity() + "n" +
- " , pressure: " + cn.currentCondition.getPressure() + "n" +
- " , temp_max: " + cn.temperature.getMaxTemp() + "n" +
- " , temp_min: " + cn.temperature.getMinTemp() + "n" +
- " , speed: " + cn.wind.getSpeed() + "n" +
- " , deg: " + cn.wind.getDeg() + "n" +
- " , all: " + cn.clouds.getPerc() + "n" +
- " , icon_array: " + Arrays.toString(cn.getIconArray()) + "n";
- Log.d("TAG", log);
- }
- }
- MyApplication.getInstance().getDb().close();
- }
- });
- }
- public void saveData() {
- for (Weather weather : weatherList) {
- MyApplication.getInstance().getDb().updateCityAsync(weather,
- new DataBaseHelper.DatabaseHand<Integer>() {
- @Override
- public void onComplete(boolean success, Integer result) {
- if (success) {
- }
- }
- });
- }
- read();
- }
- public class CityListAdapter extends RecyclerView.Adapter<CityListAdapter.ViewHolder> {
- private List<Weather> data;
- Context context;
- public CityListAdapter(Context context, List<Weather> data) {
- this.context = context;
- this.data = data;
- }
- @Override
- public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- View itemView = LayoutInflater.from(parent.getContext())
- .inflate(R.layout.custom_list_item, parent, false);
- return new ViewHolder(itemView);
- }
- @Override
- public void onBindViewHolder(ViewHolder holder, int position) {
- Weather weather = data.get(position);
- holder.name_city.setText(weather.location.getCity());
- holder.name_city.setTag(weather.getCityID());
- holder.temperature_city.setText(MessageFormat.format("{0}℃",
- Math.round(weather.temperature.getTemp())));
- }
- @Override
- public int getItemCount() {
- return data.size();
- }
- class ViewHolder extends RecyclerView.ViewHolder
- implements View.OnClickListener {
- private TextView name_city, temperature_city;
- public ViewHolder(View itemView) {
- super(itemView);
- name_city = (TextView) itemView.findViewById(R.id.name_city);
- temperature_city = (TextView) itemView.findViewById(R.id.temperature_city);
- name_city.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- Integer pos = (int) (long) v.getTag();
- read(pos);
- }
- }
- public void nextFragment() {
- FragmentManager manager = ((AppCompatActivity) context).getSupportFragmentManager();
- FragmentTransaction ft = manager.beginTransaction();
- ft.replace(R.id.container, new DetailFragment());
- ft.addToBackStack("detail");
- ft.commit();
- }
- private void read(long pos) {
- MyApplication.getInstance().getDb().getCityAsync(pos, new DataBaseHelper.DatabaseHand<Weather>() {
- @Override
- public void onComplete(boolean success, Weather result) {
- if (success) {
- MyApplication.getInstance().setWeather(result);
- nextFragment();
- }
- MyApplication.getInstance().getDb().close();
- }
- });
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View v = inflater.inflate(
- R.layout.detail_fragment, container, false);
- init(v);
- setDataView();
- return v;
- }
- public void setDataView() {
- tvLocation.setText(String.format("%s,%s", getInstance().getWeather().location.getCity(),
- getInstance().getWeather().location.getCountry()));
- tvTemperature.setText(String.valueOf(Math.round(getInstance().getWeather().temperature.getTemp())));
- tvTempMin.setText(String.valueOf(Math.round(getInstance().getWeather().temperature.getMinTemp())));
- tvTempMax.setText(String.valueOf(Math.round(getInstance().getWeather().temperature.getMaxTemp())));
- tvWindSpeed.setText(String.valueOf(Math.round(getInstance().getWeather().wind.getSpeed())));
- tvWindDeg.setText(String.valueOf(Math.round(getInstance().getWeather().wind.getDeg())));
- tvHum.setText(String.valueOf(Math.round(getInstance().getWeather().currentCondition.getHumidity())));
- tvPress.setText(String.valueOf(Math.round(getInstance().getWeather().currentCondition.getPressure())));
- tvSunrise.setText(convertTime(getInstance().getWeather().location.getSunrise()));
- tvSunset.setText(convertTime(getInstance().getWeather().location.getSunset()));
- int resId = getResource(getInstance().getWeather().temperature.getTemp());
- lineTxt.setBackgroundResource(resId);
- tvTempUnit.setText("°C");
- tvDescrWeather.setText(getInstance().getWeather().currentCondition.getDescr());
- }
- public void init(View v) {
- requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
- tvDescrWeather = (TextView) v.findViewById(R.id.descrWeather);
- lineTxt = (TextView) v.findViewById(R.id.lineTxt);
- tvLocation = (TextView) v.findViewById(R.id.location);
- tvTemperature = (TextView) v.findViewById(R.id.temp);
- tvTempUnit = (TextView) v.findViewById(R.id.tempUnit);
- tvTempMin = (TextView) v.findViewById(R.id.tempMin);
- tvTempMax = (TextView) v.findViewById(R.id.tempMax);
- tvHum = (TextView) v.findViewById(R.id.humidity);
- tvWindSpeed = (TextView) v.findViewById(R.id.windSpeed);
- tvWindDeg = (TextView) v.findViewById(R.id.windDeg);
- tvPress = (TextView) v.findViewById(R.id.pressure);
- weatherImage = (ImageView) v.findViewById(R.id.imgWeather);
- tvSunrise = (TextView) v.findViewById(R.id.sunrise);
- tvSunset = (TextView) v.findViewById(R.id.sunset);
- }
- public String convertTime(long time) {
- long javaTimestamp = time * 1000L;
- Date date = new Date(javaTimestamp);
- return new SimpleDateFormat("hh:mm", Locale.ENGLISH).format(date);
- }
- private int getResource(float temp) {
- int resId = 0;
- if (temp < 10)
- resId = R.drawable.line_shape_blue;
- else if (temp >= 10 && temp <= 24)
- resId = R.drawable.line_shape_green;
- else if (temp > 25)
- resId = R.drawable.line_shape_red;
- return resId;
- }
- public class MyApplication extends Application {
- public static final String TAG = MyApplication.class
- .getSimpleName();
- private Weather weather;
- private DataBaseHelper db;
- private RequestQueue mRequestQueue;
- private static MyApplication mInstance;
- RequestQueue.RequestFinishedListener requestFinishedListener;
- @Override
- public void onCreate() {
- super.onCreate();
- mInstance = this;
- db = new DataBaseHelper(this);
- weather = new Weather();
- }
- public static synchronized MyApplication getInstance() {
- return mInstance;
- }
Add Comment
Please, Sign In to add comment