Skip to content

fix: make photo editing available from the viewer menu - #4411

Draft
mvanhorn wants to merge 3 commits into
nextcloud:masterfrom
mvanhorn:fix/4332-photo-viewer-edit-action
Draft

mvanhorn wants to merge 3 commits into
nextcloud:masterfrom
mvanhorn:fix/4332-photo-viewer-edit-action

Conversation

@mvanhorn

@mvanhorn mvanhorn commented Oct 21, 2026 •

Copy link
Copy Markdown

While viewing a photo, the overflow menu offered Edit only when the original was already on the device. A photo that was only previewed, or still downloading, had no way to edit it from the viewer, and the file context menu labeled the action Modify.

The viewer menu now shows Edit when Quick Look can modify the image and the device is online or the file is stored locally. Both menus use the Edit title. If the original is local, Edit takes the existing path unchanged. Otherwise NCMediaViewerHostingController.downloadOriginalForEditing awaits the model's existing downloadOriginalImage(for:) and opens the editor only if the viewer is still on screen and showing the same photo. That call goes through loadPageIfNeeded, which already joins a load in progress for the photo, so no second download starts. In Quick Look, hasChangesQuickLook is per instance and the temporary copy goes to tmp/QuickLookEdit/<ocId>/<fileName>, so repeat edits reuse one file. NCMediaViewerLoader is unchanged.

How to verify

  1. Open a large photo whose original is not on the device yet (a slow connection makes the window easy to hit).
  2. While the original is still downloading, open the viewer's overflow menu: Edit is there. On master it is missing.
  3. Tap Edit. The editor opens once the download finishes, and the server sees a single GET for the file.
  4. Rotate, tap Done, choose to save. The edited image uploads and replaces the original.

Tested in the iOS 27 Simulator against a local Nextcloud 35.0.3 server in Docker, with the server throttled so the original was still downloading when the menu opened. Not yet tested on a physical device.

🤖 AI (if applicable)

  • The content of this PR was partly or fully generated using AI

AI coding tools helped write and test this change under my direction. Commits carry the Assisted-by trailer.

Fixes #4332

@marinofaggiana

Copy link
Copy Markdown
Member

Thanks, Matt. Showing Edit for photos that only have a preview makes sense. I’d like to address two issues before merging.

  • Isolate each Quick Look session. A pending download can finish while another photo is being edited. Creating the second editor resets the shared hasChangesQuickLook flag, so the first editor can lose its save prompt. If the files have the same name, the second editor also overwrites the temporary file the first one will upload. Please give each editor its own change flag and a unique temporary file.
  • Keep the download tied to the Edit request. If Info is already downloading the original, tapping Edit resets the transfer state and can start a second download to the same path. Please reuse the in-flight download and open Quick Look only if the originating viewer is still showing that photo when it completes.

Could you add regression coverage for these cases? The current tests verify whether Edit appears in the menu. The regressions above happen after the tap.

@marinofaggiana marinofaggiana left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read my consideration

Copilot AI balanced review requested due to automatic review settings November 10, 2026 23:25
@mvanhorn

Copy link
Copy Markdown
Author

Thanks @marinofaggiana, both are addressed in 8ce96c7.

Isolated Quick Look sessions: hasChangesQuickLook is now an instance property of NCViewerQuickLook, so opening a second editor no longer clears the first one's save prompt. Each editor also gets its own temporary copy from NCViewerQuickLook.makeEditingFileURL, which keeps the file name inside a per-session directory, so two photos with the same name never overwrite each other's upload. The Quick Look transfer selector in NCNetworking+TransferDelegate.swift now goes through the same presentEditor path.

Download tied to the Edit request: Edit from the media viewer now goes through the viewer's loader, which shares a download already running for the same photo (NCInFlightTaskRegistry in NCNextcloudMediaViewerLoader.swift). Tapping Edit while Info is downloading the original joins that transfer instead of resetting it or starting a second download to the same path, and Quick Look opens only if the originating viewer is still on screen and showing that photo when the download completes.

