Skip to content

Restrict duplicate video additions and implement DiffUtil to prevent list flickering and crashes during playlist pagination - #13977

Closed
Ayush32-ai wants to merge 3 commits into
TeamNewPipe:devfrom
Ayush32-ai:dev
Closed

Ayush32-ai wants to merge 3 commits into
TeamNewPipe:devfrom
Ayush32-ai:dev

Conversation

@Ayush32-ai

Copy link
Copy Markdown
  1. Preventing Duplicate Videos and Code Changes
    To resolve the issue of duplicate videos being added to local playlists, I implemented strict duplicate addition restrictions in both the user interface and the database manager. In PlaylistAppendDialog.java, the onPlaylistSelected() method was modified to check playlist.getTimesStreamIsContained(); if the video already exists in the selected playlist, the append action is blocked immediately and a Toast notification (playlist_add_stream_duplicate_forbidden) informs the user that the video is already in the playlist. To handle edge cases where the same video is added with different start-time parameters (such as ?t=30s, &start=15, or #t=1m), I added a cleanTimingFromUrl() utility function in LocalPlaylistManager.kt that uses regular expressions to strip timing query parameters and hash fragments, reducing URLs to their canonical video identifiers. Updates to getPlaylistDuplicates() and appendToPlaylist() in LocalPlaylistManager.kt ensure that incoming streams are cleaned and filtered against existing playlist entries before database transactions, guaranteeing that duplicate videos cannot be inserted even with varied timestamp parameters. I validated this URL cleaning logic through unit tests in LocalPlaylistManagerDuplicateTest.kt and updated strings.xml with the new duplicate warning string.

  2. DiffUtil Implementation, Created Files, and Crash Handling
    To eliminate list flickering, UI stutter, and index-out-of-bounds crashes during playlist loading and pagination, I refactored the rendering logic in PlaylistFragment.java and LocalPlaylistFragment.java to fully adopt DiffUtil. Previously, every dataset update or pull-to-refresh cleared the adapter using clearStreamItemList() and invoked notifyDataSetChanged(), causing visual glitches and scroll position resets. By overriding startLoading, handleResult, and handleNextItems, data updates now run through infoListAdapter.updateItemsWithDiff() and itemListAdapter.updateItemsWithDiff(), smoothly inserting paginated items via targeted notifyItemRangeInserted() calls. To support this, I created InfoItemDiffCallback.kt and LocalItemDiffCallback.kt along with a comprehensive unit test suite in InfoItemDiffCallbackTest.kt. Furthermore, to prevent crashes caused by duplicate streams in the same playlist, I engineered precomputed occurrence indexing (oldOccurrenceIndices and newOccurrenceIndices) in InfoItemDiffCallback.kt. This ensures Myers’ diff algorithm identifies each stream by its sequence position rather than cross-matching identical URLs, completely avoiding spurious move operations, visual duplication, and array out-of-bounds exceptions when rendering large or paginated playlists.

@github-actions github-actions Bot added size/large PRs with less than 750 changed lines template-ignored labels Oct 31, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Please link this PR to an existing issue you are assigned to. Assignees are expected to discuss the details of the PR beforehand. This PR will be closed if it is not linked to an issue.

@TobiGr

TobiGr commented Oct 31, 2026

Copy link
Copy Markdown
Member

Thank you for the PR. Please use our PR template, fill in the required information and ensure that your PR follows our contribution and AI guidelines.

@Ayush32-ai

Copy link
Copy Markdown
Author

What is it?

  • Bugfix (user facing)
  • Feature (user facing)
  • Codebase improvement (dev facing)
  • Meta improvement to the project (dev facing)

Description of the changes in your PR

This PR fixes duplicate videos being added to local playlists and improves how playlist items are updated.

  • Added a check in PlaylistAppendDialog.java to prevent adding a video if it's already in the selected playlist. A Toast message is shown when the video is already there.
  • Added cleanTimingFromUrl() in LocalPlaylistManager.kt to remove timestamp parameters from video URLs. This makes sure the same video is recognized even if it has a different start time, such as ?t=30s or #t=1m.
  • Updated getPlaylistDuplicates() and appendToPlaylist() to check for duplicates before adding items to the database.
  • Added unit tests for duplicate detection in LocalPlaylistManagerDuplicateTest.kt.
  • Updated PlaylistFragment.java and LocalPlaylistFragment.java to use DiffUtil instead of refreshing the entire list whenever the data changes.
  • Added InfoItemDiffCallback.kt and LocalItemDiffCallback.kt, along with tests for InfoItemDiffCallback.kt.
  • Handled repeated stream URLs in InfoItemDiffCallback.kt using occurrence-based indexing so identical URLs are matched correctly and don't cause incorrect moves or index-related crashes.

Before/After Screenshots/Screen Record

  • Before: Duplicate videos could be added to playlists, and refreshing the list could cause flickering or other issues.
  • After: Duplicate additions are blocked, and playlist updates are handled more efficiently.

Fixes the following issue(s)

Relies on the following changes

  • None.

Testing/QA

Devices
  • Nothing Phone (2a)
Steps for testing
  • Added a video to a local playlist and tried adding it again to check that duplicates are blocked.
  • Checked duplicate detection for URLs with different timestamp parameters.
  • Tested playlist updates, including loading and pagination, to check that items are updated correctly.
  • Ran the unit tests for duplicate detection and DiffUtil.
APK for testing

The debug APK is available under the Checks tab of the PR, in the CI artifacts.

Due diligence

WhatsApp Image 2026-11-01 at 04 00 11

@sonarqubecloud

sonarqubecloud Bot commented Nov 1, 2026

Copy link
Copy Markdown

@Ayush32-ai

Copy link
Copy Markdown
Author

@TobiGr Please check the code and pull request.

@Ayush32-ai Ayush32-ai closed this Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/large PRs with less than 750 changed lines template-ignored

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants