Skip to content

Commit

Permalink
Merge pull request #13340 from nextcloud/bugfix/start-edit-image-acti…
Browse files Browse the repository at this point in the history
…vity-when-image-is-not-down

Fix - Edit, Delete Behaviors on PreviewImageActivity
  • Loading branch information
alperozturk96 committed Jul 30, 2024
2 parents 62df824 + ed7d0aa commit 7f0d82f
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class FileDownloadWorker(
}

private fun setIdleWorkerState() {
WorkerStateLiveData.instance().setWorkState(WorkerState.Idle)
WorkerStateLiveData.instance().setWorkState(WorkerState.Idle(getCurrentFile()))
}

private fun removePendingDownload(accountName: String?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class FileUploadWorker(
}

private fun setIdleWorkerState() {
WorkerStateLiveData.instance().setWorkState(WorkerState.Idle)
WorkerStateLiveData.instance().setWorkState(WorkerState.Idle(currentUploadFileOperation?.file))
}

@Suppress("ReturnCount")
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/com/nextcloud/model/WorkerState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
package com.nextcloud.model

import com.nextcloud.client.account.User
import com.owncloud.android.datamodel.OCFile
import com.owncloud.android.db.OCUpload
import com.owncloud.android.operations.DownloadFileOperation

sealed class WorkerState {
object Idle : WorkerState()
class Download(var user: User?, var currentDownload: DownloadFileOperation?) : WorkerState()
class Upload(var user: User?, var uploads: List<OCUpload>) : WorkerState()
data class Idle(var currentFile: OCFile?) : WorkerState()
data class Download(var user: User?, var currentDownload: DownloadFileOperation?) : WorkerState()
data class Upload(var user: User?, var uploads: List<OCUpload>) : WorkerState()
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2020-2024 Andy Scherzinger <[email protected]>
* SPDX-FileCopyrightText: 2023 Alper Ozturk <[email protected]>
* SPDX-FileCopyrightText: 2022 Álvaro Brey <[email protected]>
* SPDX-FileCopyrightText: 2019 Tobias Kaminsky <[email protected]>
* SPDX-FileCopyrightText: 2019 Chris Narkiewicz <[email protected]>
* SPDX-FileCopyrightText: 2016 ownCloud Inc.
* SPDX-FileCopyrightText: 2015 María Asensio Valverde <[email protected]>
* SPDX-FileCopyrightText: 2013 David A. Velasco <[email protected]>
* SPDX-License-Identifier: GPL-2.0-only AND (AGPL-3.0-or-later OR GPL-2.0-only)
* SPDX-FileCopyrightText: 2024 Alper Ozturk <[email protected]>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package com.owncloud.android.ui.preview

Expand Down Expand Up @@ -56,6 +49,8 @@ import com.owncloud.android.ui.activity.FileDisplayActivity
import com.owncloud.android.ui.fragment.FileFragment
import com.owncloud.android.ui.fragment.GalleryFragment
import com.owncloud.android.ui.fragment.OCFileListFragment
import com.owncloud.android.ui.preview.model.PreviewImageActivityState
import com.owncloud.android.utils.DisplayUtils
import com.owncloud.android.utils.MimeTypeUtil
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
import java.io.Serializable
Expand All @@ -65,16 +60,18 @@ import kotlin.math.max
/**
* Holds a swiping gallery where image files contained in an Nextcloud directory are shown.
*/
@Suppress("TooManyFunctions")
class PreviewImageActivity : FileActivity(), FileFragment.ContainerActivity, OnRemoteOperationListener, Injectable {
private var livePhotoFile: OCFile? = null
private var viewPager: ViewPager2? = null
private var previewImagePagerAdapter: PreviewImagePagerAdapter? = null
private var savedPosition = 0
private var hasSavedPosition = false
private var requestWaitingForBinder = false
private var downloadFinishReceiver: DownloadFinishReceiver? = null
private var fullScreenAnchorView: View? = null

private var isDownloadWorkStarted = false
private var screenState = PreviewImageActivityState.Idle

@Inject
lateinit var preferences: AppPreferences
Expand Down Expand Up @@ -115,7 +112,10 @@ class PreviewImageActivity : FileActivity(), FileFragment.ContainerActivity, OnR
// to keep our UI controls visibility in line with system bars visibility
setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)

requestWaitingForBinder = savedInstanceState?.getBoolean(KEY_WAITING_FOR_BINDER) ?: false
val requestWaitingForBinder = savedInstanceState?.getBoolean(KEY_WAITING_FOR_BINDER) ?: false
if (requestWaitingForBinder) {
screenState = PreviewImageActivityState.WaitingForBinder
}

observeWorkerState()
}
Expand Down Expand Up @@ -181,7 +181,7 @@ class PreviewImageActivity : FileActivity(), FileFragment.ContainerActivity, OnR
if (position == 0 && !file.isDown) {
// this is necessary because mViewPager.setCurrentItem(0) just after setting the
// adapter does not result in a call to #onPageSelected(0)
requestWaitingForBinder = true
screenState = PreviewImageActivityState.WaitingForBinder
}
}

Expand Down Expand Up @@ -241,7 +241,7 @@ class PreviewImageActivity : FileActivity(), FileFragment.ContainerActivity, OnR

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putBoolean(KEY_WAITING_FOR_BINDER, requestWaitingForBinder)
outState.putBoolean(KEY_WAITING_FOR_BINDER, screenState == PreviewImageActivityState.WaitingForBinder)
outState.putBoolean(KEY_SYSTEM_VISIBLE, isSystemUIVisible)
}

