diff --git a/app/src/main/java/com/owncloud/android/ui/fragment/FileDetailFragment.java b/app/src/main/java/com/owncloud/android/ui/fragment/FileDetailFragment.java index 08150abc4031..11d7f1bde749 100644 --- a/app/src/main/java/com/owncloud/android/ui/fragment/FileDetailFragment.java +++ b/app/src/main/java/com/owncloud/android/ui/fragment/FileDetailFragment.java @@ -32,6 +32,8 @@ import com.nextcloud.client.network.ClientFactory; import com.nextcloud.client.network.ConnectivityService; import com.nextcloud.client.preferences.AppPreferences; +import com.nextcloud.model.WorkerState; +import com.nextcloud.model.WorkerStateLiveData; import com.nextcloud.ui.fileactions.FileActionsBottomSheet; import com.nextcloud.utils.MenuUtils; import com.nextcloud.utils.extensions.BundleExtensionsKt; @@ -581,7 +583,11 @@ public void updateFileDetails(boolean transferring, boolean refresh) { setupViewPager(); - getView().invalidate(); + if (getView() != null) { + getView().invalidate(); + } + + observeWorkerState(); } private void setFileModificationTimestamp(OCFile file, boolean showDetailedTimestamp) { @@ -681,14 +687,19 @@ private void setButtonsForTransferring() { // show the progress bar for the transfer binding.progressBlock.setVisibility(View.VISIBLE); binding.progressText.setVisibility(View.VISIBLE); - if (FileDownloadHelper.Companion.instance().isDownloading(user, getFile())) { + } + } + + private void observeWorkerState() { + WorkerStateLiveData.Companion.instance().observe(getViewLifecycleOwner(), state -> { + if (state instanceof WorkerState.DownloadStarted) { binding.progressText.setText(R.string.downloader_download_in_progress_ticker); + } else if (state instanceof WorkerState.UploadStarted) { + binding.progressText.setText(R.string.uploader_upload_in_progress_ticker); } else { - if (FileUploadHelper.Companion.instance().isUploading(user, getFile())) { - binding.progressText.setText(R.string.uploader_upload_in_progress_ticker); - } + binding.progressBlock.setVisibility(View.GONE); } - } + }); } /** diff --git a/app/src/main/java/com/owncloud/android/ui/preview/FileDownloadFragment.java b/app/src/main/java/com/owncloud/android/ui/preview/FileDownloadFragment.java index 3004dce0fd36..3dbbb10679c4 100644 --- a/app/src/main/java/com/owncloud/android/ui/preview/FileDownloadFragment.java +++ b/app/src/main/java/com/owncloud/android/ui/preview/FileDownloadFragment.java @@ -23,6 +23,8 @@ import com.nextcloud.client.account.User; import com.nextcloud.client.di.Injectable; import com.nextcloud.client.jobs.download.FileDownloadHelper; +import com.nextcloud.model.WorkerState; +import com.nextcloud.model.WorkerStateLiveData; import com.nextcloud.utils.extensions.BundleExtensionsKt; import com.nextcloud.utils.extensions.FileExtensionsKt; import com.owncloud.android.R; @@ -44,7 +46,6 @@ * This Fragment is used to monitor the progress of a file downloading. */ public class FileDownloadFragment extends FileFragment implements OnClickListener, Injectable { - public static final String EXTRA_FILE = "FILE"; public static final String EXTRA_USER = "USER"; private static final String EXTRA_ERROR = "ERROR"; @@ -52,19 +53,20 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene private static final String ARG_FILE = "FILE"; private static final String ARG_IGNORE_FIRST = "IGNORE_FIRST"; private static final String ARG_USER = "USER"; + private static final String ARG_FILE_POSITION = "FILE_POSITION"; private View mView; private User user; @Inject ViewThemeUtils viewThemeUtils; - public ProgressListener mProgressListener; + private ProgressListener mProgressListener; private boolean mListening; private static final String TAG = FileDownloadFragment.class.getSimpleName(); private boolean mIgnoreFirstSavedState; private boolean mError; - + private Integer filePosition; /** * Public factory method to create a new fragment that shows the progress of a file download. @@ -81,9 +83,10 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter} * TODO better solution */ - public static Fragment newInstance(OCFile file, User user, boolean ignoreFirstSavedState) { + public static Fragment newInstance(OCFile file, User user, boolean ignoreFirstSavedState, Integer filePosition) { FileDownloadFragment frag = new FileDownloadFragment(); Bundle args = new Bundle(); + args.putInt(ARG_FILE_POSITION, filePosition); args.putParcelable(ARG_FILE, file); args.putParcelable(ARG_USER, user); args.putBoolean(ARG_IGNORE_FIRST, ignoreFirstSavedState); @@ -106,7 +109,6 @@ public FileDownloadFragment() { mError = false; } - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -116,6 +118,7 @@ public void onCreate(Bundle savedInstanceState) { mIgnoreFirstSavedState = args.getBoolean(ARG_IGNORE_FIRST); user = BundleExtensionsKt.getParcelableArgument(args, ARG_USER, User.class); + filePosition = args.getInt(ARG_FILE_POSITION); } @@ -152,11 +155,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, if (mError) { setButtonsForRemote(); - } - else { + } else { setButtonsForTransferring(); } + observeWorkerState(); + return mView; } @@ -219,11 +223,24 @@ public void onClick(View v) { } } + private void observeWorkerState() { + WorkerStateLiveData.Companion.instance().observe(getViewLifecycleOwner(), state -> { + if (state instanceof WorkerState.DownloadFinished) { + if (requireActivity() instanceof PreviewImageActivity activity && filePosition != null) { + activity.setPreviewImagePagerCurrentItem(filePosition); + } + } + }); + } /** * Enables or disables buttons for a file being downloaded */ private void setButtonsForTransferring() { + if (getView() == null) { + return; + } + getView().findViewById(R.id.cancelBtn).setVisibility(View.VISIBLE); // show the progress bar for the transfer @@ -243,6 +260,10 @@ private void setButtonsForTransferring() { * Currently, this is only used when a download was failed */ private void setButtonsForRemote() { + if (getView() == null) { + return; + } + getView().findViewById(R.id.cancelBtn).setVisibility(View.GONE); // hides the progress bar and message diff --git a/app/src/main/java/com/owncloud/android/ui/preview/PreviewImageActivity.kt b/app/src/main/java/com/owncloud/android/ui/preview/PreviewImageActivity.kt index ff6d81ded961..1548ce486941 100644 --- a/app/src/main/java/com/owncloud/android/ui/preview/PreviewImageActivity.kt +++ b/app/src/main/java/com/owncloud/android/ui/preview/PreviewImageActivity.kt @@ -6,12 +6,15 @@ */ package com.owncloud.android.ui.preview +import android.annotation.SuppressLint import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.content.IntentFilter import android.os.Build import android.os.Bundle +import android.os.Handler +import android.os.Looper import android.view.MenuItem import android.view.View import android.view.WindowInsets @@ -168,7 +171,7 @@ class PreviewImageActivity : FileActivity(), FileFragment.ContainerActivity, OnR var position = if (hasSavedPosition) savedPosition else previewImagePagerAdapter?.getFilePosition(file) position = position?.toDouble()?.let { max(it, 0.0).toInt() } - viewPager?.setAdapter(previewImagePagerAdapter) + viewPager?.adapter = previewImagePagerAdapter viewPager?.registerOnPageChangeCallback(object : OnPageChangeCallback() { override fun onPageSelected(position: Int) { selectPage(position) @@ -185,6 +188,17 @@ class PreviewImageActivity : FileActivity(), FileFragment.ContainerActivity, OnR } } + @SuppressLint("NotifyDataSetChanged") + fun setPreviewImagePagerCurrentItem(position: Int) { + if (user.isPresent) { + Handler(Looper.getMainLooper()).post { + initViewPager(user.get()) + viewPager?.setCurrentItem(position, false) + viewPager?.adapter?.notifyDataSetChanged() + } + } + } + override fun onBackPressed() { sendRefreshSearchEventBroadcast() super.onBackPressed() diff --git a/app/src/main/java/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.kt b/app/src/main/java/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.kt index 38f350999b32..fdb08f37baca 100644 --- a/app/src/main/java/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.kt +++ b/app/src/main/java/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.kt @@ -151,11 +151,11 @@ class PreviewImagePagerAdapter : FragmentStateAdapter { addVideoOfLivePhoto(file) if (mDownloadErrors.remove(i)) { - fragment = FileDownloadFragment.newInstance(file, user, true) + fragment = FileDownloadFragment.newInstance(file, user, true, i) (fragment as FileDownloadFragment).setError(true) } else { fragment = if (file.isEncrypted) { - FileDownloadFragment.newInstance(file, user, mObsoletePositions.contains(i)) + FileDownloadFragment.newInstance(file, user, mObsoletePositions.contains(i), i) } else if (PreviewMediaFragment.canBePreviewed(file)) { PreviewMediaFragment.newInstance(file, user, 0, false, file.livePhotoVideo != null) } else {