Show current expiration date selected in date picker while modifying it
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / ShareFileFragment.java
index ee979c6..d9a9b44 100644 (file)
@@ -47,10 +47,13 @@ import com.owncloud.android.lib.resources.shares.OCShare;
 import com.owncloud.android.lib.resources.shares.ShareType;
 import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.adapter.ShareUserListAdapter;
+import com.owncloud.android.ui.dialog.ExpirationDatePickerDialogFragment;
 import com.owncloud.android.utils.DisplayUtils;
 import com.owncloud.android.utils.MimetypeIconUtil;
 
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Date;
 
 /**
  * Fragment for Sharing a file with sharees (users or groups) or creating
@@ -92,6 +95,18 @@ public class ShareFileFragment extends Fragment
     /** Public share bound to the file */
     private OCShare mPublicShare;
 
+    /** Listener for changes on switch to share / unshare publicly */
+    private CompoundButton.OnCheckedChangeListener mOnShareViaLinkSwitchCheckedChangeListener;
+
+    /**
+     * Listener for user actions to set, update or clear password on public link
+     */
+    private OnPasswordInteractionListener mOnPasswordInteractionListener = null;
+
+    /**
+     * Listener for user actions to set, update or clear expiration date on public link
+     */
+    private OnExpirationDateInteractionListener mOnExpirationDateInteractionListener = null;
 
     /**
      * Public factory method to create new ShareFileFragment instances.
@@ -125,6 +140,7 @@ public class ShareFileFragment extends Fragment
         }
     }
 
+
     /**
      * {@inheritDoc}
      */
@@ -175,67 +191,190 @@ public class ShareFileFragment extends Fragment
         });
 
         // Switch to create public share
-        Switch shareViaLinkSwitch = (Switch) view.findViewById(R.id.shareViaLinkSectionSwitch);
-        shareViaLinkSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+        mOnShareViaLinkSwitchCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
             @Override
             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+                if (!isResumed()) {
+                    // very important, setCheched(...) is called automatically during
+                    // Fragment recreation on device rotations
+                    return;
+                 }
                 if (isChecked) {
-                    // TODO real implementation: create public share
-                    // expand section
-                    getExpirationDateSection().setVisibility(View.VISIBLE);
-                    getPasswordSection().setVisibility(View.VISIBLE);
-                    getGetLinkButton().setVisibility(View.VISIBLE);
+                    ((FileActivity) getActivity()).getFileOperationsHelper().
+                            shareFileViaLink(mFile);
 
                 } else {
-                    // TODO real implementation: unshare
-                    // collapse section
-                    getExpirationDateSection().setVisibility(View.GONE);
-                    getPasswordSection().setVisibility(View.GONE);
-                    getGetLinkButton().setVisibility(View.GONE);
+                    ((FileActivity) getActivity()).getFileOperationsHelper().
+                            unshareFileViaLink(mFile);
                 }
             }
-        });
+        };
+        Switch shareViaLinkSwitch = (Switch) view.findViewById(R.id.shareViaLinkSectionSwitch);
+        shareViaLinkSwitch.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener);
 
