]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / profiles / ProfileDetailFragment.java
index 100918956c71b71659185b47464182393596237d..99934543e7b544bf876fcc0010282d9cb373801e 100644 (file)
@@ -19,6 +19,7 @@ package net.ktnx.mobileledger.ui.profiles;
 
 import android.app.Activity;
 import android.app.AlertDialog;
+import android.app.backup.BackupManager;
 import android.graphics.Typeface;
 import android.os.Bundle;
 import android.text.Editable;
@@ -45,6 +46,7 @@ import com.google.android.material.textfield.TextInputLayout;
 
 import net.ktnx.mobileledger.BuildConfig;
 import net.ktnx.mobileledger.R;
+import net.ktnx.mobileledger.dao.BaseDAO;
 import net.ktnx.mobileledger.dao.ProfileDAO;
 import net.ktnx.mobileledger.databinding.ProfileDetailBinding;
 import net.ktnx.mobileledger.db.DB;
@@ -63,6 +65,7 @@ import org.jetbrains.annotations.NotNull;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.List;
+import java.util.Objects;
 
 import static net.ktnx.mobileledger.utils.Logger.debug;
 
@@ -80,7 +83,6 @@ public class ProfileDetailFragment extends Fragment {
     public static final String ARG_HUE = "hue";
     @NonNls
 
-    private Profile mProfile;
     private boolean defaultCommoditySet;
     private boolean syncingModelFromUI = false;
     private ProfileDetailBinding binding;
@@ -100,21 +102,37 @@ public class ProfileDetailFragment extends Fragment {
         menuDeleteProfile.setOnMenuItemClickListener(item -> onDeleteProfile());
         final List<Profile> profiles = Data.profiles.getValue();
 
-        if (BuildConfig.DEBUG) {
-            final MenuItem menuWipeProfileData = menu.findItem(R.id.menuWipeData);
+        final MenuItem menuWipeProfileData = menu.findItem(R.id.menuWipeData);
+        if (BuildConfig.DEBUG)
             menuWipeProfileData.setOnMenuItemClickListener(ignored -> onWipeDataMenuClicked());
-            menuWipeProfileData.setVisible(mProfile != null);
-        }
+
+        getModel().getProfileId()
+                  .observe(getViewLifecycleOwner(), id -> {
+                      menuDeleteProfile.setVisible(id > 0);
+                      if (BuildConfig.DEBUG)
+                          menuWipeProfileData.setVisible(id > 0);
+                  });
     }
     private boolean onDeleteProfile() {
         AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
-        builder.setTitle(mProfile.getName());
+        @NotNull ProfileDetailModel model = getModel();
+        builder.setTitle(model.getProfileName());
         builder.setMessage(R.string.remove_profile_dialog_message);
         builder.setPositiveButton(R.string.Remove, (dialog, which) -> {
-            debug("profiles", String.format("[fragment] removing profile %s", mProfile.getId()));
+            final long profileId = Objects.requireNonNull(model.getProfileId()
+                                                               .getValue());
+            debug("profiles", String.format("[fragment] removing profile %s", profileId));
             ProfileDAO dao = DB.get()
                                .getProfileDAO();
-            dao.delete(mProfile, () -> dao.updateOrderSync(dao.getAllOrderedSync()));
+            dao.getById(profileId)
+               .observe(getViewLifecycleOwner(), profile -> {
+                   if (profile != null)
+                       BaseDAO.runAsync(() -> DB.get()
+                                                .runInTransaction(() -> {
+                                                    dao.deleteSync(profile);
+                                                    dao.updateOrderSync(dao.getAllOrderedSync());
+                                                }));
+               });
 
             final FragmentActivity activity = getActivity();
             if (activity != null)
@@ -125,7 +143,14 @@ public class ProfileDetailFragment extends Fragment {
     }
     private boolean onWipeDataMenuClicked() {
         // this is a development option, so no confirmation
-        mProfile.wipeAllData();
+        DB.get()
+          .getProfileDAO()
+          .getById(Objects.requireNonNull(getModel().getProfileId()
+                                                    .getValue()))
+          .observe(getViewLifecycleOwner(), profile -> {
+              if (profile != null)
+                  profile.wipeAllData();
+          });
         return true;
     }
     private void hookTextChangeSyncRoutine(TextView view, TextChangeSyncRoutine syncRoutine) {
@@ -316,6 +341,9 @@ public class ProfileDetailFragment extends Fragment {
             if (itemId == R.id.api_version_menu_html) {
                 apiVer = API.html;
             }
+            else if (itemId == R.id.api_version_menu_1_23) {
+                apiVer = API.v1_23;
+            }
             else if (itemId == R.id.api_version_menu_1_19_1) {
                 apiVer = API.v1_19_1;
             }
@@ -373,18 +401,19 @@ public class ProfileDetailFragment extends Fragment {
         ProfileDAO dao = DB.get()
                            .getProfileDAO();
 
-        if (mProfile != null) {
-            model.updateProfile(mProfile);
-            dao.update(mProfile, null);
+        Profile profile = new Profile();
+        model.updateProfile(profile);
+        if (profile.getId() > 0) {
+            dao.update(profile);
             debug("profiles", "profile stored in DB");
 //                debug("profiles", String.format("Selected item is %d", mProfile.getThemeHue()));
         }
         else {
-            mProfile = new Profile();
-            model.updateProfile(mProfile);
-            dao.insertLast(mProfile, null);
+            dao.insertLast(profile, null);
         }
 
+        BackupManager.dataChanged(BuildConfig.APPLICATION_ID);
+
         Activity activity = getActivity();
         if (activity != null)
             activity.finish();