Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the subscription db performance #1300

Merged
merged 10 commits into from
Aug 18, 2024
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ dependencies {
// implementation 'com.github.TeamNewPipe.NewPipeExtractor:NewPipeExtractor:v0.22.6'
implementation 'com.github.SkyTubeTeam.NewPipeExtractor:NewPipeExtractor:skytube-2024-07-24'

implementation ('com.github.SkyTubeTeam.components:okhttp-client:0.0.7') {
def skytubeComponents = '0.0.9'
implementation ("com.github.SkyTubeTeam.components:okhttp-client:$skytubeComponents") {
exclude group: 'com.github.SkyTubeTeam.NewPipeExtractor', module: 'extractor'
}
implementation "com.github.SkyTubeTeam.components:android-utils:$skytubeComponents"

// Java HTML parser
implementation "org.jsoup:jsoup:1.17.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.android.gms.cast.framework.media.RemoteMediaClient;

import free.rm.skytube.R;
import free.rm.skytube.businessobjects.YouTube.POJOs.YouTubeChannel;
import free.rm.skytube.businessobjects.db.DatabaseTasks;
import free.rm.skytube.databinding.FragmentChromecastControllerBinding;
import free.rm.skytube.databinding.VideoDescriptionBinding;
Expand Down Expand Up @@ -106,8 +107,9 @@ private void setupDescription() {

compositeDisposable.add(
DatabaseTasks.getChannelInfo(requireContext(), video.getChannelId(), false)
.subscribe(youTubeChannel -> {
videoDescriptionBinding.videoDescSubscribeButton.setChannel(youTubeChannel);
.subscribe(subscribedChannel -> {
YouTubeChannel youTubeChannel = subscribedChannel.channel();
videoDescriptionBinding.videoDescSubscribeButton.setChannel(youTubeChannel);

Glide.with(requireContext())
.load(youTubeChannel.getThumbnailUrl())
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/free/rm/skytube/app/FeedUpdateTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ public synchronized boolean start(Context context) {
if (refreshInProgress) {
return false;
}
createNotificationChannel(context);
if (!SkyTubeApp.isConnected(context)) {
return false;
}
createNotificationChannel(context);
SkyTubeApp.getSettings().setRefreshSubsFeedFull(false);
refreshInProgress = true;

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/free/rm/skytube/app/SkyTubeApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ public static boolean openContent(Context ctx, ContentId content) {
public static void launchChannel(ChannelId channelId, Context context) {
if (channelId != null) {
DatabaseTasks.getChannelInfo(context, channelId, true)
.subscribe(youTubeChannel -> launchChannel(youTubeChannel, context));
.subscribe(youTubeChannel -> launchChannel(youTubeChannel.channel(), context));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,12 @@ private void fileDownloadStatus() {
.query(new DownloadManager.Query().setFilterById(downloadId));

if (cursor != null && cursor.moveToFirst()) {
final int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
downloadSuccessful = (cursor.getInt(columnIndex) == DownloadManager.STATUS_SUCCESSFUL);
downloadSuccessful = (cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL);

if (downloadSuccessful) {
downloadedFileUri = Uri.parse(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)));
downloadedFileUri = Uri.parse(cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI)));
} else {
final int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
final int columnReason = cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_REASON);
final int reason = cursor.getInt(columnReason);

// output why the download has failed...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,11 @@
import androidx.annotation.StringRes;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import free.rm.skytube.BuildConfig;
import free.rm.skytube.R;
import free.rm.skytube.app.SkyTubeApp;
import free.rm.skytube.businessobjects.YouTube.newpipe.NewPipeService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Objects;

import free.rm.skytube.businessobjects.YouTube.newpipe.ChannelId;
import free.rm.skytube.businessobjects.YouTube.newpipe.NewPipeException;
import free.rm.skytube.businessobjects.YouTube.newpipe.NewPipeService;
import free.rm.skytube.businessobjects.YouTube.newpipe.VideoPager;
Expand All @@ -27,11 +28,11 @@
*/
public class NewPipeChannelVideos extends NewPipeVideos {

private String channelId;
private ChannelId channelId;

// Important, this is called from the channel tab
public void setQuery(String query) {
this.channelId = Objects.requireNonNull(query, "query missing");
this.channelId = new ChannelId(query);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@
package free.rm.skytube.businessobjects.YouTube.POJOs;

import free.rm.skytube.businessobjects.YouTube.newpipe.ChannelId;
import free.rm.skytube.businessobjects.model.Status;

public class ChannelView {
private final ChannelId id;
private final String title;
private final String thumbnailUrl;
private boolean newVideosSinceLastVisit;
private final Status status;

public ChannelView(ChannelId id, String title, String thumbnailUrl, boolean newVideosSinceLastVisit) {
public ChannelView(ChannelId id, String title, String thumbnailUrl, boolean newVideosSinceLastVisit, Status status) {
this.id = id;
this.title = title;
this.thumbnailUrl = thumbnailUrl;
this.newVideosSinceLastVisit = newVideosSinceLastVisit;
this.status = status;
}

public ChannelId getId() {
Expand All @@ -47,6 +50,10 @@ public boolean isNewVideosSinceLastVisit() {
return newVideosSinceLastVisit;
}

public Status status() {
return status;
}

public void setNewVideosSinceLastVisit(boolean newVideosSinceLastVisit) {
this.newVideosSinceLastVisit = newVideosSinceLastVisit;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* SkyTube
* Copyright (C) 2024 Zsombor Gegesy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation (version 3 of the License).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package free.rm.skytube.businessobjects.YouTube.POJOs;

import java.util.Objects;

import free.rm.skytube.businessobjects.YouTube.newpipe.ChannelId;
import free.rm.skytube.businessobjects.model.Status;

public final class PersistentChannel {
final YouTubeChannel channel;
final long channelPk;
final Long subscriptionPk;
final Status status;

public PersistentChannel(YouTubeChannel channel, long channelPk, Long subscriptionPk, Status status) {
this.channel = Objects.requireNonNull(channel, "channel");
this.channelPk = channelPk;
this.subscriptionPk = subscriptionPk;
this.status = status;
}

public YouTubeChannel channel() {
return channel;
}

public Status status() {
return status;
}

public ChannelId getChannelId() {
return channel.getChannelId();
}

public long channelPk() {
return channelPk;
}

public Long subscriptionPk() {
return subscriptionPk;
}

public boolean isSubscribed() {
return subscriptionPk != null;
}

public PersistentChannel with(YouTubeChannel newInstance) {
newInstance.setUserSubscribed(isSubscribed());
return new PersistentChannel(newInstance, channelPk, subscriptionPk, status);
}

public PersistentChannel withSubscriptionPk(Long newSubscriptionPk) {
return new PersistentChannel(channel, channelPk, newSubscriptionPk, status);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void setUserSubscribed(boolean userSubscribed) {
}

public Disposable updateLastVisitTime() {
return SubscriptionsDb.getSubscriptionsDb().updateLastVisitTimeAsync(id).subscribe(timestamp -> {
return SubscriptionsDb.getSubscriptionsDb().updateLastVisitTimeAsync(getChannelId()).subscribe(timestamp -> {
lastVisitTime = timestamp;
if (lastVisitTime < 0) {
Logger.e(YouTubeChannel.this, "Unable to update channel's last visit time. ChannelID=" + id);
Expand All @@ -143,6 +143,10 @@ public long getLastVideoTime() {
return lastVideoTime;
}

public void setLastVideoTime(long lastVideoTime) {
this.lastVideoTime = lastVideoTime;
}

public List<String> getTags() {
return tags;
}
Expand Down Expand Up @@ -174,12 +178,10 @@ public Single<Boolean> blockChannel() {
* out.
*/
public Single<Boolean> blockChannel(boolean displayToastMessage) {
return SubscriptionsDb.getSubscriptionsDb().getUserSubscribedToChannel(getChannelId())
.flatMap(isSubscribed -> DatabaseTasks.subscribeToChannel(false,
null, SkyTubeApp.getContext(), this, false))
.map(result -> SkyTubeApp.getSettings().isChannelDenyListEnabled())
.flatMap(isDenyListEnabled -> {
if (isDenyListEnabled) {
return DatabaseTasks.subscribeToChannel(false,
null, SkyTubeApp.getContext(), getChannelId(), false)
.flatMap(result -> {
if (SkyTubeApp.getSettings().isChannelDenyListEnabled()) {
return dennyChannel(displayToastMessage);
} else {
return removeAllowedChannel(displayToastMessage);
Expand All @@ -196,17 +198,9 @@ public Single<Boolean> unblockChannel() {
return unblockChannel(true);
}

public Single<Boolean> unblockChannel(boolean displayToastMessage) {
return Single.fromCallable(() -> SkyTubeApp.getSettings().isChannelDenyListEnabled())
.flatMap(isDenyListEnabled -> {
if (isDenyListEnabled) {
return removeDeniedChannel(displayToastMessage);
} else {
return allowChannel(displayToastMessage);
}
})
.observeOn(AndroidSchedulers.mainThread());
}
public Single<Boolean> unblockChannel(boolean displayToastMessage) {
return (SkyTubeApp.getSettings().isChannelDenyListEnabled() ? removeDeniedChannel(displayToastMessage) : allowChannel(displayToastMessage)).observeOn(AndroidSchedulers.mainThread());
}

/**
* Denny the channel.
Expand Down Expand Up @@ -292,39 +286,6 @@ private Single<Boolean> removeDeniedChannel(boolean displayToastMessage) {
});
}

public static Disposable subscribeChannel(final Context context, final ChannelId channelId) {
if (channelId != null) {
return DatabaseTasks.getChannelInfo(context, channelId, false)
.observeOn(Schedulers.io())
.map(youTubeChannel ->
new Pair<>(youTubeChannel, SubscriptionsDb.getSubscriptionsDb().subscribe(youTubeChannel))
)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(youTubeChannelWithResult -> {
switch(youTubeChannelWithResult.second) {
case SUCCESS: {
youTubeChannelWithResult.first.setUserSubscribed(true);
EventBus.getInstance().notifyMainTabChanged(EventBus.SettingChange.SUBSCRIPTION_LIST_CHANGED);
SkyTubeApp.getSettings().setRefreshSubsFeedFromCache(true);
Toast.makeText(context, R.string.channel_subscribed, Toast.LENGTH_LONG).show();
break;
}
case NOT_MODIFIED: {
Toast.makeText(context, R.string.channel_already_subscribed, Toast.LENGTH_LONG).show();
break;
}
default: {
Toast.makeText(context, R.string.channel_subscribe_failed, Toast.LENGTH_LONG).show();
break;
}
}
});
} else {
Toast.makeText(context, "Channel is not specified", Toast.LENGTH_LONG).show();
return Disposable.empty();
}
}

public String getChannelUrl() {
return getChannelId().toURL();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class VideoBlocker {
/** Default preferred language(s) -- by default, no language shall be filtered out. */
private static final Set<String> defaultPrefLanguages = new HashSet<>(SkyTubeApp.getStringArrayAsList(R.array.languages_iso639_codes));

private Settings settings;
private final Settings settings;

public VideoBlocker() {
settings = SkyTubeApp.getSettings();
Expand Down Expand Up @@ -119,7 +119,7 @@ public List<CardData> filter(List<CardData> videosList) {
* @return True if the user wants to use the video blocker, false otherwise.
*/
private boolean isVideoBlockerEnabled() {
return SkyTubeApp.getSettings().isEnableVideoBlocker();
return settings.isEnableVideoBlocker();
}

/**
Expand Down Expand Up @@ -448,9 +448,9 @@ public String toString() {
*/
public static class BlockedVideo implements Serializable {

private YouTubeVideo video;
private FilterType filteringType;
private String reason;
private final YouTubeVideo video;
private final FilterType filteringType;
private final String reason;


BlockedVideo(YouTubeVideo video, FilterType filteringType, String reason) {
Expand Down Expand Up @@ -496,8 +496,8 @@ public interface VideoBlockerListener {
private static class LanguageDetectionSingleton {

private static LanguageDetectionSingleton languageDetectionSingleton = null;
private TextObjectFactory textObjectFactory;
private LanguageDetector languageDetector;
private final TextObjectFactory textObjectFactory;
private final LanguageDetector languageDetector;


private LanguageDetectionSingleton() throws IOException {
Expand Down
Loading
Loading