Skip to content

Commit

Permalink
Remember last selected instance
Browse files Browse the repository at this point in the history
Reloads the current instance from the persistence layer (preferences)
  • Loading branch information
litetex committed Apr 20, 2022
1 parent 09774a2 commit 0326eb1
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 44 deletions.
16 changes: 4 additions & 12 deletions app/src/main/java/org/schabi/newpipe/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@
import org.schabi.newpipe.util.TLSSocketFactoryCompat;
import org.schabi.newpipe.util.ThemeHelper;
import org.schabi.newpipe.util.services.InstanceManager;
import org.schabi.newpipe.util.services.PeertubeInstanceManager;
import org.schabi.newpipe.util.services.InstanceManagerHelper;
import org.schabi.newpipe.util.services.ServiceHelper;
import org.schabi.newpipe.util.services.YoutubeLikeInstanceManager;
import org.schabi.newpipe.views.FocusOverlayView;

import java.util.ArrayList;
Expand Down Expand Up @@ -392,16 +391,9 @@ private void showServices() {
.add(R.id.menu_services_group, s.getServiceId(), ORDER, title)
.setIcon(ServiceHelper.getIcon(s.getServiceId()));

// peertube specifics
InstanceManager<?> instanceManager = null;
if (s.getServiceId() == 0) {
instanceManager = YoutubeLikeInstanceManager.MANAGER;
} else if (s.getServiceId() == 3) {
instanceManager = PeertubeInstanceManager.MANAGER;
}
if (instanceManager != null) {
enhanceServiceMenu(menuItem, instanceManager);
}
// instance specifics
InstanceManagerHelper.getManagerForServiceId(s.getServiceId())
.ifPresent(im -> enhanceServiceMenu(menuItem, im));
}
drawerLayoutBinding.navigation.getMenu()
.getItem(ServiceHelper.getSelectedServiceId(this))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.schabi.newpipe.util.services;

import android.content.Context;
import android.util.Log;

import androidx.annotation.StringRes;
import androidx.preference.PreferenceManager;
Expand All @@ -11,6 +12,7 @@
import com.grack.nanojson.JsonStringWriter;
import com.grack.nanojson.JsonWriter;

import org.schabi.newpipe.MainActivity;
import org.schabi.newpipe.extractor.InstanceBasedStreamingService;
import org.schabi.newpipe.extractor.instance.Instance;

Expand All @@ -19,6 +21,8 @@
import java.util.stream.Collectors;

public abstract class AbstractInstanceManager<I extends Instance> implements InstanceManager<I> {
private static final String TAG = "AbsInstanceManager";

protected abstract InstanceBasedStreamingService<I> getRelatedStreamingService();

protected abstract I createInstanceFromPersistence(JsonObject jsonObject);
Expand All @@ -28,9 +32,8 @@ public abstract class AbstractInstanceManager<I extends Instance> implements Ins
@StringRes
protected abstract int getListPersistenceKey();


@Override
public List<I> saveInstanceList(final List<I> instances, final Context context) {
public void saveInstanceList(final List<I> instances, final Context context) {
final JsonStringWriter jsonWriter = JsonWriter.string().object().array("instances");
for (final I instance : instances) {
jsonWriter.object();
Expand All @@ -42,15 +45,14 @@ public List<I> saveInstanceList(final List<I> instances, final Context context)
.edit()
.putString(context.getString(getListPersistenceKey()), jsonToSave)
.apply();
return null;
}

@Override
public List<I> getInstanceList(final Context context) {
final String savedInstanceListKey = context.getString(getListPersistenceKey());
final String savedJson = PreferenceManager.getDefaultSharedPreferences(context)
.getString(savedInstanceListKey, null);
if (null == savedJson) {
if (savedJson == null) {
return getDefaultInstanceList();
}

Expand Down Expand Up @@ -90,6 +92,33 @@ public I saveCurrentInstance(final I instance, final Context context) {
return instance;
}

@Override
public void reloadCurrentInstanceFromPersistence(final Context context) {
final String json = PreferenceManager.getDefaultSharedPreferences(context)
.getString(context.getString(getSelectedInstancePersistenceKey()), null);
if (json == null) {
return;
}

final JsonObject jsonObject;
try {
jsonObject = JsonParser.object().from(json);
} catch (final JsonParserException e) {
if (MainActivity.DEBUG) {
Log.w(TAG, "Failed to load instance from settings", e);
}
return;
}

try {
getRelatedStreamingService().setInstance(createInstanceFromPersistence(jsonObject));
} catch (final Exception e) {
if (MainActivity.DEBUG) {
Log.w(TAG, "Failed to load instance from settings", e);
}
}
}

@Override
public I getCurrentInstance() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,55 @@

import java.util.List;

/**
* Manages service instances.
*
* @param <I>
*/
public interface InstanceManager<I extends Instance> {
List<I> saveInstanceList(List<I> instances, Context context);

/**
* Saves the instance list (to persistence/preferences).
*
* @param instances instance list
* @param context Context
*/
void saveInstanceList(List<I> instances, Context context);

/**
* Returns the current instance list.
*
* @param context Context
* @return instance list
*/
List<I> getInstanceList(Context context);

/**
* Returns the current instance (from memory / the corresponding streaming-service).
*
* @return The current instance
*/
I getCurrentInstance();

/**
* Reloads the current instance from the persistence layer (preferences).
*
* @param context Context
*/
void reloadCurrentInstanceFromPersistence(Context context);

/**
* Saves the instance as the currently used one (also to the persistence).
*
* @param instance The instance
* @param context Context
* @return the saved instance
*/
I saveCurrentInstance(I instance, Context context);

/**
* Restores the default values.
*
* @param context Context
*/
void restoreDefaults(Context context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.schabi.newpipe.util.services;

import org.schabi.newpipe.extractor.instance.Instance;

import java.util.Optional;

public final class InstanceManagerHelper {
private InstanceManagerHelper() {
// No impl
}

@SuppressWarnings("java:S1452") // otherwise generics won't work!
public static Optional<? extends InstanceManager<? extends Instance>> getManagerForServiceId(
final int serviceId
) {
if (serviceId == 0) {
return Optional.of(YoutubeLikeInstanceManager.MANAGER);
} else if (serviceId == 3) {
return Optional.of(PeertubeInstanceManager.MANAGER);
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,17 @@
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;
import androidx.preference.PreferenceManager;

import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;

import org.schabi.newpipe.R;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.ServiceList;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.instance.Instance;
import org.schabi.newpipe.extractor.services.peertube.PeertubeInstance;
import org.schabi.newpipe.extractor.services.youtube.invidious.InvidiousInstance;

import java.util.OptionalInt;
Expand Down Expand Up @@ -174,26 +168,8 @@ public static boolean isBeta(final StreamingService s) {
}

public static void initService(final Context context, final int serviceId) {
if (serviceId == ServiceList.PeerTube.getServiceId()) {
final SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
final String json = sharedPreferences.getString(context.getString(
R.string.peertube_selected_instance_key), null);
if (null == json) {
return;
}

final JsonObject jsonObject;
try {
jsonObject = JsonParser.object().from(json);
} catch (final JsonParserException e) {
return;
}
final String name = jsonObject.getString("name");
final String url = jsonObject.getString("url");
final PeertubeInstance instance = new PeertubeInstance(url, name);
ServiceList.PeerTube.setInstance(instance);
}
InstanceManagerHelper.getManagerForServiceId(serviceId)
.ifPresent(im -> im.reloadCurrentInstanceFromPersistence(context));
}

public static void initServices(final Context context) {
Expand Down

0 comments on commit 0326eb1

Please sign in to comment.