From 427b2317b2e9e895c294e040d6c0103f09c8304e Mon Sep 17 00:00:00 2001 From: mattcarter11 <38189440+mattcarter11@users.noreply.github.com> Date: Thu, 5 Dec 2024 21:49:44 +0100 Subject: [PATCH] download: add likedAutoDownloads --- .../java/com/dd3boh/outertune/MainActivity.kt | 2 +- .../outertune/constants/PreferenceKeys.kt | 5 ++ .../com/dd3boh/outertune/db/daos/SongsDao.kt | 3 ++ .../dd3boh/outertune/extensions/ContextExt.kt | 6 +++ .../dd3boh/outertune/playback/DownloadUtil.kt | 49 ++++++++++++++++--- .../dd3boh/outertune/playback/MusicService.kt | 7 ++- .../com/dd3boh/outertune/ui/menu/AlbumMenu.kt | 2 +- .../dd3boh/outertune/ui/menu/PlayerMenu.kt | 10 ++-- .../dd3boh/outertune/ui/menu/PlaylistMenu.kt | 2 +- .../outertune/ui/menu/SelectionSongsMenu.kt | 6 ++- .../com/dd3boh/outertune/ui/menu/SongMenu.kt | 7 ++- .../outertune/ui/menu/YouTubeAlbumMenu.kt | 2 +- .../outertune/ui/menu/YouTubePlaylistMenu.kt | 2 +- .../outertune/ui/menu/YouTubeSongMenu.kt | 9 +++- .../outertune/ui/screens/AlbumScreen.kt | 2 +- .../ui/screens/playlist/AutoPlaylistScreen.kt | 2 +- .../screens/playlist/OnlinePlaylistScreen.kt | 2 +- .../ui/screens/settings/ContentSettings.kt | 16 ++++++ .../com/dd3boh/outertune/utils/SyncUtils.kt | 7 ++- app/src/main/res/values/strings.xml | 2 + 20 files changed, 117 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/com/dd3boh/outertune/MainActivity.kt b/app/src/main/java/com/dd3boh/outertune/MainActivity.kt index 1a80e64c2..1c6d1153a 100644 --- a/app/src/main/java/com/dd3boh/outertune/MainActivity.kt +++ b/app/src/main/java/com/dd3boh/outertune/MainActivity.kt @@ -361,7 +361,7 @@ class MainActivity : ComponentActivity() { val (lookupYtmArtists) = rememberPreference(LookupYtmArtistsKey, defaultValue = true) val (autoScan) = rememberPreference(AutomaticScannerKey, defaultValue = true) LaunchedEffect(Unit) { - downloadUtil.resumeDownloadsOnStart(this@MainActivity) + downloadUtil.resumeDownloadsOnStart() CoroutineScope(Dispatchers.IO).launch { // Check if the permissions for local media access diff --git a/app/src/main/java/com/dd3boh/outertune/constants/PreferenceKeys.kt b/app/src/main/java/com/dd3boh/outertune/constants/PreferenceKeys.kt index 9c98f6d2f..2cc927033 100644 --- a/app/src/main/java/com/dd3boh/outertune/constants/PreferenceKeys.kt +++ b/app/src/main/java/com/dd3boh/outertune/constants/PreferenceKeys.kt @@ -27,6 +27,7 @@ val SlimNavBarKey = booleanPreferencesKey("slimNavBar") */ const val SYSTEM_DEFAULT = "SYSTEM_DEFAULT" val YtmSyncKey = booleanPreferencesKey("ytmSync") +val LikedAutoDownloadKey = stringPreferencesKey("likedAutoDownloadKey") val ContentLanguageKey = stringPreferencesKey("contentLanguage") val ContentCountryKey = stringPreferencesKey("contentCountry") val ProxyEnabledKey = booleanPreferencesKey("proxyEnabled") @@ -202,6 +203,10 @@ enum class SearchSource { LOCAL, ONLINE } +enum class LikedAutodownloadMode { + OFF, ON, WIFI_ONLY +} + val VisitorDataKey = stringPreferencesKey("visitorData") val InnerTubeCookieKey = stringPreferencesKey("innerTubeCookie") val AccountNameKey = stringPreferencesKey("accountName") diff --git a/app/src/main/java/com/dd3boh/outertune/db/daos/SongsDao.kt b/app/src/main/java/com/dd3boh/outertune/db/daos/SongsDao.kt index c3750b912..9feaaa11f 100644 --- a/app/src/main/java/com/dd3boh/outertune/db/daos/SongsDao.kt +++ b/app/src/main/java/com/dd3boh/outertune/db/daos/SongsDao.kt @@ -69,6 +69,9 @@ interface SongsDao { @Query("SELECT count from playCount WHERE song = :songId AND year = :year AND month = :month") fun getPlayCountByMonth(songId: String?, year: Int, month: Int): Flow + @Query("SELECT * FROM song WHERE liked AND dateDownload IS NULL") + fun likedSongsNotDownloaded(): Flow> + // region Songs Sort @Query("SELECT * FROM song WHERE inLibrary IS NOT NULL ORDER BY rowId") fun songsByRowIdAsc(): Flow> diff --git a/app/src/main/java/com/dd3boh/outertune/extensions/ContextExt.kt b/app/src/main/java/com/dd3boh/outertune/extensions/ContextExt.kt index 74b1b6148..5114bf35d 100644 --- a/app/src/main/java/com/dd3boh/outertune/extensions/ContextExt.kt +++ b/app/src/main/java/com/dd3boh/outertune/extensions/ContextExt.kt @@ -3,6 +3,8 @@ package com.dd3boh.outertune.extensions import android.content.Context import com.dd3boh.outertune.constants.InnerTubeCookieKey import com.dd3boh.outertune.constants.YtmSyncKey +import com.dd3boh.outertune.constants.LikedAutoDownloadKey +import com.dd3boh.outertune.constants.LikedAutodownloadMode import com.dd3boh.outertune.utils.dataStore import com.dd3boh.outertune.utils.get import com.zionhuang.innertube.utils.parseCookieString @@ -14,4 +16,8 @@ fun Context.isSyncEnabled(): Boolean { val cookie = dataStore[InnerTubeCookieKey] ?: "" ytmSync && "SAPISID" in parseCookieString(cookie) } +} + +fun Context.getLikeAutoDownload(): LikedAutodownloadMode { + return dataStore[LikedAutoDownloadKey].toEnum(LikedAutodownloadMode.OFF) } \ No newline at end of file diff --git a/app/src/main/java/com/dd3boh/outertune/playback/DownloadUtil.kt b/app/src/main/java/com/dd3boh/outertune/playback/DownloadUtil.kt index 9f9e5f071..8b796f90c 100644 --- a/app/src/main/java/com/dd3boh/outertune/playback/DownloadUtil.kt +++ b/app/src/main/java/com/dd3boh/outertune/playback/DownloadUtil.kt @@ -2,6 +2,7 @@ package com.dd3boh.outertune.playback import android.content.Context import android.net.ConnectivityManager +import android.net.NetworkCapabilities import androidx.core.content.getSystemService import androidx.core.net.toUri import androidx.media3.common.PlaybackException @@ -16,8 +17,10 @@ import androidx.media3.exoplayer.offline.DownloadRequest import androidx.media3.exoplayer.offline.DownloadService import com.dd3boh.outertune.constants.AudioQuality import com.dd3boh.outertune.constants.AudioQualityKey +import com.dd3boh.outertune.constants.LikedAutodownloadMode import com.dd3boh.outertune.db.MusicDatabase import com.dd3boh.outertune.db.entities.FormatEntity +import com.dd3boh.outertune.db.entities.SongEntity import com.dd3boh.outertune.di.DownloadCache import com.dd3boh.outertune.models.MediaMetadata import com.dd3boh.outertune.utils.enumPreference @@ -38,10 +41,11 @@ import java.time.ZoneOffset import java.util.concurrent.Executor import javax.inject.Inject import javax.inject.Singleton +import com.dd3boh.outertune.extensions.getLikeAutoDownload @Singleton class DownloadUtil @Inject constructor( - @ApplicationContext context: Context, + @ApplicationContext private val context: Context, val database: MusicDatabase, val databaseProvider: DatabaseProvider, @DownloadCache val downloadCache: SimpleCache, @@ -123,17 +127,25 @@ class DownloadUtil @Inject constructor( } val downloads = MutableStateFlow>(emptyMap()) + fun getDownload(songId: String): Flow = downloads.map { it[songId] } + fun download(songs: List) { + songs.forEach { song -> downloadSong(song.id, song.title) } + } + + fun download(song: MediaMetadata){ + downloadSong(song.id, song.title) + } - fun download(songs: List, context: Context) { - songs.forEach { song -> download(song, context) } + fun download(song: SongEntity){ + downloadSong(song.id, song.title) } - fun download(song: MediaMetadata, context: Context){ - val downloadRequest = DownloadRequest.Builder(song.id, song.id.toUri()) - .setCustomCacheKey(song.id) - .setData(song.title.toByteArray()) + private fun downloadSong(id: String, title: String){ + val downloadRequest = DownloadRequest.Builder(id, id.toUri()) + .setCustomCacheKey(id) + .setData(title.toByteArray()) .build() DownloadService.sendAddDownload( context, @@ -142,13 +154,34 @@ class DownloadUtil @Inject constructor( false) } - fun resumeDownloadsOnStart(context: Context){ + fun resumeDownloadsOnStart(){ DownloadService.sendResumeDownloads( context, ExoDownloadService::class.java, false) } + fun autoDownloadIfLiked(songs: List){ + songs.forEach { song -> autoDownloadIfLiked(song) } + } + + fun autoDownloadIfLiked(song: SongEntity){ + if (!song.liked || song.dateDownload != null){ + return + } + + val isWifiConnected = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork) + ?.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ?: false + + if ( + context.getLikeAutoDownload() == LikedAutodownloadMode.ON + || (context.getLikeAutoDownload() == LikedAutodownloadMode.WIFI_ONLY && isWifiConnected) + ) + { + download(song) + } + } + init { val result = mutableMapOf() val cursor = downloadManager.downloadIndex.getDownloads() diff --git a/app/src/main/java/com/dd3boh/outertune/playback/MusicService.kt b/app/src/main/java/com/dd3boh/outertune/playback/MusicService.kt index fc9428a39..e76137128 100644 --- a/app/src/main/java/com/dd3boh/outertune/playback/MusicService.kt +++ b/app/src/main/java/com/dd3boh/outertune/playback/MusicService.kt @@ -139,6 +139,9 @@ class MusicService : MediaLibraryService(), @Inject lateinit var database: MusicDatabase + @Inject + lateinit var downloadUtil: DownloadUtil + @Inject lateinit var lyricsHelper: LyricsHelper @@ -574,7 +577,9 @@ class MusicService : MediaLibraryService(), fun toggleLike() { database.query { currentSong.value?.let { - update(it.song.toggleLike()) + val song = it.song.toggleLike() + update(song) + downloadUtil.autoDownloadIfLiked(song) } } } diff --git a/app/src/main/java/com/dd3boh/outertune/ui/menu/AlbumMenu.kt b/app/src/main/java/com/dd3boh/outertune/ui/menu/AlbumMenu.kt index d239499f7..c2baf6c3c 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/menu/AlbumMenu.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/menu/AlbumMenu.kt @@ -306,7 +306,7 @@ fun AlbumMenu( val _songs = songs .filterNot { it.song.isLocal } .map{ it.toMediaMetadata() } - downloadUtil.download(_songs, context) + downloadUtil.download(_songs) }, onRemoveDownload = { songs.forEach { song -> diff --git a/app/src/main/java/com/dd3boh/outertune/ui/menu/PlayerMenu.kt b/app/src/main/java/com/dd3boh/outertune/ui/menu/PlayerMenu.kt index b4b2cd0bd..ea9acc055 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/menu/PlayerMenu.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/menu/PlayerMenu.kt @@ -3,8 +3,6 @@ package com.dd3boh.outertune.ui.menu import android.content.Intent import android.text.format.Formatter import android.widget.Toast -import androidx.activity.compose.rememberLauncherForActivityResult -import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.foundation.clickable import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.layout.Arrangement @@ -117,7 +115,6 @@ fun PlayerMenu( val playerVolume = playerConnection.service.playerVolume.collectAsState() val currentFormat by playerConnection.currentFormat.collectAsState(initial = null) val currentPlayCount by playerConnection.currentPlayCount.collectAsState(initial = null) - val activityResultLauncher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { } val librarySong by database.song(mediaMetadata.id).collectAsState(initial = null) val coroutineScope = rememberCoroutineScope() @@ -127,6 +124,12 @@ fun PlayerMenu( mutableStateOf(false) } + LaunchedEffect(librarySong?.song?.liked) { + librarySong?.let { + downloadUtil.autoDownloadIfLiked(it.song) + } + } + AddToQueueDialog( isVisible = showChooseQueueDialog, onAdd = { queueName -> @@ -411,7 +414,6 @@ fun PlayerMenu( database.transaction { insert(mediaMetadata) } - downloadUtil.download(mediaMetadata, context) }, onRemoveDownload = { DownloadService.sendRemoveDownload( diff --git a/app/src/main/java/com/dd3boh/outertune/ui/menu/PlaylistMenu.kt b/app/src/main/java/com/dd3boh/outertune/ui/menu/PlaylistMenu.kt index c573734e8..472d41eb0 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/menu/PlaylistMenu.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/menu/PlaylistMenu.kt @@ -354,7 +354,7 @@ fun PlaylistMenu( state = downloadState, onDownload = { val _songs = songs.filterNot { it.song.isLocal }.map{ it.toMediaMetadata() } - downloadUtil.download(_songs, context) + downloadUtil.download(_songs) }, onRemoveDownload = { showRemoveDownloadDialog = true diff --git a/app/src/main/java/com/dd3boh/outertune/ui/menu/SelectionSongsMenu.kt b/app/src/main/java/com/dd3boh/outertune/ui/menu/SelectionSongsMenu.kt index 7e4517b45..5f5bccd8b 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/menu/SelectionSongsMenu.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/menu/SelectionSongsMenu.kt @@ -254,7 +254,9 @@ fun SelectionMediaMetadataMenu( } } else { selection.forEach { song -> - update(song.toSongEntity().copy(liked = true)) + val likedSong = song.toSongEntity().copy(liked = true) + update(likedSong) + downloadUtil.autoDownloadIfLiked(likedSong) } } } @@ -264,7 +266,7 @@ fun SelectionMediaMetadataMenu( state = downloadState, onDownload = { val songs = selection.filterNot { it.isLocal } - downloadUtil.download(songs, context) + downloadUtil.download(songs) }, onRemoveDownload = { showRemoveDownloadDialog = true diff --git a/app/src/main/java/com/dd3boh/outertune/ui/menu/SongMenu.kt b/app/src/main/java/com/dd3boh/outertune/ui/menu/SongMenu.kt index 4e4c01cf1..30b776de2 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/menu/SongMenu.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/menu/SongMenu.kt @@ -32,6 +32,7 @@ import androidx.compose.material3.LocalContentColor import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -100,6 +101,10 @@ fun SongMenu( mutableStateOf(false) } + LaunchedEffect(song.song.liked) { + downloadUtil.autoDownloadIfLiked(song.song) + } + if (showEditDialog) { TextFieldDialog( icon = { Icon(imageVector = Icons.Rounded.Edit, contentDescription = null) }, @@ -289,7 +294,7 @@ fun SongMenu( DownloadGridMenu( state = download?.state, onDownload = { - downloadUtil.download(song.toMediaMetadata(), context) + downloadUtil.download(song.toMediaMetadata()) }, onRemoveDownload = { DownloadService.sendRemoveDownload( diff --git a/app/src/main/java/com/dd3boh/outertune/ui/menu/YouTubeAlbumMenu.kt b/app/src/main/java/com/dd3boh/outertune/ui/menu/YouTubeAlbumMenu.kt index 417fba12d..3c87bb535 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/menu/YouTubeAlbumMenu.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/menu/YouTubeAlbumMenu.kt @@ -261,7 +261,7 @@ fun YouTubeAlbumMenu( state = downloadState, onDownload = { val _songs = album?.songs?.map{ it.toMediaMetadata() } ?: emptyList() - downloadUtil.download(_songs, context) + downloadUtil.download(_songs) }, onRemoveDownload = { album?.songs?.forEach { song -> diff --git a/app/src/main/java/com/dd3boh/outertune/ui/menu/YouTubePlaylistMenu.kt b/app/src/main/java/com/dd3boh/outertune/ui/menu/YouTubePlaylistMenu.kt index a48d23a7c..583fb4109 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/menu/YouTubePlaylistMenu.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/menu/YouTubePlaylistMenu.kt @@ -335,7 +335,7 @@ fun YouTubePlaylistMenu( state = downloadState, onDownload = { val _songs = songs.map{ it.toMediaMetadata() } - downloadUtil.download(_songs, context) + downloadUtil.download(_songs) }, onRemoveDownload = { showRemoveDownloadDialog = true diff --git a/app/src/main/java/com/dd3boh/outertune/ui/menu/YouTubeSongMenu.kt b/app/src/main/java/com/dd3boh/outertune/ui/menu/YouTubeSongMenu.kt index d44178cf9..c539ae919 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/menu/YouTubeSongMenu.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/menu/YouTubeSongMenu.kt @@ -28,6 +28,7 @@ import androidx.compose.material3.LocalContentColor import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -99,6 +100,12 @@ fun YouTubeSongMenu( mutableStateOf(false) } + LaunchedEffect(librarySong?.song?.liked) { + librarySong?.let { + downloadUtil.autoDownloadIfLiked(it.song) + } + } + AddToQueueDialog( isVisible = showChooseQueueDialog, onAdd = { queueName -> @@ -258,7 +265,7 @@ fun YouTubeSongMenu( database.transaction { insert(song.toMediaMetadata()) } - downloadUtil.download(song.toMediaMetadata(), context) + downloadUtil.download(song.toMediaMetadata()) }, onRemoveDownload = { DownloadService.sendRemoveDownload( diff --git a/app/src/main/java/com/dd3boh/outertune/ui/screens/AlbumScreen.kt b/app/src/main/java/com/dd3boh/outertune/ui/screens/AlbumScreen.kt index f01151198..bb80b1c2c 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/screens/AlbumScreen.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/screens/AlbumScreen.kt @@ -293,7 +293,7 @@ fun AlbumScreen( IconButton( onClick = { val songs = albumWithSongsLocal.songs.map{ it.toMediaMetadata() } - downloadUtil.download(songs, context) + downloadUtil.download(songs) } ) { Icon( diff --git a/app/src/main/java/com/dd3boh/outertune/ui/screens/playlist/AutoPlaylistScreen.kt b/app/src/main/java/com/dd3boh/outertune/ui/screens/playlist/AutoPlaylistScreen.kt index f2a8206eb..d75184ace 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/screens/playlist/AutoPlaylistScreen.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/screens/playlist/AutoPlaylistScreen.kt @@ -382,7 +382,7 @@ fun AutoPlaylistScreen( IconButton( onClick = { val _songs = songs.map{ it.toMediaMetadata() } - downloadUtil.download(_songs, context) + downloadUtil.download(_songs) } ) { Icon( diff --git a/app/src/main/java/com/dd3boh/outertune/ui/screens/playlist/OnlinePlaylistScreen.kt b/app/src/main/java/com/dd3boh/outertune/ui/screens/playlist/OnlinePlaylistScreen.kt index 3fdd83e7f..a7a0f3872 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/screens/playlist/OnlinePlaylistScreen.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/screens/playlist/OnlinePlaylistScreen.kt @@ -387,7 +387,7 @@ fun OnlinePlaylistScreen( syncUtils.syncPlaylist(playlist.id, dbPlaylist!!.id) } val _songs = songs.map{ it.toMediaMetadata() } - downloadUtil.download(_songs, context) + downloadUtil.download(_songs) } ) { Icon( diff --git a/app/src/main/java/com/dd3boh/outertune/ui/screens/settings/ContentSettings.kt b/app/src/main/java/com/dd3boh/outertune/ui/screens/settings/ContentSettings.kt index 72b8c1208..df42405a8 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/screens/settings/ContentSettings.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/screens/settings/ContentSettings.kt @@ -6,6 +6,7 @@ import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.verticalScroll import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.rounded.ArrowBack +import androidx.compose.material.icons.rounded.Favorite import androidx.compose.material.icons.rounded.Language import androidx.compose.material.icons.rounded.LocationOn import androidx.compose.material.icons.rounded.Person @@ -31,6 +32,8 @@ import com.dd3boh.outertune.constants.ContentLanguageKey import com.dd3boh.outertune.constants.CountryCodeToName import com.dd3boh.outertune.constants.InnerTubeCookieKey import com.dd3boh.outertune.constants.LanguageCodeToName +import com.dd3boh.outertune.constants.LikedAutoDownloadKey +import com.dd3boh.outertune.constants.LikedAutodownloadMode import com.dd3boh.outertune.constants.ProxyEnabledKey import com.dd3boh.outertune.constants.ProxyTypeKey import com.dd3boh.outertune.constants.ProxyUrlKey @@ -62,6 +65,7 @@ fun ContentSettings( "SAPISID" in parseCookieString(innerTubeCookie) } val (ytmSync, onYtmSyncChange) = rememberPreference(YtmSyncKey, defaultValue = true) + val (likedAutoDownload, onLikedAutoDownload) = rememberEnumPreference(LikedAutoDownloadKey, LikedAutodownloadMode.OFF) val (contentLanguage, onContentLanguageChange) = rememberPreference(key = ContentLanguageKey, defaultValue = "system") val (contentCountry, onContentCountryChange) = rememberPreference(key = ContentCountryKey, defaultValue = "system") @@ -94,6 +98,18 @@ fun ContentSettings( onCheckedChange = onYtmSyncChange, isEnabled = isLoggedIn ) + ListPreference( + title = { Text(stringResource(R.string.like_autodownload)) }, + icon = { Icon(Icons.Rounded.Favorite, null) }, + values = listOf(LikedAutodownloadMode.OFF, LikedAutodownloadMode.ON, LikedAutodownloadMode.WIFI_ONLY), + selectedValue = likedAutoDownload, + valueText = { when (it){ + LikedAutodownloadMode.OFF -> stringResource(androidx.compose.ui.R.string.off) + LikedAutodownloadMode.ON -> stringResource(androidx.compose.ui.R.string.on) + LikedAutodownloadMode.WIFI_ONLY -> stringResource(R.string.wifi_only) + } }, + onValueSelected = onLikedAutoDownload + ) PreferenceGroupTitle( title = "LOCALIZATION" diff --git a/app/src/main/java/com/dd3boh/outertune/utils/SyncUtils.kt b/app/src/main/java/com/dd3boh/outertune/utils/SyncUtils.kt index cecbb5e8c..a3751aee3 100644 --- a/app/src/main/java/com/dd3boh/outertune/utils/SyncUtils.kt +++ b/app/src/main/java/com/dd3boh/outertune/utils/SyncUtils.kt @@ -6,6 +6,7 @@ import com.dd3boh.outertune.db.entities.PlaylistEntity import com.dd3boh.outertune.db.entities.PlaylistSongMap import com.dd3boh.outertune.db.entities.SongEntity import com.dd3boh.outertune.models.toMediaMetadata +import com.dd3boh.outertune.playback.DownloadUtil import com.zionhuang.innertube.YouTube import com.zionhuang.innertube.models.AlbumItem import com.zionhuang.innertube.models.ArtistItem @@ -34,7 +35,8 @@ import javax.inject.Singleton */ @Singleton class SyncUtils @Inject constructor( - val database: MusicDatabase + val database: MusicDatabase, + private val downloadUtil: DownloadUtil ) { private val _isSyncingRemoteLikedSongs = MutableStateFlow(false) private val _isSyncingRemoteSongs = MutableStateFlow(false) @@ -106,6 +108,9 @@ class SyncUtils @Inject constructor( } } } + + val songs = database.likedSongsNotDownloaded().first().map { it.song } + downloadUtil.autoDownloadIfLiked(songs) } finally { Timber.tag(_TAG).d("Liked songs synchronization ended") _isSyncingRemoteLikedSongs.value = false diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c06d8b0bc..a1bd091ad 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -352,6 +352,8 @@ Automatically sync with your YouTube Music account + Automatically download your liked songs + Wifi only Prevent system killing app