Browse Source

Show current expiration date selected in date picker while modifying it

David A. Velasco 9 years ago
parent
commit
6c1c63ac16

+ 8 - 9
src/com/owncloud/android/files/FileOperationsHelper.java

@@ -378,20 +378,19 @@ public class FileOperationsHelper {
      * Updates a public share on a file to set its expiration date.
      * Starts a request to do it in {@link OperationsService}
      *
-     * @param file          File which public share will be constrained with an expiration date.
-     * @param year          Year of the date expiration chosen. Negative value to remove current
-     *                      expiration date and leave the link unrestricted.
-     * @param monthOfYear   Month of the date chosen [0, 11]
-     * @param dayOfMonth    Day of the date chosen
+     * @param file                      File which public share will be constrained with an expiration date.
+     * @param expirationTimeInMillis    Expiration date to set. A negative value clears the current expiration
+     *                                  date, leaving the link unrestricted. Zero makes no change.
      */
-    public void setExpirationDateToShareViaLink(OCFile file, int year, int monthOfYear, int dayOfMonth) {
+    public void setExpirationDateToShareViaLink(OCFile file, long expirationTimeInMillis) {
         Intent updateShareIntent = new Intent(mFileActivity, OperationsService.class);
         updateShareIntent.setAction(OperationsService.ACTION_UPDATE_SHARE);
         updateShareIntent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
         updateShareIntent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
-        updateShareIntent.putExtra(OperationsService.EXTRA_SHARE_EXPIRATION_YEAR, year);
-        updateShareIntent.putExtra(OperationsService.EXTRA_SHARE_EXPIRATION_MONTH_OF_YEAR, monthOfYear);
-        updateShareIntent.putExtra(OperationsService.EXTRA_SHARE_EXPIRATION_DAY_OF_MONTH, dayOfMonth);
+        updateShareIntent.putExtra(
+                OperationsService.EXTRA_SHARE_EXPIRATION_DATE_IN_MILLIS,
+                expirationTimeInMillis
+        );
         queueShareIntent(updateShareIntent);
     }
 

+ 13 - 24
src/com/owncloud/android/operations/UpdateShareViaLinkOperation.java

@@ -42,7 +42,7 @@ public class UpdateShareViaLinkOperation extends SyncOperation {
 
     private String mPath;
     private String mPassword;
-    private Calendar mExpirationDate;
+    private long mExpirationDateInMillis;
 
     /**
      * Constructor
@@ -53,7 +53,7 @@ public class UpdateShareViaLinkOperation extends SyncOperation {
 
         mPath = path;
         mPassword = null;
-        mExpirationDate = null;
+        mExpirationDateInMillis = 0;
     }
 
 
@@ -72,12 +72,13 @@ public class UpdateShareViaLinkOperation extends SyncOperation {
     /**
      * Set expiration date to update in Share resource.
      *
-     * @param expirationDate    Expiration date to set to the public link.
-     *                          Start-of-epoch clears the current expiration date.
-     *                          Null results in no update applied to the expiration date.
+     * @param expirationDateInMillis    Expiration date to set to the public link.
+     *                                  A negative value clears the current expiration date.
+     *                                  Zero value (start-of-epoch) results in no update done on
+     *                                  the expiration date.
      */
-    public void setExpirationDate(Calendar expirationDate) {
-        mExpirationDate = expirationDate;
+    public void setExpirationDate(long expirationDateInMillis) {
+        mExpirationDateInMillis = expirationDateInMillis;
     }
 
 
@@ -91,8 +92,10 @@ public class UpdateShareViaLinkOperation extends SyncOperation {
         );
 
         if (publicShare == null) {
-            // TODO try to get remote?
-
+            // TODO try to get remote share before failing?
+            return new RemoteOperationResult(
+                    RemoteOperationResult.ResultCode.SHARE_NOT_FOUND
+            );
         }
 
         // Update remote share with password
@@ -100,23 +103,9 @@ public class UpdateShareViaLinkOperation extends SyncOperation {
             publicShare.getRemoteId()
         );
         udpateOp.setPassword(mPassword);
-        udpateOp.setExpirationDate(mExpirationDate);
+        udpateOp.setExpirationDate(mExpirationDateInMillis);
         RemoteOperationResult result = udpateOp.execute(client);
 
-        /*
-        if (!result.isSuccess() || result.getData().size() <= 0) {
-            operation = new CreateRemoteShareOperation(
-                    mPath,
-                    ShareType.PUBLIC_LINK,
-                    "",
-                    false,
-                    mPassword,
-                    OCShare.DEFAULT_PERMISSION
-            );
-            result = operation.execute(client);
-        }
-        */
-
         if (result.isSuccess()) {
             // Retrieve updated share / save directly with password? -> no; the password is not be saved
             RemoteOperation getShareOp = new GetRemoteShareOperation(publicShare.getRemoteId());

+ 8 - 27
src/com/owncloud/android/services/OperationsService.java

@@ -93,7 +93,7 @@ public class OperationsService extends Service {
     public static final String EXTRA_SHARE_PASSWORD = "SHARE_PASSWORD";
     public static final String EXTRA_SHARE_TYPE = "SHARE_TYPE";
     public static final String EXTRA_SHARE_WITH = "SHARE_WITH";
-    public static final String EXTRA_SHARE_EXPIRATION_YEAR = "SHARE_EXPIRATION_YEAR";
+    public static final String EXTRA_SHARE_EXPIRATION_DATE_IN_MILLIS = "SHARE_EXPIRATION_YEAR";
     public static final String EXTRA_SHARE_EXPIRATION_MONTH_OF_YEAR = "SHARE_EXPIRATION_MONTH_OF_YEAR";
     public static final String EXTRA_SHARE_EXPIRATION_DAY_OF_MONTH = "SHARE_EXPIRATION_DAY_OF_MONTH";
 
@@ -577,32 +577,13 @@ public class OperationsService extends Service {
                         String password = operationIntent.getStringExtra(EXTRA_SHARE_PASSWORD);
                         ((UpdateShareViaLinkOperation)operation).setPassword(password);
 
-                        int year = operationIntent.getIntExtra(EXTRA_SHARE_EXPIRATION_YEAR, 0);
-                        if (year > 0) {
-                            // expiration date is set
-                            int monthOfYear = operationIntent.getIntExtra(
-                                    EXTRA_SHARE_EXPIRATION_MONTH_OF_YEAR, 0
-                            );
-                            int dayOfMonth = operationIntent.getIntExtra(
-                                    EXTRA_SHARE_EXPIRATION_DAY_OF_MONTH, 1
-                            );
-                            Calendar expirationDate = Calendar.getInstance();
-                            expirationDate.set(Calendar.YEAR, year);
-                            expirationDate.set(Calendar.MONTH, monthOfYear);
-                            expirationDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
-                            ((UpdateShareViaLinkOperation)operation).setExpirationDate(
-                                    expirationDate
-                            );
-
-                        } else if (year < 0) {
-                            // expiration date to be cleared
-                            Calendar zeroDate = Calendar.getInstance();
-                            zeroDate.clear();
-                            ((UpdateShareViaLinkOperation)operation).setExpirationDate(
-                                    zeroDate
-                            );
-
-                        } // else, no update on expiration date
+                        long expirationDate = operationIntent.getLongExtra(
+                                EXTRA_SHARE_EXPIRATION_DATE_IN_MILLIS,
+                                0
+                        );
+                        ((UpdateShareViaLinkOperation)operation).setExpirationDate(
+                                expirationDate
+                        );
                     }
 
                 } else if (action.equals(ACTION_CREATE_SHARE_WITH_SHAREE)) {

+ 31 - 17
src/com/owncloud/android/ui/dialog/ExpirationDatePickerDialogFragment.java

@@ -46,8 +46,11 @@ public class ExpirationDatePickerDialogFragment
     /** Tag for FragmentsManager */
     public static final String DATE_PICKER_DIALOG = "DATE_PICKER_DIALOG";
 
-    /** Constructor arguments */
-    private static final String ARG_FILE = "ARG_FILE";
+    /** Parameter constant for {@link OCFile} instance to set the expiration date */
+    private static final String ARG_FILE = "FILE";
+
+    /** Parameter constant for date chosen initially */
+    private static final String ARG_CHOSEN_DATE_IN_MILLIS = "CHOSEN_DATE_IN_MILLIS";
 
     /** File to bind an expiration date */
     private OCFile mFile;
@@ -55,12 +58,14 @@ public class ExpirationDatePickerDialogFragment
     /**
      *  Factory method to create new instances
      *
-     *  @param file     File to bind an expiration date
-     *  @return         New dialog instance
+     *  @param file                 File to bind an expiration date
+     *  @param chosenDateInMillis   Date chosen when the dialog appears
+     *  @return                     New dialog instance
      */
-    public static ExpirationDatePickerDialogFragment newInstance(OCFile file) {
+    public static ExpirationDatePickerDialogFragment newInstance(OCFile file, long chosenDateInMillis) {
         Bundle arguments = new Bundle();
         arguments.putParcelable(ARG_FILE, file);
+        arguments.putLong(ARG_CHOSEN_DATE_IN_MILLIS, chosenDateInMillis);
 
         ExpirationDatePickerDialogFragment dialog = new ExpirationDatePickerDialogFragment();
         dialog.setArguments(arguments);
@@ -77,18 +82,28 @@ public class ExpirationDatePickerDialogFragment
         // Load arguments
         mFile = getArguments().getParcelable(ARG_FILE);
 
-        // Get current day
-        final Calendar c = Calendar.getInstance();
-        int year = c.get(Calendar.YEAR);
-        int month = c.get(Calendar.MONTH);
-        int day = c.get(Calendar.DAY_OF_MONTH);
-
-        // Create a new instance of DatePickerDialog, highlighting "tomorrow" as chosen day
-        DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day + 1);
+        // Chosen date received as an argument must be later than tomorrow ; default to tomorrow in other case
+        final Calendar chosenDate = Calendar.getInstance();
+        long tomorrowInMillis = chosenDate.getTimeInMillis() + DateUtils.DAY_IN_MILLIS;
+        long chosenDateInMillis = getArguments().getLong(ARG_CHOSEN_DATE_IN_MILLIS);
+        if (chosenDateInMillis > tomorrowInMillis) {
+            chosenDate.setTimeInMillis(chosenDateInMillis);
+        } else {
+            chosenDate.setTimeInMillis(tomorrowInMillis);
+        }
+
+        // Create a new instance of DatePickerDialog
+        DatePickerDialog dialog = new DatePickerDialog(
+                getActivity(),
+                this,
+                chosenDate.get(Calendar.YEAR),
+                chosenDate.get(Calendar.MONTH),
+                chosenDate.get(Calendar.DAY_OF_MONTH)
+        );
 
         // Prevent days in the past may be chosen
         DatePicker picker = dialog.getDatePicker();
-        picker.setMinDate(System.currentTimeMillis() + DateUtils.DAY_IN_MILLIS - 1000);
+        picker.setMinDate(tomorrowInMillis - 1000);
 
         // Enforce spinners view; ignored by MD-based theme in Android >=5, but calendar is REALLY buggy
         // in Android < 5, so let's be sure it never appears (in tablets both spinners and calendar are
@@ -113,12 +128,11 @@ public class ExpirationDatePickerDialogFragment
         chosenDate.set(Calendar.YEAR, year);
         chosenDate.set(Calendar.MONTH, monthOfYear);
         chosenDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
+        long chosenDateInMillis = chosenDate.getTimeInMillis();
 
         ((FileActivity)getActivity()).getFileOperationsHelper().setExpirationDateToShareViaLink(
                 mFile,
-                year,
-                monthOfYear,
-                dayOfMonth
+                chosenDateInMillis
         );
     }
 }

+ 10 - 4
src/com/owncloud/android/ui/fragment/ShareFileFragment.java

@@ -262,7 +262,7 @@ public class ShareFileFragment extends Fragment
             }
             if (isChecked) {
                 ExpirationDatePickerDialogFragment dialog =
-                        ExpirationDatePickerDialogFragment.newInstance(mFile);
+                        ExpirationDatePickerDialogFragment.newInstance(mFile, -1);
                 dialog.show(
                         getActivity().getSupportFragmentManager(),
                         ExpirationDatePickerDialogFragment.DATE_PICKER_DIALOG
@@ -270,7 +270,7 @@ public class ShareFileFragment extends Fragment
 
             } else {
                 ((FileActivity) getActivity()).getFileOperationsHelper().
-                        setExpirationDateToShareViaLink(mFile, -1, -1, -1);
+                        setExpirationDateToShareViaLink(mFile, -1);
             }
 
             // undo the toggle to grant the view will be correct if the dialog is cancelled
@@ -288,9 +288,15 @@ public class ShareFileFragment extends Fragment
         @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);
-                // TODO set the current chosen value in the dialog
+                        ExpirationDatePickerDialogFragment.newInstance(
+                                mFile,
+                                chosenDateInMillis
+                        );
                 dialog.show(
                         getActivity().getSupportFragmentManager(),
                         ExpirationDatePickerDialogFragment.DATE_PICKER_DIALOG