some caching stuff
This commit is contained in:
@@ -174,8 +174,11 @@ public class AudioDownloader {
|
||||
NotificationChannels.getNotificationManager().cancel(trackId.hashCode());
|
||||
}
|
||||
|
||||
private static void cachePlaylist(ArrayList<MusicTrack> playlist) {
|
||||
for (MusicTrack musicTrack : playlist) {
|
||||
public static void cachePlaylist(Playlist playlist) {
|
||||
var tracks = PlaylistConverter.getPlaylist(playlist);
|
||||
var tracksWithUrls = tracks.stream().filter(track -> !track.D.isEmpty()).collect(Collectors.toList());
|
||||
|
||||
for (MusicTrack musicTrack : tracksWithUrls) {
|
||||
CompletableFuture.runAsync(() -> cacheTrack(musicTrack), executor);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.util.ArrayList;
|
||||
import bruhcollective.itaysonlab.libvkx.client.LibVKXClient;
|
||||
import bruhcollective.itaysonlab.libvkx.client.LibVKXClientImpl;
|
||||
import ru.vtosters.lite.downloaders.AudioDownloader;
|
||||
import ru.vtosters.lite.music.cache.CacheDatabaseDelegate;
|
||||
import ru.vtosters.lite.utils.AndroidUtils;
|
||||
import ru.vtosters.lite.utils.NetworkUtils;
|
||||
|
||||
@@ -81,11 +82,7 @@ public class MusicBottomSheetHook {
|
||||
if (isVkxInstalled())
|
||||
actions.add(getPlayInVKXAction());
|
||||
|
||||
if (isCached(playlist)){
|
||||
actions.add(getRemoveCacheTrackAction());
|
||||
} else {
|
||||
actions.add(addToCacheTrackAction());
|
||||
}
|
||||
actions.add(addToCacheTrackAction());
|
||||
|
||||
if (isNetworkConnected()){
|
||||
actions.add(downloadAsMp3Action());
|
||||
@@ -131,12 +128,8 @@ public class MusicBottomSheetHook {
|
||||
if (actionId == AndroidUtils.getIdentifier("play_in_vkx", "id"))
|
||||
return tryPlayInVKX(null, null, playlist);
|
||||
|
||||
if (actionId == AndroidUtils.getIdentifier("remove_from_cache", "id")) {
|
||||
// TODO make playlist del cache
|
||||
AndroidUtils.sendToast("remove_from_cache");
|
||||
return true;
|
||||
} else if (actionId == AndroidUtils.getIdentifier("add_to_cache", "id")) {
|
||||
// TODO make playlist caching
|
||||
if (actionId == AndroidUtils.getIdentifier("add_to_cache", "id")) {
|
||||
AudioDownloader.cachePlaylist(playlist);
|
||||
AndroidUtils.sendToast("add_to_cache");
|
||||
return true;
|
||||
}
|
||||
|
||||
+11
-20
@@ -1,5 +1,9 @@
|
||||
package ru.vtosters.lite.music.cache;
|
||||
|
||||
import static ru.vtosters.lite.music.cache.FileCacheImplementation.getCacheDir;
|
||||
import static ru.vtosters.lite.utils.AndroidUtils.*;
|
||||
import static ru.vtosters.lite.utils.IOUtils.*;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.DatabaseUtils;
|
||||
@@ -21,10 +25,13 @@ import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import bruhcollective.itaysonlab.libvkx.ILibVkxService;
|
||||
import bruhcollective.itaysonlab.libvkx.client.LibVKXClient;
|
||||
import bruhcollective.itaysonlab.libvkx.client.LibVKXClientImpl;
|
||||
import java8.util.concurrent.CompletableFuture;
|
||||
import ru.vtosters.lite.music.converter.playlist.PlaylistConverter;
|
||||
import ru.vtosters.lite.utils.AndroidUtils;
|
||||
import ru.vtosters.lite.utils.IOUtils;
|
||||
|
||||
@@ -45,7 +52,7 @@ public class CacheDatabaseDelegate {
|
||||
private static DbHelper helperInstance;
|
||||
|
||||
protected static DbHelper getHelper() {
|
||||
if (helperInstance == null) helperInstance = new DbHelper(AndroidUtils.getGlobalContext());
|
||||
if (helperInstance == null) helperInstance = new DbHelper(getGlobalContext());
|
||||
return helperInstance;
|
||||
}
|
||||
|
||||
@@ -88,6 +95,8 @@ public class CacheDatabaseDelegate {
|
||||
|
||||
public static void clear() {
|
||||
getHelper().getReadableDatabase().execSQL(String.format("DELETE FROM %s", TABLE_NAME));
|
||||
deleteRecursive(getCacheDir());
|
||||
sendToast("Кешированные песни удалены");
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@@ -222,10 +231,6 @@ public class CacheDatabaseDelegate {
|
||||
return list;
|
||||
}
|
||||
|
||||
public static boolean isCached(Playlist playlist){
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isCached(String trackId) {
|
||||
var formula = String.format(
|
||||
"SELECT COUNT(%s) FROM %s WHERE %s = '%s'",
|
||||
@@ -270,21 +275,7 @@ public class CacheDatabaseDelegate {
|
||||
}
|
||||
|
||||
public static void removeTrackFromCache(String trackId) {
|
||||
if (LibVKXClient.isIntegrationEnabled()) {
|
||||
LibVKXClient.getInstance().runOnService((service -> {
|
||||
try {
|
||||
var splits = trackId.split("_");
|
||||
var id = Integer.parseInt(splits[1]);
|
||||
var owner = Integer.parseInt(splits[0]);
|
||||
service.deleteTrackFromCache(id, owner);
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
IOUtils.deleteRecursive(FileCacheImplementation.getTrackFolder(trackId));
|
||||
deleteRecursive(FileCacheImplementation.getTrackFolder(trackId));
|
||||
|
||||
var formula = String.format(
|
||||
"DELETE FROM %s WHERE %s = '%s'",
|
||||
|
||||
@@ -45,6 +45,7 @@ import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import ru.vtosters.lite.downloaders.VideoDownloader;
|
||||
import ru.vtosters.lite.music.Scrobbler;
|
||||
import ru.vtosters.lite.music.cache.CacheDatabaseDelegate;
|
||||
import ru.vtosters.lite.ui.adapters.ImagineArrayAdapter;
|
||||
import ru.vtosters.lite.utils.AndroidUtils;
|
||||
import ru.vtosters.lite.utils.LifecycleUtils;
|
||||
@@ -95,6 +96,12 @@ public class MediaFragment extends MaterialPreferenceToolbarFragment {
|
||||
findPreference("lastfm_enabled").setEnabled(false);
|
||||
}
|
||||
|
||||
findPreference("cached_tracks").setSummary("Всего закешировано " + CacheDatabaseDelegate.getTrackCount() + " песен");
|
||||
findPreference("cached_tracks").setOnPreferenceClickListener(preference -> {
|
||||
delcache(requireContext());
|
||||
return true;
|
||||
});
|
||||
|
||||
if (!isVkxInstalled()){
|
||||
findPreference("vkx_sett").setVisible(false);
|
||||
}
|
||||
@@ -178,6 +185,22 @@ public class MediaFragment extends MaterialPreferenceToolbarFragment {
|
||||
alert.getButton(DialogInterface.BUTTON_NEUTRAL).setTextColor(getResources().getColor(com.vtosters.lite.R.color.red));
|
||||
}
|
||||
|
||||
private void delcache(Context ctx) {
|
||||
VkAlertDialog.Builder alertDialog = new VkAlertDialog.Builder(ctx);
|
||||
alertDialog.setTitle("Внимание!");
|
||||
alertDialog.setMessage("Вы действительно хотите удалить все кешированные песни?");
|
||||
alertDialog.setPositiveButton(AndroidUtils.getString("vkim_yes"), (dialog, which) -> {
|
||||
CacheDatabaseDelegate.clear();
|
||||
});
|
||||
alertDialog.setNeutralButton(AndroidUtils.getString("vkim_no"), (dialog, which) -> {
|
||||
dialog.cancel();
|
||||
});
|
||||
var alert = alertDialog.create();
|
||||
alert.show();
|
||||
alert.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(getAccentColor());
|
||||
alert.getButton(DialogInterface.BUTTON_NEUTRAL).setTextColor(getResources().getColor(com.vtosters.lite.R.color.red));
|
||||
}
|
||||
|
||||
public static void deleteVideoHistory() {
|
||||
Thread thread = new Thread(() -> {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user