Skip to content

Commit

Permalink
refactor(develop): 功能重构
Browse files Browse the repository at this point in the history
  • Loading branch information
SOCK-MAGIC committed Oct 15, 2023
1 parent f72c3db commit 6d808a3
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 65 deletions.
43 changes: 17 additions & 26 deletions app/src/main/java/com/imcys/bilibilias/base/utils/DialogUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import com.imcys.bilibilias.common.base.extend.toAsDownloadSavePath
import com.imcys.bilibilias.common.base.utils.AsVideoNumUtils
import com.imcys.bilibilias.common.base.utils.file.AppFilePathUtils
import com.imcys.bilibilias.common.base.utils.http.KtHttpUtils
import com.imcys.bilibilias.common.network.danmaku.VideoInfoV2
import com.imcys.bilibilias.databinding.*
import com.imcys.bilibilias.home.ui.activity.AsVideoActivity
import com.imcys.bilibilias.home.ui.activity.HomeActivity
Expand Down Expand Up @@ -684,14 +685,7 @@ object DialogUtils {
val binding = DialogDownloadDmBinding.inflate(LayoutInflater.from(context))
val bottomSheetDialog = BottomSheetDialog(context, R.style.BottomSheetDialog).apply {
setOnShowListener {
window?.apply {
// 设置动态高斯模糊效果
setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
setBackgroundBlurRadius(40)
} // 设置背景模糊程度
}
setDynamicGaussianBlurEffect()
}
}
// 设置布局
Expand All @@ -706,6 +700,19 @@ object DialogUtils {
return bottomSheetDialog
}

/**
* 设置动态高斯模糊效果
*/
fun BottomSheetDialog.setDynamicGaussianBlurEffect(blurRadius: Int = 40) {
window?.apply {
setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
setBackgroundBlurRadius(40)
}
}
}

/**
* 下载字幕文件
*/
Expand All @@ -716,16 +723,7 @@ object DialogUtils {
): BottomSheetDialog {
val binding = DialogDownloadCcAssBinding.inflate(LayoutInflater.from(context))
val bottomSheetDialog = BottomSheetDialog(context, R.style.BottomSheetDialog).apply {
setOnShowListener {
window?.apply {
// 设置动态高斯模糊效果
setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
setBackgroundBlurRadius(40)
} // 设置背景模糊程度
}
}
setOnShowListener { setDynamicGaussianBlurEffect() }
}

// 设置布局
Expand Down Expand Up @@ -789,14 +787,7 @@ object DialogUtils {

val bottomSheetDialog = BottomSheetDialog(context, R.style.BottomSheetDialog).apply {
setOnShowListener {
window?.apply {
// 设置动态高斯模糊效果
setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
setBackgroundBlurRadius(40)
} // 设置背景模糊程度
}
setDynamicGaussianBlurEffect()
}
}
// 设置布局
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import androidx.activity.viewModels
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import cn.jzvd.JZDataSource
Expand All @@ -36,12 +35,12 @@ import com.imcys.bilibilias.common.base.constant.COOKIES
import com.imcys.bilibilias.common.base.constant.REFERER
import com.imcys.bilibilias.common.base.constant.ROAM_API
import com.imcys.bilibilias.common.base.constant.USER_AGENT
import com.imcys.bilibilias.common.base.extend.launchIO
import com.imcys.bilibilias.common.base.extend.launchUI
import com.imcys.bilibilias.common.base.utils.VideoNumConversion
import com.imcys.bilibilias.common.base.utils.http.HttpUtils
import com.imcys.bilibilias.common.base.utils.http.KtHttpUtils
import com.imcys.bilibilias.common.base.view.JzbdStdInfo
import com.imcys.bilibilias.common.network.base.ResBean
import com.imcys.bilibilias.danmaku.BiliDanmukuParser
import com.imcys.bilibilias.databinding.ActivityAsVideoBinding
import com.imcys.bilibilias.home.ui.adapter.BangumiSubsectionAdapter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.imcys.bilibilias.home.ui.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import com.imcys.bilibilias.R
import com.imcys.bilibilias.common.network.danmaku.VideoInfoV2
import com.imcys.bilibilias.databinding.ItemCcAssLangBinding
import com.imcys.bilibilias.home.ui.model.VideoInfoV2