Expand All @@ -254,11 +254,15 @@ class PreviewImageActivity : FileActivity(), FileFragment.ContainerActivity, OnR

previewImagePagerAdapter?.let {
if (it.itemCount <= 1) {
finish()
backToDisplayActivity()
return
}
}

if (user.isPresent) {
initViewPager(user.get())
}

viewPager?.setCurrentItem(nextPosition, true)
previewImagePagerAdapter?.delete(deletePosition)
} else if (operation is SynchronizeFileOperation) {
Expand All @@ -274,26 +278,50 @@ class PreviewImageActivity : FileActivity(), FileFragment.ContainerActivity, OnR

private fun observeWorkerState() {
WorkerStateLiveData.instance().observe(this) { state: WorkerState? ->
if (state is WorkerState.Download) {
Log_OC.d(TAG, "Download worker started")
isDownloadWorkStarted = true

if (requestWaitingForBinder) {
requestWaitingForBinder = false
Log_OC.d(
TAG,
"Simulating reselection of current page after connection " +
"of download binder"
)
selectPage(viewPager?.currentItem)
when (state) {
is WorkerState.Download -> {
Log_OC.d(TAG, "Download worker started")
isDownloadWorkStarted = true

if (screenState == PreviewImageActivityState.WaitingForBinder) {
selectPageOnDownload()
}
}

is WorkerState.Idle -> {
Log_OC.d(TAG, "Download worker stopped")
isDownloadWorkStarted = false

if (screenState == PreviewImageActivityState.Edit) {
onImageDownloadComplete(state.currentFile)
}
}

else -> {
Log_OC.d(TAG, "Download worker stopped")
isDownloadWorkStarted = false
}
} else {
Log_OC.d(TAG, "Download worker stopped")
isDownloadWorkStarted = false
}
}
}

private fun selectPageOnDownload() {
screenState = PreviewImageActivityState.Idle
Log_OC.d(
TAG,
"Simulating reselection of current page after connection " +
"of download binder"
)
selectPage(viewPager?.currentItem)
}

private fun onImageDownloadComplete(downloadedFile: OCFile?) {
dismissLoadingDialog()
screenState = PreviewImageActivityState.Idle
file = downloadedFile
startEditImageActivity()
}

override fun onResume() {
super.onResume()

Expand All @@ -316,6 +344,7 @@ class PreviewImageActivity : FileActivity(), FileFragment.ContainerActivity, OnR
}

private fun backToDisplayActivity() {
sendRefreshSearchEventBroadcast()
finish()
}

Expand All @@ -328,7 +357,7 @@ class PreviewImageActivity : FileActivity(), FileFragment.ContainerActivity, OnR
}

startActivity(intent)
finish()
backToDisplayActivity()
}

override fun showDetails(file: OCFile, activeTab: Int) {
Expand Down Expand Up @@ -356,7 +385,7 @@ class PreviewImageActivity : FileActivity(), FileFragment.ContainerActivity, OnR
val currentFile = previewImagePagerAdapter?.getFileAt(position)

if (!isDownloadWorkStarted) {
requestWaitingForBinder = true
screenState = PreviewImageActivityState.WaitingForBinder
} else {
if (currentFile != null) {
if (currentFile.isEncrypted && !currentFile.isDown &&
Expand Down Expand Up @@ -457,14 +486,31 @@ class PreviewImageActivity : FileActivity(), FileFragment.ContainerActivity, OnR

fun startImageEditor(file: OCFile) {
if (file.isDown) {
val editImageIntent = Intent(this, EditImageActivity::class.java)
editImageIntent.putExtra(EditImageActivity.EXTRA_FILE, file)
startActivity(editImageIntent)
startEditImageActivity()
} else {
showLoadingDialog(getString(R.string.preview_image_downloading_image_for_edit))
screenState = PreviewImageActivityState.Edit
requestForDownload(file, EditImageActivity.OPEN_IMAGE_EDITOR)
}
}

private fun startEditImageActivity() {
if (file == null) {
DisplayUtils.showSnackMessage(this, R.string.preview_image_file_is_not_exist)
return
}

if (!file.isDown) {
DisplayUtils.showSnackMessage(this, R.string.preview_image_file_is_not_downloaded)
return
}

val intent = Intent(this, EditImageActivity::class.java).apply {
putExtra(EditImageActivity.EXTRA_FILE, file)
}
startActivity(intent)
}

override fun onBrowsedDownTo(folder: OCFile) {
// TODO Auto-generated method stub
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2024 Alper Ozturk <[email protected]>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

package com.owncloud.android.ui.preview.model

enum class PreviewImageActivityState {
WaitingForBinder,
Edit,
Idle
}
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,9 @@
<string name="preview_media_unhandled_http_code_message">File is currently locked by another user or process and therefore not deletable. Please try again later.</string>

<string name="preview_sorry">Sorry</string>
<string name="preview_image_file_is_not_exist">File is not exist</string>
<string name="preview_image_file_is_not_downloaded">File is not downloaded</string>
<string name="preview_image_downloading_image_for_edit">Downloading image to start the edit screen, please wait…</string>
<string name="preview_image_description">Image preview</string>
<string name="preview_image_error_unknown_format">Unable to show image</string>
<string name="preview_image_error_no_local_file">There is no local file to preview</string>
Expand Down

0 comments on commit 7f0d82f

Please sign in to comment.