Skip to content

Commit

Permalink
完善下载模块
Browse files Browse the repository at this point in the history
  • Loading branch information
10miaomiao committed Jul 22, 2023
1 parent 87dd1e5 commit 9099a4a
Show file tree
Hide file tree
Showing 14 changed files with 243 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
Expand Down Expand Up @@ -92,6 +93,15 @@ class DownloadVideoCreateFragment : Fragment(), DIAware, MyPage {
viewModel.selectedItem(item)
}

val handleItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
viewModel.selectedQuality(viewModel.acceptQuality[position])
}
override fun onNothingSelected(parent: AdapterView<*>?) {

}
}

val itemUi = miaoBindingItemUi<DownloadVideoCreateParam.Page> { item, index ->
frameLayout {
setBackgroundResource(R.drawable.shape_corner)
Expand Down Expand Up @@ -167,10 +177,10 @@ class DownloadVideoCreateFragment : Fragment(), DIAware, MyPage {
mAdapter.clear()
mAdapter.addAll(viewModel.acceptDescription)
}
// miaoEffect(viewModel.spinnerSelected) {
// setSelection(it)
// }
// onItemChanged(viewModel::changedSpinnerItem)
miaoEffect(viewModel.qualityIndex) {
setSelection(it)
}
onItemSelectedListener = handleItemSelectedListener
}..lParams(width = wrapContent)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class DownloadVideoCreateViewModel(
var acceptDescription = listOf<String>()
var acceptQuality = listOf<Int>()
var quality = 64
val qualityIndex get() = acceptQuality.indexOf(quality)
val qualityDescription get() = acceptDescription[qualityIndex] ?: "未知"
val selectedList = mutableListOf<String>()
var downloadedList = mutableListOf<BiliDownloadEntryInfo>()

Expand Down Expand Up @@ -76,6 +78,12 @@ class DownloadVideoCreateViewModel(
}
}

fun selectedQuality(value: Int) {
ui.setState {
quality = value
}
}

fun startDownload () {
if (selectedList.size == 0) {
context.toast("请选择分P")
Expand All @@ -91,7 +99,7 @@ class DownloadVideoCreateViewModel(
selectedList.clear()
}

fun selectedItem (item: DownloadVideoCreateParam.Page) {
fun selectedItem(item: DownloadVideoCreateParam.Page) {
val index = selectedList.indexOf(item.cid)
ui.setState {
if (index == -1) {
Expand Down Expand Up @@ -128,6 +136,7 @@ class DownloadVideoCreateViewModel(
type_tag = quality.toString(),
cover = video.pic,
prefered_video_quality = quality,
quality_pithy_description = qualityDescription,
guessed_total_bytes = 0,
total_time_milli = 0,
danmaku_count = 1000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,13 @@ class DownloadDetailPageViewModel(
downloadService?.startDownload(item.dir_path)
}

fun pauseClick(item: DownloadItemInfo) {
downloadService?.cancelDownload()
fun pauseClick(item: DownloadItemInfo, taskId: Long) {
downloadService?.cancelDownload(taskId)
}

fun deleteDownload(
dirPath: String,
item: DownloadItemInfo,
dirPath: String,
) {
val info = downloadInfo?.value ?: return
viewModelScope.launch {
Expand Down Expand Up @@ -237,21 +237,21 @@ fun DownloadDetailPage(
items(
downloadItems,
key = { it.cid },
) {
) { item ->
DownloadDetailItem(
curDownload = curDownload,
item = it,
item = item,
onClick = {
viewModel.itemClick(it)
viewModel.itemClick(item)
},
onStartClick = {
viewModel.startClick(it)
viewModel.startClick(item)
},
onPauseClick = {
viewModel.pauseClick(it)
viewModel.pauseClick(item, it)
},
onDeleteClick = {
viewModel.deleteDownload(dirPath, it)
viewModel.deleteDownload(item, dirPath)
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fun DownloadDetailItem(
item: DownloadItemInfo,
onClick: () -> Unit,
onStartClick: () -> Unit,
onPauseClick: () -> Unit,
onPauseClick: (taskId: Long) -> Unit,
onDeleteClick: () -> Unit,
) {
var expandedMoreMenu by remember { mutableStateOf(false) }
Expand All @@ -48,7 +48,7 @@ fun DownloadDetailItem(
verticalAlignment = Alignment.CenterVertically,
) {
GlideImage(
imageModel = UrlUtil.autoHttps(item.cover),
imageModel = UrlUtil.autoHttps(item.cover) + "@672w_378h_1c_",
modifier = Modifier
.size(width = 60.dp, height = 40.dp)
.clip(RoundedCornerShape(5.dp))
Expand Down Expand Up @@ -79,7 +79,7 @@ fun DownloadDetailItem(
}
if (!item.is_completed) {
if (item.cid == curDownload?.id && curDownload.status in 100 until 200) {
IconButton(onClick = onPauseClick) {
IconButton(onClick = { onPauseClick(curDownload.taskId) }) {
Icon(Icons.Filled.Pause, null)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ fun DownloadListItem(
item: DownloadInfo,
onClick: () -> Unit
) {
val nav = localNavController()
Box(
modifier = Modifier.padding(5.dp),
) {
Expand All @@ -46,7 +45,7 @@ fun DownloadListItem(
verticalAlignment = Alignment.CenterVertically,
) {
GlideImage(
imageModel = UrlUtil.autoHttps(item.cover),
imageModel = UrlUtil.autoHttps(item.cover) + "@672w_378h_1c_",
modifier = Modifier
.size(width = 120.dp, height = 80.dp)
.clip(RoundedCornerShape(5.dp))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ fun UserFollowPage(
verticalAlignment = Alignment.CenterVertically,
) {
GlideImage(
imageModel = UrlUtil.autoHttps(item.face),
imageModel = UrlUtil.autoHttps(item.face) + "@200w_200h",
modifier = Modifier
.size(50.dp)
.clip(CircleShape)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package cn.a10miaomiao.bilimiao.download
import bilibili.app.view.v1.ViewOuterClass.DM
import cn.a10miaomiao.bilimiao.download.entry.CurrentDownloadInfo
import com.a10miaomiao.bilimiao.comm.utils.DebugMiao
import com.a10miaomiao.bilimiao.comm.utils.UrlUtil
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import okhttp3.Call
import okhttp3.OkHttpClient
import okhttp3.Request
Expand All @@ -26,20 +28,22 @@ class DownloadManager(
.writeTimeout(120, TimeUnit.SECONDS)
.build()

suspend fun start(file: File, downloadedLength: Long = 0) {
create(downloadInfo, file, downloadedLength).run {
throttleFirst(200)
}.catch { e ->
callback.onTaskError(
downloadInfo.copy(status = CurrentDownloadInfo.STATUS_FAIL_DOWNLOAD),
e,
)
}.onCompletion {
callback.onTaskComplete(
downloadInfo.copy(status = CurrentDownloadInfo.STATUS_COMPLETED),
)
}.collect {
callback.onTaskRunning(it)
fun start(file: File, downloadedLength: Long = 0) {
scope.launch {
create(downloadInfo, file, downloadedLength).run {
throttleFirst(200)
}.catch { e ->
downloadInfo.status = CurrentDownloadInfo.STATUS_FAIL_DOWNLOAD
callback.onTaskError(downloadInfo, e)
}.onCompletion {
if (downloadInfo.status == CurrentDownloadInfo.STATUS_COMPLETED) {
callback.onTaskComplete(downloadInfo)
}
}.collect {
if (it.status == CurrentDownloadInfo.STATUS_DOWNLOADING) {
callback.onTaskRunning(it)
}
}
}
}

Expand Down Expand Up @@ -69,10 +73,12 @@ class DownloadManager(
}
var downloadLength = info.progress //已经下载好的长度
val request = Request.Builder()
.url(info.url.replace("http://", "https://"))
DebugMiao.log("addHeader", "RANGE", "bytes=$downloadLength-${info.size}")
.url(UrlUtil.autoHttps(info.url))
if (downloadLength > 0 && info.size != 0L) {
DebugMiao.log("addHeader2", "RANGE", "bytes=$downloadLength-${info.size}")
if (info.size == downloadLength) {
downloadInfo.status = CurrentDownloadInfo.STATUS_COMPLETED
return@flow
}
request.addHeader("RANGE", "bytes=$downloadLength-${info.size}")
}
downloadLength += downloadedLength
Expand Down Expand Up @@ -104,6 +110,8 @@ class DownloadManager(
}
if (downloadInfo.status == CurrentDownloadInfo.STATUS_PAUSE) {
call.cancel()
} else {
downloadInfo.status = CurrentDownloadInfo.STATUS_COMPLETED
}
fileOutputStream.flush()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import cn.a10miaomiao.bilimiao.download.entry.CurrentDownloadInfo
import okhttp3.internal.notify

class DownloadNotify(val context: Context) {
val ACTION_CMD = "cn.a10miaomiao.bilimiao.download.DownloadNotify"
val notificationID = 1010
val notificationID = 10000
val channelId = "cn.a10miaomiao.bilimiao.download.DownloadNotify.control"
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val builder = NotificationBuilder(context, channelId)
Expand All @@ -24,8 +25,7 @@ class DownloadNotify(val context: Context) {

fun notifyData(info: CurrentDownloadInfo) {
// val builder = NotificationCompat.Builder(context, channelId)
if (builder.info?.id == info.id
&& builder.info?.parentId == info.parentId) {
if (builder.taskId == info.taskId) {
builder.setContentText(info.statusText)
builder.setProgress(info.size.toInt(), info.progress.toInt(), false)
} else {
Expand All @@ -36,12 +36,34 @@ class DownloadNotify(val context: Context) {
builder.priority = NotificationCompat.PRIORITY_DEFAULT
builder.setOnlyAlertOnce(true)
builder.setOngoing(true)
builder.info = info
builder.taskId = info.taskId
}
val notification = builder.build()
manager.notify(notificationID, notification)
}

fun showCompletedStatusNotify(info: CurrentDownloadInfo) {
manager.notify(
notificationID + info.taskId.toInt(),
NotificationCompat.Builder(context, channelId).apply {
setContentTitle(info.name)
setContentText("下载完成")
setSmallIcon(R.drawable.ic_baseline_file_download_done_24)
}.build()
)
}

fun showErrorStatusNotify(info: CurrentDownloadInfo) {
manager.notify(
notificationID + info.taskId.toInt(),
NotificationCompat.Builder(context, channelId).apply {
setContentTitle(info.name)
setContentText("下载出错")
setSmallIcon(R.drawable.ic_baseline_error_24)
}.build()
)
}

fun cancel() {
manager.cancel(notificationID)
}
Expand All @@ -50,7 +72,7 @@ class DownloadNotify(val context: Context) {
context: Context,
channelId: String,
) : NotificationCompat.Builder(context, channelId) {
var info: CurrentDownloadInfo? = null
var taskId = 0L
}

}
Loading

0 comments on commit 9099a4a

Please sign in to comment.