Skip to content

Commit 4d97a76

Browse files
authored
Merge pull request TeamNewPipe#12450 from TeamNewPipe/yt-trending-migration
2 parents 86efde5 + 5aefa4a commit 4d97a76

11 files changed

Lines changed: 238 additions & 75 deletions

File tree

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ dependencies {
214214
// the corresponding commit hash, since JitPack sometimes deletes artifacts.
215215
// If there’s already a git hash, just add more of it to the end (or remove a letter)
216216
// to cause jitpack to regenerate the artifact.
217-
implementation 'com.github.TeamNewPipe:NewPipeExtractor:7adbc48a0aa872c016b8ec089e278d5e12772054'
217+
implementation 'com.github.TeamNewPipe:NewPipeExtractor:v0.24.8'
218218
implementation 'com.github.TeamNewPipe:NoNonsense-FilePicker:5.0.0'
219219

220220
/** Checkstyle **/

app/src/main/java/org/schabi/newpipe/MainActivity.java

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@
7878
import org.schabi.newpipe.player.event.OnKeyDownListener;
7979
import org.schabi.newpipe.player.helper.PlayerHolder;
8080
import org.schabi.newpipe.player.playqueue.PlayQueue;
81-
import org.schabi.newpipe.settings.SettingMigrations;
8281
import org.schabi.newpipe.settings.UpdateSettingsFragment;
82+
import org.schabi.newpipe.settings.migration.MigrationManager;
8383
import org.schabi.newpipe.util.Constants;
8484
import org.schabi.newpipe.util.DeviceUtils;
8585
import org.schabi.newpipe.util.KioskTranslator;
@@ -195,7 +195,7 @@ protected void onCreate(final Bundle savedInstanceState) {
195195
UpdateSettingsFragment.askForConsentToUpdateChecks(this);
196196
}
197197

198-
SettingMigrations.showUserInfoIfPresent(this);
198+
MigrationManager.showUserInfoIfPresent(this);
199199
}
200200

