diff --git a/app/src/main/java/fr/romanet/vj/apps/myweather/MainActivity.java b/app/src/main/java/fr/romanet/vj/apps/myweather/MainActivity.java index aa3b4c5..ecc524e 100644 --- a/app/src/main/java/fr/romanet/vj/apps/myweather/MainActivity.java +++ b/app/src/main/java/fr/romanet/vj/apps/myweather/MainActivity.java @@ -68,7 +68,7 @@ public class MainActivity extends AppCompatActivity { protected void onResume() { super.onResume(); - //initList(); + initList(); } public void initialisingComponents() @@ -87,14 +87,16 @@ public class MainActivity extends AppCompatActivity { public void initList() { - final List city = CityRepository.getInstance(this).getCity(); + List city = CityRepository.getInstance(this).getCity(); ArrayList listeExample = new ArrayList(); for(int i = 0; i < city.size(); i++) { listeExample.add("" + city.get(i).nameCity); + Log.d("DEBUG1", "List -> " + city.get(i).nameCity + "id:" + city.get(i).id); } ArrayAdapter adapter = new ArrayAdapter( this, android.R.layout.simple_list_item_1, listeExample); listViewCityNames.setAdapter(adapter); + Log.d("DEBUG1", "initList -> list size = " + Integer.toString(city.size())); } public void openAddCityPage() diff --git a/app/src/main/java/fr/romanet/vj/apps/myweather/ShowTemparature.java b/app/src/main/java/fr/romanet/vj/apps/myweather/ShowTemparature.java index 9cd3eae..c9072b7 100644 --- a/app/src/main/java/fr/romanet/vj/apps/myweather/ShowTemparature.java +++ b/app/src/main/java/fr/romanet/vj/apps/myweather/ShowTemparature.java @@ -9,9 +9,11 @@ import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; +import android.widget.ImageView; import android.widget.TextView; -import java.util.ArrayList; +import java.net.URI; +import java.util.Date; import java.util.List; import fr.romanet.vj.apps.myweather.bo.City; @@ -27,7 +29,12 @@ import retrofit2.converter.gson.GsonConverterFactory; public class ShowTemparature extends AppCompatActivity { private String currentCityName; - private TextView textView2; + private TextView tv_cityname; + private TextView tv_temp; + private TextView tv_feelslike; + private TextView tv_tempmin; + private TextView tv_tempmax; + private ImageView img_weather; @Override protected void onCreate(Bundle savedInstanceState) { @@ -43,7 +50,12 @@ public class ShowTemparature extends AppCompatActivity { getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); - textView2 = (TextView) findViewById(R.id.textView2); + tv_cityname = (TextView) findViewById(R.id.tv_cityname); + tv_temp = (TextView) findViewById(R.id.tv_temp); + tv_feelslike = (TextView) findViewById(R.id.tv_feelslike); + tv_tempmin = (TextView) findViewById(R.id.tv_tempmin); + tv_tempmax = (TextView) findViewById(R.id.tv_tempmax); + img_weather = (ImageView) findViewById(R.id.img_weather); // - Build Retrofit API tool Retrofit retrofit = new Retrofit.Builder() @@ -51,90 +63,57 @@ public class ShowTemparature extends AppCompatActivity { .addConverterFactory(GsonConverterFactory.create()) .build(); - // - Build Interface with Retrofit - JsonApiInterfce jsonApiInterfce = retrofit.create(JsonApiInterfce.class); - Call call = jsonApiInterfce.getPost(currentCityName, "metric", "0ac5802392867db8e1ae4f91529f7890"); + Date date= new Date(); + long time = date.getTime(); + final List city = CityRepository.getInstance(this).getCity(); + for(int i = 0; i < city.size(); i++) + { + if(city.get(i).nameCity.equals(currentCityName)) + { + if (city.get(i).last_fetch == 0 || time - city.get(i).last_fetch >= 3600000){ + // - Build Interface with Retrofit + JsonApiInterfce jsonApiInterfce = retrofit.create(JsonApiInterfce.class); + Call call = jsonApiInterfce.getPost(currentCityName, "metric", "0ac5802392867db8e1ae4f91529f7890"); - // - 'Execute' the GET query with 'enqueue' method, Retrofit will create an AsyncTask that avoids UI Thread freeze Exception. - call.enqueue(new Callback() { + // - 'Execute' the GET query with 'enqueue' method, Retrofit will create an AsyncTask that avoids UI Thread freeze Exception. + call.enqueue(new Callback() { - // - If GET has an answer... - @Override - public void onResponse(Call call, Response response) { + // - If GET has an answer... + @Override + public void onResponse(Call call, Response response) { - // - Check if that answer is not an error (like 500, 404, etc) - if(! response.isSuccessful()){ - textView2.setText("Code: " + response.code()); - return; - } + // - Check if that answer is not an error (like 500, 404, etc) + if(! response.isSuccessful()){ + tv_cityname.setText("Code: " + response.code()); + return; + } - // - If it's all right, update Activity Objects with JSON parsed answer - Weather weather = response.body(); + // - If it's all right, update Activity Objects with JSON parsed answer + Weather weather = response.body(); // textView2.append(weather.get_temps().get_temp_feels_like()); - updateCity(weather); - updateActivityObjectsFromDB(); + updateCity(weather, time); + updateActivityObjectsFromDB(); + } + + // - If GET has no answer... + @Override + public void onFailure(Call call, Throwable t) { + + // - Display error message + tv_cityname.setText("Error: " + t.getMessage()); + } + }); + }else{ + updateActivityObjectsFromDB(); + } } - - // - If GET has no answer... - @Override - public void onFailure(Call call, Throwable t) { - - // - Display error message - textView2.setText("Error: " + t.getMessage()); - } - }); + } } - @Override - protected void onResume() { - super.onResume(); - - // - Build Retrofit API tool - Retrofit retrofit = new Retrofit.Builder() - .baseUrl("https://api.openweathermap.org/data/2.5/") - .addConverterFactory(GsonConverterFactory.create()) - .build(); - - // - Build Interface with Retrofit - JsonApiInterfce jsonApiInterfce = retrofit.create(JsonApiInterfce.class); - Call call = jsonApiInterfce.getPost(currentCityName, "metric", "0ac5802392867db8e1ae4f91529f7890"); - - // - 'Execute' the GET query with 'enqueue' method, Retrofit will create an AsyncTask that avoids UI Thread freeze Exception. - call.enqueue(new Callback() { - - // - If GET has an answer... - @Override - public void onResponse(Call call, Response response) { - - // - Check if that answer is not an error (like 500, 404, etc) - if(! response.isSuccessful()){ - textView2.setText("Code: " + response.code()); - return; - } - - // - If it's all right, update Activity Objects with JSON parsed answer - Weather weather = response.body(); -// textView2.append(weather.get_temps().get_temp_feels_like()); - - updateCity(weather); - updateActivityObjectsFromDB(); - - } - - // - If GET has no answer... - @Override - public void onFailure(Call call, Throwable t) { - - // - Display error message - textView2.setText("Error: " + t.getMessage()); - } - }); - } - @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_items, menu); @@ -167,28 +146,115 @@ public class ShowTemparature extends AppCompatActivity { } } - public void updateCity(Weather weather){ - City updated_city = new City(currentCityName); - updated_city.setW_temp(weather.get_temps().get_temp()); - updated_city.setW_feelsliketemp(weather.get_temps().get_temp_feels_like()); - updated_city.setW_icon(weather.get_weatherDescription().get_icon()); - updated_city.setW_maxtemp(weather.get_temps().get_temp_max()); - updated_city.setW_mintemp(weather.get_temps().get_temp_min()); - - CityRepository.getInstance(this).updateCity(updated_city); - } - - public void updateActivityObjectsFromDB(){ - final List city = CityRepository.getInstance(this).getCity(); - String temp; + public void updateCity(Weather weather, Long currentTimestamp){ + List city = CityRepository.getInstance(this).getCity(); + City updated_city; for(int i = 0; i < city.size(); i++) { if(city.get(i).nameCity.equals(currentCityName)) { - temp = city.get(i).nameCity; + updated_city = city.get(i); + updated_city.setW_temp(weather.get_temps().get_temp()); + updated_city.setW_feelsliketemp(weather.get_temps().get_temp_feels_like()); + updated_city.setW_icon(weather.get_weatherDescription().get_icon()); + updated_city.setW_maxtemp(weather.get_temps().get_temp_max()); + updated_city.setW_mintemp(weather.get_temps().get_temp_min()); + updated_city.setLast_fetch(currentTimestamp); + + Log.d("DEBUG1", "=-=-=-=- ID:" + updated_city.id); + Log.d("DEBUG1", "new name will be:" + updated_city.nameCity); + + + CityRepository.getInstance(this).updateCity(updated_city); + city = CityRepository.getInstance(this).getCity(); + Log.d("DEBUG1", "nb city in db:" + city.size()); + + for(i = 0; i < city.size(); i++) + { +// if(city.get(i).nameCity.equals(currentCityName)) +// { +// CityRepository.getInstance(this).deleteCity(city.get(i)); +// } + Log.d("DEBUG1", "Logging city:" + Integer.toString(i) + " name -> " + city.get(i).nameCity + " temp -> " + city.get(i).w_temp + " ID -> " + city.get(i).id); + } + + } + } + +// City updated_city = new City(currentCityName); + + + + Log.d("DEBUG1", "============> " + currentCityName); + } + + public void updateActivityObjectsFromDB(){ + final List city = CityRepository.getInstance(this).getCity(); + String cityname; + String temp; + String feelslike; + String tempmin; + String tempmax; + String icon; + for(int i = 0; i < city.size(); i++) + { + if(city.get(i).nameCity.equals(currentCityName)) + { + cityname = city.get(i).nameCity; + temp = city.get(i).w_temp; + feelslike = city.get(i).w_feelsliketemp; + tempmin = city.get(i).w_mintemp; + tempmax = city.get(i).w_maxtemp; + icon = city.get(i).w_icon; + Log.d("DEBUG1", "---------------------------------------->" + temp); - textView2.setText(temp); - CityRepository.getInstance(this).deleteCity(city.get(i)); + tv_cityname.setText(cityname); + tv_temp.setText(temp + "°C"); + tv_feelslike.setText("feels like " + feelslike + "°C"); + tv_tempmin.setText("Min. " + tempmin + "°C"); + tv_tempmax.setText("Max. " + tempmax + "°C"); + + if (icon.equals("01n")){ + img_weather.setImageResource(R.drawable.img_01n); + }else if(icon.equals("50d")){ + img_weather.setImageResource(R.drawable.img50d); + }else if(icon.equals("50n")){ + img_weather.setImageResource(R.drawable.img50n); + }else if(icon.equals("01d")){ + img_weather.setImageResource(R.drawable.img_01d); + }else if(icon.equals("02d")){ + img_weather.setImageResource(R.drawable.img_02d); + }else if(icon.equals("02n")){ + img_weather.setImageResource(R.drawable.img_02n); + }else if(icon.equals("03d")){ + img_weather.setImageResource(R.drawable.img_03d); + }else if(icon.equals("03n")){ + img_weather.setImageResource(R.drawable.img_03n); + }else if(icon.equals("04d")){ + img_weather.setImageResource(R.drawable.img_04d); + }else if(icon.equals("04n")){ + img_weather.setImageResource(R.drawable.img_04n); + }else if(icon.equals("09d")){ + img_weather.setImageResource(R.drawable.img_09d); + }else if(icon.equals("09n")){ + img_weather.setImageResource(R.drawable.img_09n); + }else if(icon.equals("10d")){ + img_weather.setImageResource(R.drawable.img_10d); + }else if(icon.equals("10n")){ + img_weather.setImageResource(R.drawable.img_10n); + }else if(icon.equals("11d")){ + img_weather.setImageResource(R.drawable.img_11d); + }else if(icon.equals("11n")){ + img_weather.setImageResource(R.drawable.img_11n); + }else if(icon.equals("13d")){ + img_weather.setImageResource(R.drawable.img_13d); + }else if(icon.equals("13n")){ + img_weather.setImageResource(R.drawable.img_13n); + } + + Log.d("DEBUG1", "icon:" + icon); + + } } } diff --git a/app/src/main/java/fr/romanet/vj/apps/myweather/bo/City.java b/app/src/main/java/fr/romanet/vj/apps/myweather/bo/City.java index f7a892f..faf196c 100644 --- a/app/src/main/java/fr/romanet/vj/apps/myweather/bo/City.java +++ b/app/src/main/java/fr/romanet/vj/apps/myweather/bo/City.java @@ -59,6 +59,9 @@ final public class City implements Serializable @Nullable public String w_maxtemp = null; + @Nullable + public long last_fetch = 0; + /** * Constructor of the city class * @param nameCity The name of the new city @@ -89,6 +92,11 @@ final public class City implements Serializable this.w_maxtemp = w_maxtemp; } + public void setLast_fetch(@Nullable long last_fetch) { + this.last_fetch = last_fetch; + } + + /** * Equals function that return true or false weither both name are same and both object belongs to City class * @param o The object to compare diff --git a/app/src/main/java/fr/romanet/vj/apps/myweather/repository/CityRepository.java b/app/src/main/java/fr/romanet/vj/apps/myweather/repository/CityRepository.java index fdfb35b..e5fd6a4 100644 --- a/app/src/main/java/fr/romanet/vj/apps/myweather/repository/CityRepository.java +++ b/app/src/main/java/fr/romanet/vj/apps/myweather/repository/CityRepository.java @@ -48,7 +48,10 @@ public final class CityRepository cityDatabase.cityDao().addCity(cityToAdd); } - public void updateCity(City cityToUpdate) {cityDatabase.cityDao().updateCity(cityToUpdate);} + public void updateCity(City cityToUpdate) + { + cityDatabase.cityDao().updateCity(cityToUpdate); + } public List sortCityByName() { diff --git a/app/src/main/res/layout/activity_temparature.xml b/app/src/main/res/layout/activity_temparature.xml index 7543830..bae8209 100644 --- a/app/src/main/res/layout/activity_temparature.xml +++ b/app/src/main/res/layout/activity_temparature.xml @@ -26,7 +26,7 @@ + app:layout_constraintTop_toBottomOf="@+id/img_weather" />