2323import com .nextcloud .client .account .User ;
2424import com .nextcloud .client .di .Injectable ;
2525import com .nextcloud .client .jobs .download .FileDownloadHelper ;
26+ import com .nextcloud .model .WorkerState ;
27+ import com .nextcloud .model .WorkerStateLiveData ;
2628import com .nextcloud .utils .extensions .BundleExtensionsKt ;
2729import com .nextcloud .utils .extensions .FileExtensionsKt ;
2830import com .owncloud .android .R ;
@@ -52,6 +54,7 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene
5254 private static final String ARG_FILE = "FILE" ;
5355 private static final String ARG_IGNORE_FIRST = "IGNORE_FIRST" ;
5456 private static final String ARG_USER = "USER" ;
57+ private static final String ARG_FILE_POSITION = "FILE_POSITION" ;
5558
5659 private View mView ;
5760 private User user ;
@@ -64,6 +67,7 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene
6467
6568 private boolean mIgnoreFirstSavedState ;
6669 private boolean mError ;
70+ private Integer filePosition ;
6771
6872
6973 /**
@@ -81,10 +85,11 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene
8185 * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}
8286 * TODO better solution
8387 */
84- public static Fragment newInstance (OCFile file , User user , boolean ignoreFirstSavedState ) {
88+ public static Fragment newInstance (OCFile file , User user , boolean ignoreFirstSavedState , Integer filePosition ) {
8589 FileDownloadFragment frag = new FileDownloadFragment ();
8690 Bundle args = new Bundle ();
8791 args .putParcelable (ARG_FILE , file );
92+ args .putInt (ARG_FILE_POSITION , filePosition );
8893 args .putParcelable (ARG_USER , user );
8994 args .putBoolean (ARG_IGNORE_FIRST , ignoreFirstSavedState );
9095 frag .setArguments (args );
@@ -116,6 +121,7 @@ public void onCreate(Bundle savedInstanceState) {
116121
117122 mIgnoreFirstSavedState = args .getBoolean (ARG_IGNORE_FIRST );
118123 user = BundleExtensionsKt .getParcelableArgument (args , ARG_USER , User .class );
124+ filePosition = args .getInt (ARG_FILE_POSITION );
119125 }
120126
121127
@@ -157,15 +163,27 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
157163 setButtonsForTransferring ();
158164 }
159165
166+ observeWorkerState ();
167+
160168 return mView ;
161169 }
162170
171+ private void observeWorkerState () {
172+ WorkerStateLiveData .Companion .instance ().observe (getViewLifecycleOwner (), state -> {
173+ if (state instanceof WorkerState .DownloadFinished &&
174+ requireActivity () instanceof PreviewImageActivity activity &&
175+ filePosition != null ) {
176+ activity .setPreviewImagePagerCurrentItem (filePosition );
177+ }
178+ });
179+ }
163180
164181 @ Override
165182 public void onSaveInstanceState (@ NonNull Bundle outState ) {
166183 super .onSaveInstanceState (outState );
167184 FileExtensionsKt .logFileSize (getFile (), TAG );
168185 outState .putParcelable (FileDownloadFragment .EXTRA_FILE , getFile ());
186+ outState .putInt (FileDownloadFragment .ARG_FILE_POSITION , filePosition );
169187 outState .putParcelable (FileDownloadFragment .EXTRA_USER , user );
170188 outState .putBoolean (FileDownloadFragment .EXTRA_ERROR , mError );
171189 }
0 commit comments