Just commenting the code
This commit is contained in:
parent
44b144fadf
commit
2e5deb47e2
@ -2,18 +2,16 @@ package fr.romanet.vj.apps.myweather;
|
|||||||
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.appcompat.widget.Toolbar;
|
import androidx.appcompat.widget.Toolbar;
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import fr.romanet.vj.apps.myweather.bo.City;
|
import fr.romanet.vj.apps.myweather.bo.City;
|
||||||
import fr.romanet.vj.apps.myweather.repository.CityRepository;
|
import fr.romanet.vj.apps.myweather.repository.CityRepository;
|
||||||
|
|
||||||
|
//Class that allow us to treat everything that happens to add a city into the favourites
|
||||||
public class AddCity extends AppCompatActivity {
|
public class AddCity extends AppCompatActivity {
|
||||||
|
|
||||||
private EditText cityName;
|
private EditText cityName;
|
||||||
@ -37,7 +35,10 @@ public class AddCity extends AppCompatActivity {
|
|||||||
{
|
{
|
||||||
public void onClick(View v)
|
public void onClick(View v)
|
||||||
{
|
{
|
||||||
//When the save button is clicked, we display an error message if the editable text is empty or, we save the data into the database
|
/*When the save button is clicked, we display an error message if the editable text is empty or, we save the
|
||||||
|
data into the database. Also, if the city written by the user is already in the database,
|
||||||
|
we don't allow him to record this new city*/
|
||||||
|
|
||||||
cityName = (EditText) findViewById(R.id.editTextNewCity);
|
cityName = (EditText) findViewById(R.id.editTextNewCity);
|
||||||
final String newCityString = cityName.getEditableText().toString();
|
final String newCityString = cityName.getEditableText().toString();
|
||||||
boolean exist = existOrNot(newCityString);
|
boolean exist = existOrNot(newCityString);
|
||||||
@ -65,15 +66,13 @@ public class AddCity extends AppCompatActivity {
|
|||||||
cityName.setText(null);
|
cityName.setText(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// Method that allows us to save a city into the database by calling a method from the repository
|
||||||
* Method that allows us to save a city into the database by calling a method from the repository
|
|
||||||
* @param cityName : the city to save
|
|
||||||
*/
|
|
||||||
private void saveCity(String cityName)
|
private void saveCity(String cityName)
|
||||||
{
|
{
|
||||||
CityRepository.getInstance(this).addCity(new City(cityName));
|
CityRepository.getInstance(this).addCity(new City(cityName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Method that evaluate the existence of the input city (by the user) in the database. Return true if exist and false if not
|
||||||
public boolean existOrNot(String nameOfCity)
|
public boolean existOrNot(String nameOfCity)
|
||||||
{
|
{
|
||||||
boolean exist = false;
|
boolean exist = false;
|
||||||
|
|||||||
@ -2,29 +2,21 @@ package fr.romanet.vj.apps.myweather;
|
|||||||
|
|
||||||
import android.content.*;
|
import android.content.*;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.recyclerview.widget.DividerItemDecoration;
|
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.AdapterView;
|
import android.widget.AdapterView;
|
||||||
import android.widget.ArrayAdapter;
|
import android.widget.ArrayAdapter;
|
||||||
import android.widget.EditText;
|
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.ListView;
|
import android.widget.ListView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||||
|
|
||||||
import org.w3c.dom.Text;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import fr.romanet.vj.apps.myweather.bo.City;
|
import fr.romanet.vj.apps.myweather.bo.City;
|
||||||
import fr.romanet.vj.apps.myweather.repository.CityRepository;
|
import fr.romanet.vj.apps.myweather.repository.CityRepository;
|
||||||
|
|
||||||
|
//Class that allow us to treat everything that happens in the main screen (hiding the umbrella, showing the list....)
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private ListView listViewCityNames;
|
private ListView listViewCityNames;
|
||||||
@ -43,8 +35,13 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
imgPara = (ImageView) findViewById(R.id.imageView);
|
imgPara = (ImageView) findViewById(R.id.imageView);
|
||||||
addCityText = (TextView) findViewById(R.id.textViewAddCity);
|
addCityText = (TextView) findViewById(R.id.textViewAddCity);
|
||||||
|
|
||||||
|
/*We call this method to see from the begining if there are some data or not in the database so we can manage how we
|
||||||
|
will display this main screen on the first time that the user launch the application*/
|
||||||
initialisingComponents();
|
initialisingComponents();
|
||||||
|
|
||||||
|
/*
|
||||||
|
Getting to the show temperature screen while the user click on city present in the list
|
||||||
|
*/
|
||||||
listViewCityNames.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
listViewCityNames.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
|
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
|
||||||
@ -55,6 +52,9 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
Else, if it is the button that is clicked on, we oppen the add city page
|
||||||
|
*/
|
||||||
buttonPlus.setOnClickListener(new View.OnClickListener() {
|
buttonPlus.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View view)
|
public void onClick(View view)
|
||||||
@ -64,6 +64,10 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Defining the onResume method, calling the initList method so we can update the list on the screen for the user every
|
||||||
|
time we go back to this main activity
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void onResume()
|
protected void onResume()
|
||||||
{
|
{
|
||||||
@ -71,6 +75,8 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
initList();
|
initList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*Looking if there are cities or not in the database in order to manage how we show the umbrella and text to the user
|
||||||
|
(if we have to hide them or not)*/
|
||||||
public void initialisingComponents()
|
public void initialisingComponents()
|
||||||
{
|
{
|
||||||
final List<City> city = CityRepository.getInstance(this).getCity();
|
final List<City> city = CityRepository.getInstance(this).getCity();
|
||||||
@ -85,6 +91,9 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
initList();
|
initList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Just updating the listView by fetching all the elements of the database in it
|
||||||
|
*/
|
||||||
public void initList()
|
public void initList()
|
||||||
{
|
{
|
||||||
List<City> city = CityRepository.getInstance(this).getCity();
|
List<City> city = CityRepository.getInstance(this).getCity();
|
||||||
@ -99,6 +108,9 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
Log.d("DEBUG1", "initList -> list size = " + Integer.toString(city.size()));
|
Log.d("DEBUG1", "initList -> list size = " + Integer.toString(city.size()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Function that allows us to go to the addCity activity when the plus button is clicked
|
||||||
|
*/
|
||||||
public void openAddCityPage()
|
public void openAddCityPage()
|
||||||
{
|
{
|
||||||
Intent intent = new Intent(MainActivity.this, AddCity.class);
|
Intent intent = new Intent(MainActivity.this, AddCity.class);
|
||||||
|
|||||||
@ -12,7 +12,6 @@ import android.view.MenuItem;
|
|||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -113,7 +112,10 @@ public class ShowTemparature extends AppCompatActivity {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Function that allows us to get the 3 dots point on the right of the toolbar by calling the menu_items component created
|
||||||
|
on the xml file (on purpose to delete a city)
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
getMenuInflater().inflate(R.menu.menu_items, menu);
|
getMenuInflater().inflate(R.menu.menu_items, menu);
|
||||||
@ -121,10 +123,13 @@ public class ShowTemparature extends AppCompatActivity {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
We handle the click on the menu item so we call the function cityToDelete in order to delete a city and to go back
|
||||||
|
to the main activity
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(@NonNull MenuItem item)
|
public boolean onOptionsItemSelected(@NonNull MenuItem item)
|
||||||
{
|
{
|
||||||
//We handle the click on a menu item
|
|
||||||
if (item.getItemId() == R.id.item_delete)
|
if (item.getItemId() == R.id.item_delete)
|
||||||
{
|
{
|
||||||
cityToDelete();
|
cityToDelete();
|
||||||
@ -134,6 +139,10 @@ public class ShowTemparature extends AppCompatActivity {
|
|||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Function cityToDelete that goes throught the database in order to find the element that has the exact same name
|
||||||
|
of the current city that we want to delete. When finded, we delete it from the database
|
||||||
|
*/
|
||||||
public void cityToDelete()
|
public void cityToDelete()
|
||||||
{
|
{
|
||||||
final List<City> city = CityRepository.getInstance(this).getCity();
|
final List<City> city = CityRepository.getInstance(this).getCity();
|
||||||
@ -146,6 +155,17 @@ public class ShowTemparature extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Function cityToDelete that goes throught the database in order to find the element that has the exact same name
|
||||||
|
of the current city that we want to update. In order to do that, we update the object City in this java code
|
||||||
|
by calling all the setters and then we will be able to update it in the database by calling the repository's update
|
||||||
|
method. The way of managing the object is the following one :
|
||||||
|
- The user create a city --> we create a city only with the name in input (it means, only the name is a non null
|
||||||
|
attribute)
|
||||||
|
- Then, when the user wants to display the features of a city --> we use the GET method and update all the attributes
|
||||||
|
of this same object.
|
||||||
|
By doing that, we can respect the business rule defined in the subject of this assignement
|
||||||
|
*/
|
||||||
public void updateCity(Weather weather, Long currentTimestamp){
|
public void updateCity(Weather weather, Long currentTimestamp){
|
||||||
List<City> city = CityRepository.getInstance(this).getCity();
|
List<City> city = CityRepository.getInstance(this).getCity();
|
||||||
City updated_city;
|
City updated_city;
|
||||||
|
|||||||
@ -44,18 +44,33 @@ final public class City implements Serializable
|
|||||||
@NonNull
|
@NonNull
|
||||||
public String nameCity;
|
public String nameCity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The icon of the city
|
||||||
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public String w_icon = null;
|
public String w_icon = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The temperature of the city
|
||||||
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public String w_temp = null;
|
public String w_temp = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The feels like temperature of the city
|
||||||
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public String w_feelsliketemp = null;
|
public String w_feelsliketemp = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The minimum temperature of the city
|
||||||
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public String w_mintemp = null;
|
public String w_mintemp = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The maximum temperature of the city
|
||||||
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public String w_maxtemp = null;
|
public String w_maxtemp = null;
|
||||||
|
|
||||||
@ -72,6 +87,7 @@ final public class City implements Serializable
|
|||||||
this.nameCity = nameCity;
|
this.nameCity = nameCity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//All the setters in order to update previous attributes
|
||||||
public void setW_icon(@Nullable String w_icon) {
|
public void setW_icon(@Nullable String w_icon) {
|
||||||
this.w_icon = w_icon;
|
this.w_icon = w_icon;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,21 +14,41 @@ import java.util.List;
|
|||||||
@Dao
|
@Dao
|
||||||
public interface CityDao extends ICityService
|
public interface CityDao extends ICityService
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Query that fetch all the data in our database (only one table which is City)
|
||||||
|
* @return A list of all the cities
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
@Query("SELECT * FROM City")
|
@Query("SELECT * FROM City")
|
||||||
List<City> getCity();
|
List<City> getCity();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Query that allows us to delete a city from the database
|
||||||
|
* @param cityToDelete the city that we are looking for to be deleted
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
@Delete
|
@Delete
|
||||||
void deleteCity(City cityToDelete);
|
void deleteCity(City cityToDelete);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Query that allows us to add a city in the database
|
||||||
|
* @param cityToAdd the city that we want to add
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
void addCity(City cityToAdd);
|
void addCity(City cityToAdd);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Query that allows us to update a city
|
||||||
|
* @param cityToUpdate The city that we want to update
|
||||||
|
*/
|
||||||
@Update
|
@Update
|
||||||
void updateCity(City cityToUpdate);
|
void updateCity(City cityToUpdate);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Select query that will sort the cities by name (not really used in the project)
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
@Query("SELECT * FROM City ORDER BY nameCity DESC")
|
@Query("SELECT * FROM City ORDER BY nameCity DESC")
|
||||||
List<City> sortCityByName();
|
List<City> sortCityByName();
|
||||||
|
|||||||
@ -3,9 +3,9 @@ package fr.romanet.vj.apps.myweather.network;
|
|||||||
import fr.romanet.vj.apps.myweather.weather.Weather;
|
import fr.romanet.vj.apps.myweather.weather.Weather;
|
||||||
import retrofit2.Call;
|
import retrofit2.Call;
|
||||||
import retrofit2.http.GET;
|
import retrofit2.http.GET;
|
||||||
import retrofit2.http.Path;
|
|
||||||
import retrofit2.http.Query;
|
import retrofit2.http.Query;
|
||||||
|
|
||||||
|
//Public interface that definie the GET method to call the API by using our APIKey and the city that we are looking for
|
||||||
public interface JsonApiInterfce {
|
public interface JsonApiInterfce {
|
||||||
|
|
||||||
@GET("weather")
|
@GET("weather")
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import java.util.List;
|
|||||||
import fr.romanet.vj.apps.myweather.bo.City;
|
import fr.romanet.vj.apps.myweather.bo.City;
|
||||||
import fr.romanet.vj.apps.myweather.database.CityDatabase;
|
import fr.romanet.vj.apps.myweather.database.CityDatabase;
|
||||||
|
|
||||||
|
//CityRepository class that calls the Queries from the dao class
|
||||||
public final class CityRepository
|
public final class CityRepository
|
||||||
{
|
{
|
||||||
private static volatile CityRepository instance;
|
private static volatile CityRepository instance;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user