Modify the files to add a property in the database
This commit is contained in:
parent
2b51b3751d
commit
e6a1d6616c
@ -34,7 +34,6 @@
|
||||
|
||||
<activity
|
||||
android:name=".view.AddPropertyActivity"
|
||||
android:parentActivityName=".view.PropertiesActivity"
|
||||
android:theme="@style/Theme.MyRealEstateAgency"></activity>
|
||||
|
||||
<activity android:name=".view.MapsActivity"></activity>
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency.dao;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.room.Dao;
|
||||
import androidx.room.Insert;
|
||||
import androidx.room.Query;
|
||||
@ -12,7 +13,7 @@ import fr.romanet.vj.apps.myrealestateagency.entities.Property;
|
||||
@Dao
|
||||
public interface PropertyDao {
|
||||
@Query("SELECT * FROM property")
|
||||
List<Property> getPropertyList();
|
||||
LiveData<List<Property>> getPropertyList();
|
||||
|
||||
@Insert
|
||||
void insertProperty(Property property);
|
||||
|
||||
@ -45,7 +45,7 @@ public class Property {
|
||||
@ColumnInfo(name = "belongs_agency_id", index = true)
|
||||
public int belongsToAgencyId;
|
||||
|
||||
public Property(String description, String address, double longitude, double latitude, int belongsToAgencyId)
|
||||
public Property(String description, String address, double longitude, double latitude, int belongsToAgencyId, PropertyStatue propertyStatue, PropertyType propertyType)
|
||||
{
|
||||
propertyId = 0;
|
||||
this.description = description;
|
||||
@ -53,6 +53,8 @@ public class Property {
|
||||
this.longitude = longitude;
|
||||
this.latitude = latitude;
|
||||
this.belongsToAgencyId = belongsToAgencyId;
|
||||
this.propertyStatue = propertyStatue;
|
||||
this.propertyType = propertyType;
|
||||
}
|
||||
|
||||
public static class Converters {
|
||||
@ -70,9 +72,10 @@ public class Property {
|
||||
|
||||
public static Property[] populatePropertyTable() {
|
||||
return new Property[]{
|
||||
new Property("Amazing property in the best district of Paris", "15 rue Champs-Elysée", 15.8, 17.8, 1),
|
||||
new Property("Best appartment in Velizy-Villacoublay", "Rue de Villacoublay, 78140 Vélizy-Villacoublay", 19.8, 20.8, 1),
|
||||
new Property("Amazing property in the best district of Marseille", "15 rue de Marseille", 15.8, 17.8, 1),
|
||||
new Property("Amazing property in the best district of Paris", "15 rue Champs-Elysée", 15.8, 17.8, 1,
|
||||
new PropertyStatue(false, null, 15.8), new PropertyType("Appartment", 3, 9.8))
|
||||
//new Property("Best appartment in Velizy-Villacoublay", "Rue de Villacoublay, 78140 Vélizy-Villacoublay", 19.8, 20.8, 1),
|
||||
//new Property("Amazing property in the best district of Marseille", "15 rue de Marseille", 15.8, 17.8, 1),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,4 +17,11 @@ public class PropertyStatue{
|
||||
|
||||
@ColumnInfo(name = "price")
|
||||
public double price;
|
||||
|
||||
public PropertyStatue(boolean statueSale, Date soldDate, double price)
|
||||
{
|
||||
this.statueSale = statueSale;
|
||||
this.soldDate = soldDate;
|
||||
this.price = price;
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,6 +11,13 @@ public class PropertyType {
|
||||
public int numberRooms;
|
||||
|
||||
@ColumnInfo(name = "surface_area")
|
||||
public float surfaceArea;
|
||||
public double surfaceArea;
|
||||
|
||||
public PropertyType(String typeDescription, int numberRooms, double surfaceArea)
|
||||
{
|
||||
this.typeDescription = typeDescription;
|
||||
this.numberRooms = numberRooms;
|
||||
this.surfaceArea = surfaceArea;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,32 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency.repository;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import fr.romanet.vj.apps.myrealestateagency.dao.PropertyDao;
|
||||
import fr.romanet.vj.apps.myrealestateagency.database.RealEstateAgencyDatabase;
|
||||
import fr.romanet.vj.apps.myrealestateagency.entities.Property;
|
||||
|
||||
public class PropertyRepository {
|
||||
|
||||
public PropertyDao propertyDao;
|
||||
public LiveData<List<Property>> allProperties;
|
||||
|
||||
public PropertyRepository(Application application) {
|
||||
RealEstateAgencyDatabase realEstateAgencyDatabase = RealEstateAgencyDatabase.getInstance(application);
|
||||
propertyDao = realEstateAgencyDatabase.propertyDao();
|
||||
allProperties = propertyDao.getPropertyList();
|
||||
}
|
||||
|
||||
public LiveData<List<Property>> getAllProperties() {
|
||||
return allProperties;
|
||||
}
|
||||
|
||||
public void insertProperty(Property property)
|
||||
{
|
||||
propertyDao.insertProperty(property);
|
||||
}
|
||||
}
|
||||
@ -1,15 +1,115 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency.view;
|
||||
|
||||
import android.os.Bundle;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import fr.romanet.vj.apps.myrealestateagency.R;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import fr.romanet.vj.apps.myrealestateagency.R;
|
||||
import fr.romanet.vj.apps.myrealestateagency.entities.Agency;
|
||||
import fr.romanet.vj.apps.myrealestateagency.viewmodel.AddPropertyActivityViewModel;
|
||||
import fr.romanet.vj.apps.myrealestateagency.viewmodel.AddPropertyActivityViewModel.Event;
|
||||
import fr.romanet.vj.apps.myrealestateagency.viewmodel.AddPropertyActivityViewModelFactory;
|
||||
import fr.romanet.vj.apps.myrealestateagency.viewmodel.PropertiesActivityViewModelFactory;
|
||||
|
||||
public class AddPropertyActivity extends AppCompatActivity implements OnClickListener {
|
||||
|
||||
public static final String AGENCY_EXTRA = "agencyExtra";
|
||||
|
||||
private Agency agency;
|
||||
|
||||
private EditText address;
|
||||
|
||||
private EditText type;
|
||||
|
||||
private EditText numberRooms;
|
||||
|
||||
private EditText price;
|
||||
|
||||
private EditText surface;
|
||||
|
||||
private EditText description;
|
||||
|
||||
private AddPropertyActivityViewModel viewModel;
|
||||
|
||||
public class AddPropertyActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState)
|
||||
{
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_add_property);
|
||||
|
||||
address = findViewById(R.id.addPropertyAddress);
|
||||
type = findViewById(R.id.addPropertyType);
|
||||
numberRooms = findViewById(R.id.addPropertyNumberOfRooms);
|
||||
price = findViewById(R.id.addPropertyPrice);
|
||||
surface = findViewById(R.id.addPropertyPrice);
|
||||
description = findViewById(R.id.addPropertyDescription);
|
||||
|
||||
findViewById(R.id.addPropertyButton).setOnClickListener(this);
|
||||
agency = (Agency) getIntent().getSerializableExtra("agencyExtra");
|
||||
viewModel = new ViewModelProvider(this, new AddPropertyActivityViewModelFactory(this.getApplication(), agency)).get(AddPropertyActivityViewModel.class);
|
||||
|
||||
observeEvent();
|
||||
}
|
||||
|
||||
private void observeEvent()
|
||||
{
|
||||
viewModel.event.observe(this, new Observer<Event>()
|
||||
{
|
||||
@Override
|
||||
public void onChanged(Event event)
|
||||
{
|
||||
if (event == Event.ResetForm)
|
||||
{
|
||||
resetForm();
|
||||
}
|
||||
else if (event == Event.DisplayError)
|
||||
{
|
||||
displayError();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void displayError()
|
||||
{
|
||||
Toast.makeText(this, R.string.cannot_add_property, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
private void resetForm()
|
||||
{
|
||||
address.setText(null);
|
||||
type.setText(null);
|
||||
numberRooms.setText(null);
|
||||
price.setText(null);
|
||||
surface.setText(null);
|
||||
description.setText(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v)
|
||||
{
|
||||
final String propertyAddress = address.getEditableText().toString();
|
||||
final String propertyType = type.getEditableText().toString();
|
||||
final int propertyNumberRooms = Integer.parseInt(numberRooms.getEditableText().toString());
|
||||
final double propertyPrice = Double.parseDouble(price.getEditableText().toString());
|
||||
final double propertySurface = Double.parseDouble(numberRooms.getEditableText().toString());
|
||||
final String propertyDescription = price.getEditableText().toString();
|
||||
|
||||
try {
|
||||
viewModel.saveProperty(propertyAddress, propertyType, propertyNumberRooms, propertyPrice, propertySurface, propertyDescription);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -62,13 +62,27 @@ public class PropertiesActivity extends AppCompatActivity implements OnClickList
|
||||
protected void onResume()
|
||||
{
|
||||
super.onResume();
|
||||
viewModel.loadProperties(agency);
|
||||
viewModel = new ViewModelProvider(this, new PropertiesActivityViewModelFactory(this.getApplication(), agency)).get(PropertiesActivityViewModel.class);
|
||||
observeProperties();
|
||||
}
|
||||
|
||||
/*@Override
|
||||
protected void onSaveInstanceState(Bundle savedInstanceState) {
|
||||
super.onSaveInstanceState(savedInstanceState);
|
||||
savedInstanceState.putSerializable(AGENCY_EXTRA, agency);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onRestoreInstanceState(Bundle savedInstanceState) {
|
||||
super.onRestoreInstanceState(savedInstanceState);
|
||||
agency = (Agency) savedInstanceState.getSerializable(AGENCY_EXTRA);
|
||||
}*/
|
||||
|
||||
@Override
|
||||
public void onClick(View v)
|
||||
{
|
||||
final Intent intent = new Intent(this, AddPropertyActivity.class);
|
||||
intent.putExtra(AddPropertyActivity.AGENCY_EXTRA, agency);
|
||||
startActivity(intent);
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,69 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency.viewmodel;
|
||||
|
||||
|
||||
import android.app.Application;
|
||||
import android.location.Address;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.AndroidViewModel;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import fr.romanet.vj.apps.myrealestateagency.MyApp;
|
||||
import fr.romanet.vj.apps.myrealestateagency.entities.Agency;
|
||||
import fr.romanet.vj.apps.myrealestateagency.entities.Property;
|
||||
import fr.romanet.vj.apps.myrealestateagency.entities.PropertyStatue;
|
||||
import fr.romanet.vj.apps.myrealestateagency.entities.PropertyType;
|
||||
import fr.romanet.vj.apps.myrealestateagency.repository.MapsActivityRepository;
|
||||
import fr.romanet.vj.apps.myrealestateagency.repository.PropertyRepository;
|
||||
import fr.romanet.vj.apps.myrealestateagency.view.AddPropertyActivity;
|
||||
|
||||
public class AddPropertyActivityViewModel extends AndroidViewModel {
|
||||
|
||||
public enum Event
|
||||
{
|
||||
ResetForm, DisplayError
|
||||
}
|
||||
|
||||
public MutableLiveData<Event> event = new MutableLiveData<>();
|
||||
|
||||
private Agency agency;
|
||||
|
||||
public PropertyRepository propertyRepository;
|
||||
|
||||
public AddPropertyActivityViewModel(@NonNull Application application, Agency agency)
|
||||
{
|
||||
super(application);
|
||||
this.agency = agency;
|
||||
propertyRepository = new PropertyRepository(application);
|
||||
}
|
||||
|
||||
public void saveProperty(String propertyAddress, String propertyType, int propertyNumberRooms, double propertyPrice, double propertySurface, String propertyDescription) throws IOException {
|
||||
|
||||
final boolean canAddProperty = checkFormEntries(propertyAddress, propertyType, propertyNumberRooms, propertyPrice, propertySurface, propertyDescription);
|
||||
|
||||
if (canAddProperty == true)
|
||||
{
|
||||
Address address = MapsActivityRepository.get_lat_long_from_address(propertyAddress, MyApp.getContext());
|
||||
double latitude = address.getLatitude();
|
||||
double longitude = address.getLongitude();
|
||||
Property propertyToAdd = new Property(propertyDescription, propertyAddress, longitude, latitude, this.agency.getAgencyId(), new PropertyStatue(false, null, propertyPrice),
|
||||
new PropertyType(propertyType, propertyNumberRooms, propertySurface));
|
||||
propertyRepository.insertProperty(propertyToAdd);
|
||||
event.postValue(Event.ResetForm);
|
||||
}
|
||||
else
|
||||
{
|
||||
event.postValue(Event.DisplayError);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean checkFormEntries(String propertyAddress, String propertyType, int propertyNumberRooms, double propertyPrice, double propertySurface, String propertyDescription)
|
||||
{
|
||||
return TextUtils.isEmpty(propertyAddress) == false && TextUtils.isEmpty(propertyType) == false && TextUtils.isEmpty(propertyDescription) == false;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency.viewmodel;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import androidx.lifecycle.ViewModel;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import fr.romanet.vj.apps.myrealestateagency.entities.Agency;
|
||||
|
||||
public class AddPropertyActivityViewModelFactory implements ViewModelProvider.Factory {
|
||||
private Application mApplication;
|
||||
private Agency mParam;
|
||||
|
||||
|
||||
public AddPropertyActivityViewModelFactory(Application application, Agency param) {
|
||||
mApplication = application;
|
||||
mParam = param;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public <T extends ViewModel> T create(Class<T> modelClass) {
|
||||
return (T) new AddPropertyActivityViewModel(mApplication, mParam);
|
||||
}
|
||||
}
|
||||
@ -38,7 +38,7 @@
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="@string/Type"
|
||||
android:inputType="phone"
|
||||
android:inputType="textPersonName"
|
||||
app:layout_constraintBottom_toTopOf="@id/addPropertyNumberOfRooms"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
@ -50,7 +50,7 @@
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="@string/Number_of_room"
|
||||
android:inputType="textPostalAddress"
|
||||
android:inputType="phone"
|
||||
app:layout_constraintBottom_toTopOf="@id/addPropertyPrice"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
@ -62,13 +62,25 @@
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="@string/price"
|
||||
android:inputType="textPostalAddress"
|
||||
app:layout_constraintBottom_toTopOf="@id/addPropertyDescription"
|
||||
android:inputType="textPersonName"
|
||||
app:layout_constraintBottom_toTopOf="@id/addPropertySurface"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/addPropertyNumberOfRooms"
|
||||
/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/addPropertySurface"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="@string/surface"
|
||||
android:inputType="textPersonName"
|
||||
app:layout_constraintBottom_toTopOf="@id/addPropertyDescription"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/addPropertyPrice"
|
||||
/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/addPropertyDescription"
|
||||
android:layout_width="0dp"
|
||||
@ -77,14 +89,14 @@
|
||||
android:inputType="textMultiLine"
|
||||
android:minLines="4"
|
||||
android:lines="4"
|
||||
app:layout_constraintBottom_toTopOf="@id/save"
|
||||
app:layout_constraintBottom_toTopOf="@id/addPropertyButton"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/addPropertyPrice"
|
||||
/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/save"
|
||||
android:id="@+id/addPropertyButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:backgroundTint="@color/purple_700"
|
||||
|
||||
@ -10,5 +10,7 @@
|
||||
<string name="edit">Edit</string>
|
||||
<string name="delete">Delete</string>
|
||||
<string name="map">Map</string>
|
||||
<string name="surface">Surface</string>
|
||||
<string name="cannot_add_property">Cannot add the property</string>
|
||||
<string name="converter">Convert</string>
|
||||
</resources>
|
||||
Loading…
Reference in New Issue
Block a user