From aed8c94939652d3d8ccb343253aead510f646121 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Thu, 29 Aug 2024 17:06:25 +0200 Subject: [PATCH 1/4] Fix Signed-off-by: alperozturk --- .../ui/fragment/FileDetailFragment.java | 23 +++++++++++----- .../ui/preview/FileDownloadFragment.java | 26 +++++++++++++++++-- 2 files changed, 41 insertions(+), 8 deletions(-) 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..dfd4dc9b687e 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.Download) { binding.progressText.setText(R.string.downloader_download_in_progress_ticker); + } else if (state instanceof WorkerState.Upload) { + 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..b92ea8456201 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; @@ -152,11 +154,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, if (mError) { setButtonsForRemote(); - } - else { + } else { setButtonsForTransferring(); } + observeWorkerState(); + return mView; } @@ -219,11 +222,26 @@ public void onClick(View v) { } } + private void observeWorkerState() { + WorkerStateLiveData.Companion.instance().observe(getViewLifecycleOwner(), state -> { + if (state instanceof WorkerState.Idle) { + if (getView() == null) return; + + // TODO Open downloaded file immediately + + } + }); + } + /** * 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 +261,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 From 094a9ab5173063cd0db7f2e47af46788d8ffa639 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Fri, 30 Aug 2024 11:49:19 +0200 Subject: [PATCH 2/4] Open image after download completed Signed-off-by: alperozturk --- .../ui/preview/FileDownloadFragment.java | 21 ++++++++++--------- .../ui/preview/PreviewImageActivity.kt | 16 +++++++++++++- .../ui/preview/PreviewImagePagerAdapter.kt | 4 ++-- 3 files changed, 28 insertions(+), 13 deletions(-) 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 b92ea8456201..d7a43f631bac 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 @@ -46,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"; @@ -54,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. @@ -83,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); @@ -108,7 +109,6 @@ public FileDownloadFragment() { mError = false; } - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -118,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); } @@ -224,11 +225,11 @@ public void onClick(View v) { private void observeWorkerState() { WorkerStateLiveData.Companion.instance().observe(getViewLifecycleOwner(), state -> { - if (state instanceof WorkerState.Idle) { - if (getView() == null) return; - - // TODO Open downloaded file immediately - + if (state instanceof WorkerState.Idle idleState) { + if (idleState.getCurrentFile() == null) return; + if (requireActivity() instanceof PreviewImageActivity activity && filePosition != null) { + activity.setPreviewImagePagerCurrentItem(filePosition); + } } }); } 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 { From 8a405a6278a97313c3b27c28d08d6f32473d584d Mon Sep 17 00:00:00 2001 From: alperozturk Date: Fri, 30 Aug 2024 11:51:58 +0200 Subject: [PATCH 3/4] Remove unnecessary condition Signed-off-by: alperozturk --- .../com/owncloud/android/ui/preview/FileDownloadFragment.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) 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 d7a43f631bac..764e7fe114e2 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 @@ -225,8 +225,7 @@ public void onClick(View v) { private void observeWorkerState() { WorkerStateLiveData.Companion.instance().observe(getViewLifecycleOwner(), state -> { - if (state instanceof WorkerState.Idle idleState) { - if (idleState.getCurrentFile() == null) return; + if (state instanceof WorkerState.Idle) { if (requireActivity() instanceof PreviewImageActivity activity && filePosition != null) { activity.setPreviewImagePagerCurrentItem(filePosition); } @@ -234,7 +233,6 @@ private void observeWorkerState() { }); } - /** * Enables or disables buttons for a file being downloaded */ From 4d5d0bbb093ec3ceb4196415a7a2c4df70713c59 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Wed, 4 Sep 2024 10:12:51 +0200 Subject: [PATCH 4/4] Update branch Signed-off-by: alperozturk --- .../com/owncloud/android/ui/fragment/FileDetailFragment.java | 4 ++-- .../com/owncloud/android/ui/preview/FileDownloadFragment.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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 dfd4dc9b687e..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 @@ -692,9 +692,9 @@ private void setButtonsForTransferring() { private void observeWorkerState() { WorkerStateLiveData.Companion.instance().observe(getViewLifecycleOwner(), state -> { - if (state instanceof WorkerState.Download) { + if (state instanceof WorkerState.DownloadStarted) { binding.progressText.setText(R.string.downloader_download_in_progress_ticker); - } else if (state instanceof WorkerState.Upload) { + } else if (state instanceof WorkerState.UploadStarted) { binding.progressText.setText(R.string.uploader_upload_in_progress_ticker); } else { 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 764e7fe114e2..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 @@ -225,7 +225,7 @@ public void onClick(View v) { private void observeWorkerState() { WorkerStateLiveData.Companion.instance().observe(getViewLifecycleOwner(), state -> { - if (state instanceof WorkerState.Idle) { + if (state instanceof WorkerState.DownloadFinished) { if (requireActivity() instanceof PreviewImageActivity activity && filePosition != null) { activity.setPreviewImagePagerCurrentItem(filePosition); }