From 348fd6866ceeb2a2cfd903eee5c759861c6212f2 Mon Sep 17 00:00:00 2001 From: aminecmi Date: Sun, 12 Jul 2015 18:04:50 +0200 Subject: [PATCH] final frag --- .../amine/myterio/app/DetailsActivity.java | 115 -------------- .../app/fragments/DetailsFragment.java | 141 ++++++++++++++++++ .../res/layout-large/activity_details.xml | 44 ++---- .../res/layout-large/details_fragment.xml | 45 ++++++ .../res/layout-normal/activity_details.xml | 48 ++---- .../res/layout-normal/details_fragment.xml | 45 ++++++ .../res/layout-small/activity_details.xml | 50 ++----- .../res/layout-small/details_fragment.xml | 47 ++++++ .../res/layout-xlarge/activity_details.xml | 48 ++---- .../res/layout-xlarge/details_fragment.xml | 45 ++++++ app/src/main/res/layout/activity_details.xml | 48 ++---- app/src/main/res/layout/details_fragment.xml | 45 ++++++ 12 files changed, 413 insertions(+), 308 deletions(-) create mode 100644 app/src/main/java/com/amine/myterio/app/fragments/DetailsFragment.java create mode 100644 app/src/main/res/layout-large/details_fragment.xml create mode 100644 app/src/main/res/layout-normal/details_fragment.xml create mode 100644 app/src/main/res/layout-small/details_fragment.xml create mode 100644 app/src/main/res/layout-xlarge/details_fragment.xml create mode 100644 app/src/main/res/layout/details_fragment.xml diff --git a/app/src/main/java/com/amine/myterio/app/DetailsActivity.java b/app/src/main/java/com/amine/myterio/app/DetailsActivity.java index a5853c7..7183b67 100644 --- a/app/src/main/java/com/amine/myterio/app/DetailsActivity.java +++ b/app/src/main/java/com/amine/myterio/app/DetailsActivity.java @@ -2,25 +2,9 @@ package com.amine.myterio.app; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; import android.view.Menu; import android.view.MenuItem; -import android.view.View; -import android.widget.ImageView; -import android.widget.TextView; -import android.widget.Toast; -import com.amine.myterio.app.adapters.ForecastAdapter; -import com.amine.myterio.app.api.WeatherAdapters; -import com.amine.myterio.app.api.WeatherApis; -import com.amine.myterio.app.config.Config; -import com.amine.myterio.app.db.CityDAO; -import com.amine.myterio.app.model.City; -import com.amine.myterio.app.model.Forecast; import com.melnykov.fab.FloatingActionButton; -import retrofit.Callback; -import retrofit.RetrofitError; -import retrofit.client.Response; public class DetailsActivity extends AppCompatActivity { @@ -31,105 +15,6 @@ public class DetailsActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_details); - - String cityName = getIntent().getExtras().getString("city_name"); - - WeatherAdapters adapters = new WeatherAdapters(); - final Forecast[] f = {null}; - final RecyclerView list = (RecyclerView) findViewById(R.id.weekForecast); - LinearLayoutManager layoutManager = new LinearLayoutManager(this); - list.setLayoutManager(layoutManager); - list.setHasFixedSize(true); - - CityDAO dao = CityDAO.getInstance(this); - if (cityName != null) { - - isFav = dao.getCity(cityName) != null; - WeatherApis.WeatherDailyForecastLocationApi s = adapters.getWeatherForecastLocationAdapter(); - - s.cityForecast(cityName, Config.country, new Callback() { - @Override - public void success(Forecast forecast, Response response) { - f[0] = forecast; - list.setAdapter(new ForecastAdapter(DetailsActivity.this, f[0])); - - } - - @Override - public void failure(RetrofitError error) { - Toast.makeText(getApplicationContext(), getString(R.string.weather_not_available), Toast.LENGTH_SHORT).show(); - } - }); - - WeatherApis.WeatherLocationApi sWeather = adapters.getWeatherLocationAdapter(); - sWeather.locationWeather(cityName, Config.country, new Callback() { - @Override - public void success(City c, Response response) { - ImageView image = (ImageView) findViewById(R.id.weatherImage); - image.setImageDrawable(c.getWeather().get(0).getIconDrawable(DetailsActivity.this, DetailsActivity.this.getPackageName())); - } - - @Override - public void failure(RetrofitError error) { - Toast.makeText(DetailsActivity.this, getString(R.string.weather_get_error), Toast.LENGTH_SHORT).show(); - } - }); - } else { - City city = getIntent().getExtras().getParcelable("city"); - cityName = city.getName(); - isFav = dao.getCity(city.getCityIdentifier()) != null; - WeatherApis.WeatherDailyForecastApi s = adapters.getWeatherForecastAdapter(); - - s.cityForecast(city.getCityIdentifier(), Config.country, new Callback() { - @Override - public void success(Forecast forecast, Response response) { - f[0] = forecast; - list.setAdapter(new ForecastAdapter(DetailsActivity.this, f[0])); - } - - @Override - public void failure(RetrofitError error) { - Toast.makeText(getApplicationContext(), getString(R.string.weather_not_available), Toast.LENGTH_SHORT).show(); - } - }); - - WeatherApis.WeatherCityApi sWeather = adapters.getWeatherCityAdapter(); - sWeather.cityWeather(city.getCityIdentifier(), Config.country, new Callback() { - @Override - public void success(City c, Response response) { - ImageView image = (ImageView) findViewById(R.id.weatherImage); - image.setImageDrawable(c.getWeather().get(0).getIconDrawable(DetailsActivity.this, DetailsActivity.this.getPackageName())); - } - - @Override - public void failure(RetrofitError error) { - Toast.makeText(DetailsActivity.this, getString(R.string.weather_get_error), Toast.LENGTH_SHORT).show(); - } - }); - } - - TextView name = (TextView) findViewById(R.id.cityName); - name.setText(cityName); - - fab = (FloatingActionButton) findViewById(R.id.fab); - if (isFav) { - fab.setImageResource(R.mipmap.ic_favorite_border_black_24dp); - } else { - fab.setImageResource(R.mipmap.ic_favorite_black_24dp); - } - fab.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - CityDAO dao = CityDAO.getInstance(DetailsActivity.this); - if (DetailsActivity.isFav) { - dao.deleteCity(f[0].getCity()); - fab.setImageResource(R.mipmap.ic_favorite_black_24dp); - } else { - dao.insertCity(f[0].getCity()); - fab.setImageResource(R.mipmap.ic_favorite_border_black_24dp); - } - } - }); } diff --git a/app/src/main/java/com/amine/myterio/app/fragments/DetailsFragment.java b/app/src/main/java/com/amine/myterio/app/fragments/DetailsFragment.java new file mode 100644 index 0000000..51185e5 --- /dev/null +++ b/app/src/main/java/com/amine/myterio/app/fragments/DetailsFragment.java @@ -0,0 +1,141 @@ +package com.amine.myterio.app.fragments; + +import android.app.Activity; +import android.app.Fragment; +import android.os.Bundle; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; +import android.widget.Toast; +import com.amine.myterio.app.R; +import com.amine.myterio.app.adapters.ForecastAdapter; +import com.amine.myterio.app.api.WeatherAdapters; +import com.amine.myterio.app.api.WeatherApis; +import com.amine.myterio.app.config.Config; +import com.amine.myterio.app.db.CityDAO; +import com.amine.myterio.app.model.City; +import com.amine.myterio.app.model.Forecast; +import com.melnykov.fab.FloatingActionButton; +import retrofit.Callback; +import retrofit.RetrofitError; +import retrofit.client.Response; + +public class DetailsFragment extends Fragment { + static boolean isFav = true; + static FloatingActionButton fab; + private Activity mActivity; + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + mActivity = activity; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + final View fragView = inflater.inflate(R.layout.details_fragment, null, false); + String cityName = getActivity().getIntent().getExtras().getString("city_name"); + + WeatherAdapters adapters = new WeatherAdapters(); + final Forecast[] f = {null}; + final RecyclerView list = (RecyclerView) fragView.findViewById(R.id.weekForecast); + LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity); + list.setLayoutManager(layoutManager); + list.setHasFixedSize(true); + + CityDAO dao = CityDAO.getInstance(mActivity); + if (cityName != null) { + + isFav = dao.getCity(cityName) != null; + WeatherApis.WeatherDailyForecastLocationApi s = adapters.getWeatherForecastLocationAdapter(); + + s.cityForecast(cityName, Config.country, new Callback() { + @Override + public void success(Forecast forecast, Response response) { + f[0] = forecast; + list.setAdapter(new ForecastAdapter(mActivity, f[0])); + + } + + @Override + public void failure(RetrofitError error) { + Toast.makeText(mActivity, getString(R.string.weather_not_available), Toast.LENGTH_SHORT).show(); + } + }); + + WeatherApis.WeatherLocationApi sWeather = adapters.getWeatherLocationAdapter(); + sWeather.locationWeather(cityName, Config.country, new Callback() { + @Override + public void success(City c, Response response) { + ImageView image = (ImageView) fragView.findViewById(R.id.weatherImage); + image.setImageDrawable(c.getWeather().get(0).getIconDrawable(mActivity, mActivity.getPackageName())); + } + + @Override + public void failure(RetrofitError error) { + Toast.makeText(mActivity, getString(R.string.weather_get_error), Toast.LENGTH_SHORT).show(); + } + }); + } else { + City city = getActivity().getIntent().getExtras().getParcelable("city"); + cityName = city.getName(); + isFav = dao.getCity(city.getCityIdentifier()) != null; + WeatherApis.WeatherDailyForecastApi s = adapters.getWeatherForecastAdapter(); + + s.cityForecast(city.getCityIdentifier(), Config.country, new Callback() { + @Override + public void success(Forecast forecast, Response response) { + f[0] = forecast; + list.setAdapter(new ForecastAdapter(mActivity, f[0])); + } + + @Override + public void failure(RetrofitError error) { + Toast.makeText(mActivity, getString(R.string.weather_not_available), Toast.LENGTH_SHORT).show(); + } + }); + + WeatherApis.WeatherCityApi sWeather = adapters.getWeatherCityAdapter(); + sWeather.cityWeather(city.getCityIdentifier(), Config.country, new Callback() { + @Override + public void success(City c, Response response) { + ImageView image = (ImageView) fragView.findViewById(R.id.weatherImage); + image.setImageDrawable(c.getWeather().get(0).getIconDrawable(mActivity, mActivity.getPackageName())); + } + + @Override + public void failure(RetrofitError error) { + Toast.makeText(mActivity, getString(R.string.weather_get_error), Toast.LENGTH_SHORT).show(); + } + }); + } + + TextView name = (TextView) fragView.findViewById(R.id.cityName); + name.setText(cityName); + + fab = (FloatingActionButton) fragView.findViewById(R.id.fab); + if (isFav) { + fab.setImageResource(R.mipmap.ic_favorite_border_black_24dp); + } else { + fab.setImageResource(R.mipmap.ic_favorite_black_24dp); + } + fab.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + CityDAO dao = CityDAO.getInstance(mActivity); + if (isFav) { + dao.deleteCity(f[0].getCity()); + fab.setImageResource(R.mipmap.ic_favorite_black_24dp); + } else { + dao.insertCity(f[0].getCity()); + fab.setImageResource(R.mipmap.ic_favorite_border_black_24dp); + } + } + }); + return fragView; + } +} diff --git a/app/src/main/res/layout-large/activity_details.xml b/app/src/main/res/layout-large/activity_details.xml index ff9ad39..c758535 100644 --- a/app/src/main/res/layout-large/activity_details.xml +++ b/app/src/main/res/layout-large/activity_details.xml @@ -1,4 +1,4 @@ - - + - - - - - - - + diff --git a/app/src/main/res/layout-large/details_fragment.xml b/app/src/main/res/layout-large/details_fragment.xml new file mode 100644 index 0000000..ff9ad39 --- /dev/null +++ b/app/src/main/res/layout-large/details_fragment.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + diff --git a/app/src/main/res/layout-normal/activity_details.xml b/app/src/main/res/layout-normal/activity_details.xml index 7ec40f4..a51752d 100644 --- a/app/src/main/res/layout-normal/activity_details.xml +++ b/app/src/main/res/layout-normal/activity_details.xml @@ -1,45 +1,15 @@ - - + - - - - - - - + diff --git a/app/src/main/res/layout-normal/details_fragment.xml b/app/src/main/res/layout-normal/details_fragment.xml new file mode 100644 index 0000000..7ec40f4 --- /dev/null +++ b/app/src/main/res/layout-normal/details_fragment.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + diff --git a/app/src/main/res/layout-small/activity_details.xml b/app/src/main/res/layout-small/activity_details.xml index 0d8f83a..a51752d 100644 --- a/app/src/main/res/layout-small/activity_details.xml +++ b/app/src/main/res/layout-small/activity_details.xml @@ -1,47 +1,15 @@ - - + - - - - - - - + diff --git a/app/src/main/res/layout-small/details_fragment.xml b/app/src/main/res/layout-small/details_fragment.xml new file mode 100644 index 0000000..0d8f83a --- /dev/null +++ b/app/src/main/res/layout-small/details_fragment.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + diff --git a/app/src/main/res/layout-xlarge/activity_details.xml b/app/src/main/res/layout-xlarge/activity_details.xml index 7a702f1..a51752d 100644 --- a/app/src/main/res/layout-xlarge/activity_details.xml +++ b/app/src/main/res/layout-xlarge/activity_details.xml @@ -1,45 +1,15 @@ - - + - - - - - - - + diff --git a/app/src/main/res/layout-xlarge/details_fragment.xml b/app/src/main/res/layout-xlarge/details_fragment.xml new file mode 100644 index 0000000..7a702f1 --- /dev/null +++ b/app/src/main/res/layout-xlarge/details_fragment.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_details.xml b/app/src/main/res/layout/activity_details.xml index 7ec40f4..a51752d 100644 --- a/app/src/main/res/layout/activity_details.xml +++ b/app/src/main/res/layout/activity_details.xml @@ -1,45 +1,15 @@ - - + - - - - - - - + diff --git a/app/src/main/res/layout/details_fragment.xml b/app/src/main/res/layout/details_fragment.xml new file mode 100644 index 0000000..7ec40f4 --- /dev/null +++ b/app/src/main/res/layout/details_fragment.xml @@ -0,0 +1,45 @@ + + + + + + + + + + +