Retrofit API + Dollar Rate Objects 80% done
This commit is contained in:
parent
6c4d6c03c0
commit
7498a5ab5e
@ -44,5 +44,8 @@ dependencies {
|
||||
testImplementation 'junit:junit:4.+'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
|
||||
|
||||
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
|
||||
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,34 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class DollarCurrencyObj implements Serializable {
|
||||
|
||||
@SerializedName("rates")
|
||||
private DollarCurrencyRateObj dollarCurrencyRateObj;
|
||||
@SerializedName("date")
|
||||
private String fetchDate;
|
||||
|
||||
public DollarCurrencyObj(DollarCurrencyRateObj dollarCurrencyRateObj, String fetchDate) {
|
||||
this.dollarCurrencyRateObj = dollarCurrencyRateObj;
|
||||
this.fetchDate = fetchDate;
|
||||
}
|
||||
|
||||
public DollarCurrencyRateObj getDollarCurrencyRateObj() {
|
||||
return dollarCurrencyRateObj;
|
||||
}
|
||||
|
||||
public void setDollarCurrencyRateObj(DollarCurrencyRateObj dollarCurrencyRateObj) {
|
||||
this.dollarCurrencyRateObj = dollarCurrencyRateObj;
|
||||
}
|
||||
|
||||
public String getFetchDate() {
|
||||
return fetchDate;
|
||||
}
|
||||
|
||||
public void setFetchDate(String fetchDate) {
|
||||
this.fetchDate = fetchDate;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class DollarCurrencyRateObj implements Serializable {
|
||||
|
||||
@SerializedName("USD")
|
||||
double value;
|
||||
|
||||
public DollarCurrencyRateObj(double value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public double getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(double value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
@ -3,6 +3,8 @@ package fr.romanet.vj.apps.myrealestateagency.dao;
|
||||
import androidx.room.Dao;
|
||||
import androidx.room.Query;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import fr.romanet.vj.apps.myrealestateagency.entities.DollarCurrency;
|
||||
@ -12,4 +14,7 @@ public interface DollarCurrencyDao {
|
||||
|
||||
@Query("SELECT * FROM dollar")
|
||||
List<DollarCurrency> getDollarCurrencyList();
|
||||
|
||||
// @Query("UPDATE dollar SET value = :value WHERE fetch_date = :fetchDate")
|
||||
// void updateDollarRate(double value, Date fetchDate);
|
||||
}
|
||||
|
||||
@ -22,7 +22,7 @@ import fr.romanet.vj.apps.myrealestateagency.dao.AgentDao;
|
||||
import fr.romanet.vj.apps.myrealestateagency.entities.DollarCurrency;
|
||||
import fr.romanet.vj.apps.myrealestateagency.entities.Property;
|
||||
|
||||
@Database(entities = {Agency.class, Agent.class, Property.class, DollarCurrency.class}, version = 1, exportSchema = false)
|
||||
@Database(entities = {Agency.class, Agent.class, Property.class, DollarCurrency.class}, version = 2, exportSchema = false)
|
||||
@TypeConverters({Property.Converters.class})
|
||||
public abstract class RealEstateAgencyDatabase extends RoomDatabase {
|
||||
|
||||
|
||||
@ -6,11 +6,15 @@ import androidx.room.Entity;
|
||||
import androidx.room.Index;
|
||||
import androidx.room.PrimaryKey;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.sql.Date;
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Entity(tableName = "dollar",
|
||||
primaryKeys = {"value", "fetch_date"})
|
||||
public class DollarCurrency {
|
||||
public class DollarCurrency implements Serializable {
|
||||
|
||||
@NonNull
|
||||
@ColumnInfo(name = "value")
|
||||
@ -18,9 +22,9 @@ public class DollarCurrency {
|
||||
|
||||
@NonNull
|
||||
@ColumnInfo(name = "fetch_date")
|
||||
private Date fetchDate;
|
||||
private String fetchDate;
|
||||
|
||||
public DollarCurrency(@NonNull double value, @NonNull Date fetchDate)
|
||||
public DollarCurrency(@NonNull double value, @NonNull String fetchDate)
|
||||
{
|
||||
this.value = value;
|
||||
this.fetchDate = fetchDate;
|
||||
@ -36,12 +40,12 @@ public class DollarCurrency {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Date getFetchDate()
|
||||
public String getFetchDate()
|
||||
{
|
||||
return fetchDate;
|
||||
}
|
||||
|
||||
public void setFetchDate(Date fetchDate)
|
||||
public void setFetchDate(String fetchDate)
|
||||
{
|
||||
this.fetchDate = fetchDate;
|
||||
}
|
||||
|
||||
@ -0,0 +1,21 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency.entities;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
public class Rate {
|
||||
|
||||
@SerializedName("USD")
|
||||
private String value;
|
||||
|
||||
public Rate(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency.network;
|
||||
|
||||
import fr.romanet.vj.apps.myrealestateagency.DollarCurrencyObj;
|
||||
import fr.romanet.vj.apps.myrealestateagency.entities.DollarCurrency;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.Query;
|
||||
|
||||
public interface JsonApiInterface {
|
||||
|
||||
@GET("latest")
|
||||
Call<DollarCurrencyObj> getDollarRate(@Query("symbols") String symbols);
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency.repository;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import fr.romanet.vj.apps.myrealestateagency.dao.DollarCurrencyDao;
|
||||
import fr.romanet.vj.apps.myrealestateagency.database.RealEstateAgencyDatabase;
|
||||
import fr.romanet.vj.apps.myrealestateagency.entities.DollarCurrency;
|
||||
|
||||
public class DollarCurrencyRepository {
|
||||
private DollarCurrencyDao dollarCurrencyDao;
|
||||
|
||||
public DollarCurrencyRepository(Application application) {
|
||||
RealEstateAgencyDatabase realEstateAgencyDatabase = RealEstateAgencyDatabase.getInstance(application);
|
||||
dollarCurrencyDao = realEstateAgencyDatabase.dollarCurrencyDao();
|
||||
}
|
||||
|
||||
public DollarCurrency get_dollar_currency_rate()
|
||||
{
|
||||
return dollarCurrencyDao.getDollarCurrencyList().get(0);
|
||||
}
|
||||
|
||||
// public void update_dollar_rate(Double value, Date fetch_date){
|
||||
// dollarCurrencyDao.updateDollarRate(value, fetch_date);
|
||||
// }
|
||||
|
||||
}
|
||||
@ -1,20 +1,29 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency.repository;
|
||||
|
||||
import android.app.Application;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import fr.romanet.vj.apps.myrealestateagency.DollarCurrencyObj;
|
||||
import fr.romanet.vj.apps.myrealestateagency.dao.PropertyDao;
|
||||
import fr.romanet.vj.apps.myrealestateagency.database.RealEstateAgencyDatabase;
|
||||
import fr.romanet.vj.apps.myrealestateagency.entities.Agency;
|
||||
import fr.romanet.vj.apps.myrealestateagency.entities.Property;
|
||||
import fr.romanet.vj.apps.myrealestateagency.network.JsonApiInterface;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
public class PropertyRepository {
|
||||
|
||||
public PropertyDao propertyDao;
|
||||
public LiveData<List<Property>> allProperties;
|
||||
private double dollar_rate;
|
||||
|
||||
public PropertyRepository(Application application) {
|
||||
RealEstateAgencyDatabase realEstateAgencyDatabase = RealEstateAgencyDatabase.getInstance(application);
|
||||
@ -22,6 +31,50 @@ public class PropertyRepository {
|
||||
allProperties = propertyDao.getPropertyList();
|
||||
}
|
||||
|
||||
public double get_dollar_rate(){
|
||||
|
||||
// - Answer
|
||||
dollar_rate = 1;
|
||||
Log.d("CAT", "---------> Call...");
|
||||
|
||||
|
||||
// - Build Retrofit API tool
|
||||
Retrofit retrofit = new Retrofit.Builder()
|
||||
.baseUrl("https://api.exchangeratesapi.io/")
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build();
|
||||
|
||||
JsonApiInterface jsonApiInterface = retrofit.create(JsonApiInterface.class);
|
||||
// Call<DollarCurrency> call = jsonApiInterface.getDollarRate("USD");
|
||||
Call<DollarCurrencyObj> call = jsonApiInterface.getDollarRate("USD");
|
||||
call.enqueue(new Callback<DollarCurrencyObj>() {
|
||||
@Override
|
||||
public void onResponse(Call<DollarCurrencyObj> call, Response<DollarCurrencyObj> response) {
|
||||
if(! response.isSuccessful()){
|
||||
Log.d("CAT", "---------> RESP[unsuc]:" + String.valueOf(response.code()));
|
||||
dollar_rate = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Log.d("CAT", "---------> RESP ALL:" + response.toString());
|
||||
DollarCurrencyObj dollarCurrencyObj = response.body();
|
||||
Log.d("CAT", "---------> RESP:" + dollarCurrencyObj.getDollarCurrencyRateObj().getValue() + " | " + String.valueOf(dollarCurrencyObj.getFetchDate()));
|
||||
dollar_rate = dollarCurrencyObj.getDollarCurrencyRateObj().getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<DollarCurrencyObj> call, Throwable t) {
|
||||
Log.d("CAT", "---------> FAILED:" + String.valueOf(t));
|
||||
dollar_rate = -1;
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
Log.d("CAT", "---------> Call...END");
|
||||
return dollar_rate;
|
||||
}
|
||||
|
||||
|
||||
public Property getOnePropertyById(int propertyId){return propertyDao.getOnePropertyById(propertyId);}
|
||||
|
||||
|
||||
@ -129,4 +129,10 @@ public class PropertiesDetailActivity extends AppCompatActivity {
|
||||
Intent i = new Intent(PropertiesDetailActivity.this, MapsActivity.class);
|
||||
PropertiesDetailActivity.this.startActivity(i);
|
||||
}
|
||||
|
||||
public void convert_currency(View view){
|
||||
double price_value = viewModel.convert_currency(property);
|
||||
price.setText("RRPrice : " + (new DecimalFormat("##.##").format(price_value)));
|
||||
// price.setText("RRPrice : " + (new DecimalFormat("##.##").format(1.1)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,10 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency.viewmodel;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.SavedStateHandle;
|
||||
import androidx.lifecycle.Transformations;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
import fr.romanet.vj.apps.myrealestateagency.MyApp;
|
||||
@ -13,15 +16,34 @@ public class PropertiesDetailActivityViewModel extends ViewModel {
|
||||
public MutableLiveData<Property> property = new MutableLiveData<>();
|
||||
private PropertyRepository propertyRepository;
|
||||
|
||||
private Boolean current_currency_is_eur;
|
||||
|
||||
public PropertiesDetailActivityViewModel(SavedStateHandle savedStateHandle)
|
||||
{
|
||||
final Property propertyExtra = savedStateHandle.get(PropertiesDetailActivity.PROPERTY_EXTRA);
|
||||
property.postValue(propertyExtra);
|
||||
propertyRepository = new PropertyRepository(MyApp.getInstance());
|
||||
current_currency_is_eur = true;
|
||||
}
|
||||
|
||||
public void deleteProperty()
|
||||
{
|
||||
propertyRepository.deleteProperty(property.getValue());
|
||||
}
|
||||
|
||||
|
||||
public double convert_currency(Property property){
|
||||
|
||||
if(current_currency_is_eur){
|
||||
double dollar_rate = propertyRepository.get_dollar_rate();
|
||||
double usd_price = property.propertyStatue.price * dollar_rate;
|
||||
current_currency_is_eur = false;
|
||||
Log.d("CAT", "----------> Convert was EUR (dollar rate:" + String.valueOf(dollar_rate) + ")");
|
||||
return usd_price;
|
||||
}else{
|
||||
current_currency_is_eur = true;
|
||||
Log.d("CAT", "----------> Convert was USD");
|
||||
return property.propertyStatue.price;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -153,6 +153,7 @@
|
||||
android:layout_width="112dp"
|
||||
android:layout_height="42dp"
|
||||
android:text="Convert"
|
||||
android:onClick="convert_currency"
|
||||
app:layout_constraintBottom_toTopOf="@+id/detailsPropertySurface"
|
||||
app:layout_constraintEnd_toEndOf="@+id/detailsPropertyNumberOfRooms"
|
||||
app:layout_constraintHorizontal_bias="0.355"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user