Regression coverage is in NCViewerPhotoEditTests: separate change flags per editor, separate temporary files for same-name photos, joining a running download, and not opening the editor after the viewer moved to another photo, closed, or the download failed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved issues allow stale photo editors to be presented, queued downloads to continue after cancellation, and full-resolution temporary image copies to accumulate without cleanup.

3 open issues
What changed in this PR

This PR addresses #4332 by making photo editing available from the viewer’s overflow menu, including eligible photos whose originals are not yet downloaded.

Changes:

  • Uses the Edit label in both viewer and file context menus.
  • Adds viewer-aware editing, shared downloads, and isolated Quick Look editing sessions.
  • Adds unit tests for menu eligibility, download sharing, and editing-session behavior.
File Description
Tests/​NextcloudUnitTests/​NCViewerPhotoEditTests.swift Tests session isolation, shared downloads, and viewer-validity checks.
Tests/​NextcloudUnitTests/​NCContextMenuViewerTests.swift Tests Edit availability and side-effect-free menu creation.
Tests/​NextcloudUnitTests/​MockViewerPhotoEditSource.swift Provides a controllable viewer and download mock.
Nextcloud.xcodeproj/​project.pbxproj Registers the new tests and mock in the unit test target.
iOSClient/​Viewer/​NCViewerQuickLook/​NCViewerQuickLook.swift Isolates editing state and temporary files; centralizes editor presentation.
iOSClient/​Viewer/​NCViewerMedia/​NCNextcloudMediaViewerLoader.swift Shares in-flight original-file downloads.
iOSClient/​Viewer/​NCViewerMedia/​NCMediaViewerHostingController.swift Connects the media viewer to the editing flow.
iOSClient/​Networking/​NCNetworking+TransferDelegate.swift Uses the shared editor-presentation helper.
iOSClient/​Menu/​NCContextMenuViewer.swift Adds eligible Edit actions and viewer-aware download handling.
iOSClient/​Menu/​NCContextMenuMain.swift Renames the file context menu action to Edit.

Review effort: Balanced

Comment on lines +304 to +305
let current = await NCManageDatabase.shared.getMetadataFromOcIdAsync(metadata.ocId) ?? metadata
NCViewerQuickLook.presentEditor(metadata: current, from: controller)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed at 8abde67: the present closure in makeModifyPhoto (NCContextMenuViewer.swift) awaits getMetadataFromOcIdAsync after the guard in NCViewerPhotoEdit.edit and then calls NCViewerQuickLook.presentEditor without rechecking. The fix is a !Task.isCancelled and source.isShowingMedia(ocId:) guard right after that database read, before presentEditor, so a viewer closed or swiped during the read opens nothing.

