Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BugFix - Observe Worker State For Download & Upload Indicators #13489

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
});
}

/**
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 All @@ -44,27 +46,27 @@
* 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";

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.
Expand All @@ -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);
Expand All @@ -106,7 +109,6 @@ public FileDownloadFragment() {
mError = false;
}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -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);
}


Expand Down Expand Up @@ -152,11 +155,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,

if (mError) {
setButtonsForRemote();
}
else {
} else {
setButtonsForTransferring();
}

observeWorkerState();

return mView;
}

Expand Down Expand Up @@ -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
Expand All @@ -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
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 @@ -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)
Expand All @@ -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
Loading