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.app.AppCompatActivity;
|
||||||
import androidx.appcompat.widget.Toolbar;
|
import androidx.appcompat.widget.Toolbar;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
|
||||||
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 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;
|
||||||
|
|
||||||
public class AddCity extends AppCompatActivity {
|
public class AddCity extends AppCompatActivity {
|
||||||
|
|
||||||
private EditText cityName;
|
private EditText cityName;
|
||||||
|
private Toolbar toolbarAddCity;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState)
|
protected void onCreate(Bundle savedInstanceState)
|
||||||
@ -23,9 +25,10 @@ public class AddCity extends AppCompatActivity {
|
|||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.add_city_page);
|
setContentView(R.layout.add_city_page);
|
||||||
|
|
||||||
Toolbar toolbarAddCity = (Toolbar) findViewById(R.id.toolbarAddCity);
|
toolbarAddCity = (Toolbar) findViewById(R.id.toolbarAddCity);
|
||||||
setSupportActionBar(toolbarAddCity);
|
setSupportActionBar(toolbarAddCity);
|
||||||
|
|
||||||
|
//Ading the arrow back functionnality in the toolbar
|
||||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||||
getSupportActionBar().setDisplayShowHomeEnabled(true);
|
getSupportActionBar().setDisplayShowHomeEnabled(true);
|
||||||
|
|
||||||
@ -34,32 +37,59 @@ 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
|
||||||
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);
|
||||||
if(newCityString.isEmpty())
|
if(newCityString.isEmpty())
|
||||||
{
|
{
|
||||||
cityName.setError("Please, enter a city name");
|
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 {
|
else {
|
||||||
saveCity(newCityString);
|
saveCity(newCityString);
|
||||||
resetForm();
|
resetForm();
|
||||||
openAddCityPage();
|
openMainActivity();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Method that reset the form to a blank editable text
|
||||||
private void resetForm()
|
private void resetForm()
|
||||||
{
|
{
|
||||||
cityName.setText(null);
|
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)
|
private void saveCity(String cityName)
|
||||||
{
|
{
|
||||||
CityRepository.getInstance(this).addCity(new City(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);
|
Intent intent = new Intent(AddCity.this, MainActivity.class);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
|
|||||||
@ -24,9 +24,12 @@ final public class City implements Serializable
|
|||||||
@Override
|
@Override
|
||||||
public int compare(City o1, City o2)
|
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