import android.widget.TextView;
import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AlertDialog;
import androidx.core.view.GravityCompat;
import net.ktnx.mobileledger.R;
import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
import net.ktnx.mobileledger.databinding.ActivityMainBinding;
+import net.ktnx.mobileledger.db.DB;
+import net.ktnx.mobileledger.db.Option;
+import net.ktnx.mobileledger.db.Profile;
import net.ktnx.mobileledger.model.Data;
-import net.ktnx.mobileledger.model.MobileLedgerProfile;
import net.ktnx.mobileledger.ui.FabManager;
import net.ktnx.mobileledger.ui.MainModel;
import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
import net.ktnx.mobileledger.ui.new_transaction.NewTransactionActivity;
+import net.ktnx.mobileledger.ui.profiles.ProfileDetailActivity;
import net.ktnx.mobileledger.ui.profiles.ProfilesRecyclerViewAdapter;
import net.ktnx.mobileledger.ui.templates.TemplatesActivity;
import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
import net.ktnx.mobileledger.utils.Colors;
import net.ktnx.mobileledger.utils.Logger;
-import net.ktnx.mobileledger.utils.MLDB;
import org.jetbrains.annotations.NotNull;
private DrawerLayout.SimpleDrawerListener drawerListener;
private ActionBarDrawerToggle barDrawerToggle;
private ViewPager2.OnPageChangeCallback pageChangeCallback;
- private MobileLedgerProfile profile;
+ private Profile profile;
private MainModel mainModel;
private ActivityMainBinding b;
private int fabVerticalOffset;
Data.observeProfile(this, this::onProfileChanged);
Data.profiles.observe(this, this::onProfileListChanged);
+
Data.backgroundTaskProgress.observe(this, this::onRetrieveProgress);
Data.backgroundTasksRunning.observe(this, this::onRetrieveRunningChanged);
markDrawerItemCurrent(R.id.nav_account_summary);
b.mainPager.setAdapter(mSectionsPagerAdapter);
+ b.mainPager.setOffscreenPageLimit(1);
if (pageChangeCallback == null) {
pageChangeCallback = new ViewPager2.OnPageChangeCallback() {
.setValue(savedInstanceState.getString(STATE_ACC_FILTER, null));
}
- b.btnNoProfilesAdd.setOnClickListener(
- v -> MobileLedgerProfile.startEditProfileActivity(this, null));
+ b.btnNoProfilesAdd.setOnClickListener(v -> ProfileDetailActivity.start(this, null));
b.btnAddTransaction.setOnClickListener(this::fabNewTransactionClicked);
- b.navNewProfileButton.setOnClickListener(
- v -> MobileLedgerProfile.startEditProfileActivity(this, null));
+ b.navNewProfileButton.setOnClickListener(v -> ProfileDetailActivity.start(this, null));
b.transactionListCancelDownload.setOnClickListener(this::onStopTransactionRefreshClick);
mainModel.scheduleTransactionListRetrieval();
}
}
- private void createShortcuts(List<MobileLedgerProfile> list) {
+ private void createShortcuts(List<Profile> list) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1)
return;
ShortcutManager sm = getSystemService(ShortcutManager.class);
List<ShortcutInfo> shortcuts = new ArrayList<>();
int i = 0;
- for (MobileLedgerProfile p : list) {
+ for (Profile p : list) {
if (shortcuts.size() >= sm.getMaxShortcutCountPerActivity())
break;
- if (!p.isPostingPermitted())
+ if (!p.permitPosting())
continue;
final ShortcutInfo.Builder builder =
ProfileThemedActivity.PARAM_PROFILE_ID, p.getId())
.putExtra(
ProfileThemedActivity.PARAM_THEME,
- p.getThemeHue()))
+ p.getTheme()))
.setRank(i)
.build();
shortcuts.add(si);
}
sm.setDynamicShortcuts(shortcuts);
}
- private void onProfileListChanged(List<MobileLedgerProfile> newList) {
+ private void onProfileListChanged(List<Profile> newList) {
if ((newList == null) || newList.isEmpty()) {
b.noProfilesLayout.setVisibility(View.VISIBLE);
b.mainAppLayout.setVisibility(View.GONE);
(int) (getResources().getDimension(R.dimen.thumb_row_height) * newList.size()));
Logger.debug("profiles", "profile list changed");
- mProfileListAdapter.notifyDataSetChanged();
+ mProfileListAdapter.setProfileList(newList);
createShortcuts(newList);
+
+ Profile currentProfile = Data.getProfile();
+ if (currentProfile == null || !newList.contains(currentProfile)) {
+ Logger.debug(TAG, "Switching profile because the current is no longer available");
+ Data.setCurrentProfile(newList.get(0));
+ }
}
/**
* called when the current profile has changed
*/
- private void onProfileChanged(MobileLedgerProfile profile) {
+ private void onProfileChanged(@Nullable Profile newProfile) {
if (this.profile == null) {
- if (profile == null)
+ if (newProfile == null)
return;
}
else {
- if (this.profile.equals(profile))
+ if (this.profile.equals(newProfile))
return;
}
- boolean haveProfile = profile != null;
+ boolean haveProfile = newProfile != null;
if (haveProfile)
- setTitle(profile.getName());
+ setTitle(newProfile.getName());
else
setTitle(R.string.app_name);
- mainModel.setProfile(profile);
-
- this.profile = profile;
-
- int newProfileTheme = haveProfile ? profile.getThemeHue() : -1;
+ int newProfileTheme = haveProfile ? newProfile.getTheme() : -1;
if (newProfileTheme != Colors.profileThemeId) {
Logger.debug("profiles",
String.format(Locale.ENGLISH, "profile theme %d → %d", Colors.profileThemeId,
return;
}
+ final boolean sameProfileId = (newProfile != null) && (this.profile != null) &&
+ this.profile.getId() == newProfile.getId();
+
+ this.profile = newProfile;
+
b.noProfilesLayout.setVisibility(haveProfile ? View.GONE : View.VISIBLE);
b.pagerLayout.setVisibility(haveProfile ? View.VISIBLE : View.VISIBLE);
mProfileListAdapter.notifyDataSetChanged();
- mainModel.clearAccounts();
- mainModel.clearTransactions();
-
if (haveProfile) {
- Logger.debug("transactions", "requesting list reload");
- mainModel.scheduleTransactionListReload();
-
- if (profile.isPostingPermitted()) {
+ if (newProfile.permitPosting()) {
b.toolbar.setSubtitle(null);
b.btnAddTransaction.show();
}
}
updateLastUpdateTextFromDB();
+
+ if (sameProfileId) {
+ Logger.debug(TAG, String.format(Locale.ROOT, "Short-cut profile 'changed' to %d",
+ newProfile.getId()));
+ return;
+ }
+
+ mainModel.stopTransactionsRetrieval();
+
+ mainModel.clearTransactions();
+
+ if (haveProfile) {
+ Logger.debug("transactions", "requesting list reload");
+ mainModel.scheduleTransactionListReload();
+ }
}
private void profileThemeChanged() {
// un-hook all observed LiveData
public void fabNewTransactionClicked(View view) {
Intent intent = new Intent(this, NewTransactionActivity.class);
intent.putExtra(ProfileThemedActivity.PARAM_PROFILE_ID, profile.getId());
- intent.putExtra(ProfileThemedActivity.PARAM_THEME, profile.getThemeHue());
+ intent.putExtra(ProfileThemedActivity.PARAM_THEME, profile.getTheme());
startActivity(intent);
overridePendingTransition(R.anim.slide_in_up, R.anim.dummy);
}
if (profile == null)
return;
- long lastUpdate = profile.getLongOption(MLDB.OPT_LAST_SCRAPE, 0L);
-
- Logger.debug("transactions", String.format(Locale.ENGLISH, "Last update = %d", lastUpdate));
- if (lastUpdate == 0) {
- Data.lastUpdateDate.postValue(null);
- }
- else {
- Data.lastUpdateDate.postValue(new Date(lastUpdate));
- }
-
- scheduleDataRetrievalIfStale(lastUpdate);
-
+ DB.get()
+ .getOptionDAO()
+ .load(profile.getId(), Option.OPT_LAST_SCRAPE)
+ .observe(this, opt -> {
+ long lastUpdate = 0;
+ if (opt != null) {
+ try {
+ lastUpdate = Long.parseLong(opt.getValue());
+ }
+ catch (NumberFormatException ex) {
+ Logger.debug(TAG, String.format("Error parsing '%s' as long", opt.getValue()),
+ ex);
+ }
+ }
+
+ if (lastUpdate == 0) {
+ Data.lastUpdateDate.postValue(null);
+ }
+ else {
+ Data.lastUpdateDate.postValue(new Date(lastUpdate));
+ }
+
+ scheduleDataRetrievalIfStale(lastUpdate);
+ });
}
private void refreshLastUpdateInfo() {
final int formatFlags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR |
b.transactionProgressLayout.setVisibility(View.GONE);
}
}
- public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
- if (progress.getState() == RetrieveTransactionsTask.ProgressState.FINISHED) {
+ public void onRetrieveProgress(@Nullable RetrieveTransactionsTask.Progress progress) {
+ if (progress == null ||
+ progress.getState() == RetrieveTransactionsTask.ProgressState.FINISHED)
+ {
Logger.debug("progress", "Done");
b.transactionProgressLayout.setVisibility(View.GONE);
mainModel.transactionRetrievalDone();
- String error = progress.getError();
+ String error = (progress == null) ? null : progress.getError();
if (error != null) {
if (error.equals(RetrieveTransactionsTask.Result.ERR_JSON_PARSER_ERROR))
error = getResources().getString(R.string.err_json_parser_error);
builder.setMessage(error);
builder.setPositiveButton(R.string.btn_profile_options, (dialog, which) -> {
Logger.debug("error", "will start profile editor");
- MobileLedgerProfile.startEditProfileActivity(this, profile);
+ ProfileDetailActivity.start(this, profile);
});
builder.create()
.show();
}
}
public void fabShouldShow() {
- if ((profile != null) && profile.isPostingPermitted() && !b.drawerLayout.isOpen())
+ if ((profile != null) && profile.permitPosting() && !b.drawerLayout.isOpen())
fabManager.showFab();
}
@Override