Skip to content

Commit 1e0db99

Browse files
committed
fix getPreviousFile
Signed-off-by: alperozturk <[email protected]>
1 parent 5c2549b commit 1e0db99

File tree

2 files changed

+47
-116
lines changed

2 files changed

+47
-116
lines changed

app/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.kt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ import com.owncloud.android.R
8989
import com.owncloud.android.databinding.FilesBinding
9090
import com.owncloud.android.datamodel.FileDataStorageManager
9191
import com.owncloud.android.datamodel.OCFile
92-
import com.owncloud.android.datamodel.OCFileDepth
9392
import com.owncloud.android.datamodel.SyncedFolderProvider
9493
import com.owncloud.android.datamodel.VirtualFolderType
9594
import com.owncloud.android.files.services.NameCollisionPolicy
@@ -1186,15 +1185,6 @@ class FileDisplayActivity :
11861185
}
11871186
}
11881187

1189-
// shared root
1190-
fragment is SharedListFragment && fragment.fileDepth == OCFileDepth.Root -> {
1191-
openDrawer()
1192-
}
1193-
1194-
fragment is SharedListFragment && fragment.fileDepth == OCFileDepth.FirstLevel -> {
1195-
openSharedTab()
1196-
}
1197-
11981188
// Normal folder navigation (go up) also works for shared tab
11991189
else -> {
12001190
browseUp(fragment)

app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java

Lines changed: 47 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,6 @@
126126
import java.util.List;
127127
import java.util.Objects;
128128
import java.util.Set;
129-
import java.util.concurrent.CompletableFuture;
130-
import java.util.concurrent.Executors;
131-
import java.util.concurrent.Future;
132129

133130
import javax.inject.Inject;
134131

@@ -954,128 +951,72 @@ private void updateSortAndGridMenuItems() {
954951
}
955952
}
956953

957-
private boolean shouldNavigateWithoutFilter(OCFile topParent) {
958-
int menuItemId = DrawerActivity.menuItemId;
959-
return (menuItemId != R.id.nav_shared && menuItemId != R.id.nav_favorites) ||
960-
(menuItemId == R.id.nav_shared && topParent != null && topParent.isShared()) ||
961-
(menuItemId == R.id.nav_favorites && topParent != null && topParent.isFavorite());
962-
}
963954

