@@ -162,7 +162,10 @@ class PreviewImageActivity :
162162 override fun onNewIntent (intent : Intent ? ) {
163163 super .onNewIntent(intent)
164164
165- initializeContent(intent)
165+ // every later read goes through getIntent(), so the selection of the new intent would be lost otherwise
166+ intent?.let { setIntent(it) }
167+
168+ initializeContent()
166169 }
167170
168171 override fun downloadFile (file : OCFile , packageName : String , activityName : String ) {
@@ -320,47 +323,61 @@ class PreviewImageActivity :
320323 public override fun onStart () {
321324 super .onStart()
322325 registerReceivers()
323- initializeContent(intent )
326+ initializeContent()
324327 }
325328
326- private fun initializeContent (newIntent : Intent ? ) {
327- val intent = newIntent ? : intent
329+ private fun initializeContent () {
330+ val optionalUser = user
331+ if (! optionalUser.isPresent) {
332+ finish()
333+ return
334+ }
335+
328336 livePhotoFile = intent.getParcelableArgument(EXTRA_LIVE_PHOTO_FILE , OCFile ::class .java)
329337
330338 val chosenFile = intent.getParcelableArgument(EXTRA_FILE , OCFile ::class .java)
339+ val requestedFile = chosenFile ? : file ? : throw IllegalStateException (" Instanced with a NULL OCFile" )
340+ require(MimeTypeUtil .isImageOrVideo(requestedFile)) { " Non-image/video file passed as argument" }
331341
332- val optionalUser = user
333- if (! optionalUser.isPresent) {
342+ val currentFile = requestedFile.refreshedFromDatabase()
343+ if (currentFile == null ) {
344+ // handled file not in the current Account
334345 finish()
335346 return
336347 }
337348
338- var file: OCFile ? = chosenFile ? : file ? : throw IllegalStateException (" Instanced with a NULL OCFile" )
339- // updateActionBarTitle(file?.fileName)
340- // / Validate handled file (first media item to preview)
341- require(MimeTypeUtil .isImageOrVideo(file)) { " Non-image/video file passed as argument" }
349+ val isNewSelection = currentFile != file
350+
351+ setFile(currentFile)
352+ showFile(currentFile, optionalUser.get(), isNewSelection)
353+ }
354+
355+ private fun OCFile.refreshedFromDatabase (): OCFile ? = if (fileId > FileDataStorageManager .ROOT_PARENT_ID ) {
356+ storageManager.getFileById(fileId)
357+ } else {
358+ this
359+ }
360+
361+ private fun showFile (file : OCFile , user : User , isNewSelection : Boolean ) {
362+ val position = previewMediaPagerAdapter?.getFilePosition(file) ? : NO_POSITION
363+ val pagerNeedsRebuild = position == NO_POSITION
342364
343- // Update file according to DB file, if it is possible
344- if (file !! .fileId > FileDataStorageManager . ROOT_PARENT_ID ) {
345- file = storageManager.getFileById(file.fileId)
365+ // a restart must not pull the user back to the file the activity was started with
366+ if (! pagerNeedsRebuild && ! isNewSelection ) {
367+ return
346368 }
347369
348- if (file != null ) {
349- // / Refresh the activity according to the Account and OCFile set
350- setFile(file) // reset after getting it fresh from storageManager
351- updateActionBarTitle(getFile()?.fileName)
352- if (previewMediaPagerAdapter == null || previewMediaPagerAdapter?.getFilePosition(file) == - 1 ) {
353- savedPosition = null
354- initViewPager(optionalUser.get())
355- } else {
356- previewMediaPagerAdapter?.getFilePosition(file)?.let {
357- viewPager?.currentItem = it
358- }
359- }
360- } else {
361- // handled file not in the current Account
362- finish()
370+ updateActionBarTitle(file.fileName)
371+
372+ if (pagerNeedsRebuild) {
373+ savedPosition = null
374+ initViewPager(user)
375+ return
363376 }
377+
378+ // a rebuilt pager restores the remembered position, so it has to follow the selection right away
379+ savedPosition = position
380+ viewPager?.setCurrentItem(position, false )
364381 }
365382
366383 override fun onSaveInstanceState (outState : Bundle ) {
0 commit comments