]> git.ktnx.net Git - mobile-ledger.git/commitdiff
multiple profile list fixes
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 10 Jan 2019 21:28:06 +0000 (21:28 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 10 Jan 2019 21:28:06 +0000 (21:28 +0000)
show fab upon activity start (for example after editing/deleting a profile)

fix the flow of events when the current profile is changed or deleted

app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java
app/src/main/java/net/ktnx/mobileledger/utils/ObservableList.java

index bbcf57fa7696041816c31d705d16996bc7b70ee1..36e317af42be4a0714a76a1e483891ab1953bee0 100644 (file)
@@ -273,7 +273,7 @@ public final class MobileLedgerProfile {
     }
     public void removeFromDB() {
         SQLiteDatabase db = MLDB.getWritableDatabase();
-        Log.d("db", String.format("removinf progile %s from DB", uuid));
+        Log.d("db", String.format("removing progile %s from DB", uuid));
         db.execSQL("delete from profiles where uuid=?", new Object[]{uuid});
     }
 }
index 7e9469a296524d706f8fa7adb3cc6e147458a269..0991a694a3b28d2ea0f5000d5ae4d9a3e4185ed4 100644 (file)
@@ -54,8 +54,6 @@ import java.text.DateFormat;
 import java.time.ZoneId;
 import java.time.format.DateTimeFormatter;
 import java.util.Date;