964-
private boolean shouldNavigateWithFilter() {
965-
int menuItemId = DrawerActivity.menuItemId;
966-
return menuItemId == R.id.nav_shared || menuItemId == R.id.nav_favorites;
955+
/**
956+
* Call this, when the user presses the up button.
957+
* <p>
958+
* Tries to move up the current folder one level. If the parent folder was removed from the database, it continues
959+
* browsing up until finding an existing folders.
960+
* <p>
961+
* return Count of folder levels browsed up.
962+
*/
963+
public int onBrowseUp() {
964+
if (mFile == null) {
965+
return 0;
966+
}
967+
968+
Pair<Integer, OCFile> result = getPreviousFile();
969+
mFile = result.second;
970+
setFileDepth(mFile);
971+
972+
// since on browse down sets it to the false, browse up should set back to true if current search type is not NO_SEARCH
973+
if (mFile.isRootDirectory() && currentSearchType != NO_SEARCH) {
974+
searchFragment = true;
975+
}
976+
977+
updateFileList();
978+
return result.first;
967979
}
968980

969-
private Pair<Integer, OCFile> getPreviousFileWithoutFilter(FileDataStorageManager storageManager) {
981+
private Pair<Integer, OCFile> getPreviousFile() {
982+
if (mFile == null) {
983+
return new Pair<>(0, null);
984+
}
985+
986+
FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
970987
int moveCount = 0;
971-
OCFile parentDir = null;
972-
String parentPath = null;
988+
String parentPath;
989+
OCFile parentDir;
973990

974991
if (mFile.getParentId() != FileDataStorageManager.ROOT_PARENT_ID) {
975992
parentPath = new File(mFile.getRemotePath()).getParent();
976-
977-
if (parentPath != null) {
978-
parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
979-
parentDir = storageManager.getFileByPath(parentPath);
980-
moveCount++;
981-
}
993+
parentPath = ensureTrailingSeparator(parentPath);
994+
parentDir = storageManager.getFileByPath(parentPath);
995+
moveCount++;
982996
} else {
983997
parentDir = storageManager.getFileByPath(ROOT_PATH);
998+
parentPath = ROOT_PATH;
984999
}
9851000

986-
while (parentDir == null) {
1001+
// Keep going up until we find a valid folder
1002+
while (parentDir == null && !ROOT_PATH.equals(parentPath)) {
9871003
parentPath = new File(parentPath).getParent();
988-
989-
if (parentPath != null) {
990-
parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath :
991-
parentPath + OCFile.PATH_SEPARATOR;
992-
parentDir = storageManager.getFileByPath(parentPath);
993-
moveCount++;
1004+
if (parentPath == null) {
1005+
parentPath = ROOT_PATH; // fallback to root
9941006
}
1007+
parentPath = ensureTrailingSeparator(parentPath);
1008+
parentDir = storageManager.getFileByPath(parentPath);
1009+
moveCount++;
9951010
}
9961011

9971012
return new Pair<>(moveCount, parentDir);
9981013
}
9991014

1000-
private OCFile getPreviousFileWithFilter(FileDataStorageManager storageManager, OCFile currentFile) {
1001-
while (true) {
1002-
OCFile parent = storageManager.getFileById(currentFile.getParentId());
1003-
if (parent == null) {
1004-
return currentFile;
1005-
}
1006-
1007-
if (parent.isRootDirectory()) {
1008-
return parent;
1009-
}
1010-
1011-
if ((DrawerActivity.menuItemId == R.id.nav_shared && parent.isShared()) ||
1012-
(DrawerActivity.menuItemId == R.id.nav_favorites && parent.isFavorite())) {
1013-
return parent;
1014-
}
1015-
1016-
currentFile = parent;
1017-
}
1018-
}
1019-
1020-
private Future<Pair<Integer, OCFile>> getPreviousFile() {
1021-
CompletableFuture<Pair<Integer, OCFile>> completableFuture = new CompletableFuture<>();
1022-
1023-
Executors.newCachedThreadPool().execute(() -> {
1024-
var result = new Pair<Integer, OCFile>(null, null);
1025-
1026-
FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
1027-
OCFile currentFile = getCurrentFile();
1028-
OCFile topParent = storageManager.getTopParent(currentFile);
1029-
1030-
if (shouldNavigateWithoutFilter(topParent)) {
1031-
result = getPreviousFileWithoutFilter(storageManager);
1032-
} else if (shouldNavigateWithFilter()) {
1033-
OCFile previousFileWithFilter = getPreviousFileWithFilter(storageManager, currentFile);
1034-
result = new Pair<>(0, previousFileWithFilter);
1035-
}
1036-
1037-
completableFuture.complete(result);
1038-
1039-
});
1040-
1041-
return completableFuture;
1042-
}
1043-
1044-
/**
1045-
* Call this, when the user presses the up button.
1046-
* <p>
1047-
* Tries to move up the current folder one level. If the parent folder was removed from the database, it continues
1048-
* browsing up until finding an existing folders.
1049-
* <p>
1050-
* return Count of folder levels browsed up.
1051-
*/
1052-
public int onBrowseUp() {
1053-
if (mFile == null) {
1054-
return 0;
1055-
}
1056-
1057-
try {
1058-
Future<Pair<Integer, OCFile>> futureResult = getPreviousFile();
1059-
Pair<Integer, OCFile> result = futureResult.get();
1060-
mFile = result.second;
1061-
setFileDepth(mFile);
1062-
1063-
// since on browse down sets it to the false, browse up should set back to true if current search type is not NO_SEARCH
1064-
if (mFile.isRootDirectory() && currentSearchType != NO_SEARCH) {
1065-
searchFragment = true;
1066-
}
1067-
1068-
updateFileList();
1069-
return result.first;
1070-
} catch (Exception e) {
1071-
Log_OC.e(TAG,"Error caught in onBrowseUp " + e + " getPreviousFileWithoutFilter() used: ");
1072-
1073-
FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
1074-
var result = getPreviousFileWithoutFilter(storageManager);
1075-
mFile = result.second;
1076-
updateFileList();
1077-
return result.first;
1015+
private String ensureTrailingSeparator(String path) {
1016+
if (path == null) {
1017+
return ROOT_PATH;
10781018
}
1019+
return path.endsWith(OCFile.PATH_SEPARATOR) ? path : path + OCFile.PATH_SEPARATOR;
10791020
}
10801021

10811022
private void updateFileList() {

0 commit comments

Comments
 (0)