201201
@Override
@@ -263,19 +263,6 @@ public void onDrawerClosed(final View drawerView) {
263263
*/
264264
private void addDrawerMenuForCurrentService() throws ExtractionException {
265265
//Tabs
266-
final int currentServiceId = ServiceHelper.getSelectedServiceId(this);
267-
final StreamingService service = NewPipe.getService(currentServiceId);
268-
269-
int kioskMenuItemId = 0;
270-
271-
for (final String ks : service.getKioskList().getAvailableKiosks()) {
272-
drawerLayoutBinding.navigation.getMenu()
273-
.add(R.id.menu_tabs_group, kioskMenuItemId, 0, KioskTranslator
274-
.getTranslatedKioskName(ks, this))
275-
.setIcon(KioskTranslator.getKioskIcon(ks));
276-
kioskMenuItemId++;
277-
}
278-
279266
drawerLayoutBinding.navigation.getMenu()
280267
.add(R.id.menu_tabs_group, ITEM_ID_SUBSCRIPTIONS, ORDER,
281268
R.string.tab_subscriptions)
@@ -293,6 +280,20 @@ private void addDrawerMenuForCurrentService() throws ExtractionException {
293280
.add(R.id.menu_tabs_group, ITEM_ID_HISTORY, ORDER, R.string.action_history)
294281
.setIcon(R.drawable.ic_history);
295282

283+
//Kiosks
284+
final int currentServiceId = ServiceHelper.getSelectedServiceId(this);
285+
final StreamingService service = NewPipe.getService(currentServiceId);
286+
287+
int kioskMenuItemId = 0;
288+
289+
for (final String ks : service.getKioskList().getAvailableKiosks()) {
290+
drawerLayoutBinding.navigation.getMenu()
291+
.add(R.id.menu_kiosks_group, kioskMenuItemId, 0, KioskTranslator
292+
.getTranslatedKioskName(ks, this))
293+
.setIcon(KioskTranslator.getKioskIcon(ks));
294+
kioskMenuItemId++;
295+
}
296+
296297
//Settings and About
297298
drawerLayoutBinding.navigation.getMenu()
298299
.add(R.id.menu_options_about_group, ITEM_ID_SETTINGS, ORDER, R.string.settings)
@@ -312,10 +313,13 @@ private boolean drawerItemSelected(final MenuItem item) {
312313
changeService(item);
313314
break;
314315
case R.id.menu_tabs_group:
316+
tabSelected(item);
317+
break;
318+
case R.id.menu_kiosks_group:
315319
try {
316-
tabSelected(item);
320+
kioskSelected(item);
317321
} catch (final Exception e) {
318-
ErrorUtil.showUiErrorSnackbar(this, "Selecting main page tab", e);
322+
ErrorUtil.showUiErrorSnackbar(this, "Selecting drawer kiosk", e);
319323
}
320324
break;
321325
case R.id.menu_options_about_group:
@@ -339,7 +343,7 @@ private void changeService(final MenuItem item) {
339343
.setChecked(true);
340344
}
341345

342-
private void tabSelected(final MenuItem item) throws ExtractionException {
346+
private void tabSelected(final MenuItem item) {
343347
switch (item.getItemId()) {
344348
case ITEM_ID_SUBSCRIPTIONS:
345349
NavigationHelper.openSubscriptionFragment(getSupportFragmentManager());
@@ -356,18 +360,19 @@ private void tabSelected(final MenuItem item) throws ExtractionException {
356360
case ITEM_ID_HISTORY:
357361
NavigationHelper.openStatisticFragment(getSupportFragmentManager());
358362
break;
359-
default:
360-
final StreamingService currentService = ServiceHelper.getSelectedService(this);
361-
int kioskMenuItemId = 0;
362-
for (final String kioskId : currentService.getKioskList().getAvailableKiosks()) {
363-
if (kioskMenuItemId == item.getItemId()) {
364-
NavigationHelper.openKioskFragment(getSupportFragmentManager(),
365-
currentService.getServiceId(), kioskId);
366-
break;
367-
}
368-
kioskMenuItemId++;
369-
}
363+
}
364+
}
365+
366+
private void kioskSelected(final MenuItem item) throws ExtractionException {
367+
final StreamingService currentService = ServiceHelper.getSelectedService(this);
368+
int kioskMenuItemId = 0;
369+
for (final String kioskId : currentService.getKioskList().getAvailableKiosks()) {
370+
if (kioskMenuItemId == item.getItemId()) {
371+
NavigationHelper.openKioskFragment(getSupportFragmentManager(),
372+
currentService.getServiceId(), kioskId);
370373
break;
374+
}
375+
kioskMenuItemId++;
371376
}
372377
}
373378

@@ -408,6 +413,7 @@ private void toggleServices() {
408413

409414
drawerLayoutBinding.navigation.getMenu().removeGroup(R.id.menu_services_group);
410415
drawerLayoutBinding.navigation.getMenu().removeGroup(R.id.menu_tabs_group);
416+
drawerLayoutBinding.navigation.getMenu().removeGroup(R.id.menu_kiosks_group);
411417
drawerLayoutBinding.navigation.getMenu().removeGroup(R.id.menu_options_about_group);
412418

413419
// Show up or down arrow

app/src/main/java/org/schabi/newpipe/settings/NewPipeSettings.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import org.schabi.newpipe.App;
1515
import org.schabi.newpipe.R;
16+
import org.schabi.newpipe.settings.migration.MigrationManager;
1617
import org.schabi.newpipe.util.DeviceUtils;
1718

1819
import java.io.File;
@@ -46,7 +47,7 @@ private NewPipeSettings() { }
4647

4748
public static void initSettings(final Context context) {
4849
// first run migrations, then setDefaultValues, since the latter requires the correct types
49-
SettingMigrations.runMigrationsIfNeeded(context);
50+
MigrationManager.runMigrationsIfNeeded(context);
5051

5152
// readAgain is true so that if new settings are added their default value is set
5253
PreferenceManager.setDefaultValues(context, R.xml.main_settings, true);
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package org.schabi.newpipe.settings.migration;
2+
3+
import android.content.Context;
4+
5+
import androidx.annotation.NonNull;
6+
import androidx.appcompat.app.AlertDialog;
7+
import androidx.core.util.Consumer;
8+
9+
import org.schabi.newpipe.R;
10+
import org.schabi.newpipe.error.ErrorUtil;
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
/**
16+
* MigrationManager is responsible for running migrations and showing the user information about
17+
* the migrations that were applied.
18+
*/
19+
public final class MigrationManager {
20+
21+
private static final String TAG = MigrationManager.class.getSimpleName();
22+
/**
23+
* List of UI actions that are performed after the UI is initialized (e.g. showing alert
24+
* dialogs) to inform the user about changes that were applied by migrations.
25+
*/
26+
private static final List<Consumer<Context>> MIGRATION_INFO = new ArrayList<>();
27+
28+
private MigrationManager() {
29+
// MigrationManager is a utility class that is completely static
30+
}
31+
32+
/**
33+
* Run all migrations that are needed for the current version of NewPipe.
34+
* This method should be called at the start of the application, before any other operations
35+
* that depend on the settings.
36+
*
37+
* @param context Context that can be used to run migrations
38+
*/
39+
public static void runMigrationsIfNeeded(@NonNull final Context context) {
40+
SettingMigrations.runMigrationsIfNeeded(context);
41+
}
42+
43+
/**
44+
* Perform UI actions informing about migrations that took place if they are present.
45+
* @param context Context that can be used to show dialogs/snackbars/toasts
46+
*/
47+
public static void showUserInfoIfPresent(@NonNull final Context context) {
48+
if (MIGRATION_INFO.isEmpty()) {
49+
return;
50+
}
51+
52+
try {
53+
MIGRATION_INFO.get(0).accept(context);
54+
} catch (final Exception e) {
55+
ErrorUtil.showUiErrorSnackbar(context, "Showing migration info to the user", e);
56+
// Remove the migration that caused the error and continue with the next one
57+
MIGRATION_INFO.remove(0);
58+
showUserInfoIfPresent(context);
59+
}
60+
}
61+
62+
/**
63+
* Add a migration info action that will be executed after the UI is initialized.
64+
* This can be used to show dialogs/snackbars/toasts to inform the user about changes that
65+
* were applied by migrations.
66+
*
67+
* @param info the action to be executed
68+
*/
69+
public static void addMigrationInfo(final Consumer<Context> info) {
70+
MIGRATION_INFO.add(info);
71+
}
72+
73+
/**
74+
* This method should be called when the user dismisses the migration info
75+
* to check if there are any more migration info actions to be shown.
76+
* @param context Context that can be used to show dialogs/snackbars/toasts
77+
*/
78+
public static void onMigrationInfoDismissed(@NonNull final Context context) {
79+
MIGRATION_INFO.remove(0);
80+
showUserInfoIfPresent(context);
81+
}
82+
83+
/**
84+
* Creates a dialog to inform the user about the migration.
85+
* @param uiContext Context that can be used to show dialogs/snackbars/toasts
86+
* @param title the title of the dialog
87+
* @param message the message of the dialog
88+
* @return the dialog that can be shown to the user with a custom dismiss listener
89+
*/
90+
static AlertDialog createMigrationInfoDialog(@NonNull final Context uiContext,
91+
@NonNull final String title,
92+
@NonNull final String message) {
93+
return new AlertDialog.Builder(uiContext)
94+
.setTitle(title)
95+
.setMessage(message)
96+
.setPositiveButton(R.string.ok, null)
97+
.setOnDismissListener(dialog ->
98+
MigrationManager.onMigrationInfoDismissed(uiContext))
99+
.setCancelable(false) // prevents the dialog from being dismissed accidentally
100+
.create();
101+
}
102+
103+
}

0 commit comments

Comments
 (0)