-import java.util.Observable;
-import java.util.Observer;
 
 public class MainActivity extends AppCompatActivity {
     public MobileLedgerListFragment currentFragment = null;
@@ -96,15 +94,12 @@ public class MainActivity extends AppCompatActivity {
         Toolbar toolbar = findViewById(R.id.toolbar);
         setSupportActionBar(toolbar);
 
-        Data.profile.addObserver(new Observer() {
-            @Override
-            public void update(Observable o, Object arg) {
-                MobileLedgerProfile profile = Data.profile.get();
-                runOnUiThread(() -> {
-                    if (profile == null) toolbar.setSubtitle("");
-                    else toolbar.setSubtitle(profile.getName());
-                });
-            }
+        Data.profile.addObserver((o, arg) -> {
+            MobileLedgerProfile profile = Data.profile.get();
+            runOnUiThread(() -> {
+                if (profile == null) toolbar.setSubtitle("");
+                else toolbar.setSubtitle(profile.getName());
+            });
         });
 
         setupProfile();
@@ -215,8 +210,7 @@ public class MainActivity extends AppCompatActivity {
 
         if (profile == null) throw new AssertionError("profile must have a value");
 
-        Data.profile.set(profile);
-        MLDB.set_option_value(MLDB.OPT_PROFILE_UUID, profile.getUuid());
+        Data.setCurrentProfile(profile);
 
         if (profile.getUrl().isEmpty()) {
             Intent intent = new Intent(this, ProfileListActivity.class);
index 5ba6714d45534169c3cca10996235981bd31fd3f..cb37f5f18d0456c16609bacbdd4647169951334c 100644 (file)
@@ -39,10 +39,10 @@ import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
 import net.ktnx.mobileledger.ui.profiles.ProfileDetailActivity;
 import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
-import net.ktnx.mobileledger.utils.MLDB;
 
 import java.util.Collections;
-import java.util.List;
+import java.util.Observable;
+import java.util.Observer;
 
 /**
  * An activity representing a list of Profiles. This activity
@@ -98,12 +98,10 @@ public class ProfileListActivity extends AppCompatActivity {
             Log.d("profiles", "got edit profile action");
             int index = getIntent().getIntExtra(ARG_PROFILE_INDEX, -1);
             if (index >= 0) {
-                List<MobileLedgerProfile> list = MobileLedgerProfile.loadAllFromDB();
-                if (index < list.size()) {
-                    ProfilesRecyclerViewAdapter adapter =
-                            (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
-                    if (adapter != null) adapter.editProfile(recyclerView, list.get(index));
-                }
+                MobileLedgerProfile profile = Data.profiles.get(index);
+                ProfilesRecyclerViewAdapter adapter =
+                        (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
+                if (adapter != null) adapter.editProfile(recyclerView, profile);
             }
         }
     }
@@ -168,21 +166,23 @@ public class ProfileListActivity extends AppCompatActivity {
         private final ProfileListActivity mParentActivity;
         private final boolean mTwoPane;
         private final View.OnClickListener mOnClickListener = view -> {
-            MobileLedgerProfile item = (MobileLedgerProfile) ((View) view.getParent()).getTag();
-            editProfile(view, item);
+            MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag();
+            editProfile(view, profile);
         };
         ProfilesRecyclerViewAdapter(ProfileListActivity parent, boolean twoPane) {
             mParentActivity = parent;
             mTwoPane = twoPane;
             Data.profiles.addObserver((o, arg) -> {
                 Log.d("profiles", "profile list changed");
-                notifyDataSetChanged();
+                if (arg == null) notifyDataSetChanged();
+                else notifyItemChanged((int) arg);
             });
         }
-        private void editProfile(View view, MobileLedgerProfile item) {
+        private void editProfile(View view, MobileLedgerProfile profile) {
+            int index = Data.profiles.indexOf(profile);
             if (mTwoPane) {
                 Bundle arguments = new Bundle();
-                arguments.putString(ProfileDetailFragment.ARG_ITEM_ID, item.getUuid());
+                arguments.putInt(ProfileDetailFragment.ARG_ITEM_ID, index);
                 ProfileDetailFragment fragment = new ProfileDetailFragment();
                 fragment.setArguments(arguments);
                 mParentActivity.getSupportFragmentManager().beginTransaction()
@@ -191,8 +191,7 @@ public class ProfileListActivity extends AppCompatActivity {
             else {
                 Context context = view.getContext();
                 Intent intent = new Intent(context, ProfileDetailActivity.class);
-                intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID,
-                        (item == null) ? null : item.getUuid());
+                if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
 
                 context.startActivity(intent);
             }
@@ -206,8 +205,7 @@ public class ProfileListActivity extends AppCompatActivity {
             Data.profile.addObserver((o, arg) -> {
                 MobileLedgerProfile newProfile = Data.profile.get();
                 MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag();
-                holder.mRadioView.setChecked(
-                        newProfile != null && newProfile.getUuid().equals(profile.getUuid()));
+                holder.mRadioView.setChecked(profile.equals(newProfile));
             });
             return holder;
         }
@@ -217,21 +215,24 @@ public class ProfileListActivity extends AppCompatActivity {
             final MobileLedgerProfile currentProfile = Data.profile.get();
             Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
                     currentProfile.getUuid()));
-            View.OnClickListener profileSelector = v -> holder.mRadioView.setChecked(true);
+            View.OnClickListener profileSelector = v -> {
+                holder.mRadioView.setChecked(true);
+                Data.setCurrentProfile(profile);
+            };
+            Data.profile.addObserver(new Observer() {
+                @Override
+                public void update(Observable o, Object arg) {
+                    holder.mRadioView.setChecked(Data.profile.get().equals(profile));
+                }
+            });
             holder.mTitle.setText(profile.getName());
             holder.mTitle.setOnClickListener(profileSelector);
             holder.mSubTitle.setText(profile.getUrl());
             holder.mSubTitle.setOnClickListener(profileSelector);
             holder.mRadioView.setChecked(profile.getUuid().equals(currentProfile.getUuid()));
-            holder.mRadioView.setOnCheckedChangeListener((buttonView, isChecked) -> {
-                if (!isChecked) return;
-                MLDB.set_option_value(MLDB.OPT_PROFILE_UUID, profile.getUuid());
-                Data.profile.set(profile);
-                    });
 
             holder.itemView.setTag(profile);
             holder.mEditButton.setOnClickListener(mOnClickListener);
-
         }
         @Override
         public int getItemCount() {
index 982fccfa4512f86a0e90fe7e18597f279dd94c48..1db1429acfd78b2c9c398008c4c0518b7f3d3af9 100644 (file)
@@ -74,28 +74,19 @@ public class ProfileDetailActivity extends AppCompatActivity {
         // http://developer.android.com/guide/components/fragments.html
         //
         if (savedInstanceState == null) {
-            final String profileUUID =
-                    getIntent().getStringExtra(ProfileDetailFragment.ARG_ITEM_ID);
+            final int index = getIntent().getIntExtra(ProfileDetailFragment.ARG_ITEM_ID, -1);
 
-            if (profileUUID != null) {
-                int i = 0;
-                for (MobileLedgerProfile p : Data.profiles.getList()) {
-                    if (p.getUuid().equals(profileUUID)) {
-                        Log.d("profiles", String.format("found profile %s at %d", profileUUID, i));
-                        profile = p;
-                        break;
-                    }
-                    i++;
-                }
+            if (index != -1) {
+                profile = Data.profiles.get(index);
                 if (profile == null) throw new AssertionError(
-                        String.format("Can't get profile " + "(uuid:%s) from the " + "global list",
-                                profileUUID));
+                        String.format("Can't get profile " + "(index:%d) from the global list",
+                                index));
             }
 
             // Create the detail fragment and add it to the activity
             // using a fragment transaction.
             Bundle arguments = new Bundle();
-            arguments.putString(ProfileDetailFragment.ARG_ITEM_ID, profileUUID);
+            arguments.putInt(ProfileDetailFragment.ARG_ITEM_ID, index);
             ProfileDetailFragment fragment = new ProfileDetailFragment();
             fragment.setArguments(arguments);
             getSupportFragmentManager().beginTransaction()
@@ -109,10 +100,13 @@ public class ProfileDetailActivity extends AppCompatActivity {
         getMenuInflater().inflate(R.menu.profile_details, menu);
         MenuItem menuDeleteProfile = menu.findItem(R.id.menuDelete);
         menuDeleteProfile.setOnMenuItemClickListener(item -> {
-            Log.d("profiles", String.format("deleting profile %s", profile.getUuid()));
+            Log.d("profiles", String.format("[activity] deleting profile %s", profile.getUuid()));
             profile.removeFromDB();
             Data.profiles.remove(profile);
-            Data.profile.set(Data.profiles.get(0));
+            if (Data.profile.get().equals(profile)) {
+                Log.d("profiles", "[activity] selecting profile 0");
+                Data.setCurrentProfile(Data.profiles.get(0));
+            }
             finish();
             return true;
         });
index 5a1b729a205cb4bc9e3c1e434012659500593a7b..bf2affd0c77ca73d2fcf2ec22af85cd7c027777c 100644 (file)
@@ -81,8 +81,9 @@ public class ProfileDetailFragment extends Fragment {
             Log.d("profiles", String.format("[fragment] removing profile %s", mProfile.getUuid()));
             mProfile.removeFromDB();
             Data.profiles.remove(mProfile);
-            if (Data.profile.get().getUuid().equals(mProfile.getUuid())) {
-                Data.profile.set(Data.profiles.get(0));
+            if (Data.profile.get().equals(mProfile)) {
+                Log.d("profiles", "[fragment] setting current profile to 0");
+                Data.setCurrentProfile(Data.profiles.get(0));
             }
             return false;
         });
@@ -96,9 +97,8 @@ public class ProfileDetailFragment extends Fragment {
             // Load the dummy content specified by the fragment
             // arguments. In a real-world scenario, use a Loader
             // to load content from a content provider.
-            String uuid = getArguments().getString(ARG_ITEM_ID);
-            if (uuid != null) mProfile =
-                    MobileLedgerProfile.loadUUIDFromDB(getArguments().getString(ARG_ITEM_ID));
+            int index = getArguments().getInt(ARG_ITEM_ID, -1);
+            if (index != -1) mProfile = Data.profiles.get(index);
 
             Activity activity = this.getActivity();
             if (activity == null) throw new AssertionError();
@@ -122,6 +122,8 @@ public class ProfileDetailFragment extends Fragment {
                 mProfile.setAuthUserName(userName.getText());
                 mProfile.setAuthPassword(password.getText());
                 mProfile.storeInDB();
+                Log.d("profiles", "profile stored in DB");
+                Data.profiles.triggerItemChangedNotification(mProfile);
 
 
                 if (mProfile.getUuid().equals(Data.profile.get().getUuid())) {
index 6d40d858aa9a18e5fa5c92e0ccc794def9917595..383148fa8b3a3827a32fbd083ccbac345d7b51a4 100644 (file)
@@ -21,6 +21,7 @@ import android.os.Build;
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
 import android.support.annotation.RequiresApi;
+import android.util.Log;
 
 import java.util.Collection;
 import java.util.Comparator;
@@ -43,9 +44,9 @@ public class ObservableList<T> extends Observable {
         setChanged();
         notifyObservers();
     }
-    private void forceNotify(Object arg) {
+    private void forceNotify(int index) {
         setChanged();
-        notifyObservers(arg);
+        notifyObservers(index);
     }
     public int size() {
         return list.size();
@@ -167,11 +168,23 @@ public class ObservableList<T> extends Observable {
     public void forEach(Consumer<? super T> action) {
         list.forEach(action);
     }
+    public List<T> getList() {
+        return list;
+    }
     public void setList(List<T> aList) {
         list = aList;
         forceNotify();
     }
-    public List<T> getList() {
-        return list;
+    public void triggerItemChangedNotification(T item) {
+        int index = list.indexOf(item);
+        if (index == -1) {
+            Log.d("ObList", "??? not sending notifications for item not found in the list");
+            return;
+        }
+        Log.d("ObList", "Notifying item change observers");
+        triggerItemChangedNotification(index);
+    }
+    public void triggerItemChangedNotification(int index) {
+        forceNotify(index);
     }
 }
\ No newline at end of file