class CCAssAdapter(private val selectEvent: (index: Int) -> Unit) :
ListAdapter<VideoInfoV2.Subtitle.MSubtitle, ViewHolder>(object :
Expand All @@ -24,7 +23,7 @@ class CCAssAdapter(private val selectEvent: (index: Int) -> Unit) :
oldItem: VideoInfoV2.Subtitle.MSubtitle,
newItem: VideoInfoV2.Subtitle.MSubtitle,
): Boolean {
return oldItem.check == newItem.check
return oldItem == newItem
}
}) {

Expand All @@ -39,8 +38,9 @@ class CCAssAdapter(private val selectEvent: (index: Int) -> Unit) :
ViewHolder(root)
}

override fun onBindViewHolder(holder: ViewHolder, @SuppressLint("RecyclerView") position: Int) {
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
DataBindingUtil.getBinding<ItemCcAssLangBinding>(holder.itemView)?.apply {
val position = holder.bindingAdapterPosition
if (selectIndex == position) {
getItem(position).check = true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.imcys.bilibilias.home.ui.viewmodel

import com.imcys.bilibilias.common.network.danmaku.VideoInfoV2

data class AsVideoState(val videoInfoV2: VideoInfoV2 = VideoInfoV2())
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import com.imcys.bilibilias.common.base.constant.BILIBILI_URL
import com.imcys.bilibilias.common.base.constant.COOKIE
import com.imcys.bilibilias.common.base.constant.COOKIES
import com.imcys.bilibilias.common.base.constant.REFERER
import com.imcys.bilibilias.common.base.extend.Result
import com.imcys.bilibilias.common.base.extend.launchIO
import com.imcys.bilibilias.common.base.extend.launchUI
import com.imcys.bilibilias.common.base.utils.VideoNumConversion
import com.imcys.bilibilias.common.base.utils.file.FileUtils
import com.imcys.bilibilias.common.base.utils.http.HttpUtils
import com.imcys.bilibilias.common.base.utils.http.KtHttpUtils
import com.imcys.bilibilias.common.network.danmaku.DanmakuRepository
import com.imcys.bilibilias.danmaku.change.CCJsonToAss
import com.imcys.bilibilias.danmaku.change.DmXmlToAss
import com.imcys.bilibilias.home.ui.activity.AsVideoActivity
Expand All @@ -39,6 +41,9 @@ import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.get
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import okio.BufferedSink
import okio.buffer
Expand All @@ -52,7 +57,7 @@ import javax.inject.Inject
*/

@HiltViewModel
class AsVideoViewModel @Inject constructor() : ViewModel() {
class AsVideoViewModel @Inject constructor(private val danmakuRepository: DanmakuRepository) : ViewModel() {

@Inject
lateinit var http: HttpClient
Expand Down Expand Up @@ -97,8 +102,8 @@ class AsVideoViewModel @Inject constructor() : ViewModel() {
title = "止步于此"
content =
"鉴于你的账户未转正,请前往B站完成答题,否则无法为您提供缓存服务。\n" +
"作者也是B站UP主,见到了许多盗取视频现象,更有甚者缓存番剧后发布内容到其他平台。\n" +
"而你的账户甚至是没有转正的,bilibilias自然不会想提供服务。"
"作者也是B站UP主,见到了许多盗取视频现象,更有甚者缓存番剧后发布内容到其他平台。\n" +
"而你的账户甚至是没有转正的,bilibilias自然不会想提供服务。"
positiveButtonText = "知道了"
positiveButton = {
it.cancel()
Expand Down Expand Up @@ -151,8 +156,8 @@ class AsVideoViewModel @Inject constructor() : ViewModel() {
title = "止步于此"
content =
"鉴于你的账户未转正,请前往B站完成答题,否则无法为您提供缓存服务。\n" +
"作者也是B站UP主,见到了许多盗取视频现象,更有甚者缓存番剧后发布内容到其他平台。\n" +
"而你的账户甚至是没有转正的,bilibilias自然不会想提供服务。"
"作者也是B站UP主,见到了许多盗取视频现象,更有甚者缓存番剧后发布内容到其他平台。\n" +
"而你的账户甚至是没有转正的,bilibilias自然不会想提供服务。"
positiveButtonText = "知道了"
positiveButton = {
it.cancel()
Expand Down Expand Up @@ -191,22 +196,30 @@ class AsVideoViewModel @Inject constructor() : ViewModel() {
}.show()
}

private val _danmakuState = MutableStateFlow(AsVideoState())
val danmakuState = _danmakuState.asStateFlow()
fun downloadCCAss(view: View, avid: Long, cid: Long) {
val context = view.context
val dialogLoad = DialogUtils.loadDialog(context)
dialogLoad.show()
viewModelScope.launchIO {
val bvId = VideoNumConversion.toBvidOffline(avid)
val videoInfoV2 =
http
.get("${BilibiliApi.videoInfoV2}?aid=$avid&cid=$cid")
.body<ResBean<VideoInfoV2>>()

launchUI {
dialogLoad.cancel()
DialogUtils.downloadCCAssDialog(context, videoInfoV2.data) {
saveCCAss(bvId, cid, videoInfoV2.data.name, it.lan, it.subtitleUrl, context)
}.show()
danmakuRepository.getCideoInfoV2(avid, cid).collect { result ->
when (result) {
is Result.Error -> TODO()
Result.Loading -> {}
is Result.Success -> {
_danmakuState.update {
it.copy(videoInfoV2 = result.data.data)
}
launchUI {
dialogLoad.cancel()
DialogUtils.downloadCCAssDialog(context, result.data.data) {
saveCCAss(bvId, cid, result.data.data.name, it.lan, it.subtitleUrl, context)
}.show()
}
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/item_cc_ass_lang.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<variable
name="Subtitle"
type="com.imcys.bilibilias.home.ui.model.VideoInfoV2.Subtitle.MSubtitle" />
type="com.imcys.bilibilias.common.network.danmaku.VideoInfoV2" />

</data>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.imcys.bilibilias.common.base.extend

import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.HttpRequestBuilder
import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsText
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow

internal suspend inline fun <reified T> HttpClient.safeGet(
url: String,
crossinline block: HttpRequestBuilder.() -> Unit = {},
): Flow<Result<T>> =
httpBlock { get(url, block).body() }

internal suspend fun HttpClient.safeGetText(
url: String,
block: HttpRequestBuilder.() -> Unit = {},
): Flow<Result<String>> = httpBlock { get(url, block).bodyAsText() }

private fun <T> httpBlock(block: suspend () -> T): Flow<Result<T>> =
flow { emit(block()) }.asResult()
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.imcys.bilibilias.common.base.extend

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart

sealed interface Result<out T> {
data class Success<T>(val data: T) : Result<T>
data class Error(val exception: Throwable? = null) : Result<Nothing>
data object Loading : Result<Nothing>
}

fun <T> Flow<T>.asResult(): Flow<Result<T>> {
return this
.map<T, Result<T>> {
Result.Success(it)
}
.onStart { emit(Result.Loading) }
.catch { emit(Result.Error(it)) }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.imcys.bilibilias.home.ui.model
package com.imcys.bilibilias.common.network.base

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand All @@ -13,4 +13,4 @@ data class ResBean<T>(
val message: String = "",
@SerialName("ttl")
val ttl: Int = 0
):java.io.Serializable
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.imcys.bilibilias.common.network.danmaku

import com.imcys.bilibilias.common.base.api.BilibiliApi
import com.imcys.bilibilias.common.base.extend.Result
import com.imcys.bilibilias.common.base.extend.safeGet
import com.imcys.bilibilias.common.network.base.ResBean
import io.ktor.client.HttpClient
import io.ktor.client.request.parameter
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class DanmakuRepository @Inject constructor(private val http: HttpClient) {
suspend fun getCideoInfoV2(avid: Long, cid: Long): Flow<Result<ResBean<VideoInfoV2>>> =
http.safeGet(BilibiliApi.videoInfoV2) {
parameter("aid", avid)
parameter("cid", cid)
}
}
Loading

0 comments on commit 6d808a3

Please sign in to comment.