]> git.ktnx.net Git - mobile-ledger.git/commitdiff
Data: migrate profile to a private variable, provide methods for accessing it
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 15 Aug 2020 09:46:40 +0000 (09:46 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 15 Aug 2020 12:48:13 +0000 (12:48 +0000)
18 files changed:
app/src/main/java/net/ktnx/mobileledger/App.java
app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java
app/src/main/java/net/ktnx/mobileledger/model/Currency.java
app/src/main/java/net/ktnx/mobileledger/model/Data.java
app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java
app/src/main/java/net/ktnx/mobileledger/ui/CurrencySelectorFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileThemedActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java
app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java
app/src/main/java/net/ktnx/mobileledger/utils/Colors.java
app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java

index b47e6d7faf14533a272517e4ac971badb0266061..f0f7c013230f92913d82e6978e1da715f0420bee 100644 (file)
@@ -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();
index ab114531b9e78ad711fe013cc7f6fd525c7f926f..404e5127b4fdaa061399802dcc097d66d8a04e71 100644 (file)
@@ -34,9 +34,7 @@ import static net.ktnx.mobileledger.utils.Logger.debug;
 
 public class UpdateTransactionsTask extends AsyncTask<String, Void, String> {
     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();
index e32513f2170a9ecba710631ea86a168df6f14ef7..596cdf1c493c50c5b308a5b3f63c717ece831b5b 100644 (file)
@@ -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() {
index a6d3fd9825dd4ce69e59712d1654986132d3ee9e..02ba81d454ad9acddbeadad301091df7a7c251d4 100644 (file)
@@ -53,7 +53,6 @@ public final class Data {
     public static final MutableLiveData<Boolean> backgroundTasksRunning =
             new MutableLiveData<>(false);
     public static final MutableLiveData<Date> lastUpdateDate = new MutableLiveData<>();
-    public static final MutableLiveData<MobileLedgerProfile> profile = new InertMutableLiveData<>();
     public static final MutableLiveData<ArrayList<MobileLedgerProfile>> profiles =
             new MutableLiveData<>(null);
     public static final MutableLiveData<String> accountFilter = new MutableLiveData<>();
@@ -61,10 +60,16 @@ public final class Data {
             new MutableLiveData<>();
     public static final MutableLiveData<Boolean> currencyGap = new MutableLiveData<>(true);
     public static final MutableLiveData<Locale> locale = new MutableLiveData<>(Locale.getDefault());
+    private static final MutableLiveData<MobileLedgerProfile> profile =
+            new InertMutableLiveData<>();
     private static final AtomicInteger backgroundTaskCount = new AtomicInteger(0);
     private static final Locker profilesLocker = new Locker();
     public static MutableLiveData<Integer> foundTransactionItemIndex = new MutableLiveData<>(null);
     private static RetrieveTransactionsTask retrieveTransactionsTask;
+    @NonNull
+    public static MobileLedgerProfile getProfile() {
+        return Objects.requireNonNull(profile.getValue());
+    }
     public static final MutableLiveData<Boolean> 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<MobileLedgerProfile> 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
index 638a692cff56d21417e15786183c2bb0a14109a2..e31d1582c4cfe551b83d0aa6dc32f7654396cc7a 100644 (file)
@@ -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);
index 76a640ce287998432b17ac64516ebb263a5429aa..9c527fe076e53b9d2c349cdfc59b14d60feaf823 100644 (file)
@@ -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();
index dbc200eb8f6e8ff592ae02048c9682efd8326877..eb61dcc6b0bbee9f37a4ff0188687971afe5e113 100644 (file)
@@ -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<LedgerAccount> accounts) {
         Logger.debug("async-acc",
index 33bcbb005c32a855819091f4bedebf21b4a1d802..72ace25ba1077159a79df6a64ceafe21d7ddc7df 100644 (file)
@@ -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);
 
index 89362d777b699780a508fe169a1b5045b2f5d9b2..45e75b5b9b585b3532bf0c6cf76ed849e11abb34 100644 (file)
@@ -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);
index e54e8d6115af6c42c6b07b7020263613b0f7d827..9f69644436b000eb316afebad568bc554ba2b795 100644 (file)
@@ -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);
         });
index f283aee53e124f609bd4084b46ada0920743f5fd..e311eb94ba450c9542ef9f0151328ddb2003853d 100644 (file)
@@ -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();
index 3350664eac43c41a32d9032f8c3edbfee6696988..535b8614530b8ed722f8f800508edec8b2792a90 100644 (file)
@@ -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() {
index f4d482a5b7a93b356684115fb3f0c0c822d0c26d..d33608a6ea63e8be6c31c861976dbc2efb3c072b 100644 (file)
@@ -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();
     }
 }
index 13b6a8bae93ef11a42e9ab262a9b60d0103bc684..7a7afef6221f2ec7c25b133b49a2119b2b4d2676 100644 (file)
@@ -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<MobileLedgerProfile> 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();
index 974c979322f79ff0e2f7c74aab3dc07e8cd93d2f..b2b5cb7fe18f9ee86867b041ad134c1625c6270e 100644 (file)
@@ -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<MobileLedgerProfile> 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) ? "<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()) {
index ef93d6436b4be23b148f82c8193810dc18c391e4..16b9c2dc77ec6c9e48aba1793e036e27a9bf3e42 100644 (file)
@@ -32,7 +32,6 @@ public class TransactionListViewModel extends ViewModel {
     public static ObservableValue<String> updateError = new ObservableValue<>();
 
     public static void scheduleTransactionListReload() {
-        if (Data.profile.getValue() == null) return;
 
         String filter = Data.accountFilter.getValue();
         AsyncTask<String, Void, String> task = new UTT();
index d13496fe813b8a3b158acffd7442ad6434c85a23..476c56b3a247407bac8a94dd6b5e2a8e3cb2aae4 100644 (file)
@@ -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);
     }
index 03b207c8f02c017e1643e131e1a8ac6b3acde32e..178b7b2017484f6e93fa16bf09a60dbc8671b76e 100644 (file)
@@ -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 " +