Skip to content

Commit 5fbbcf4

Browse files
committed
Fix inconsistency when LocalPlaylist is used as MainFragment tab
1 parent 3b84ce8 commit 5fbbcf4

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.schabi.newpipe.databinding.FragmentMainBinding;
3939
import org.schabi.newpipe.error.ErrorUtil;
4040
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
41+
import org.schabi.newpipe.local.playlist.LocalPlaylistFragment;
4142
import org.schabi.newpipe.settings.tabs.Tab;
4243
import org.schabi.newpipe.settings.tabs.TabsManager;
4344
import org.schabi.newpipe.util.NavigationHelper;
@@ -217,6 +218,12 @@ private void updateTitleForTab(final int tabPosition) {
217218
setTitle(tabsList.get(tabPosition).getTabName(requireContext()));
218219
}
219220

221+
public void commitPlaylistTabs() {
222+
pagerAdapter.getLocalPlaylistFragments()
223+
.stream()
224+
.forEach(LocalPlaylistFragment::commitChanges);
225+
}
226+
220227
private void updateTabLayoutPosition() {
221228
final ScrollableTabLayout tabLayout = binding.mainTabLayout;
222229
final ViewPager viewPager = binding.pager;
@@ -268,10 +275,18 @@ public void onTabReselected(final TabLayout.Tab tab) {
268275
updateTitleForTab(tab.getPosition());
269276
}
270277

271-
private static final class SelectedTabsPagerAdapter
278+
public static final class SelectedTabsPagerAdapter
272279
extends FragmentStatePagerAdapterMenuWorkaround {
273280
private final Context context;
274281
private final List<Tab> internalTabsList;
282+
/**
283+
* Keep reference to LocalPlaylistFragments, because their data can be modified by the user
284+
* during runtime and changes are not committed immediately. However, in some cases,
285+
* the changes need to be committed immediately by calling
286+
* {@link LocalPlaylistFragment#commitChanges()}.
287+
* The fragments are removed when {@link LocalPlaylistFragment#onDestroy()} is called.
288+
*/
289+
private final List<LocalPlaylistFragment> localPlaylistFragments = new ArrayList<>();
275290

276291
private SelectedTabsPagerAdapter(final Context context,
277292
final FragmentManager fragmentManager,
@@ -298,9 +313,17 @@ public Fragment getItem(final int position) {
298313
((BaseFragment) fragment).useAsFrontPage(true);
299314
}
300315

316+
if (fragment instanceof LocalPlaylistFragment) {
317+
localPlaylistFragments.add((LocalPlaylistFragment) fragment);
318+
}
319+
301320
return fragment;
302321
}
303322

323+
public List<LocalPlaylistFragment> getLocalPlaylistFragments() {
324+
return localPlaylistFragments;
325+
}
326+
304327
@Override
305328
public int getItemPosition(@NonNull final Object object) {
306329
// Causes adapter to reload all Fragments when

app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
import org.schabi.newpipe.fragments.BackPressable;
8686
import org.schabi.newpipe.fragments.BaseStateFragment;
8787
import org.schabi.newpipe.fragments.EmptyFragment;
88+
import org.schabi.newpipe.fragments.MainFragment;
8889
import org.schabi.newpipe.fragments.list.comments.CommentsFragment;
8990
import org.schabi.newpipe.fragments.list.videos.RelatedItemsFragment;
9091
import org.schabi.newpipe.ktx.AnimationType;
@@ -482,6 +483,8 @@ private void setOnClickListeners() {
482483
// commit previous pending changes to database
483484
if (fragment instanceof LocalPlaylistFragment) {
484485
((LocalPlaylistFragment) fragment).commitChanges();
486+
} else if (fragment instanceof MainFragment) {
487+
((MainFragment) fragment).commitPlaylistTabs();
485488
}
486489

487490
disposables.add(PlaylistDialog.createCorrespondingDialog(requireContext(),

app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.schabi.newpipe.error.ErrorInfo;
4242
import org.schabi.newpipe.error.UserAction;
4343
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
44+
import org.schabi.newpipe.fragments.MainFragment;
4445
import org.schabi.newpipe.fragments.list.playlist.PlaylistControlViewHolder;
4546
import org.schabi.newpipe.info_list.dialog.InfoItemDialog;
4647
import org.schabi.newpipe.info_list.dialog.StreamDialogDefaultEntry;
@@ -71,7 +72,7 @@
7172

7273
public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistStreamEntry>, Void>
7374
implements PlaylistControlViewHolder {
74-
// Save the list 10 seconds after the last change occurred
75+
/** Save the list 10 seconds after the last change occurred. */
7576
private static final long SAVE_DEBOUNCE_MILLIS = 10000;
7677
private static final int MINIMUM_INITIAL_DRAG_VELOCITY = 12;
7778
@State
@@ -92,13 +93,20 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
9293
private PublishSubject<Long> debouncedSaveSignal;
9394
private CompositeDisposable disposables;
9495

95-
/* Has the playlist been fully loaded from db */
96+
/** Whether the playlist has been fully loaded from db. */
9697
private AtomicBoolean isLoadingComplete;
97-
/* Has the playlist been modified (e.g. items reordered or deleted) */
98+
/** Whether the playlist has been modified (e.g. items reordered or deleted) */
9899
private AtomicBoolean isModified;
99-
/* Flag to prevent simultaneous rewrites of the playlist */
100+
/** Flag to prevent simultaneous rewrites of the playlist. */
100101
private boolean isRewritingPlaylist = false;
101102

103+
/**
104+
* The pager adapter that the fragment is created from when it is used as frontpage, i.e.
105+
* {@link #useAsFrontPage} is {@link true}.
106+
*/
107+
@Nullable
108+
private MainFragment.SelectedTabsPagerAdapter tabsPagerAdapter = null;
109+
102110
public static LocalPlaylistFragment getInstance(final long playlistId, final String name) {
103111
final LocalPlaylistFragment instance = new LocalPlaylistFragment();
104112
instance.setInitialData(playlistId, name);
@@ -158,9 +166,11 @@ protected ViewBinding getListHeader() {
158166
return headerBinding;
159167
}
160168

161-
// Commit changes immediately when the user turns to the player.
162-
// Delete operations will be committed to ensure that the database
163-
// is up to date when the user adds the just deleted stream by the player.
169+
/**
170+
* <p>Commit changes immediately if the playlist has been modified.</p>
171+
* Delete operations and other modifications will be committed to ensure that the database
172+
* is up to date, e.g. when the user adds the just deleted stream from another fragment.
173+
*/
164174
public void commitChanges() {
165175
if (isModified != null && isModified.get()) {
166176
saveImmediate();
@@ -300,6 +310,9 @@ public void onDestroy() {
300310
if (disposables != null) {
301311
disposables.dispose();
302312
}
313+
if (tabsPagerAdapter != null) {
314+
tabsPagerAdapter.getLocalPlaylistFragments().remove(this);
315+
}
303316

304317
debouncedSaveSignal = null;
305318
playlistManager = null;
@@ -886,5 +899,10 @@ private void createShareConfirmationDialog() {
886899
)
887900
.show();
888901
}
902+
903+
public void setTabsPagerAdapter(
904+
@Nullable final MainFragment.SelectedTabsPagerAdapter tabsPagerAdapter) {
905+
this.tabsPagerAdapter = tabsPagerAdapter;
906+
}
889907
}
890908

0 commit comments

Comments
 (0)