-        // Switch for expiration date
-        Switch shareViaLinkExpirationSwitch = (Switch) view.findViewById(R.id.shareViaLinkExpirationSwitch);
-        shareViaLinkExpirationSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
-            @Override
-            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
-                if (isChecked) {
-                    // TODO real implementation: update share with expiration date
-                    // show value of expiration date
-                    getExpirationDateValue().setText(R.string.placeholder_timestamp);
+        // Set listener for user actions on expiration date
+        initExpirationListener(view);
 
-                } else {
-                    // TODO real implementation: update share without expiration date
-                    // empty value
-                    getExpirationDateValue().setText(R.string.empty);
-                }
+        // Set listener for user actions on password
+        initPasswordListener(view);
+
+        return view;
+    }
+
+
+    /**
+     * Binds listener for user actions that start any update on a expiration date
+     * for the public link to the views receiving the user events.
+     *
+     * @param shareView     Root view in the fragment.
+     */
+    private void initExpirationListener(View shareView) {
+        mOnExpirationDateInteractionListener = new OnExpirationDateInteractionListener();
+
+        ((Switch) shareView.findViewById(R.id.shareViaLinkExpirationSwitch)).
+                setOnCheckedChangeListener(mOnExpirationDateInteractionListener);
+
+        shareView.findViewById(R.id.shareViaLinkExpirationLabel).
+                setOnClickListener(mOnExpirationDateInteractionListener);
+
+        shareView.findViewById(R.id.shareViaLinkExpirationValue).
+                setOnClickListener(mOnExpirationDateInteractionListener);
+    }
+
+    /**
+     * Listener for user actions that start any update on the expiration date for the public link.
+     */
+    private class OnExpirationDateInteractionListener
+            implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {
+
+        /**
+         * Called by R.id.shareViaLinkExpirationSwitch to set or clear the expiration date.
+         *
+         * @param switchView    {@link Switch} toggled by the user, R.id.shareViaLinkExpirationSwitch
+         * @param isChecked     New switch state.
+         */
+        @Override
+        public void onCheckedChanged(CompoundButton switchView, boolean isChecked) {
+            if (!isResumed()) {
+                // very important, setCheched(...) is called automatically during
+                // Fragment recreation on device rotations
+                return;
+            }
+            if (isChecked) {
+                ExpirationDatePickerDialogFragment dialog =
+                        ExpirationDatePickerDialogFragment.newInstance(mFile, -1);
+                dialog.show(
+                        getActivity().getSupportFragmentManager(),
+                        ExpirationDatePickerDialogFragment.DATE_PICKER_DIALOG
+                );
+
+            } else {
+                ((FileActivity) getActivity()).getFileOperationsHelper().
+                        setExpirationDateToShareViaLink(mFile, -1);
             }
-        });
 
-        // Switch for password
-        Switch shareViaLinkPasswordSwitch = (Switch) view.findViewById(R.id.shareViaLinkPasswordSwitch);
-        shareViaLinkPasswordSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
-            @Override
-            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
-                if (isChecked) {
-                    // TODO real implementation: update share with password
-                    // show
-                    getExpirationPasswordValue().setVisibility(View.VISIBLE);
+            // undo the toggle to grant the view will be correct if the dialog is cancelled
+            switchView.setOnCheckedChangeListener(null);
+            switchView.toggle();
+            switchView.setOnCheckedChangeListener(mOnExpirationDateInteractionListener);
+        }
 
