Skip to content

Commit

Permalink
Merge pull request #14485 from nextcloud/bugfix/observe-download-state
Browse files Browse the repository at this point in the history
BugFix - Observe Download State In Encrypted Folder
  • Loading branch information
tobiasKaminsky authored Feb 10, 2025
2 parents 108c04e + b4e6a5c commit de0a88d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,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;
Expand Down Expand Up @@ -608,6 +610,20 @@ public void updateFileDetails(boolean transferring, boolean refresh) {
if (getView() != null) {
getView().invalidate();
}

observeWorkerState();
}

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 {
binding.progressBlock.setVisibility(View.GONE);
}
});
}

private void setFileModificationTimestamp(OCFile file, boolean showDetailedTimestamp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,6 +54,7 @@ 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;
Expand All @@ -64,6 +67,7 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene

private boolean mIgnoreFirstSavedState;
private boolean mError;
private Integer filePosition;


/**
Expand All @@ -81,10 +85,11 @@ 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.putParcelable(ARG_FILE, file);
args.putInt(ARG_FILE_POSITION, filePosition);
args.putParcelable(ARG_USER, user);
args.putBoolean(ARG_IGNORE_FIRST, ignoreFirstSavedState);
frag.setArguments(args);
Expand Down Expand Up @@ -116,6 +121,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);
}


Expand Down Expand Up @@ -157,15 +163,27 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
setButtonsForTransferring();
}

observeWorkerState();

return mView;
}

private void observeWorkerState() {
WorkerStateLiveData.Companion.instance().observe(getViewLifecycleOwner(), state -> {
if (state instanceof WorkerState.DownloadFinished &&
requireActivity() instanceof PreviewImageActivity activity &&
filePosition != null) {
activity.setPreviewImagePagerCurrentItem(filePosition);
}
});
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
FileExtensionsKt.logFileSize(getFile(), TAG);
outState.putParcelable(FileDownloadFragment.EXTRA_FILE, getFile());
outState.putInt(FileDownloadFragment.ARG_FILE_POSITION, filePosition);
outState.putParcelable(FileDownloadFragment.EXTRA_USER, user);
outState.putBoolean(FileDownloadFragment.EXTRA_ERROR, mError);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit de0a88d

Please sign in to comment.