Comment on lines +316 to +318
let directory = temporaryDirectory.appendingPathComponent("QuickLookEdit-" + UUID().uuidString, isDirectory: true)
do {
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed at 8abde67: makeEditingFileURL creates a QuickLookEdit- directory per session and nothing in NCViewerQuickLook removes it. The fix for this thread is NCViewerQuickLook owning that directory and deleting it when the editor is dismissed without changes, after the uploadAsync/readFileAsync step in saveModifiedFile completes, and inside presentEditor when writing or copying the image fails.

@mvanhorn
mvanhorn force-pushed the fix/4332-photo-viewer-edit-action branch from 0923dc8 to b0a1f4c Compare November 11, 2026 01:55
@mvanhorn

Copy link
Copy Markdown
Author

Merged current master into this branch in 8abde67 to clear the merge conflicts. Upstream's checks that block editing encrypted files are kept in NCContextMenuViewer, the Quick Look transfer path and NCViewerQuickLook, alongside this PR's editor changes.

@mvanhorn

Copy link
Copy Markdown
Author

Merged current master into this branch in 96b0629 to clear the new conflict. Upstream removed NCNextcloudMediaViewerLoader.swift in #4489 and #4491, so this PR's shared download now lives in NCMediaViewerLoader (with NCInFlightTaskRegistry in its own file under Core). Edit from the viewer reuses upstream's buffered original download, joins a download already running for the same photo, and cancels it once no caller is waiting. Upstream's encrypted-file edit checks are unchanged.

@marinofaggiana marinofaggiana left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sticking with this, @mvanhorn, and for addressing each round of feedback.

Stepping back, the PR has grown far beyond what the feature needs. Showing Edit for preview-only photos is a small change, but the diff is now +731/−89 and replaces the download cancellation in NCMediaViewerLoader, which we just reworked in #4489, #4491 and #4495. That is more risk and review time than a menu item is worth, so let's reset the scope.

Per our AI Contribution Policy, I'm converting this to a draft and asking for a focused rewrite:

Scope

  • Leave NCMediaViewerLoader as it is on master, and drop NCInFlightTaskRegistry.
  • Menu: show Edit when the image is modifiable with Quick Look and the device is online or the file is stored locally.
  • Action: if the file is local, keep today's path. Otherwise await the viewer model's existing downloadOriginalImage(for:), then present the editor only if the viewer still shows that photo.
  • If a duplicate download is really a concern, a small [ocId: Task] map on the @MainActor model is enough.
  • Quick Look: keep the per-instance hasChangesQuickLook. For the temporary copy, use a stable per-photo path such as tmp/QuickLookEdit/<ocId>/<fileName>, so copies never pile up and no cleanup logic is needed. Right now every Edit leaves a full-resolution JPEG behind.
  • One type per file (see AGENTS.md): NCViewerPhotoEditSource and NCViewerPhotoEdit shouldn't live in NCContextMenuViewer.swift.
  • The fallback branch in makeModifyPhoto for non-media viewers looks unreachable for images. Please drop it unless there's a case for it.

Process

  • Rebase on master with a few clean commits, no merge commits.
  • Add an Assisted-by: AGENT_NAME:MODEL_VERSION trailer to every AI-assisted commit (not just Assisted-by: AI), and tick the AI checkbox in the description.
  • Test the change on a live Nextcloud instance yourself, and add short steps to verify it to the description. Please leave out raw tool output.

A small, focused version of this would be very welcome. If it's easier, open a fresh PR and close this one. If there's no update within a month, we'll close it.

Assisted by ClaudeCode:claude-opus-5-5

@marinofaggiana
marinofaggiana marked this pull request as draft November 26, 2026 09:33
@marinofaggiana

marinofaggiana commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

One more thought, just to be clear on expectations: using AI is totally fine with us. What matters is that you own the result. Before the next push, please make sure you understand each change, could explain the design in your own words, and have tried it yourself on a device against a live instance. That makes the review much smoother for both of us. Thanks!

Keep hasChangesQuickLook per NCViewerQuickLook instance, so opening
another editor no longer resets the save prompt of an earlier one.

Copy the photo to edit to a stable per-photo path,
tmp/QuickLookEdit/<ocId>/<fileName>, so photos with the same name never
share a temporary file and repeated edits replace the copy instead of
leaving new ones behind.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
The viewer menu showed Modify only when the original was already on the
device. Show Edit for images Quick Look can modify when the device is
online or the file is stored locally, and use the Edit title in the file
context menu too.

When the original is missing, the action awaits the media viewer model's
existing downloadOriginalImage(for:), which joins a download already in
progress for that photo, and opens the editor only if the viewer is still
on screen and showing the same photo. Local files keep the existing path.

Fixes nextcloud#4332

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
@mvanhorn
mvanhorn force-pushed the fix/4332-photo-viewer-edit-action branch from 96b0629 to 705a13d Compare November 26, 2026 16:13
@mvanhorn

Copy link
Copy Markdown
Author

Thanks for the reset, it was the right call. I've rewritten this as two commits on current master, +30/-11.

NCMediaViewerLoader is untouched and NCInFlightTaskRegistry is gone. The viewer menu now shows Edit when Quick Look can modify the image and the device is online or the file is local. If the original is already local, Edit takes the same path as before. Otherwise it awaits the model's existing downloadOriginalImage(for:) through a small method on the hosting controller, and opens the editor only if the viewer is still on screen and showing the same photo. I didn't add an [ocId: Task] map because downloadOriginalImage goes through loadPageIfNeeded, which already joins a load in progress for that photo. In Quick Look, hasChangesQuickLook is per instance, and the temporary copy goes to tmp/QuickLookEdit//, so edits reuse one file per photo. The unreachable fallback branch and the extra types are gone.

I tested it in the iOS Simulator against a live local Nextcloud server (35.0.3 in Docker), not on a physical device. I throttled the server so the original was still downloading when I opened the menu. Edit was there, the editor opened as soon as the download finished, the server saw a single GET, and saving uploaded the rotated image. Verification steps are in the description now.

@marinofaggiana

Copy link
Copy Markdown
Member

Thanks Matt, this is exactly the kind of change we were hoping for. It's small and focused, and it builds on what's already there.

A few notes from reading it:

  • Using downloadOriginalImage(for:) and then checking that the viewer is still showing the same photo is a clean solution.
  • Moving hasChangesQuickLook from a file-level global to the instance is a real fix: a second editor could reset the first one's save prompt.
  • The per-photo temp path also stops two photos with the same name from sharing one temporary file.

One thing we need before merging: visible feedback while the original downloads. Right now, when the original isn't on the device and the user taps Edit, nothing happens until the download finishes. On a slow connection it looks like the tap didn't work, and if the download fails the user is never told. Please:

  • show a progress indicator while the original downloads; reusing the viewer's existing progress state for that page is fine;
  • if the download fails, show an error instead of silently returning.

Please keep it as small as the rest of the change. Once that's in, mark the PR as ready for review so CI can run, and if it's green I'll approve.

@marinofaggiana

Copy link
Copy Markdown
Member

One more note on downloadOriginalForEditing: downloadOriginalImage(for:) doesn't download by itself. It goes through loadPageIfNeeded, which only fetches the original when NCMediaViewerLoadingPolicy.shouldDownloadOriginalImage allows it. Today that's always the case in the app (automaticallyDownloadsOriginalImages: true in NCMediaViewerModel+App.swift), so it works. But if that policy ever changes, for example to save data on cellular, loadPageIfNeeded returns without downloading, downloadOriginalImage throws CancellationError, and Edit silently does nothing. The same happens while a thumbnail scrub is in progress.

Please add a short comment at the call site stating this dependency, so it doesn't stay implicit. It also ties in with the error feedback above: if the download didn't happen for any reason, the user should be told.

While you're on the progress indicator: with the current policy, the viewer usually starts downloading the original as soon as the photo is shown, and the page already carries a progress in its .image state. Please check whether that's already visible and reuse it instead of adding a new indicator.

Opening Edit from a photo preview can take time or fail before the editor appears. Show that the original is downloading and explain failures while the requested photo is still selected.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
@mvanhorn

Copy link
Copy Markdown
Author

Added in f46eae1. The viewer had no visible indicator for the original download, so the selected photo now shows a small spinner over its preview while the page is in its existing .downloading state, and Edit shows the "Download failed" error banner if downloadOriginalImage(for:) throws while you are still on that photo (leaving the viewer or swiping away still just skips the editor). The call site now has a comment explaining that it goes through loadPageIfNeeded, which only fetches the original when NCMediaViewerLoadingPolicy.shouldDownloadOriginalImage allows it, and that the resulting CancellationError is reported as that error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Modify action only shown in context menu

3 participants