1313 */
1414package com .owncloud .android .ui .activity ;
1515
16- import android .Manifest ;
1716import android .accounts .Account ;
1817import android .accounts .AuthenticatorException ;
1918import android .annotation .SuppressLint ;
3231import android .os .Build ;
3332import android .os .Bundle ;
3433import android .os .Environment ;
35- import android .os .Handler ;
3634import android .os .IBinder ;
37- import android .os .Looper ;
3835import android .os .Parcelable ;
3936import android .text .TextUtils ;
4037import android .view .Menu ;
6562import com .nextcloud .client .utils .IntentUtil ;
6663import com .nextcloud .model .WorkerState ;
6764import com .nextcloud .model .WorkerStateLiveData ;
68- import com .nextcloud .utils .BuildHelper ;
6965import com .nextcloud .utils .extensions .ActivityExtensionsKt ;
7066import com .nextcloud .utils .extensions .BundleExtensionsKt ;
7167import com .nextcloud .utils .extensions .FileExtensionsKt ;
7672import com .owncloud .android .R ;
7773import com .owncloud .android .databinding .FilesBinding ;
7874import com .owncloud .android .datamodel .FileDataStorageManager ;
79- import com .owncloud .android .datamodel .MediaFolderType ;
8075import com .owncloud .android .datamodel .OCFile ;
81- import com .owncloud .android .datamodel .SyncedFolder ;
8276import com .owncloud .android .datamodel .SyncedFolderProvider ;
8377import com .owncloud .android .datamodel .VirtualFolderType ;
8478import com .owncloud .android .files .services .NameCollisionPolicy ;
124118import com .owncloud .android .ui .fragment .UnifiedSearchFragment ;
125119import com .owncloud .android .ui .helpers .FileOperationsHelper ;
126120import com .owncloud .android .ui .helpers .UriUploader ;
121+ import com .owncloud .android .ui .interfaces .TransactionInterface ;
127122import com .owncloud .android .ui .preview .PreviewImageActivity ;
128123import com .owncloud .android .ui .preview .PreviewImageFragment ;
129124import 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}
0 commit comments