Making sure a city can not be insert 2 times
This commit is contained in:
parent
01572fb0c2
commit
8eacdc4c01
@ -3,19 +3,21 @@ package fr.romanet.vj.apps.myweather;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
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;
|
||||
|
||||
public class AddCity extends AppCompatActivity {
|
||||
|
||||
private EditText cityName;
|
||||
private Toolbar toolbarAddCity;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState)
|
||||
@ -23,9 +25,10 @@ public class AddCity extends AppCompatActivity {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.add_city_page);
|
||||
|
||||
Toolbar toolbarAddCity = (Toolbar) findViewById(R.id.toolbarAddCity);
|
||||
toolbarAddCity = (Toolbar) findViewById(R.id.toolbarAddCity);
|
||||
setSupportActionBar(toolbarAddCity);
|
||||
|
||||
//Ading the arrow back functionnality in the toolbar
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
getSupportActionBar().setDisplayShowHomeEnabled(true);
|
||||
|
||||
@ -34,32 +37,59 @@ 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
|
||||
cityName = (EditText) findViewById(R.id.editTextNewCity);
|
||||
final String newCityString = cityName.getEditableText().toString();
|
||||
boolean exist = existOrNot(newCityString);
|
||||
if(newCityString.isEmpty())
|
||||
{
|
||||
cityName.setError("Please, enter a city name");
|
||||
}
|
||||
else if(exist)
|
||||
{
|
||||
cityName.setError("Please, enter a city name that is not in your favorite");
|
||||
resetForm();
|
||||
}
|
||||
else {
|
||||
saveCity(newCityString);
|
||||
resetForm();
|
||||
openAddCityPage();
|
||||
openMainActivity();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//Method that reset the form to a blank editable text
|
||||
private void resetForm()
|
||||
{
|
||||
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
|
||||
*/
|
||||
private void saveCity(String cityName)
|
||||
{
|
||||
CityRepository.getInstance(this).addCity(new City(cityName));
|
||||
}
|
||||
|
||||
public void openAddCityPage()
|
||||
public boolean existOrNot(String nameOfCity)
|
||||
{
|
||||
boolean exist = false;
|
||||
final List<City> city = CityRepository.getInstance(this).getCity();
|
||||
for(int i = 0; i < city.size(); i++)
|
||||
{
|
||||
if(city.get(i).nameCity.equals(nameOfCity))
|
||||
{
|
||||
exist = true;
|
||||
}
|
||||
}
|
||||
return exist;
|
||||
}
|
||||
|
||||
//Calling the main activity screen
|
||||
public void openMainActivity()
|
||||
{
|
||||
Intent intent = new Intent(AddCity.this, MainActivity.class);
|
||||
startActivity(intent);
|
||||
|
||||
@ -24,9 +24,12 @@ final public class City implements Serializable
|
||||
@Override
|
||||
public int compare(City o1, City o2)
|
||||
{
|
||||
return o1.nameCity.compareTo(o2.nameCity);
|
||||
if(o1.id == o2.id)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
Reference in New Issue
Block a user