Skip to content

Commit dfbc614

Browse files
Merge pull request nextcloud#15090 from nextcloud/bugfix/execute-pending-transcation-in-fda
BugFix - Execute Pending Transaction In FileDisplayActivity
2 parents 87ce71c + 2403500 commit dfbc614

2 files changed

Lines changed: 67 additions & 47 deletions

File tree

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

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
*/
1414
package com.owncloud.android.ui.activity;
1515

16-
import android.Manifest;
1716
import android.accounts.Account;
1817
import android.accounts.AuthenticatorException;
1918
import android.annotation.SuppressLint;
@@ -32,9 +31,7 @@
3231
import android.os.Build;
3332
import android.os.Bundle;
3433
import android.os.Environment;
35-
import android.os.Handler;
3634
import android.os.IBinder;
37-
import android.os.Looper;
3835
import android.os.Parcelable;
3936
import android.text.TextUtils;
4037
import android.view.Menu;
@@ -65,7 +62,6 @@
6562
import com.nextcloud.client.utils.IntentUtil;
6663
import com.nextcloud.model.WorkerState;
6764
import com.nextcloud.model.WorkerStateLiveData;
68-
import com.nextcloud.utils.BuildHelper;
6965
import com.nextcloud.utils.extensions.ActivityExtensionsKt;
7066
import com.nextcloud.utils.extensions.BundleExtensionsKt;
7167
import com.nextcloud.utils.extensions.FileExtensionsKt;
@@ -76,9 +72,7 @@
7672
import com.owncloud.android.R;
7773
import com.owncloud.android.databinding.FilesBinding;
7874
import com.owncloud.android.datamodel.FileDataStorageManager;
79-
import com.owncloud.android.datamodel.MediaFolderType;
8075
import com.owncloud.android.datamodel.OCFile;
81-
import com.owncloud.android.datamodel.SyncedFolder;
8276
import com.owncloud.android.datamodel.SyncedFolderProvider;
8377
import com.owncloud.android.datamodel.VirtualFolderType;
8478
import com.owncloud.android.files.services.NameCollisionPolicy;
@@ -124,6 +118,7 @@
124118
import com.owncloud.android.ui.fragment.UnifiedSearchFragment;
125119
import com.owncloud.android.ui.helpers.FileOperationsHelper;
126120
import com.owncloud.android.ui.helpers.UriUploader;
121+
import com.owncloud.android.ui.interfaces.TransactionInterface;
127122
import com.owncloud.android.ui.preview.PreviewImageActivity;
128123
import com.owncloud.android.ui.preview.PreviewImageFragment;
129124
import com.owncloud.android.ui.preview.PreviewMediaActivity;
@@ -489,7 +484,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
489484
// If request is cancelled, result arrays are empty.
490485
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
491486
// permission was granted
492-
getOCFileListFragmentFromFile().directCameraUpload();
487+
getOCFileListFragmentFromFile(OCFileListFragment::directCameraUpload);
493488
}
494489
break;
495490
default:
@@ -635,6 +630,11 @@ private void setLeftFragment(Fragment fragment) {
635630
}
636631

