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:
parent
8eacdc4c01
commit
44b144fadf
@ -68,7 +68,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
protected void onResume()
|
protected void onResume()
|
||||||
{
|
{
|
||||||
super.onResume();
|
super.onResume();
|
||||||
//initList();
|
initList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initialisingComponents()
|
public void initialisingComponents()
|
||||||
@ -87,14 +87,16 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
public void initList()
|
public void initList()
|
||||||
{
|
{
|
||||||
final List<City> city = CityRepository.getInstance(this).getCity();
|
List<City> city = CityRepository.getInstance(this).getCity();
|
||||||
ArrayList<String> listeExample = new ArrayList<String>();
|
ArrayList<String> listeExample = new ArrayList<String>();
|
||||||
for(int i = 0; i < city.size(); i++)
|
for(int i = 0; i < city.size(); i++)
|
||||||
{
|
{
|
||||||
listeExample.add("" + city.get(i).nameCity);
|
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);
|
ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, listeExample);
|
||||||
listViewCityNames.setAdapter(adapter);
|
listViewCityNames.setAdapter(adapter);
|
||||||
|
Log.d("DEBUG1", "initList -> list size = " + Integer.toString(city.size()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void openAddCityPage()
|
public void openAddCityPage()
|
||||||
|
|||||||
@ -9,9 +9,11 @@ import android.os.Bundle;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
|
import android.widget.ImageView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.net.URI;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import fr.romanet.vj.apps.myweather.bo.City;
|
import fr.romanet.vj.apps.myweather.bo.City;
|
||||||
@ -27,7 +29,12 @@ import retrofit2.converter.gson.GsonConverterFactory;
|
|||||||
public class ShowTemparature extends AppCompatActivity {
|
public class ShowTemparature extends AppCompatActivity {
|
||||||
|
|
||||||
private String currentCityName;
|
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
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@ -43,7 +50,12 @@ public class ShowTemparature extends AppCompatActivity {
|
|||||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||||
getSupportActionBar().setDisplayShowHomeEnabled(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
|
// - Build Retrofit API tool
|
||||||
Retrofit retrofit = new Retrofit.Builder()
|
Retrofit retrofit = new Retrofit.Builder()
|
||||||
@ -51,6 +63,14 @@ public class ShowTemparature extends AppCompatActivity {
|
|||||||
.addConverterFactory(GsonConverterFactory.create())
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
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
|
// - Build Interface with Retrofit
|
||||||
JsonApiInterfce jsonApiInterfce = retrofit.create(JsonApiInterfce.class);
|
JsonApiInterfce jsonApiInterfce = retrofit.create(JsonApiInterfce.class);
|
||||||
Call<Weather> call = jsonApiInterfce.getPost(currentCityName, "metric", "0ac5802392867db8e1ae4f91529f7890");
|
Call<Weather> call = jsonApiInterfce.getPost(currentCityName, "metric", "0ac5802392867db8e1ae4f91529f7890");
|
||||||
@ -64,7 +84,7 @@ public class ShowTemparature extends AppCompatActivity {
|
|||||||
|
|
||||||
// - Check if that answer is not an error (like 500, 404, etc)
|
// - Check if that answer is not an error (like 500, 404, etc)
|
||||||
if(! response.isSuccessful()){
|
if(! response.isSuccessful()){
|
||||||
textView2.setText("Code: " + response.code());
|
tv_cityname.setText("Code: " + response.code());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,7 +92,7 @@ public class ShowTemparature extends AppCompatActivity {
|
|||||||
Weather weather = response.body();
|
Weather weather = response.body();
|
||||||
// textView2.append(weather.get_temps().get_temp_feels_like());
|
// textView2.append(weather.get_temps().get_temp_feels_like());
|
||||||
|
|
||||||
updateCity(weather);
|
updateCity(weather, time);
|
||||||
updateActivityObjectsFromDB();
|
updateActivityObjectsFromDB();
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -82,58 +102,17 @@ public class ShowTemparature extends AppCompatActivity {
|
|||||||
public void onFailure(Call<Weather> call, Throwable t) {
|
public void onFailure(Call<Weather> call, Throwable t) {
|
||||||
|
|
||||||
// - Display error message
|
// - Display error message
|
||||||
textView2.setText("Error: " + t.getMessage());
|
tv_cityname.setText("Error: " + t.getMessage());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}else{
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@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();
|
updateActivityObjectsFromDB();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// - If GET has no answer...
|
|
||||||
@Override
|
|
||||||
public void onFailure(Call<Weather> call, Throwable t) {
|
|
||||||
|
|
||||||
// - Display error message
|
|
||||||
textView2.setText("Error: " + t.getMessage());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
@ -167,28 +146,115 @@ public class ShowTemparature extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateCity(Weather weather){
|
public void updateCity(Weather weather, Long currentTimestamp){
|
||||||
City updated_city = new City(currentCityName);
|
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))
|
||||||
|
{
|
||||||
|
updated_city = city.get(i);
|
||||||
updated_city.setW_temp(weather.get_temps().get_temp());
|
updated_city.setW_temp(weather.get_temps().get_temp());
|
||||||
updated_city.setW_feelsliketemp(weather.get_temps().get_temp_feels_like());
|
updated_city.setW_feelsliketemp(weather.get_temps().get_temp_feels_like());
|
||||||
updated_city.setW_icon(weather.get_weatherDescription().get_icon());
|
updated_city.setW_icon(weather.get_weatherDescription().get_icon());
|
||||||
updated_city.setW_maxtemp(weather.get_temps().get_temp_max());
|
updated_city.setW_maxtemp(weather.get_temps().get_temp_max());
|
||||||
updated_city.setW_mintemp(weather.get_temps().get_temp_min());
|
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);
|
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(){
|
public void updateActivityObjectsFromDB(){
|
||||||
final List<City> city = CityRepository.getInstance(this).getCity();
|
final List<City> city = CityRepository.getInstance(this).getCity();
|
||||||
|
String cityname;
|
||||||
String temp;
|
String temp;
|
||||||
|
String feelslike;
|
||||||
|
String tempmin;
|
||||||
|
String tempmax;
|
||||||
|
String icon;
|
||||||
for(int i = 0; i < city.size(); i++)
|
for(int i = 0; i < city.size(); i++)
|
||||||
{
|
{
|
||||||
if(city.get(i).nameCity.equals(currentCityName))
|
if(city.get(i).nameCity.equals(currentCityName))
|
||||||
{
|
{
|
||||||
temp = city.get(i).nameCity;
|
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);
|
Log.d("DEBUG1", "---------------------------------------->" + temp);
|
||||||
textView2.setText(temp);
|
tv_cityname.setText(cityname);
|
||||||
CityRepository.getInstance(this).deleteCity(city.get(i));
|
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);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -59,6 +59,9 @@ final public class City implements Serializable
|
|||||||
@Nullable
|
@Nullable
|
||||||
public String w_maxtemp = null;
|
public String w_maxtemp = null;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public long last_fetch = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor of the city class
|
* Constructor of the city class
|
||||||
* @param nameCity The name of the new city
|
* @param nameCity The name of the new city
|
||||||
@ -89,6 +92,11 @@ final public class City implements Serializable
|
|||||||
this.w_maxtemp = w_maxtemp;
|
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
|
* 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
|
* @param o The object to compare
|
||||||
|
|||||||
@ -48,7 +48,10 @@ public final class CityRepository
|
|||||||
cityDatabase.cityDao().addCity(cityToAdd);
|
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()
|
public List<City> sortCityByName()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -26,7 +26,7 @@
|
|||||||
</com.google.android.material.appbar.AppBarLayout>
|
</com.google.android.material.appbar.AppBarLayout>
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/textView2"
|
android:id="@+id/tv_cityname"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginTop="56dp"
|
android:layout_marginTop="56dp"
|
||||||
@ -39,13 +39,13 @@
|
|||||||
app:layout_constraintTop_toBottomOf="@+id/appBarLayout2" />
|
app:layout_constraintTop_toBottomOf="@+id/appBarLayout2" />
|
||||||
|
|
||||||
<ImageView
|
<ImageView
|
||||||
android:id="@+id/imageView2"
|
android:id="@+id/img_weather"
|
||||||
android:layout_width="167dp"
|
android:layout_width="167dp"
|
||||||
android:layout_height="100dp"
|
android:layout_height="100dp"
|
||||||
android:layout_marginTop="60dp"
|
android:layout_marginTop="60dp"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="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" />
|
app:srcCompat="@drawable/ic_baseline_umbrella_24" />
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
@ -57,7 +57,7 @@
|
|||||||
android:textSize="36sp"
|
android:textSize="36sp"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@+id/imageView2" />
|
app:layout_constraintTop_toBottomOf="@+id/img_weather" />
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/tv_feelslike"
|
android:id="@+id/tv_feelslike"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user