Skip to content

Commit

Permalink
优化导入
Browse files Browse the repository at this point in the history
移除无用文件;提取playVideo 作为参数
  • Loading branch information
storytellerF committed Aug 26, 2023
1 parent 93b73c2 commit 0f6cf23
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 193 deletions.
97 changes: 0 additions & 97 deletions app/src/main/java/com/storyteller_f/bi/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,100 +37,3 @@ class App : Application() {
readUserInfo()
}
}

suspend fun BasePlayerRepository.subtitleMediaSources(
context: Context
): List<SingleSampleMediaSource> {
return subtitleConfigurations(context).map {
SingleSampleMediaSource.Factory(DefaultDataSource.Factory(context))
.createMediaSource(it, C.TIME_UNSET)
}
}

suspend fun BasePlayerRepository.subtitleConfigurations(
context: Context
): List<MediaItem.SubtitleConfiguration> {

val subtitles = getSubtitles()

val pairs = subtitles.mapNotNull { info: SubtitleSourceInfo ->
val file = File(context.cacheDir, "/subtitle/$id/${info.lan}.srt")
val parent = file.parentFile
when {
parent == null -> null
!parent.exists() && !parent.mkdirs() -> null
file.exists() -> file to info to null
!file.createNewFile() -> null
else -> {
val res = MiaoHttp.request {
url = UrlUtil.autoHttps(info.subtitle_url)
}.awaitCall().gson<SubtitleJsonInfo>()
file to info to res
}
}
}
val lanList = pairs.map { it.first.second.lan }
val infoList = pairs.mapNotNull {
it.second
}

val configurations = pairs.map { (it, jInfo) ->
val (file, info) = it
if (jInfo != null) {
writeToFile(jInfo, file)
}
MediaItem.SubtitleConfiguration.Builder(file.toUri())
.setMimeType(MimeTypes.APPLICATION_SUBRIP)
.setSelectionFlags(C.SELECTION_FLAG_DEFAULT)
.setLabel(info.lan_doc)
.setId(info.id)
.setRoleFlags(C.ROLE_FLAG_SUBTITLE)
.setLanguage(info.lan).build()
}
val file = File(context.cacheDir, "subtitle/$id/mix.srt")
val parentFile = file.parentFile
if (pairs.size == 2 && infoList.size == 2 && subtitles.size == 2 && lanList.any {
it.contains("zh")
} && lanList.any {
it.contains(
"en"
)
} && parentFile != null && (parentFile.exists() || parentFile.mkdirs()) && (file.exists() || withContext(Dispatchers.IO) {
file.createNewFile()
})
) {
val first = infoList.first()
val jsonInfo = infoList.last()

writeToFile(first, file) { it, i ->
"${it.content}\n${jsonInfo.body[i].content}"
}
return configurations + MediaItem.SubtitleConfiguration.Builder(file.toUri())
.setMimeType(MimeTypes.APPLICATION_SUBRIP)
.setSelectionFlags(C.SELECTION_FLAG_DEFAULT)
.setLabel("mix")
.setRoleFlags(C.ROLE_FLAG_SUBTITLE).build()
}
return configurations
}

private suspend fun writeToFile(
jInfo: SubtitleJsonInfo,
file: File,
c: (SubtitleJsonInfo.ItemInfo, Int) -> String = { it, _ -> it.content }
) {
val content = jInfo.body.mapIndexed { index, itemInfo ->
val toDuration = PlayerDelegate.formatDuration(
itemInfo.to
)
val fromDuration = PlayerDelegate.formatDuration(itemInfo.from)
"""
${index + 1}
$fromDuration --> $toDuration
${c(itemInfo, index)}
""".trimIndent()
}.joinToString("\n\n")
withContext(Dispatchers.IO) {
file.writeText(content)
}
}
63 changes: 62 additions & 1 deletion app/src/main/java/com/storyteller_f/bi/Common.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package com.storyteller_f.bi

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Color
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
Expand All @@ -17,6 +24,7 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalView
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
Expand All @@ -25,6 +33,9 @@ import androidx.lifecycle.viewmodel.MutableCreationExtras
import androidx.paging.LoadState
import androidx.paging.compose.LazyPagingItems
import com.a10miaomiao.bilimiao.comm.entity.ResultInfo2
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.qrcode.QRCodeWriter
import com.storyteller_f.bi.components.BangumiViewModel
import com.storyteller_f.bi.components.CommentId
import com.storyteller_f.bi.components.CommentReplyViewModel
Expand All @@ -40,8 +51,11 @@ import com.storyteller_f.bi.components.VideoViewModel
import com.storyteller_f.bi.components.error
import com.storyteller_f.bi.components.loaded
import com.storyteller_f.bi.components.loading
import com.storyteller_f.bi.unstable.userInfo
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.Collections
import java.util.stream.IntStream

sealed class LoadingState {
class Loading(val state: String) : LoadingState()
Expand Down Expand Up @@ -206,4 +220,51 @@ inline fun <T> request(
val state = handler.state
val data = handler.data
request(state, data, service)
}
}


@Composable
fun StandBy(modifier: Modifier, me: @Composable () -> Unit) {
val view = LocalView.current
if (view.isInEditMode) {
Box(modifier.background(MaterialTheme.colorScheme.primaryContainer))
} else {
me()
}
}


