X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Factivity%2FMainActivity.java;h=03617c5923c8fe9ef1f431bc35c86e13ebc0e2ac;hb=9a56eed6dcbfe4434a9a46b198320c16b288d86f;hp=bce01de75c9ffc29ee61bc21b5ee04c04e8f30fa;hpb=8a7234e211e68af2e54f082606c8bb4fe5924f0c;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java index bce01de7..03617c59 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java @@ -27,6 +27,7 @@ import android.graphics.Color; import android.graphics.drawable.Icon; import android.os.Build; import android.os.Bundle; +import android.text.format.DateUtils; import android.util.Log; import android.view.View; import android.view.animation.AnimationUtils; @@ -65,11 +66,11 @@ import net.ktnx.mobileledger.utils.MLDB; import org.jetbrains.annotations.NotNull; -import java.text.DateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Locale; +import java.util.Objects; /* * TODO: reports @@ -234,8 +235,6 @@ public class MainActivity extends ProfileThemedActivity { .setValue(savedInstanceState.getString(STATE_ACC_FILTER, null)); } - mainModel.lastUpdateDate.observe(this, this::updateLastUpdateDisplay); - findViewById(R.id.btn_no_profiles_add).setOnClickListener( v -> startEditProfileActivity(null)); @@ -334,6 +333,9 @@ public class MainActivity extends ProfileThemedActivity { .show(); mainModel.clearUpdateError(); }); + Data.locale.observe(this, l -> refreshLastUpdateInfo()); + Data.lastUpdateDate.observe(this, date -> refreshLastUpdateInfo()); + Data.lastUpdateTransactionCount.observe(this, date -> refreshLastUpdateInfo()); } private void scheduleDataRetrievalIfStale(long lastUpdate) { long now = new Date().getTime(); @@ -459,28 +461,14 @@ public class MainActivity extends ProfileThemedActivity { updateLastUpdateTextFromDB(); } - private void updateLastUpdateDisplay(Date newValue) { - LinearLayout l = findViewById(R.id.transactions_last_update_layout); - TextView v = findViewById(R.id.transactions_last_update); - if (newValue == null) { - l.setVisibility(View.INVISIBLE); - Logger.debug("main", "no last update date :("); - } - else { - final String text = DateFormat.getDateTimeInstance() - .format(newValue); - v.setText(text); - l.setVisibility(View.VISIBLE); - Logger.debug("main", String.format("Date formatted: %s", text)); - } - } private void profileThemeChanged() { storeThemeIdInPrefs(profile.getThemeHue()); // un-hook all observed LiveData Data.removeProfileObservers(this); Data.profiles.removeObservers(this); - mainModel.lastUpdateDate.removeObservers(this); + Data.lastUpdateTransactionCount.removeObservers(this); + Data.lastUpdateDate.removeObservers(this); recreate(); } @@ -568,18 +556,29 @@ public class MainActivity extends ProfileThemedActivity { if (profile == null) return; - long last_update = profile.getLongOption(MLDB.OPT_LAST_SCRAPE, 0L); + long lastUpdate = profile.getLongOption(MLDB.OPT_LAST_SCRAPE, 0L); - Logger.debug("transactions", - String.format(Locale.ENGLISH, "Last update = %d", last_update)); - if (last_update == 0) { - mainModel.lastUpdateDate.postValue(null); + Logger.debug("transactions", String.format(Locale.ENGLISH, "Last update = %d", lastUpdate)); + if (lastUpdate == 0) { + Data.lastUpdateDate.postValue(null); } else { - mainModel.lastUpdateDate.postValue(new Date(last_update)); + Data.lastUpdateDate.postValue(new Date(lastUpdate)); } - scheduleDataRetrievalIfStale(last_update); + scheduleDataRetrievalIfStale(lastUpdate); + + } + private void refreshLastUpdateInfo() { + final int formatFlags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR | + DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_NUMERIC_DATE; + String template = getResources().getString(R.string.transaction_count_summary); + Integer transactionCount = Data.lastUpdateTransactionCount.getValue(); + Date lastUpdate = Data.lastUpdateDate.getValue(); + Data.lastUpdateText.set((lastUpdate == null) ? "----" : String.format( + Objects.requireNonNull(Data.locale.getValue()), template, + (transactionCount == null) ? 0 : transactionCount, + DateUtils.formatDateTime(this, lastUpdate.getTime(), formatFlags))); } public void onStopTransactionRefreshClick(View view) { Logger.debug("interactive", "Cancelling transactions refresh");