637632
private void setLeftFragment(Fragment fragment, boolean showSortListGroup) {
633+
prepareFragmentBeforeCommit(showSortListGroup);
634+
commitFragment(fragment, isFragmentCommitted -> Log_OC.d(TAG, "Left fragment committed: " + isFragmentCommitted));
635+
}
636+
637+
private void prepareFragmentBeforeCommit(boolean showSortListGroup) {
638638
if (searchView != null) {
639639
searchView.post(() -> searchView.setQuery(searchQuery, true));
640640
}
@@ -644,51 +644,57 @@ private void setLeftFragment(Fragment fragment, boolean showSortListGroup) {
644644
clearToolbarSubtitle();
645645

646646
showSortListGroup(showSortListGroup);
647+
}
647648

649+
private void commitFragment(@NonNull Fragment fragment, CompletionCallback callback) {
648650
FragmentManager fragmentManager = getSupportFragmentManager();
649651
if (ActivityExtensionsKt.isActive(this) && !fragmentManager.isDestroyed()) {
650652
FragmentTransaction transaction = fragmentManager.beginTransaction();
651653
transaction.addToBackStack(null);
652654
transaction.replace(R.id.left_fragment_container, fragment, TAG_LIST_OF_FILES);
653655
transaction.commit();
656+
callback.onComplete(true);
657+
} else {
658+
callback.onComplete(false);
654659
}
655660
}
656661

657-
private OCFileListFragment getOCFileListFragmentFromFile() {
662+
private void getOCFileListFragmentFromFile(TransactionInterface transaction) {
658663
final Fragment leftFragment = getLeftFragment();
659-
OCFileListFragment listOfFiles;
660664

661-
if (leftFragment instanceof OCFileListFragment) {
662-
listOfFiles = (OCFileListFragment) leftFragment;
663-
} else {
664-
listOfFiles = new OCFileListFragment();
665-
Bundle args = new Bundle();
666-
args.putBoolean(OCFileListFragment.ARG_ALLOW_CONTEXTUAL_ACTIONS, true);
667-
listOfFiles.setArguments(args);
665+
if (leftFragment instanceof OCFileListFragment result) {
666+
transaction.onOCFileListFragmentComplete(result);
667+
return;
668+
}
668669

669-
FragmentManager fm = getSupportFragmentManager();
670-
boolean isExecutingTransactions = !fm.isStateSaved() && !fm.executePendingTransactions();
670+
OCFileListFragment listOfFiles = new OCFileListFragment();
671+
Bundle args = new Bundle();
672+
args.putBoolean(OCFileListFragment.ARG_ALLOW_CONTEXTUAL_ACTIONS, true);
673+
listOfFiles.setArguments(args);
671674

672-
if (isExecutingTransactions) {
673-
setLeftFragment(listOfFiles);
674-
fm.executePendingTransactions();
675-
} else {
676-
new Handler(Looper.getMainLooper()).post(() -> {
677-
setLeftFragment(listOfFiles);
678-
fm.executePendingTransactions();
675+
runOnUiThread(() -> {
676+
FragmentManager fm = getSupportFragmentManager();
677+
if (!fm.isStateSaved() && !fm.isDestroyed()) {
678+
prepareFragmentBeforeCommit(true);
679+
commitFragment(listOfFiles, isFragmentCommitted -> {
680+
if (isFragmentCommitted) {
681+
Log_OC.d(TAG, "OCFileListFragment committed, executing pending transaction");
682+
fm.executePendingTransactions();
683+
transaction.onOCFileListFragmentComplete(listOfFiles);
684+
} else {
685+
Log_OC.d(TAG, "OCFileListFragment not committed, skipping executing pending transaction");
686+
}
679687
});
680688
}
681-
}
682-
683-
return listOfFiles;
689+
});
684690
}
685691

686-
687692
public void showFileActions(OCFile file) {
688693
dismissLoadingDialog();
689-
OCFileListFragment listOfFiles = getOCFileListFragmentFromFile();
690-
browseUp(listOfFiles);
691-
listOfFiles.onOverflowIconClicked(file, null);
694+
getOCFileListFragmentFromFile(listOfFiles -> {
695+
browseUp(listOfFiles);
696+
listOfFiles.onOverflowIconClicked(file, null);
697+
});
692698
}
693699

694700
public @Nullable Fragment getLeftFragment() {
@@ -2721,22 +2727,22 @@ public void setMainFabVisible(final boolean visible) {
27212727
public void showFile(OCFile selectedFile, String message) {
27222728
dismissLoadingDialog();
27232729

2724-
OCFileListFragment listOfFiles = getOCFileListFragmentFromFile();
2725-
2726-
if (TextUtils.isEmpty(message)) {
2727-
OCFile temp = getFile();
2728-
setFile(getCurrentDir());
2729-
listOfFiles.listDirectory(getCurrentDir(), temp, MainApp.isOnlyOnDevice(), false);
2730-
updateActionBarTitleAndHomeButton(null);
2731-
} else {
2732-
final var view = listOfFiles.getView();
2733-
if (view != null) {
2734-
DisplayUtils.showSnackMessage(view, message);
2730+
getOCFileListFragmentFromFile(listOfFiles -> {
2731+
if (TextUtils.isEmpty(message)) {
2732+
OCFile temp = getFile();
2733+
setFile(getCurrentDir());
2734+
listOfFiles.listDirectory(getCurrentDir(), temp, MainApp.isOnlyOnDevice(), false);
2735+
updateActionBarTitleAndHomeButton(null);
2736+
} else {
2737+
final var view = listOfFiles.getView();
2738+
if (view != null) {
2739+
DisplayUtils.showSnackMessage(view, message);
2740+
}
27352741
}
2736-
}
27372742

2738-
if (selectedFile != null) {
2739-
listOfFiles.onItemClicked(selectedFile);
2740-
}
2743+
if (selectedFile != null) {
2744+
listOfFiles.onItemClicked(selectedFile);
2745+
}
2746+
});
27412747
}
27422748
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Nextcloud - Android Client
3+
*
4+
* SPDX-FileCopyrightText: 2025 Alper Ozturk <alper.ozturk@nextcloud.com>
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
package com.owncloud.android.ui.interfaces
9+
10+
import com.owncloud.android.ui.fragment.OCFileListFragment
11+
12+
interface TransactionInterface {
13+
fun onOCFileListFragmentComplete(fragment: OCFileListFragment)
14+
}

0 commit comments

Comments
 (0)