diff --git a/app/src/main/java/fr/romanet/vj/apps/myweather/AddCity.java b/app/src/main/java/fr/romanet/vj/apps/myweather/AddCity.java index 7df5ab3..4e63735 100644 --- a/app/src/main/java/fr/romanet/vj/apps/myweather/AddCity.java +++ b/app/src/main/java/fr/romanet/vj/apps/myweather/AddCity.java @@ -2,18 +2,16 @@ package fr.romanet.vj.apps.myweather; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; - import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; - import java.util.List; - import fr.romanet.vj.apps.myweather.bo.City; 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 { private EditText cityName; @@ -37,7 +35,10 @@ public class AddCity extends AppCompatActivity { { 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); final String newCityString = cityName.getEditableText().toString(); boolean exist = existOrNot(newCityString); @@ -65,15 +66,13 @@ public class AddCity extends AppCompatActivity { cityName.setText(null); } - /** - * Method that allows us to save a city into the database by calling a method from the repository - * @param cityName : the city to save - */ + // Method that allows us to save a city into the database by calling a method from the repository private void saveCity(String 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) { boolean exist = false; 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 ecc524e..01fbf6b 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 @@ -2,29 +2,21 @@ package fr.romanet.vj.apps.myweather; import android.content.*; import androidx.appcompat.app.AppCompatActivity; -import androidx.recyclerview.widget.DividerItemDecoration; -import androidx.recyclerview.widget.RecyclerView; - import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; -import android.widget.EditText; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; - import com.google.android.material.floatingactionbutton.FloatingActionButton; - -import org.w3c.dom.Text; - import java.util.ArrayList; import java.util.List; - import fr.romanet.vj.apps.myweather.bo.City; 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 { private ListView listViewCityNames; @@ -43,8 +35,13 @@ public class MainActivity extends AppCompatActivity { imgPara = (ImageView) findViewById(R.id.imageView); 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(); + /* + Getting to the show temperature screen while the user click on city present in the list + */ listViewCityNames.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override 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() { @Override 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 protected void onResume() { @@ -71,6 +75,8 @@ public class MainActivity extends AppCompatActivity { 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() { final List city = CityRepository.getInstance(this).getCity(); @@ -85,6 +91,9 @@ public class MainActivity extends AppCompatActivity { initList(); } + /* + Just updating the listView by fetching all the elements of the database in it + */ public void initList() { List city = CityRepository.getInstance(this).getCity(); @@ -99,6 +108,9 @@ public class MainActivity extends AppCompatActivity { 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() { Intent intent = new Intent(MainActivity.this, AddCity.class); 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 c9072b7..b98babb 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 @@ -12,7 +12,6 @@ import android.view.MenuItem; import android.widget.ImageView; import android.widget.TextView; -import java.net.URI; import java.util.Date; 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 public boolean onCreateOptionsMenu(Menu 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 public boolean onOptionsItemSelected(@NonNull MenuItem item) { - //We handle the click on a menu item if (item.getItemId() == R.id.item_delete) { cityToDelete(); @@ -134,6 +139,10 @@ public class ShowTemparature extends AppCompatActivity { 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() { final List 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){ List city = CityRepository.getInstance(this).getCity(); City updated_city; 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 faf196c..0359138 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 @@ -44,18 +44,33 @@ final public class City implements Serializable @NonNull public String nameCity; + /** + * The icon of the city + */ @Nullable public String w_icon = null; + /** + * The temperature of the city + */ @Nullable public String w_temp = null; + /** + * The feels like temperature of the city + */ @Nullable public String w_feelsliketemp = null; + /** + * The minimum temperature of the city + */ @Nullable public String w_mintemp = null; + /** + * The maximum temperature of the city + */ @Nullable public String w_maxtemp = null; @@ -72,6 +87,7 @@ final public class City implements Serializable this.nameCity = nameCity; } + //All the setters in order to update previous attributes public void setW_icon(@Nullable String w_icon) { this.w_icon = w_icon; } diff --git a/app/src/main/java/fr/romanet/vj/apps/myweather/dao/CityDao.java b/app/src/main/java/fr/romanet/vj/apps/myweather/dao/CityDao.java index ddb53d7..08c1181 100644 --- a/app/src/main/java/fr/romanet/vj/apps/myweather/dao/CityDao.java +++ b/app/src/main/java/fr/romanet/vj/apps/myweather/dao/CityDao.java @@ -14,21 +14,41 @@ import java.util.List; @Dao 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 @Query("SELECT * FROM City") List 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 @Delete 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 @Insert(onConflict = OnConflictStrategy.REPLACE) void addCity(City cityToAdd); + /** + * Query that allows us to update a city + * @param cityToUpdate The city that we want to update + */ @Update void updateCity(City cityToUpdate); + /** + * Select query that will sort the cities by name (not really used in the project) + * @return + */ @Override @Query("SELECT * FROM City ORDER BY nameCity DESC") List sortCityByName(); diff --git a/app/src/main/java/fr/romanet/vj/apps/myweather/network/JsonApiInterfce.java b/app/src/main/java/fr/romanet/vj/apps/myweather/network/JsonApiInterfce.java index 246ce12..257e8f5 100644 --- a/app/src/main/java/fr/romanet/vj/apps/myweather/network/JsonApiInterfce.java +++ b/app/src/main/java/fr/romanet/vj/apps/myweather/network/JsonApiInterfce.java @@ -3,9 +3,9 @@ package fr.romanet.vj.apps.myweather.network; import fr.romanet.vj.apps.myweather.weather.Weather; import retrofit2.Call; import retrofit2.http.GET; -import retrofit2.http.Path; 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 { @GET("weather") 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 e5fd6a4..ff3aa21 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 @@ -6,6 +6,7 @@ import java.util.List; import fr.romanet.vj.apps.myweather.bo.City; import fr.romanet.vj.apps.myweather.database.CityDatabase; +//CityRepository class that calls the Queries from the dao class public final class CityRepository { private static volatile CityRepository instance;