-                } else {
-                    // TODO real implementation: update share without password
-                    // empty value
-                    getExpirationPasswordValue().setVisibility(View.INVISIBLE);
+        /**
+         * Called by R.id.shareViaLinkExpirationLabel or R.id.shareViaLinkExpirationValue
+         * to change the current expiration date.
+         *
+         * @param expirationView      Label or value view touched by the user.
+         */
+        @Override
+        public void onClick(View expirationView) {
+            if (mPublicShare != null && mPublicShare.getExpirationDate() > 0) {
+                long chosenDateInMillis = -1;
+                if (mPublicShare != null) {
+                    chosenDateInMillis = mPublicShare.getExpirationDate();
                 }
+                ExpirationDatePickerDialogFragment dialog =
+                        ExpirationDatePickerDialogFragment.newInstance(
+                                mFile,
+                                chosenDateInMillis
+                        );
+                dialog.show(
+                        getActivity().getSupportFragmentManager(),
+                        ExpirationDatePickerDialogFragment.DATE_PICKER_DIALOG
+                );
             }
-        });
+        }
+    }
 
 
-        return view;
+    /**
+     * Binds listener for user actions that start any update on a password for the public link
+     * to the views receiving the user events.
+     *
+     * @param shareView     Root view in the fragment.
+     */
+    private void initPasswordListener(View shareView) {
+        mOnPasswordInteractionListener = new OnPasswordInteractionListener();
+
+        ((Switch) shareView.findViewById(R.id.shareViaLinkPasswordSwitch)).
+                setOnCheckedChangeListener(mOnPasswordInteractionListener);
+
+        shareView.findViewById(R.id.shareViaLinkPasswordLabel).
+                setOnClickListener(mOnPasswordInteractionListener);
+
+        shareView.findViewById(R.id.shareViaLinkPasswordValue).
+                setOnClickListener(mOnPasswordInteractionListener);
+    }
+
+
+    /**
+     * Listener for user actions that start any update on a password for the public link.
+     */
+    private class OnPasswordInteractionListener
+            implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {
+
+        /**
+         * Called by R.id.shareViaLinkPasswordSwitch to set or clear the password.
+         *
+         * @param switchView    {@link Switch} toggled by the user, R.id.shareViaLinkPasswordSwitch
+         * @param isChecked     New switch state.
+         */
+        @Override
+        public void onCheckedChanged(CompoundButton switchView, boolean isChecked) {
+            if (!isResumed()) {
+                // very important, setCheched(...) is called automatically during
+                // Fragment recreation on device rotations
+                return;
+            }
+            if (isChecked) {
+                ((FileActivity) getActivity()).getFileOperationsHelper().
+                        requestPasswordForShareViaLink(mFile);
+            } else {
+                ((FileActivity) getActivity()).getFileOperationsHelper().
+                        setPasswordToShareViaLink(mFile, "");   // "" clears
+            }
+
+            // undo the toggle to grant the view will be correct if the dialog is cancelled
+            switchView.setOnCheckedChangeListener(null);
+            switchView.toggle();
+            switchView.setOnCheckedChangeListener(mOnPasswordInteractionListener);
+        }
+
+        /**
+         * Called by R.id.shareViaLinkPasswordLabel or R.id.shareViaLinkPasswordValue
+         * to change the current password.
+         *
+         * @param passwordView      Label or value view touched by the user.
+         */
+        @Override
+        public void onClick(View passwordView) {
+            if (mPublicShare != null && mPublicShare.isPasswordProtected()) {
+                ((FileActivity) getActivity()).getFileOperationsHelper().
+                        requestPasswordForShareViaLink(mFile);
+            }
+        }
     }
 
