Retrofit API + Dollar Rate Objects 100% done
This commit is contained in:
parent
7498a5ab5e
commit
e832e5139b
@ -5,6 +5,7 @@ import fr.romanet.vj.apps.myrealestateagency.entities.Agency;
|
|||||||
public class Config {
|
public class Config {
|
||||||
|
|
||||||
private static String sharedPreferences = "prefs";
|
private static String sharedPreferences = "prefs";
|
||||||
|
public static String DOLLAR_RATE = "dollarrate";
|
||||||
private static String selected_agency_index;
|
private static String selected_agency_index;
|
||||||
|
|
||||||
public static String getSharedPreferences() {
|
public static String getSharedPreferences() {
|
||||||
@ -15,6 +16,10 @@ public class Config {
|
|||||||
return selected_agency_index;
|
return selected_agency_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getDollarRate() {
|
||||||
|
return DOLLAR_RATE;
|
||||||
|
}
|
||||||
|
|
||||||
public static void setSelected_agency_index(String selected_agency_index) {
|
public static void setSelected_agency_index(String selected_agency_index) {
|
||||||
Config.selected_agency_index = selected_agency_index;
|
Config.selected_agency_index = selected_agency_index;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,6 +16,11 @@ public class SharedPrefs {
|
|||||||
return sharedPreferences.getInt(Config.get_selected_agency_index(), -1);
|
return sharedPreferences.getInt(Config.get_selected_agency_index(), -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double get_dollar_rate(){
|
||||||
|
sharedPreferences = MyApp.getContext().getSharedPreferences(Config.getSharedPreferences(), MODE_PRIVATE);
|
||||||
|
return Double.parseDouble(sharedPreferences.getString(Config.DOLLAR_RATE, "0"));
|
||||||
|
}
|
||||||
|
|
||||||
public void set_logged_agent_id(Agent agent){
|
public void set_logged_agent_id(Agent agent){
|
||||||
sharedPreferences = MyApp.getContext().getSharedPreferences(Config.getSharedPreferences(), MODE_PRIVATE);
|
sharedPreferences = MyApp.getContext().getSharedPreferences(Config.getSharedPreferences(), MODE_PRIVATE);
|
||||||
sharedPreferences.edit()
|
sharedPreferences.edit()
|
||||||
@ -23,5 +28,12 @@ public class SharedPrefs {
|
|||||||
.apply();
|
.apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void set_dollar_rate(double rate){
|
||||||
|
sharedPreferences = MyApp.getContext().getSharedPreferences(Config.getSharedPreferences(), MODE_PRIVATE);
|
||||||
|
sharedPreferences.edit()
|
||||||
|
.putString(Config.DOLLAR_RATE, String.valueOf(rate))
|
||||||
|
.apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,10 +4,14 @@ import android.app.Application;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.lifecycle.LiveData;
|
import androidx.lifecycle.LiveData;
|
||||||
|
import androidx.lifecycle.ViewModel;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.time.temporal.ValueRange;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import fr.romanet.vj.apps.myrealestateagency.DollarCurrencyObj;
|
import fr.romanet.vj.apps.myrealestateagency.DollarCurrencyObj;
|
||||||
|
import fr.romanet.vj.apps.myrealestateagency.SharedPrefs;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.dao.PropertyDao;
|
import fr.romanet.vj.apps.myrealestateagency.dao.PropertyDao;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.database.RealEstateAgencyDatabase;
|
import fr.romanet.vj.apps.myrealestateagency.database.RealEstateAgencyDatabase;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.entities.Agency;
|
import fr.romanet.vj.apps.myrealestateagency.entities.Agency;
|
||||||
@ -31,10 +35,11 @@ public class PropertyRepository {
|
|||||||
allProperties = propertyDao.getPropertyList();
|
allProperties = propertyDao.getPropertyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public double get_dollar_rate(){
|
public void get_dollar_rate() throws IOException {
|
||||||
|
|
||||||
|
|
||||||
// - Answer
|
// - Answer
|
||||||
dollar_rate = 1;
|
dollar_rate = -1;
|
||||||
Log.d("CAT", "---------> Call...");
|
Log.d("CAT", "---------> Call...");
|
||||||
|
|
||||||
|
|
||||||
@ -45,8 +50,8 @@ public class PropertyRepository {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
JsonApiInterface jsonApiInterface = retrofit.create(JsonApiInterface.class);
|
JsonApiInterface jsonApiInterface = retrofit.create(JsonApiInterface.class);
|
||||||
// Call<DollarCurrency> call = jsonApiInterface.getDollarRate("USD");
|
|
||||||
Call<DollarCurrencyObj> call = jsonApiInterface.getDollarRate("USD");
|
Call<DollarCurrencyObj> call = jsonApiInterface.getDollarRate("USD");
|
||||||
|
|
||||||
call.enqueue(new Callback<DollarCurrencyObj>() {
|
call.enqueue(new Callback<DollarCurrencyObj>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(Call<DollarCurrencyObj> call, Response<DollarCurrencyObj> response) {
|
public void onResponse(Call<DollarCurrencyObj> call, Response<DollarCurrencyObj> response) {
|
||||||
@ -61,6 +66,12 @@ public class PropertyRepository {
|
|||||||
DollarCurrencyObj dollarCurrencyObj = response.body();
|
DollarCurrencyObj dollarCurrencyObj = response.body();
|
||||||
Log.d("CAT", "---------> RESP:" + dollarCurrencyObj.getDollarCurrencyRateObj().getValue() + " | " + String.valueOf(dollarCurrencyObj.getFetchDate()));
|
Log.d("CAT", "---------> RESP:" + dollarCurrencyObj.getDollarCurrencyRateObj().getValue() + " | " + String.valueOf(dollarCurrencyObj.getFetchDate()));
|
||||||
dollar_rate = dollarCurrencyObj.getDollarCurrencyRateObj().getValue();
|
dollar_rate = dollarCurrencyObj.getDollarCurrencyRateObj().getValue();
|
||||||
|
|
||||||
|
SharedPrefs sharedPrefs = new SharedPrefs();
|
||||||
|
sharedPrefs.set_dollar_rate(dollar_rate);
|
||||||
|
Log.d("CAT", "---------> SAVED:" + String.valueOf(dollar_rate));
|
||||||
|
dollar_rate = sharedPrefs.get_dollar_rate();
|
||||||
|
Log.d("CAT", "---------> RETRI:" + String.valueOf(dollar_rate));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -72,7 +83,9 @@ public class PropertyRepository {
|
|||||||
});
|
});
|
||||||
|
|
||||||
Log.d("CAT", "---------> Call...END");
|
Log.d("CAT", "---------> Call...END");
|
||||||
return dollar_rate;
|
|
||||||
|
// - Dont return anything, RetroFit asynctasks finishes after function execution.
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -6,9 +6,11 @@ import android.view.View;
|
|||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
import androidx.lifecycle.Observer;
|
||||||
import androidx.lifecycle.SavedStateViewModelFactory;
|
import androidx.lifecycle.SavedStateViewModelFactory;
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
@ -130,9 +132,23 @@ public class PropertiesDetailActivity extends AppCompatActivity {
|
|||||||
PropertiesDetailActivity.this.startActivity(i);
|
PropertiesDetailActivity.this.startActivity(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void convert_currency(View view){
|
public void observePrice(){
|
||||||
double price_value = viewModel.convert_currency(property);
|
viewModel.property_price.observe(this, new Observer<Double>() {
|
||||||
price.setText("RRPrice : " + (new DecimalFormat("##.##").format(price_value)));
|
@Override
|
||||||
// price.setText("RRPrice : " + (new DecimalFormat("##.##").format(1.1)));
|
public void onChanged(Double aDouble) {
|
||||||
|
if(aDouble == 0){
|
||||||
|
price.setText("Price : Please wait...");
|
||||||
|
}else{
|
||||||
|
price.setText("Price : " + (new DecimalFormat("##.##").format(aDouble)));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void convert_currency(View view) throws IOException {
|
||||||
|
viewModel.convert_currency(property);
|
||||||
|
observePrice();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package fr.romanet.vj.apps.myrealestateagency.viewmodel;
|
package fr.romanet.vj.apps.myrealestateagency.viewmodel;
|
||||||
|
|
||||||
|
import android.os.AsyncTask;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.lifecycle.MutableLiveData;
|
import androidx.lifecycle.MutableLiveData;
|
||||||
@ -7,13 +8,19 @@ import androidx.lifecycle.SavedStateHandle;
|
|||||||
import androidx.lifecycle.Transformations;
|
import androidx.lifecycle.Transformations;
|
||||||
import androidx.lifecycle.ViewModel;
|
import androidx.lifecycle.ViewModel;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import fr.romanet.vj.apps.myrealestateagency.MyApp;
|
import fr.romanet.vj.apps.myrealestateagency.MyApp;
|
||||||
|
import fr.romanet.vj.apps.myrealestateagency.SharedPrefs;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.entities.Property;
|
import fr.romanet.vj.apps.myrealestateagency.entities.Property;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.repository.PropertyRepository;
|
import fr.romanet.vj.apps.myrealestateagency.repository.PropertyRepository;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.view.PropertiesDetailActivity;
|
import fr.romanet.vj.apps.myrealestateagency.view.PropertiesDetailActivity;
|
||||||
|
|
||||||
public class PropertiesDetailActivityViewModel extends ViewModel {
|
public class PropertiesDetailActivityViewModel extends ViewModel {
|
||||||
|
|
||||||
public MutableLiveData<Property> property = new MutableLiveData<>();
|
public MutableLiveData<Property> property = new MutableLiveData<>();
|
||||||
|
public MutableLiveData<Double> property_price = new MutableLiveData<>();
|
||||||
|
|
||||||
private PropertyRepository propertyRepository;
|
private PropertyRepository propertyRepository;
|
||||||
|
|
||||||
private Boolean current_currency_is_eur;
|
private Boolean current_currency_is_eur;
|
||||||
@ -32,18 +39,71 @@ public class PropertiesDetailActivityViewModel extends ViewModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public double convert_currency(Property property){
|
private class AsyncTaskRunner extends AsyncTask<String, String, String> {
|
||||||
|
|
||||||
|
private String resp;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String doInBackground(String... params) {
|
||||||
|
publishProgress("Sleeping..."); // Calls onProgressUpdate()
|
||||||
|
try {
|
||||||
|
int time = 1000;
|
||||||
|
|
||||||
|
Thread.sleep(time);
|
||||||
|
resp = "Slept for " + 1 + " seconds";
|
||||||
|
SharedPrefs sharedPrefs = new SharedPrefs();
|
||||||
|
property_price.postValue(Double.parseDouble(params[0]) * sharedPrefs.get_dollar_rate());
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
resp = e.getMessage();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
resp = e.getMessage();
|
||||||
|
}
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(String result) {
|
||||||
|
// execution of result of Long time consuming operation
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPreExecute() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onProgressUpdate(String... text) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void convert_currency(Property property) throws IOException {
|
||||||
|
|
||||||
|
if( current_currency_is_eur){
|
||||||
|
propertyRepository.get_dollar_rate();
|
||||||
|
|
||||||
|
SharedPrefs sharedPrefs = new SharedPrefs();
|
||||||
|
double dollar_rate = sharedPrefs.get_dollar_rate();
|
||||||
|
|
||||||
if(current_currency_is_eur){
|
|
||||||
double dollar_rate = propertyRepository.get_dollar_rate();
|
|
||||||
double usd_price = property.propertyStatue.price * dollar_rate;
|
double usd_price = property.propertyStatue.price * dollar_rate;
|
||||||
|
property_price.postValue(usd_price);
|
||||||
|
AsyncTaskRunner runner = new AsyncTaskRunner();
|
||||||
|
runner.execute(String.valueOf(property.propertyStatue.price));
|
||||||
|
|
||||||
current_currency_is_eur = false;
|
current_currency_is_eur = false;
|
||||||
Log.d("CAT", "----------> Convert was EUR (dollar rate:" + String.valueOf(dollar_rate) + ")");
|
Log.d("CAT", "----------> Convert was EUR (dollar rate:" + String.valueOf(dollar_rate) + ")");
|
||||||
return usd_price;
|
return;
|
||||||
}else{
|
}else{
|
||||||
|
property_price.postValue(property.propertyStatue.price);
|
||||||
|
|
||||||
current_currency_is_eur = true;
|
current_currency_is_eur = true;
|
||||||
Log.d("CAT", "----------> Convert was USD");
|
Log.d("CAT", "----------> Convert was USD");
|
||||||
return property.propertyStatue.price;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user