Skip to content

Commit

Permalink
Merge pull request #6596 from Bnyro/master
Browse files Browse the repository at this point in the history
feat: downloads fragment rework, playing queue and audio player support
  • Loading branch information
Bnyro authored Oct 6, 2024
2 parents ad1be01 + 5633011 commit 4113279
Show file tree
Hide file tree
Showing 22 changed files with 806 additions and 723 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ object IntentData {
const val videoList = "videoList"
const val nextPage = "nextPage"
const val videoInfo = "videoInfo"
const val offlinePlayer = "offlinePlayer"
const val downloadTab = "downloadTab"
}
13 changes: 12 additions & 1 deletion app/src/main/java/com/github/libretube/db/obj/Download.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.libretube.db.obj
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.github.libretube.api.obj.StreamItem
import kotlinx.datetime.LocalDate
import java.nio.file.Path

Expand All @@ -17,4 +18,14 @@ data class Download(
val duration: Long? = null,
val uploadDate: LocalDate? = null,
val thumbnailPath: Path? = null
)
) {
fun toStreamItem() = StreamItem(
url = videoId,
title = title,
shortDescription = description,
thumbnail = thumbnailPath?.toUri()?.toString(),
duration = duration,
uploadedDate = uploadDate?.toString(),
uploaderName = uploader,
)
}
14 changes: 14 additions & 0 deletions app/src/main/java/com/github/libretube/db/obj/DownloadWithItems.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.github.libretube.db.obj

import androidx.room.Embedded
import androidx.room.Relation
import com.github.libretube.enums.FileType
import com.github.libretube.ui.fragments.DownloadTab

data class DownloadWithItems(
@Embedded val download: Download,
Expand All @@ -16,3 +18,15 @@ data class DownloadWithItems(
)
val downloadChapters: List<DownloadChapter> = emptyList()
)

fun List<DownloadWithItems>.filterByTab(tab: DownloadTab) = filter { dl ->
when (tab) {
DownloadTab.AUDIO -> {
dl.downloadItems.any { it.type == FileType.AUDIO } && dl.downloadItems.none { it.type == FileType.VIDEO }
}

DownloadTab.VIDEO -> {
dl.downloadItems.any { it.type == FileType.VIDEO }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.parcelable.PlayerData
import com.github.libretube.services.OfflinePlayerService
import com.github.libretube.services.OnlinePlayerService
import com.github.libretube.ui.fragments.DownloadTab
import com.github.libretube.ui.fragments.PlayerFragment

/**
Expand Down Expand Up @@ -50,13 +51,9 @@ object BackgroundHelper {
/**
* Stop the [OnlinePlayerService] service if it is running.
*/
fun stopBackgroundPlay(
context: Context,
serviceClass: Class<*> = OnlinePlayerService::class.java
) {
if (isBackgroundServiceRunning(context, serviceClass)) {
// Intent to stop background mode service
val intent = Intent(context, serviceClass)
fun stopBackgroundPlay(context: Context) {
arrayOf(OnlinePlayerService::class.java, OfflinePlayerService::class.java).forEach {
val intent = Intent(context, it)
context.stopService(intent)
}
}
Expand All @@ -79,11 +76,13 @@ object BackgroundHelper {
* @param context the current context
* @param videoId the videoId of the video or null if all available downloads should be shuffled
*/
fun playOnBackgroundOffline(context: Context, videoId: String?) {
fun playOnBackgroundOffline(context: Context, videoId: String?, downloadTab: DownloadTab) {
stopBackgroundPlay(context)

val playerIntent = Intent(context, OfflinePlayerService::class.java)
.putExtra(IntentData.videoId, videoId)
.putExtra(IntentData.downloadTab, downloadTab)

context.stopService(playerIntent)
ContextCompat.startForegroundService(context, playerIntent)
}
}
21 changes: 10 additions & 11 deletions app/src/main/java/com/github/libretube/helpers/ImageHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package com.github.libretube.helpers

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.net.Uri
import android.widget.ImageView
import androidx.core.graphics.drawable.toBitmap
import androidx.core.graphics.drawable.toBitmapOrNull
import coil.ImageLoader
import coil.disk.DiskCache
Expand All @@ -15,7 +14,6 @@ import com.github.libretube.BuildConfig
import com.github.libretube.api.CronetHelper
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.extensions.toAndroidUri
import com.github.libretube.extensions.toAndroidUriOrNull
import com.github.libretube.util.DataSaverMode
import com.google.net.cronet.okhttptransport.CronetInterceptor
import kotlinx.coroutines.Dispatchers
Expand All @@ -26,7 +24,7 @@ import java.io.File
import java.nio.file.Path

object ImageHelper {
lateinit var imageLoader: ImageLoader
private lateinit var imageLoader: ImageLoader

private val Context.coilFile get() = cacheDir.resolve("coil")

Expand Down Expand Up @@ -119,14 +117,15 @@ object ImageHelper {
return imageLoader.execute(request).drawable?.toBitmapOrNull()
}

fun getDownloadedImage(context: Context, path: Path): Bitmap? {
return path.toAndroidUriOrNull()?.let { getImage(context, it) }
}
fun getImageWithCallback(context: Context, url: String?, onBitmap: (Bitmap) -> Unit) {
val request = ImageRequest.Builder(context)
.data(url)
.target { drawable ->
onBitmap(drawable.toBitmap())
}
.build()

private fun getImage(context: Context, imagePath: Uri): Bitmap? {
return context.contentResolver.openInputStream(imagePath)?.use {
BitmapFactory.decodeStream(it)
}
imageLoader.enqueue(request)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,13 @@ object NavigationHelper {
/**
* Start the audio player fragment
*/
fun startAudioPlayer(context: Context, minimizeByDefault: Boolean = false) {
fun startAudioPlayer(context: Context, offlinePlayer: Boolean = false, minimizeByDefault: Boolean = false) {
val activity = ContextHelper.unwrapActivity(context)
activity.supportFragmentManager.commitNow {
val args = bundleOf(IntentData.minimizeByDefault to minimizeByDefault)
val args = bundleOf(
IntentData.minimizeByDefault to minimizeByDefault,
IntentData.offlinePlayer to offlinePlayer
)
replace<AudioPlayerFragment>(R.id.container, args = args)
}
}
Expand Down
Loading

0 comments on commit 4113279

Please sign in to comment.