From 0ce370cea3c5c980b6eeb14acf965188ae951f51 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sat, 15 Aug 2020 09:46:40 +0000 Subject: [PATCH] Data: migrate profile to a private variable, provide methods for accessing it --- .../main/java/net/ktnx/mobileledger/App.java | 4 +-- .../async/UpdateTransactionsTask.java | 4 +-- .../net/ktnx/mobileledger/model/Currency.java | 2 +- .../net/ktnx/mobileledger/model/Data.java | 31 +++++++++++++++++-- .../mobileledger/model/LedgerTransaction.java | 2 +- .../ui/CurrencySelectorFragment.java | 2 +- .../AccountSummaryFragment.java | 10 +++--- .../ui/activity/MainActivity.java | 4 +-- .../ui/activity/NewTransactionActivity.java | 2 +- .../ui/activity/NewTransactionFragment.java | 5 +-- .../ui/activity/NewTransactionItemHolder.java | 4 +-- .../ui/activity/NewTransactionModel.java | 2 +- .../ui/activity/ProfileThemedActivity.java | 12 +------ .../ui/profiles/ProfileDetailFragment.java | 14 ++++----- .../profiles/ProfilesRecyclerViewAdapter.java | 8 ++--- .../TransactionListViewModel.java | 1 - .../net/ktnx/mobileledger/utils/Colors.java | 4 +-- .../net/ktnx/mobileledger/utils/MLDB.java | 4 +-- 18 files changed, 60 insertions(+), 55 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/App.java b/app/src/main/java/net/ktnx/mobileledger/App.java index b47e6d7f..f0f7c013 100644 --- a/app/src/main/java/net/ktnx/mobileledger/App.java +++ b/app/src/main/java/net/ktnx/mobileledger/App.java @@ -55,8 +55,8 @@ public class App extends Application { Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { - MobileLedgerProfile p = Data.profile.getValue(); - if ((p != null) && p.isAuthEnabled()) { + MobileLedgerProfile p = Data.getProfile(); + if (p.isAuthEnabled()) { try { final URL url = new URL(p.getUrl()); final String requestingHost = getRequestingHost(); diff --git a/app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java index ab114531..404e5127 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java @@ -34,9 +34,7 @@ import static net.ktnx.mobileledger.utils.Logger.debug; public class UpdateTransactionsTask extends AsyncTask { protected String doInBackground(String[] filterAccName) { - final MobileLedgerProfile profile = Data.profile.getValue(); - if (profile == null) - return "Profile not configured"; + final MobileLedgerProfile profile = Data.getProfile(); String profile_uuid = profile.getUuid(); Data.backgroundTaskStarted(); diff --git a/app/src/main/java/net/ktnx/mobileledger/model/Currency.java b/app/src/main/java/net/ktnx/mobileledger/model/Currency.java index e32513f2..596cdf1c 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/Currency.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/Currency.java @@ -81,7 +81,7 @@ public class Currency { this.hasGap = hasGap; } public static Currency loadByName(String name) { - MobileLedgerProfile profile = Data.profile.getValue(); + MobileLedgerProfile profile = Data.getProfile(); return profile.loadCurrencyByName(name); } public int getId() { diff --git a/app/src/main/java/net/ktnx/mobileledger/model/Data.java b/app/src/main/java/net/ktnx/mobileledger/model/Data.java index a6d3fd98..02ba81d4 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/Data.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/Data.java @@ -53,7 +53,6 @@ public final class Data { public static final MutableLiveData backgroundTasksRunning = new MutableLiveData<>(false); public static final MutableLiveData lastUpdateDate = new MutableLiveData<>(); - public static final MutableLiveData profile = new InertMutableLiveData<>(); public static final MutableLiveData> profiles = new MutableLiveData<>(null); public static final MutableLiveData accountFilter = new MutableLiveData<>(); @@ -61,10 +60,16 @@ public final class Data { new MutableLiveData<>(); public static final MutableLiveData currencyGap = new MutableLiveData<>(true); public static final MutableLiveData locale = new MutableLiveData<>(Locale.getDefault()); + private static final MutableLiveData profile = + new InertMutableLiveData<>(); private static final AtomicInteger backgroundTaskCount = new AtomicInteger(0); private static final Locker profilesLocker = new Locker(); public static MutableLiveData foundTransactionItemIndex = new MutableLiveData<>(null); private static RetrieveTransactionsTask retrieveTransactionsTask; + @NonNull + public static MobileLedgerProfile getProfile() { + return Objects.requireNonNull(profile.getValue()); + } public static final MutableLiveData drawerOpen = new MutableLiveData<>(false); public static void backgroundTaskStarted() { int cnt = backgroundTaskCount.incrementAndGet(); @@ -80,8 +85,8 @@ public final class Data { cnt)); backgroundTasksRunning.postValue(cnt > 0); } - public static void setCurrentProfile(MobileLedgerProfile newProfile) { - MLDB.setOption(MLDB.OPT_PROFILE_UUID, (newProfile == null) ? null : newProfile.getUuid()); + public static void setCurrentProfile(@NonNull MobileLedgerProfile newProfile) { + MLDB.setOption(MLDB.OPT_PROFILE_UUID, newProfile.getUuid()); stopTransactionsRetrieval(); profile.setValue(newProfile); } @@ -130,6 +135,7 @@ public final class Data { return -1; } + @Nullable public static MobileLedgerProfile getProfile(String profileUUID) { MobileLedgerProfile profile; try (LockHolder readLock = profilesLocker.lockForReading()) { @@ -201,4 +207,23 @@ public final class Data { currencySymbolPosition.setValue(Currency.Position.none); } + public static void observeProfile(LifecycleOwner lifecycleOwner, + Observer observer) { + profile.observe(lifecycleOwner, observer); + } + public synchronized static MobileLedgerProfile initProfile() { + MobileLedgerProfile currentProfile = profile.getValue(); + if (currentProfile != null) + return currentProfile; + + String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null); + MobileLedgerProfile startupProfile = getProfile(profileUUID); + if (startupProfile != null) + setCurrentProfile(startupProfile); + return startupProfile; + } + + public static void removeProfileObservers(LifecycleOwner owner) { + profile.removeObservers(owner); + } } \ No newline at end of file diff --git a/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java b/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java index 638a692c..e31d1582 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java @@ -78,7 +78,7 @@ public class LedgerTransaction { dataLoaded = false; } public LedgerTransaction(Integer id, SimpleDate date, String description) { - this(id, date, description, Data.profile.getValue()); + this(id, date, description, Data.getProfile()); } public LedgerTransaction(SimpleDate date, String description) { this(null, date, description); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/CurrencySelectorFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/CurrencySelectorFragment.java index 76a640ce..9c527fe0 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/CurrencySelectorFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/CurrencySelectorFragment.java @@ -108,7 +108,7 @@ public class CurrencySelectorFragment extends AppCompatDialogFragment model = new ViewModelProvider(this).get(CurrencySelectorModel.class); if (onCurrencySelectedListener != null) model.setOnCurrencySelectedListener(onCurrencySelectedListener); - MobileLedgerProfile profile = Objects.requireNonNull(Data.profile.getValue()); + MobileLedgerProfile profile = Objects.requireNonNull(Data.getProfile()); model.currencies.setValue(new CopyOnWriteArrayList<>(profile.getCurrencies())); CurrencySelectorRecyclerViewAdapter adapter = new CurrencySelectorRecyclerViewAdapter(); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java index dbc200eb..eb61dcc6 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java @@ -96,12 +96,10 @@ public class AccountSummaryFragment extends MobileLedgerListFragment { Data.scheduleTransactionListRetrieval(mainActivity); }); - MobileLedgerProfile profile = Data.profile.getValue(); - if (profile != null) { - profile.getDisplayedAccounts() - .observe(getViewLifecycleOwner(), - (accounts) -> onAccountsChanged(profile, accounts)); - } + MobileLedgerProfile profile = Data.getProfile(); + profile.getDisplayedAccounts() + .observe(getViewLifecycleOwner(), + (accounts) -> onAccountsChanged(profile, accounts)); } private void onAccountsChanged(MobileLedgerProfile profile, List accounts) { Logger.debug("async-acc", 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 33bcbb00..72ace25b 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 @@ -171,7 +171,7 @@ public class MainActivity extends ProfileThemedActivity { mToolbar = findViewById(R.id.toolbar); setSupportActionBar(mToolbar); - Data.profile.observe(this, this::onProfileChanged); + Data.observeProfile(this, this::onProfileChanged); Data.profiles.observe(this, this::onProfileListChanged); @@ -468,7 +468,7 @@ public class MainActivity extends ProfileThemedActivity { storeThemeIdInPrefs(profile.getThemeHue()); // un-hook all observed LiveData - Data.profile.removeObservers(this); + Data.removeProfileObservers(this); Data.profiles.removeObservers(this); Data.lastUpdateDate.removeObservers(this); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java index 89362d77..45e75b5b 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java @@ -50,7 +50,7 @@ public class NewTransactionActivity extends ProfileThemedActivity implements Tas setContentView(R.layout.activity_new_transaction); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); - Data.profile.observe(this, + Data.observeProfile(this, mobileLedgerProfile -> toolbar.setSubtitle(mobileLedgerProfile.getName())); navController = Navigation.findNavController(this, R.id.new_transaction_nav); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java index e54e8d61..9f696444 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java @@ -116,11 +116,12 @@ public class NewTransactionFragment extends Fragment { viewModel = new ViewModelProvider(activity).get(NewTransactionModel.class); viewModel.observeDataProfile(this); - mProfile = Data.profile.getValue(); + mProfile = Data.getProfile(); listAdapter = new NewTransactionItemsAdapter(viewModel, mProfile); list.setAdapter(listAdapter); list.setLayoutManager(new LinearLayoutManager(activity)); - Data.profile.observe(getViewLifecycleOwner(), profile -> { + + Data.observeProfile(getViewLifecycleOwner(), profile -> { mProfile = profile; listAdapter.setProfile(profile); }); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java index f283aee5..e311eb94 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java @@ -132,9 +132,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder tvTransactionComment.requestFocus(); }); - mProfile = Data.profile.getValue(); - if (mProfile == null) - throw new AssertionError(); + mProfile = Data.getProfile(); View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> { final int id = v.getId(); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java index 3350664e..535b8614 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java @@ -66,7 +66,7 @@ public class NewTransactionModel extends ViewModel { } void observeDataProfile(LifecycleOwner activity) { if (!observingDataProfile) - Data.profile.observe(activity, profileObserver); + Data.observeProfile(activity, profileObserver); observingDataProfile = true; } boolean getSimulateSave() { diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileThemedActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileThemedActivity.java index f4d482a5..d33608a6 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileThemedActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileThemedActivity.java @@ -25,7 +25,6 @@ import androidx.annotation.Nullable; import net.ktnx.mobileledger.model.Data; import net.ktnx.mobileledger.model.MobileLedgerProfile; import net.ktnx.mobileledger.utils.Colors; -import net.ktnx.mobileledger.utils.MLDB; @SuppressLint("Registered") public class ProfileThemedActivity extends CrashReportingActivity { @@ -44,15 +43,6 @@ public class ProfileThemedActivity extends CrashReportingActivity { super.onCreate(savedInstanceState); } protected void initProfile() { - mProfile = Data.profile.getValue(); - if (mProfile == null) { - String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null); - MobileLedgerProfile startupProfile; - - - startupProfile = Data.getProfile(profileUUID); - Data.setCurrentProfile(startupProfile); - mProfile = startupProfile; - } + mProfile = Data.initProfile(); } } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java index 13b6a8ba..7a7afef6 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java @@ -79,9 +79,7 @@ public class ProfileDetailFragment extends Fragment { public static final String ARG_HUE = "hue"; @NonNls - /** - * The content this fragment is presenting. - */ private MobileLedgerProfile mProfile; + private MobileLedgerProfile mProfile; private TextView url; private TextView defaultCommodity; private View defaultCommodityLayout; @@ -130,7 +128,7 @@ public class ProfileDetailFragment extends Fragment { ArrayList newList = new ArrayList<>(oldList); newList.remove(mProfile); Data.profiles.setValue(newList); - if (mProfile.equals(Data.profile.getValue())) { + if (mProfile.equals(Data.getProfile())) { debug("profiles", "[fragment] setting current profile to 0"); Data.setCurrentProfile(newList.get(0)); } @@ -155,7 +153,7 @@ public class ProfileDetailFragment extends Fragment { private boolean onWipeDataMenuClicked() { // this is a development option, so no confirmation mProfile.wipeAllData(); - if (mProfile.equals(Data.profile.getValue())) + if (mProfile.equals(Data.getProfile())) triggerProfileChange(); return true; } @@ -171,8 +169,8 @@ public class ProfileDetailFragment extends Fragment { if (viewAdapter != null) viewAdapter.notifyItemChanged(index); - if (mProfile.equals(Data.profile.getValue())) - Data.profile.setValue(newProfile); + if (mProfile.equals(Data.getProfile())) + Data.setCurrentProfile(newProfile); } private void hookTextChangeSyncRoutine(TextView view, TextChangeSyncProc syncRoutine) { view.addTextChangedListener(new TextWatcher() { @@ -452,7 +450,7 @@ public class ProfileDetailFragment extends Fragment { // first profile ever? if (newList.size() == 1) - Data.profile.setValue(mProfile); + Data.setCurrentProfile(mProfile); } Activity activity = getActivity(); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java index 974c9793..b2b5cb7f 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java @@ -136,7 +136,7 @@ public class ProfilesRecyclerViewAdapter if (profile == null) throw new IllegalStateException("Profile row without associated profile"); debug("profiles", "Setting profile to " + profile.getName()); - if (Data.profile.getValue() != profile ) + if (Data.getProfile() != profile) Data.drawerOpen.setValue(false); Data.setCurrentProfile(profile); } @@ -177,9 +177,9 @@ public class ProfilesRecyclerViewAdapter final ArrayList profiles = Data.profiles.getValue(); if (profiles == null) throw new AssertionError(); final MobileLedgerProfile profile = profiles.get(position); - final MobileLedgerProfile currentProfile = Data.profile.getValue(); + final MobileLedgerProfile currentProfile = Data.getProfile(); debug("profiles", String.format(Locale.ENGLISH, "pos %d: %s, current: %s", position, - profile.getUuid(), (currentProfile == null) ? "" : currentProfile.getUuid())); + profile.getUuid(), currentProfile.getUuid())); holder.itemView.setTag(profile); int hue = profile.getThemeHue(); @@ -192,7 +192,7 @@ public class ProfilesRecyclerViewAdapter holder.mEditButton.setOnClickListener(mOnClickListener); - final boolean sameProfile = (currentProfile != null) && currentProfile.equals(profile); + final boolean sameProfile = currentProfile.equals(profile); holder.itemView .setBackground(sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null); if (editingProfiles.getValue()) { diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java index ef93d643..16b9c2dc 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java @@ -32,7 +32,6 @@ public class TransactionListViewModel extends ViewModel { public static ObservableValue updateError = new ObservableValue<>(); public static void scheduleTransactionListReload() { - if (Data.profile.getValue() == null) return; String filter = Data.accountFilter.getValue(); AsyncTask task = new UTT(); diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/Colors.java b/app/src/main/java/net/ktnx/mobileledger/utils/Colors.java index d13496fe..476c56b3 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/Colors.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/Colors.java @@ -160,10 +160,10 @@ public class Colors { return result; } public static void setupTheme(Activity activity) { - MobileLedgerProfile profile = Data.profile.getValue(); + MobileLedgerProfile profile = Data.getProfile(); setupTheme(activity, profile); } - public static void setupTheme(Activity activity, MobileLedgerProfile profile) { + public static void setupTheme(Activity activity, @Nullable MobileLedgerProfile profile) { final int themeHue = (profile == null) ? -1 : profile.getThemeHue(); setupTheme(activity, themeHue); } diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java b/app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java index 03b207c8..178b7b20 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java @@ -165,9 +165,7 @@ public final class MLDB { String sql; String[] params; if (profileSpecific) { - MobileLedgerProfile p = (profile == null) ? Data.profile.getValue() : profile; - if (p == null) - throw new AssertionError(); + MobileLedgerProfile p = (profile == null) ? Data.getProfile() : profile; sql = String.format( "SELECT rowid as _id, %s, CASE WHEN %s_upper LIKE ?||'%%' THEN 1 " + "WHEN %s_upper LIKE '%%:'||?||'%%' then 2 " + -- 2.39.5