+
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
@@ -265,7 +404,7 @@ public class ShareFileFragment extends Fragment
     }
 
     /**
-     * Get users and groups from the DB to fill in the "share with" list
+     * Get users and groups from the DB to fill in the "share with" list.
      *
      * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
      * instance ready to use. If not ready, does nothing.
@@ -318,7 +457,7 @@ public class ShareFileFragment extends Fragment
 
 
     /**
-     * Get public link from the DB to fill in the "Share link" section
+     * Get public link from the DB to fill in the "Share link" section in the UI.
      *
      * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
      * instance ready to use. If not ready, does nothing.
@@ -332,7 +471,7 @@ public class ShareFileFragment extends Fragment
                     ""
             );
 
-            // Update list of users/groups
+            // Update public share section
             updatePublicShareSection();
         }
     }
@@ -343,21 +482,86 @@ public class ShareFileFragment extends Fragment
      */
     private void updatePublicShareSection() {
         if (mPublicShare != null && ShareType.PUBLIC_LINK.equals(mPublicShare.getShareType())) {
-            // public share bound -> expand section
-            getShareViaLinkSwitch().setChecked(true);
+            /// public share bound -> expand section
+            Switch shareViaLinkSwitch = getShareViaLinkSwitch();
+            if (!shareViaLinkSwitch.isChecked()) {
+                // set null listener before setChecked() to prevent infinite loop of calls
+                shareViaLinkSwitch.setOnCheckedChangeListener(null);
+                shareViaLinkSwitch.setChecked(true);
+                shareViaLinkSwitch.setOnCheckedChangeListener(
+                        mOnShareViaLinkSwitchCheckedChangeListener
+                );
+            }
             getExpirationDateSection().setVisibility(View.VISIBLE);
             getPasswordSection().setVisibility(View.VISIBLE);
             getGetLinkButton().setVisibility(View.VISIBLE);
 
+            /// update state of expiration date switch and message depending on expiration date
+            /// update state of expiration date switch and message depending on expiration date
+            Switch expirationDateSwitch = getExpirationDateSwitch();
+            // set null listener before setChecked() to prevent infinite loop of calls
+            expirationDateSwitch.setOnCheckedChangeListener(null);
+            long expirationDate = mPublicShare.getExpirationDate();
+            if (expirationDate > 0) {
+                if (!expirationDateSwitch.isChecked()) {
+                    expirationDateSwitch.toggle();
+                }
+                String formattedDate =
+                        SimpleDateFormat.getDateInstance().format(
+                                new Date(expirationDate)
+                        );
+                getExpirationDateValue().setText(formattedDate);
+            } else {
+                if (expirationDateSwitch.isChecked()) {
+                    expirationDateSwitch.toggle();
+                }
+                getExpirationDateValue().setText(R.string.empty);
+            }
+            // recover listener
+            expirationDateSwitch.setOnCheckedChangeListener(
+                    mOnExpirationDateInteractionListener
+            );
+
+            /// update state of password switch and message depending on password protection
+            Switch passwordSwitch = getPasswordSwitch();
+            // set null listener before setChecked() to prevent infinite loop of calls
+            passwordSwitch.setOnCheckedChangeListener(null);
+            if (mPublicShare.isPasswordProtected()) {
+                if (!passwordSwitch.isChecked()) {
+                    passwordSwitch.toggle();
+                }
+                getPasswordValue().setVisibility(View.VISIBLE);
+            } else {
+                if (passwordSwitch.isChecked()) {
+                    passwordSwitch.toggle();
+                }
+                getPasswordValue().setVisibility(View.INVISIBLE);
+            }
+            // recover listener
+            passwordSwitch.setOnCheckedChangeListener(
+                    mOnPasswordInteractionListener
+            );
+
+
         } else {
-            // no public share -> collapse section
-            getShareViaLinkSwitch().setChecked(false);
+            /// no public share -> collapse section
+            Switch shareViaLinkSwitch = getShareViaLinkSwitch();
+            if (shareViaLinkSwitch.isChecked()) {
+                shareViaLinkSwitch.setOnCheckedChangeListener(null);
+                getShareViaLinkSwitch().setChecked(false);
+                shareViaLinkSwitch.setOnCheckedChangeListener(
+                        mOnShareViaLinkSwitchCheckedChangeListener
+                );
+            }
             getExpirationDateSection().setVisibility(View.GONE);
             getPasswordSection().setVisibility(View.GONE);
             getGetLinkButton().setVisibility(View.GONE);
         }
     }
 
+
+    /// BEWARE: next methods will failed with NullPointerException if called before onCreateView() finishes
+
     private Switch getShareViaLinkSwitch() {
         return (Switch) getView().findViewById(R.id.shareViaLinkSectionSwitch);
     }
@@ -366,6 +570,10 @@ public class ShareFileFragment extends Fragment
         return getView().findViewById(R.id.shareViaLinkExpirationSection);
     }
 
+    private Switch getExpirationDateSwitch() {
+        return (Switch) getView().findViewById(R.id.shareViaLinkExpirationSwitch);
+    }
+
     private TextView getExpirationDateValue() {
         return (TextView) getView().findViewById(R.id.shareViaLinkExpirationValue);
     }
@@ -374,7 +582,11 @@ public class ShareFileFragment extends Fragment
         return getView().findViewById(R.id.shareViaLinkPasswordSection);
     }
 
-    private TextView getExpirationPasswordValue() {
+    private Switch getPasswordSwitch() {
+        return (Switch) getView().findViewById(R.id.shareViaLinkPasswordSwitch);
+    }
+
+    private TextView getPasswordValue() {
         return (TextView) getView().findViewById(R.id.shareViaLinkPasswordValue);
     }