Pārlūkot izejas kodu

TimSort, first fix values, then sort

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
tobiasKaminsky 6 gadi atpakaļ
vecāks
revīzija
06f45bd3af

+ 36 - 3
src/main/java/com/owncloud/android/db/OCUpload.java

@@ -4,7 +4,9 @@
  * @author LukeOwncloud
  * @author masensio
  * @author David A. Velasco
+ * @author Tobias Kaminsky
  * Copyright (C) 2016 ownCloud Inc.
+ * Copyright (C) 2018 Nextcloud GmbH.
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2,
@@ -117,6 +119,14 @@ public class OCUpload implements Parcelable {
      */
     private String mFolderUnlockToken;
 
+    /**
+     * temporary values, used for sorting
+     */
+    private UploadStatus mFixedUploadStatus;
+    private boolean mFixedUploadingNow;
+    private long mFixedUploadEndTimeStamp;
+    private long mFixedId;
+
     /**
      * Main constructor.
      *
@@ -170,6 +180,13 @@ public class OCUpload implements Parcelable {
         mFolderUnlockToken = "";
     }
 
+    public void setDataFixed(FileUploader.FileUploaderBinder binder) {
+        mFixedUploadStatus = mUploadStatus;
+        mFixedUploadingNow = binder != null && binder.isUploadingNow(this);
+        mFixedUploadEndTimeStamp = mUploadEndTimeStamp;
+        mFixedId = mId;
+    }
+
     // Getters & Setters
     public void setUploadId(long id) {
         mId = id;
@@ -229,7 +246,7 @@ public class OCUpload implements Parcelable {
     }
 
     /**
-     * @param remotePath
+     * @param remotePath the remotePath
      */
     public void setRemotePath(String remotePath) {
         mRemotePath = remotePath;
@@ -391,11 +408,11 @@ public class OCUpload implements Parcelable {
      *
      * @param source The source parcel
      */
-    protected OCUpload(Parcel source) {
+    private OCUpload(Parcel source) {
         readFromParcel(source);
     }
 
-    public void readFromParcel(Parcel source) {
+    private void readFromParcel(Parcel source) {
         mId = source.readLong();
         mLocalPath = source.readString();
         mRemotePath = source.readString();
@@ -443,5 +460,21 @@ public class OCUpload implements Parcelable {
         dest.writeString(mFolderUnlockToken);
     }
 
+    public UploadStatus getFixedUploadStatus() {
+        return mFixedUploadStatus;
+    }
+
+    public boolean isFixedUploadingNow() {
+        return mFixedUploadingNow;
+    }
+
+    public long getFixedUploadEndTimestamp() {
+        return mFixedUploadEndTimeStamp;
+    }
+
+    public Long getFixedUploadId() {
+        return mFixedId;
+    }
+
     enum CanUploadFileNowStatus {NOW, LATER, FILE_GONE, ERROR}
 }

+ 5 - 5
src/main/java/com/owncloud/android/files/services/FileUploader.java

@@ -870,11 +870,11 @@ public class FileUploader extends Service
 
         public boolean isUploadingNow(OCUpload upload) {
             return (
-                upload != null  &&
-                mCurrentAccount != null &&
-                mCurrentUpload != null &&
-                upload.getAccountName().equals(mCurrentAccount.name) &&
-                upload.getRemotePath().equals(mCurrentUpload.getRemotePath())
+                    upload != null &&
+                            mCurrentAccount != null &&
+                            mCurrentUpload != null &&
+                            upload.getAccountName().equals(mCurrentAccount.name) &&
+                            upload.getRemotePath().equals(mCurrentUpload.getRemotePath())
             );
         }
 

+ 24 - 19
src/main/java/com/owncloud/android/ui/adapter/UploadListAdapter.java

@@ -113,24 +113,21 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
         mUploadGroups[0] = new UploadGroup(mParentActivity.getString(R.string.uploads_view_group_current_uploads)) {
             @Override
             public void refresh() {
-                setItems(mUploadsStorageManager.getCurrentAndPendingUploadsForCurrentAccount());
-                Arrays.sort(getItems(), comparator);
+                fixAndSortItems(mUploadsStorageManager.getCurrentAndPendingUploadsForCurrentAccount());
             }
         };
 
         mUploadGroups[1] = new UploadGroup(mParentActivity.getString(R.string.uploads_view_group_failed_uploads)) {
             @Override
             public void refresh() {
-                setItems(mUploadsStorageManager.getFailedButNotDelayedUploadsForCurrentAccount());
-                Arrays.sort(getItems(), comparator);
+                fixAndSortItems(mUploadsStorageManager.getFailedButNotDelayedUploadsForCurrentAccount());
             }
         };
 
         mUploadGroups[2] = new UploadGroup(mParentActivity.getString(R.string.uploads_view_group_finished_uploads)) {
             @Override
             public void refresh() {
-                setItems(mUploadsStorageManager.getFinishedUploadsForCurrentAccount());
-                Arrays.sort(getItems(), comparator);
+                fixAndSortItems(mUploadsStorageManager.getFinishedUploadsForCurrentAccount());
             }
         };
         loadUploadItemsFromDb();
@@ -612,6 +609,17 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
             this.items = items;
         }
 
+        void fixAndSortItems(OCUpload[] array) {
+            FileUploader.FileUploaderBinder binder = mParentActivity.getFileUploaderBinder();
+
+            for (OCUpload upload : array) {
+                upload.setDataFixed(binder);
+            }
+            Arrays.sort(array, comparator);
+
+            setItems(array);
+        }
+
         private int getGroupItemCount() {
             return items == null ? 0 : items.length;
         }
@@ -628,23 +636,20 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
                 if (upload2 == null) {
                     return 1;
                 }
-                if (UploadStatus.UPLOAD_IN_PROGRESS.equals(upload1.getUploadStatus())) {
-                    if (!UploadStatus.UPLOAD_IN_PROGRESS.equals(upload2.getUploadStatus())) {
+                if (UploadStatus.UPLOAD_IN_PROGRESS.equals(upload1.getFixedUploadStatus())) {
+                    if (!UploadStatus.UPLOAD_IN_PROGRESS.equals(upload2.getFixedUploadStatus())) {
                         return -1;
                     }
                     // both are in progress
-                    FileUploader.FileUploaderBinder binder = mParentActivity.getFileUploaderBinder();
-                    if (binder != null) {
-                        if (binder.isUploadingNow(upload1)) {
-                            return -1;
-                        } else if (binder.isUploadingNow(upload2)) {
-                            return 1;
-                        }
+                    if (upload1.isFixedUploadingNow()) {
+                        return -1;
+                    } else if (upload2.isFixedUploadingNow()) {
+                        return 1;
                     }
-                } else if (upload2.getUploadStatus().equals(UploadStatus.UPLOAD_IN_PROGRESS)) {
+                } else if (upload2.getFixedUploadStatus().equals(UploadStatus.UPLOAD_IN_PROGRESS)) {
                     return 1;
                 }
-                if (upload1.getUploadEndTimestamp() == 0 || upload2.getUploadEndTimestamp() == 0) {
+                if (upload1.getFixedUploadEndTimestamp() == 0 || upload2.getFixedUploadEndTimestamp() == 0) {
                     return compareUploadId(upload1, upload2);
                 } else {
                     return compareUpdateTime(upload1, upload2);
@@ -653,12 +658,12 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
 
             @SuppressFBWarnings("Bx")
             private int compareUploadId(OCUpload upload1, OCUpload upload2) {
-                return Long.valueOf(upload1.getUploadId()).compareTo(upload2.getUploadId());
+                return Long.valueOf(upload1.getFixedUploadId()).compareTo(upload2.getFixedUploadId());
             }
 
             @SuppressFBWarnings("Bx")
             private int compareUpdateTime(OCUpload upload1, OCUpload upload2) {
-                return Long.valueOf(upload2.getUploadEndTimestamp()).compareTo(upload1.getUploadEndTimestamp());
+                return Long.valueOf(upload2.getFixedUploadEndTimestamp()).compareTo(upload1.getFixedUploadEndTimestamp());
             }
         };
     }