Download V2 UI Flow
Overview
The download-v2 UI is rooted at app/…/DownloadPageV2.kt. DownloadPageV2 composes the visible queue from DownloaderV2.getTaskStateMap(), opens the new-task sheet through DownloadDialogViewModel, and translates queue-card actions into downloader/file/browser side effects: cancel/remove/restart via DownloaderV2, open/share completed files through FileUtil, and copy/open URLs through the clipboard or LocalUriHandler.
The queue body is DownloadPageImplV2 in DownloadPageV2.kt. It owns only transient UI state: active filter, grid/list mode, selected task for the action sheet, scroll/header offset, and the Material bottom sheet state. The durable queue state stays in the SnapshotStateMap<Task, Task.State> returned by DownloaderV2. Filtering is by Filter.predict: Downloading includes idle/fetching/ready/running states, Canceled includes both canceled and error states, and Finished includes completed tasks. Queue items sort by Task.State.downloadState, render as adaptive-grid cards or full-width list rows, and open ActionSheet.kt when the card overflow is tapped.
Task cards and list rows live in app/…/VideoCardV2.kt. They render Task.ViewState fields (thumbnail, title, uploader, duration, approximate size) plus state-specific indicators and action buttons. Card/list primary actions are intentionally state-driven: running/fetching/idle cancels, canceled/error resumes, and completed opens the output file. The overflow menu always routes through the action sheet rather than performing work directly.
app/…/ActionSheet.kt is the per-task command surface. SheetContent shows a title block, a horizontal row of primary actions, then media metadata. The available buttons depend on Task.DownloadState: completed tasks can open/share/delete; errors can resume/copy an error report; active tasks can cancel; restartable or completed tasks can delete; all tasks can copy/open the source URL, and tasks with thumbnails can open the thumbnail URL. The sheet posts UiAction back to DownloadPageV2, which is the only place in this package that performs downloader mutation or Android intents for queue actions.
New-task entry starts from the FAB in DownloadPageV2.kt, which posts DownloadDialogViewModel.Action.ShowSheet. The view model in app/…/DownloadDialogViewModel.kt owns three flows: sheetValueFlow for visible/hidden, sheetStateFlow for the bottom-sheet page (InputUrl, Configure, Loading, Error), and selectionStateFlow for full-screen follow-up selection (FormatSelection, PlaylistSelection, or idle). It also owns the bridge into the core downloader: preset downloads enqueue Task(url, preferences), custom commands enqueue a Task with Task.TypeInfo.CustomCommand, and format/playlist selection paths fetch metadata before later enqueuing tasks.
The URL input sheet is implemented in app/…/InputUrlDialog.kt. InputUrlPage reads URLs from the clipboard on entry, keeps saved links in Compose state backed by Config.savedLinks, and persists saved-link changes on disposal through Config.updatePreferences. The user can type a URL, paste the first detected clipboard URL, select multiple clipboard URLs via URLSelectionDialog, or pick/delete saved URLs via SavedUrlDialogImpl. All successful URL choices post Action.ProceedWithURLs, moving the sheet to SheetState.Configure.
Configuration UI is in app/…/DownloadDialogV2.kt. DownloadDialog hosts the bottom sheet and optional quick settings dialogs for video/audio presets. Config snapshots preference-backed UI defaults: selected DownloadType, whether format selection is enabled, command availability, and saved links. Single-URL configuration uses ConfigurePage: users choose Audio, Video, Playlist, or Command; choose preset versus custom format selection; optionally edit quick presets; expand additional settings for cookies/subtitles/thumbnails; then either enqueue immediately (DownloadWithPreset), fetch metadata (FetchFormats/FetchPlaylist), or enqueue a custom-command task (RunCommand). Multiple URL configuration uses ConfigurePagePlaylistVariant, limited to audio/video preset downloads, and enqueues each URL with extractAudio set from the chosen type.
Metadata fetching is asynchronous and centralized in DownloadDialogViewModel.kt. FetchFormats calls DownloadUtil.fetchVideoInfoFromUrl with a task key of FetchFormat_$url; FetchPlaylist calls DownloadUtil.getPlaylistOrVideoInfo with task key FetchPlaylist_$url. While a fetch runs, sheetStateFlow is Loading(taskKey, job). Hiding or canceling the sheet calls YoutubeDL.destroyProcessById(taskKey) and cancels the coroutine job if the process was destroyed. Fetch failures surface as SheetState.Error, whose retry button reposts the original action and whose copy button includes app version, URL, and the throwable message.
Custom format selection lives in app/…/FormatPage.kt. The view-model wrapper in DownloadDialogV2.kt presents it as a modal sheet and resets selectionStateFlow on close. The page partitions VideoInfo.formats into audio-only, video-only, and combined video/audio formats, preserves yt-dlp requested formats as a suggested default, and lets users choose formats, rename the title, clip by time range, split by chapters, open/share format URLs, and select subtitles. Pressing the FAB creates a configured task through TaskFactory.createWithConfigurations and enqueues it through DownloaderV2. If selected subtitle languages are outside the saved subtitle-language regex, UpdateSubtitleLanguageDialog can append them to SUBTITLE_LANGUAGE before closing.
Playlist selection is in app/…/PlaylistSelectionPage.kt. PlaylistSelectionPage wraps the full-screen selection UI in a non-gesture modal sheet, then opens a second configuration sheet before enqueueing. PlaylistSelectionPageImpl lets the user select entries individually, select all, or choose a range through PlaylistSelectionDialog; confirmation calls TaskFactory.createWithPlaylistResult with the selected 1-based indices and empty preferences. The second sheet applies the final audio/video preset preferences to each produced TaskWithState before enqueuing it with DownloaderV2.enqueue(taskWithState).
Nested-scroll behavior for the compact queue header is isolated in app/…/TopBarNestedScrollConnection.kt. DownloadPageImplV2 uses it only on compact width classes to collapse/restore the header spacer between 0f and HeaderSpacingDp.dp.toPx(). Expanded/tablet layouts skip this custom connection and keep the header fixed.
Path routing guidance: changes to the queue shell, filters, FAB, grid/list toggle, empty state, or high-level downloader action routing belong in DownloadPageV2.kt; changes to card/list rendering and state-specific inline buttons belong in VideoCardV2.kt; changes to the per-task bottom sheet belong in ActionSheet.kt; changes to URL entry and saved/clipboard URL behavior belong in configure/InputUrlDialog.kt; changes to bottom-sheet state transitions, metadata fetch/cancel, and enqueue dispatch belong in configure/DownloadDialogViewModel.kt; changes to download-type/preset/custom-command configuration belong in configure/DownloadDialogV2.kt; changes to explicit format/subtitle/clip/rename selection belong in configure/FormatPage.kt; changes to playlist entry selection and playlist preset enqueueing belong in configure/PlaylistSelectionPage.kt; changes to compact-header collapse behavior belong in TopBarNestedScrollConnection.kt.
Coupling
- If
Task.DownloadStatechanges in the downloader layer, update DownloadPageV2.kt, VideoCardV2.kt, and ActionSheet.kt together. Filters, sort display, inline card buttons, list/card status text, and action-sheet buttons all branch on the exact sealed-state set. - If
Task.ViewStatechanges, verify DownloadPageV2.kt, VideoCardV2.kt, and ActionSheet.kt; the queue cards and action sheet directly read thumbnail/title/uploader/duration/size/formats/url fields and there is no adapter layer inside this package. - If
DownloadUtil.DownloadPreferencesor preference keys for format/cookies/subtitles/thumbnails change, check DownloadDialogV2.kt, InputUrlDialog.kt, FormatPage.kt, and PlaylistSelectionPage.kt. These screens snapshot preferences into Compose state and also mutate global preference keys from quick-setting dialogs before rebuilding preferences. - If yt-dlp process task-key conventions change, update DownloadDialogViewModel.kt alongside any downloader utility changes. Canceling metadata fetches depends on the exact
FetchFormat_$urlandFetchPlaylist_$urlIDs passed toYoutubeDL.destroyProcessById. - If
TaskFactory.createWithConfigurationsorTaskFactory.createWithPlaylistResultchanges its contract, verify FormatPage.kt and PlaylistSelectionPage.kt; these are the only download-v2 screens that build configured task objects after metadata selection instead of directly creating simpleTask(url, preferences)instances. - If adding a new route into this flow from outside the package, prefer posting
DownloadDialogViewModel.Action.ShowSheet(urlList)rather than bypassing the URL/config sheets. DownloadPageV2.kt already handles a non-empty URL list by enteringSheetState.Configure, while an empty list entersInputUrl.
Risks
- app/…/DownloadPageV2.kt clears a removed selected task with
selectedTask == nullinsideLaunchedEffect(selectedTask, taskDownloadStateMap.size). Because this is a comparison, not assignment, deleting/removing a selected task can leave stale selection state until the sheet is dismissed. - app/…/TopBarNestedScrollConnection.kt computes fling consumption after calling
onOffsetUpdate(newOffset), then comparesnewOffsettooffset()again. Ifoffset()reflects the just-updated value, consumed distance becomes zero and fling settling can cancel early, making compact header collapse/expand feel sticky. - app/…/PlaylistSelectionPage.kt passes
initialDownloadTypetoonDownloadinstead of the currentselectedTypeinConfigurePagePlaylistVariant. If the user switches between video and audio in that sheet, the enqueued playlist tasks may still use the initial type. - app/…/FormatPage.kt collects auto-caption selections in
SubtitleSelectionDialog, but the caller only copiessubsback intoselectedSubtitlesand does not applyautoSubstoselectedAutoCaptions. Selecting auto captions in the dialog can therefore fail to affect the task created byTaskFactory.createWithConfigurations.