Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Feature/exchange rate providers #147

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.preference:preference:1.1.0'
implementation 'androidx.biometric:biometric:1.0.0'

testImplementation 'org.json:json:20190722'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
Expand Down
15 changes: 4 additions & 11 deletions app/src/main/java/zapsolutions/zap/HomeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import androidx.lifecycle.OnLifecycleEvent;
import androidx.lifecycle.ProcessLifecycleOwner;

import com.android.volley.toolbox.JsonObjectRequest;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.concurrent.Executors;
Expand All @@ -32,16 +31,16 @@

import zapsolutions.zap.baseClasses.App;
import zapsolutions.zap.baseClasses.BaseAppCompatActivity;
import zapsolutions.zap.connection.HttpClient;
import zapsolutions.zap.connection.establishConnectionToLnd.LndConnection;
import zapsolutions.zap.connection.internetConnectionStatus.NetworkChangeReceiver;
import zapsolutions.zap.fragments.SettingsFragment;
import zapsolutions.zap.fragments.WalletFragment;
import zapsolutions.zap.interfaces.UserGuardianInterface;
import zapsolutions.zap.transactionHistory.TransactionHistoryFragment;
import zapsolutions.zap.util.MonetaryUtil;
import zapsolutions.zap.util.ExchangeRateUtil;
import zapsolutions.zap.util.PinScreenUtil;
import zapsolutions.zap.util.PrefsUtil;
import zapsolutions.zap.util.RefConstants;
import zapsolutions.zap.util.TimeOutUtil;
import zapsolutions.zap.util.TorUtil;
import zapsolutions.zap.util.UserGuardian;
Expand Down Expand Up @@ -132,22 +131,16 @@ private void setupExchangeRateSchedule() {

if (!mIsExchangeRateSchedulerRunning) {
mIsExchangeRateSchedulerRunning = true;
final JsonObjectRequest request = MonetaryUtil.getInstance().getExchangeRates();

mExchangeRateScheduler =
Executors.newSingleThreadScheduledExecutor();

mExchangeRateScheduler.scheduleAtFixedRate
(new Runnable() {
public void run() {
if (!MonetaryUtil.getInstance().getSecondCurrency().isBitcoin() ||
!PrefsUtil.getPrefs().contains(PrefsUtil.AVAILABLE_FIAT_CURRENCIES)) {
ZapLog.debug(LOG_TAG, "Fiat exchange rate request initiated");
// Adding request to request queue
HttpClient.getInstance().addToRequestQueue(request, "rateRequest");
}
ExchangeRateUtil.getInstance().getExchangeRates();
}
}, 0, 3, TimeUnit.MINUTES);
}, 0, RefConstants.EXCHANGE_RATE_PERIOD, RefConstants.EXCHANGE_RATE_PERIOD_UNIT);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import zapsolutions.zap.R;
import zapsolutions.zap.interfaces.UserGuardianInterface;
import zapsolutions.zap.util.BiometricUtil;
import zapsolutions.zap.util.ExchangeRateUtil;
import zapsolutions.zap.util.PrefsUtil;
import zapsolutions.zap.util.RefConstants;
import zapsolutions.zap.util.UserGuardian;

Expand Down Expand Up @@ -45,6 +47,17 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
}
});

// Request exchange rates when the provider changed
ListPreference listExchangeRateProvider = findPreference("exchangeRateProvider");
listExchangeRateProvider.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
PrefsUtil.edit().putString(PrefsUtil.EXCHANGE_RATE_PROVIDER, newValue.toString()).commit();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If setOnPreferenceChangeListener is invoked, doesn't it imply that the value already has been changed and this line changes it again?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I want to update the exchange rates when changing the provider.
If I don't have that line here, it will update the exchange rates before the provider is switched and therefore fetching the rates from the wrong provider.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or in other words: The value is not changed until true is returned. That's why I have to do it earlier.

ExchangeRateUtil.getInstance().getExchangeRates();
return true;
}
});

// Create invoice expiry display entries. For the sake of plurals this has to be done by code.
mListLnExpiry = findPreference("lightning_expiry");
createLnExpiryDisplayEntries();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,10 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
// we received from our provider. Therefore when the provider adds new currencies,
// they will automatically show up in Zap.
mListCurrency = findPreference("secondCurrency");
createSecondCurrencyList();
mListCurrency.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
createSecondCurrencyList();

return true;
}
});
Expand Down Expand Up @@ -385,10 +383,13 @@ private void createSecondCurrencyList() {
@Override
public void onResume() {
super.onResume();
mListCurrency.setValue(PrefsUtil.getSecondCurrency());
mListCurrency.setSummary(PrefsUtil.getSecondCurrency());
createSecondCurrencyList();
pinOptionVisibility();
}

private void pinOptionVisibility(){
private void pinOptionVisibility() {
// Display add or change pin
if (PrefsUtil.isPinEnabled()) {
mAddPin.setVisible(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import zapsolutions.zap.interfaces.UserGuardianInterface;
import zapsolutions.zap.setup.SetupActivity;
import zapsolutions.zap.util.Balances;
import zapsolutions.zap.util.ExchangeRateUtil;
import zapsolutions.zap.util.MonetaryUtil;
import zapsolutions.zap.util.OnSingleClickListener;
import zapsolutions.zap.util.PrefsUtil;
Expand All @@ -46,7 +47,7 @@
* A simple {@link Fragment} subclass.
*/
public class WalletFragment extends Fragment implements SharedPreferences.OnSharedPreferenceChangeListener,
Wallet.BalanceListener, Wallet.InfoListener, Wallet.WalletLoadedListener, MonetaryUtil.ExchangeRateListener, UserGuardianInterface {
Wallet.BalanceListener, Wallet.InfoListener, Wallet.WalletLoadedListener, ExchangeRateUtil.ExchangeRateListener, UserGuardianInterface {

private static final String LOG_TAG = WalletFragment.class.getName();

Expand Down Expand Up @@ -445,7 +446,7 @@ public void onResume() {
mWalletLoadedListenerRegistered = true;
}
if (!mExchangeRateListenerRegistered) {
MonetaryUtil.getInstance().registerExchangeRateListener(this);
ExchangeRateUtil.getInstance().registerExchangeRateListener(this);
mExchangeRateListenerRegistered = true;
}

Expand All @@ -465,7 +466,7 @@ public void onDestroy() {
Wallet.getInstance().unregisterBalanceListener(this);
Wallet.getInstance().unregisterInfoListener(this);
Wallet.getInstance().unregisterWalletLoadedListener(this);
MonetaryUtil.getInstance().unregisterExchangeRateListener(this);
ExchangeRateUtil.getInstance().unregisterExchangeRateListener(this);
}

@Override
Expand Down
Loading