]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java
a welcome screen directs to the new profile activity when there are no profiles defined
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / profiles / ProfileDetailFragment.java
index 5a1b729a205cb4bc9e3c1e434012659500593a7b..67f28439553cf010d5e530f1d71dcd43ff52a24d 100644 (file)
@@ -18,9 +18,9 @@
 package net.ktnx.mobileledger.ui.profiles;
 
 import android.app.Activity;
-import android.content.Context;
 import android.os.Bundle;
 import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
 import android.support.design.widget.CollapsingToolbarLayout;
 import android.support.design.widget.FloatingActionButton;
 import android.support.v4.app.Fragment;
@@ -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();
@@ -109,11 +109,13 @@ public class ProfileDetailFragment extends Fragment {
             }
         }
     }
-
     @Override
-    public void onAttach(Context context) {
-        super.onAttach(context);
-        fab = ((Activity) context).findViewById(R.id.fab);
+    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
+        super.onActivityCreated(savedInstanceState);
+        Activity context = getActivity();
+        if (context == null) return;
+
+        fab = context.findViewById(R.id.fab);
         fab.setOnClickListener(v -> {
             if (mProfile != null) {
                 mProfile.setName(profileName.getText());
@@ -122,9 +124,12 @@ 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())) {
+                    // dummy update to notify the observers of the possibly new name/URL
                     Data.profile.set(mProfile);
                 }
             }