fun String.createQRImage(width: Int, height: Int): Bitmap {
val bitMatrix = QRCodeWriter().encode(
this,
BarcodeFormat.QR_CODE,
width,
height,
Collections.singletonMap(EncodeHintType.CHARACTER_SET, "utf-8")
)
return Bitmap.createBitmap(
IntStream.range(0, height).flatMap { h: Int ->
IntStream.range(0, width).map { w: Int ->
if (bitMatrix[w, h]
) Color.BLACK else Color.WHITE
}
}.toArray(),
width, height, Bitmap.Config.ARGB_8888
)
}

@Composable
fun UserAware(login: () -> Unit = {}, content: @Composable () -> Unit) {
val u by userInfo.observeAsState()
if (u == null) {
OneCenter {
Button(onClick = {
login()
}) {
Text(text = "login")
}
}
} else {
content()
}
}
77 changes: 2 additions & 75 deletions app/src/main/java/com/storyteller_f/bi/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
package com.storyteller_f.bi

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Color
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.displayCutoutPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationRail
import androidx.compose.material3.NavigationRailItem
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass
import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
Expand All @@ -33,17 +25,13 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.unit.dp
import androidx.core.view.WindowCompat
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.qrcode.QRCodeWriter
import com.storyteller_f.bi.components.HomeNavigation
import com.storyteller_f.bi.components.NavItemIcon
import com.storyteller_f.bi.components.Screen
Expand All @@ -52,8 +40,6 @@ import com.storyteller_f.bi.components.VideoPage
import com.storyteller_f.bi.components.homeNav
import com.storyteller_f.bi.ui.theme.BiTheme
import com.storyteller_f.bi.unstable.userInfo
import java.util.Collections
import java.util.stream.IntStream

class SideVideo(val id: String, val kid: String, val business: String, val progress: Long)

Expand Down Expand Up @@ -103,9 +89,9 @@ class MainActivity : ComponentActivity() {

val wideMode =
calculateWindowSizeClass.widthSizeClass != WindowWidthSizeClass.Compact
val openVideo: (String, String, String, Long) -> Unit = { kid, oid, business, progress ->
val openVideo: (String?, String?, String, Long) -> Unit = { kid, oid, business, progress ->
if (wideMode) {
adaptiveVideo = SideVideo(oid, kid, business, progress)
adaptiveVideo = SideVideo(oid!!, kid!!, business, progress)
} else {
context.playVideo(kid, oid, business, progress)
}
Expand Down Expand Up @@ -167,62 +153,3 @@ class MainActivity : ComponentActivity() {
}

}

@Composable
fun UserAware(login: () -> Unit = {}, content: @Composable () -> Unit) {
val u by userInfo.observeAsState()
if (u == null) {
OneCenter {
Button(onClick = {
login()
}) {
Text(text = "login")
}
}
} else {
content()
}
}

@Composable
fun StandBy(modifier: Modifier, me: @Composable () -> Unit) {
val view = LocalView.current
if (view.isInEditMode) {
Box(modifier.background(MaterialTheme.colorScheme.primaryContainer))
} else {
me()
}
}


fun String.createQRImage(width: Int, height: Int): Bitmap {
val bitMatrix = QRCodeWriter().encode(
this,
BarcodeFormat.QR_CODE,
width,
height,
Collections.singletonMap(EncodeHintType.CHARACTER_SET, "utf-8")
)
return Bitmap.createBitmap(
IntStream.range(0, height).flatMap { h: Int ->
IntStream.range(0, width).map { w: Int ->
if (bitMatrix[w, h]
) Color.BLACK else Color.WHITE
}
}.toArray(),
width, height, Bitmap.Config.ARGB_8888
)
}

fun Context.playVideo(kid: String?, oid: String?, business: String, progress: Long = 0L) {
startActivity(
Intent(
this,
VideoActivity::class.java
).apply {
putExtra("videoId", oid)
putExtra("extra", kid)
putExtra("progress", progress)
putExtra("business", business)
})
}
15 changes: 15 additions & 0 deletions app/src/main/java/com/storyteller_f/bi/VideoActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.storyteller_f.bi

import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.os.Bundle
Expand Down Expand Up @@ -54,4 +56,17 @@ class VideoActivity : ComponentActivity() {
companion object {
private const val TAG = "VideoActivity"
}
}

fun Context.playVideo(kid: String?, oid: String?, business: String, progress: Long = 0L) {
startActivity(
Intent(
this,
VideoActivity::class.java
).apply {
putExtra("videoId", oid)
putExtra("extra", kid)
putExtra("progress", progress)
putExtra("business", business)
})
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ import com.storyteller_f.bi.playVideo
object FavoriteIdKey : CreationExtras.Key<String>

@Composable
fun FavoriteDetailPage(id: String) {
val current = LocalContext.current
fun FavoriteDetailPage(id: String, playVideo: (String?, String?, String, Long) -> Unit) {
val detailViewModel = viewModel<FavoriteDetailViewModel>(
factory = defaultFactory,
extras = MutableCreationExtras().apply {
Expand All @@ -51,7 +50,7 @@ fun FavoriteDetailPage(id: String) {
item?.title.orEmpty(),
item?.upper?.name.orEmpty()
) {
current.playVideo(item?.id, item?.id, "archive", 0)
playVideo(item?.id, item?.id, "archive", 0)
}
}
}
Expand Down
Loading

0 comments on commit 0f6cf23

Please sign in to comment.