Skip to content

Commit

Permalink
download deleting status
Browse files Browse the repository at this point in the history
(cherry picked from commit 35ea7c2)
  • Loading branch information
crackededed committed Mar 13, 2024
1 parent ae2e952 commit ba53aa0
Show file tree
Hide file tree
Showing 17 changed files with 75 additions and 53 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ android {
applicationId = "com.github.andreyasadchy.xtra"
minSdk = 21
targetSdk = 34
versionCode = 229
versionName = "2.30.2"
versionCode = 230
versionName = "2.30.3"
resourceConfigurations += listOf("ar", "de", "en", "es", "fr", "in", "ja", "pt-rBR", "ru", "tr", "zh-rTW", "zh-rCN")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ data class OfflineVideo(
const val STATUS_DOWNLOADING = 1
const val STATUS_DOWNLOADED = 2
const val STATUS_MOVING = 3
const val STATUS_DELETING = 4
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class DownloadsAdapter(
options.setOnClickListener { it ->
PopupMenu(context, it).apply {
inflate(R.menu.offline_item)
if (item.status == OfflineVideo.STATUS_DOWNLOADED || item.status == OfflineVideo.STATUS_MOVING) {
if (item.status == OfflineVideo.STATUS_DOWNLOADED || item.status == OfflineVideo.STATUS_MOVING || item.status == OfflineVideo.STATUS_DELETING) {
menu.findItem(R.id.moveVideo).apply {
isVisible = true
title = context.getString(if (item.url.toUri().scheme == ContentResolver.SCHEME_CONTENT) {
Expand Down Expand Up @@ -221,6 +221,7 @@ class DownloadsAdapter(
text = when (item.status) {
OfflineVideo.STATUS_DOWNLOADING -> context.getString(R.string.downloading_progress, ((item.progress.toFloat() / item.maxProgress) * 100f).toInt())
OfflineVideo.STATUS_MOVING -> context.getString(R.string.download_moving)
OfflineVideo.STATUS_DELETING -> context.getString(R.string.download_deleting)
else -> context.getString(R.string.download_pending)
}
visible()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,66 +243,74 @@ class DownloadsViewModel @Inject internal constructor(
}

fun delete(context: Context, video: OfflineVideo, keepFiles: Boolean) {
repository.deleteVideo(context, video)
GlobalScope.launch {
val useWorkManager = Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE || context.prefs().getBoolean(C.DEBUG_WORKMANAGER_DOWNLOADS, false)
if (video.status == OfflineVideo.STATUS_DOWNLOADED || video.status == OfflineVideo.STATUS_MOVING || useWorkManager) {
if (useWorkManager) {
WorkManager.getInstance(context).cancelUniqueWork(video.id.toString())
}
if (!keepFiles) {
if (video.url.toUri().scheme == ContentResolver.SCHEME_CONTENT) {
if (video.vod) {
val directory = DocumentFile.fromTreeUri(context, video.url.substringBefore("/document/").toUri()) ?: return@launch
val videoDirectory = directory.findFile(video.url.substringBeforeLast("%2F").substringAfterLast("%2F").substringAfterLast("%3A")) ?: return@launch
val playlistFile = videoDirectory.findFile(video.url.substringAfterLast("%2F")) ?: return@launch
val playlists = videoDirectory.listFiles().filter { it.name?.endsWith(".m3u8") == true && it.uri != playlistFile.uri }
if (playlists.isEmpty()) {
videoDirectory.delete()
} else {
val playlist = context.contentResolver.openInputStream(video.url.toUri()).use {
PlaylistParser(it, Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT).parse()
}
val tracksToDelete = playlist.mediaPlaylist.tracks.toMutableSet()
playlists.forEach { file ->
val p = context.contentResolver.openInputStream(file.uri).use {
if (!movingVideos.contains(video)) {
movingVideos.add(video)
GlobalScope.launch {
repository.updateVideo(video.apply {
status = OfflineVideo.STATUS_DELETING
})
val useWorkManager = Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE || context.prefs().getBoolean(C.DEBUG_WORKMANAGER_DOWNLOADS, false)
if (video.status == OfflineVideo.STATUS_DOWNLOADED || video.status == OfflineVideo.STATUS_MOVING || video.status == OfflineVideo.STATUS_DELETING || useWorkManager) {
if (useWorkManager) {
WorkManager.getInstance(context).cancelUniqueWork(video.id.toString())
}
if (!keepFiles) {
if (video.url.toUri().scheme == ContentResolver.SCHEME_CONTENT) {
if (video.vod) {
val directory = DocumentFile.fromTreeUri(context, video.url.substringBefore("/document/").toUri()) ?: return@launch
val videoDirectory = directory.findFile(video.url.substringBeforeLast("%2F").substringAfterLast("%2F").substringAfterLast("%3A")) ?: return@launch
val playlistFile = videoDirectory.findFile(video.url.substringAfterLast("%2F")) ?: return@launch
val playlists = videoDirectory.listFiles().filter { it.name?.endsWith(".m3u8") == true && it.uri != playlistFile.uri }
if (playlists.isEmpty()) {
videoDirectory.delete()
} else {
val playlist = context.contentResolver.openInputStream(video.url.toUri()).use {
PlaylistParser(it, Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT).parse()
}
tracksToDelete.removeAll(p.mediaPlaylist.tracks.toSet())
val tracksToDelete = playlist.mediaPlaylist.tracks.toMutableSet()
playlists.forEach { file ->
val p = context.contentResolver.openInputStream(file.uri).use {
PlaylistParser(it, Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT).parse()
}
tracksToDelete.removeAll(p.mediaPlaylist.tracks.toSet())
}
tracksToDelete.forEach { videoDirectory.findFile(it.uri.substringAfterLast("%2F"))?.delete() }
playlistFile.delete()
}
playlistFile.delete()
tracksToDelete.forEach { videoDirectory.findFile(it.uri.substringAfterLast("%2F"))?.delete() }
} else {
DocumentFile.fromSingleUri(context, video.url.toUri())?.delete()
}
} else {
DocumentFile.fromSingleUri(context, video.url.toUri())?.delete()
}
} else {
val playlistFile = File(video.url)
if (!playlistFile.exists()) {
return@launch
}
if (video.vod) {
val directory = playlistFile.parentFile
val playlists = directory.listFiles(FileFilter { it.extension == "m3u8" && it != playlistFile })
if (playlists.isEmpty()) {
directory.deleteRecursively()
} else {
val playlist = PlaylistParser(playlistFile.inputStream(), Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT).parse()
val tracksToDelete = playlist.mediaPlaylist.tracks.toMutableSet()
playlists.forEach {
val p = PlaylistParser(it.inputStream(), Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT).parse()
tracksToDelete.removeAll(p.mediaPlaylist.tracks.toSet())
val playlistFile = File(video.url)
if (!playlistFile.exists()) {
return@launch
}
if (video.vod) {
val directory = playlistFile.parentFile
val playlists = directory.listFiles(FileFilter { it.extension == "m3u8" && it != playlistFile })
if (playlists.isEmpty()) {
directory.deleteRecursively()
} else {
val playlist = PlaylistParser(playlistFile.inputStream(), Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT).parse()
val tracksToDelete = playlist.mediaPlaylist.tracks.toMutableSet()
playlists.forEach {
val p = PlaylistParser(it.inputStream(), Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT).parse()
tracksToDelete.removeAll(p.mediaPlaylist.tracks.toSet())
}
tracksToDelete.forEach { File(it.uri).delete() }
playlistFile.delete()
}
} else {
playlistFile.delete()
tracksToDelete.forEach { File(it.uri).delete() }
}
} else {
playlistFile.delete()
}
}
} else {
fetchProvider.get(video.id).deleteGroup(video.id)
}
} else {
fetchProvider.get(video.id).deleteGroup(video.id)
}.invokeOnCompletion {
movingVideos.remove(video)
repository.deleteVideo(context, video)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -611,4 +611,5 @@
<string name="move_to_app_storage">Move to app storage</string>
<string name="download_moving">Moving</string>
<string name="theme_adapted_username_color">Readable colors based on the app theme</string>
<string name="download_deleting">Deleting</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -611,4 +611,5 @@
<string name="move_to_app_storage">Move to app storage</string>
<string name="download_moving">Moving</string>
<string name="theme_adapted_username_color">Readable colors based on the app theme</string>
<string name="download_deleting">Deleting</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -611,4 +611,5 @@
<string name="move_to_app_storage">Move to app storage</string>
<string name="download_moving">Moving</string>
<string name="theme_adapted_username_color">Readable colors based on the app theme</string>
<string name="download_deleting">Deleting</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -611,4 +611,5 @@
<string name="move_to_app_storage">Move to app storage</string>
<string name="download_moving">Moving</string>
<string name="theme_adapted_username_color">Readable colors based on the app theme</string>
<string name="download_deleting">Deleting</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-in/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -611,4 +611,5 @@
<string name="move_to_app_storage">Move to app storage</string>
<string name="download_moving">Moving</string>
<string name="theme_adapted_username_color">Readable colors based on the app theme</string>
<string name="download_deleting">Deleting</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -611,4 +611,5 @@
<string name="move_to_app_storage">Move to app storage</string>
<string name="download_moving">Moving</string>
<string name="theme_adapted_username_color">Readable colors based on the app theme</string>
<string name="download_deleting">Deleting</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-pt-rBR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -611,4 +611,5 @@
<string name="move_to_app_storage">Move to app storage</string>
<string name="download_moving">Moving</string>
<string name="theme_adapted_username_color">Readable colors based on the app theme</string>
<string name="download_deleting">Deleting</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -611,4 +611,5 @@
<string name="move_to_app_storage">Move to app storage</string>
<string name="download_moving">Moving</string>
<string name="theme_adapted_username_color">Адаптировать цвета в зависимости от темы</string>
<string name="download_deleting">Deleting</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-tr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -611,4 +611,5 @@
<string name="move_to_app_storage">Uygulama depolama alanına taşı</string>
<string name="download_moving">Taşınma</string>
<string name="theme_adapted_username_color">Readable colors based on the app theme</string>
<string name="download_deleting">Deleting</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -611,4 +611,5 @@
<string name="move_to_app_storage">Move to app storage</string>
<string name="download_moving">Moving</string>
<string name="theme_adapted_username_color">Readable colors based on the app theme</string>
<string name="download_deleting">Deleting</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -611,4 +611,5 @@
<string name="move_to_app_storage">Move to app storage</string>
<string name="download_moving">Moving</string>
<string name="theme_adapted_username_color">Readable colors based on the app theme</string>
<string name="download_deleting">Deleting</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -625,4 +625,5 @@
<string name="move_to_app_storage">Move to app storage</string>
<string name="download_moving">Moving</string>
<string name="theme_adapted_username_color">Readable colors based on the app theme</string>
<string name="download_deleting">Deleting</string>
</resources>
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ material = "1.11.0"
media3 = "1.3.0"
navigation = "2.7.7"
okhttp = "5.0.0-alpha.12"
okio = "3.8.0"
okio = "3.9.0"
open-m3u8 = "0.2.6"
paging = "3.2.1"
preference = "1.2.1"
Expand Down

0 comments on commit ba53aa0

Please sign in to comment.