Load all UI info from DB

Update DB from API corrected
Added "lastfetch" attribute into local DB (last fetch from API)
This commit is contained in:
valentin 2020-11-15 22:03:21 +01:00
parent 8eacdc4c01
commit 44b144fadf
5 changed files with 178 additions and 99 deletions

View File

@ -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> city = CityRepository.getInstance(this).getCity();
List<City> city = CityRepository.getInstance(this).getCity();
ArrayList<String> listeExample = new ArrayList<String>();
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<String> adapter = new ArrayAdapter<String>( 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()

View File

@ -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<Weather> call = jsonApiInterfce.getPost(currentCityName, "metric", "0ac5802392867db8e1ae4f91529f7890");
Date date= new Date();
long time = date.getTime();
final List<City> 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<Weather> 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<Weather>() {
// - 'Execute' the GET query with 'enqueue' method, Retrofit will create an AsyncTask that avoids UI Thread freeze Exception.
call.enqueue(new Callback<Weather>() {
// - If GET has an answer...
@Override
public void onResponse(Call<Weather> call, Response<Weather> response) {
// - If GET has an answer...
@Override
public void onResponse(Call<Weather> call, Response<Weather> 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<Weather> call, Throwable t) {
// - Display error message
tv_cityname.setText("Error: " + t.getMessage());
}
});
}else{
updateActivityObjectsFromDB();
}
}
// - If GET has no answer...
@Override
public void onFailure(Call<Weather> 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<Weather> 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<Weather>() {
// - If GET has an answer...
@Override
public void onResponse(Call<Weather> call, Response<Weather> 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<Weather> 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> city = CityRepository.getInstance(this).getCity();
String temp;
public void updateCity(Weather weather, Long currentTimestamp){
List<City> 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> 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);
}
}
}

View File

@ -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

View File

@ -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<City> sortCityByName()
{

View File

@ -26,7 +26,7 @@
</com.google.android.material.appbar.AppBarLayout>
<TextView
android:id="@+id/textView2"
android:id="@+id/tv_cityname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
@ -39,13 +39,13 @@
app:layout_constraintTop_toBottomOf="@+id/appBarLayout2" />
<ImageView
android:id="@+id/imageView2"
android:id="@+id/img_weather"
android:layout_width="167dp"
android:layout_height="100dp"
android:layout_marginTop="60dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/tv_cityname"
app:srcCompat="@drawable/ic_baseline_umbrella_24" />
<TextView
@ -57,7 +57,7 @@
android:textSize="36sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2" />
app:layout_constraintTop_toBottomOf="@+id/img_weather" />
<TextView
android:id="@+id/tv_feelslike"