Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持稍后再看/现在不看 #158

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@
android:exported="false"
android:label="@string/title_activity_history"
android:theme="@style/Theme.BV" />
<activity
android:name=".activities.user.ToViewActivity"
android:exported="false"
android:label="@string/title_activity_toview"
android:theme="@style/Theme.BV" />
<activity
android:name=".activities.user.UserInfoActivity"
android:exported="false"
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/kotlin/dev/aaa1115910/bv/BVApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import dev.aaa1115910.biliapi.repositories.AuthRepository
import dev.aaa1115910.biliapi.repositories.ChannelRepository
import dev.aaa1115910.biliapi.repositories.FavoriteRepository
import dev.aaa1115910.biliapi.repositories.HistoryRepository
import dev.aaa1115910.biliapi.repositories.ToViewRepository
import dev.aaa1115910.biliapi.repositories.LoginRepository
import dev.aaa1115910.biliapi.repositories.PgcRepository
import dev.aaa1115910.biliapi.repositories.RecommendVideoRepository
Expand Down Expand Up @@ -53,6 +54,7 @@ import dev.aaa1115910.bv.viewmodel.user.FavoriteViewModel
import dev.aaa1115910.bv.viewmodel.user.FollowViewModel
import dev.aaa1115910.bv.viewmodel.user.FollowingSeasonViewModel
import dev.aaa1115910.bv.viewmodel.user.HistoryViewModel
import dev.aaa1115910.bv.viewmodel.user.ToViewViewModel
import dev.aaa1115910.bv.viewmodel.user.UpInfoViewModel
import dev.aaa1115910.bv.viewmodel.video.VideoDetailViewModel
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -156,6 +158,7 @@ val appModule = module {
single { ChannelRepository() }
single { FavoriteRepository(get()) }
single { HistoryRepository(get(), get()) }
single { ToViewRepository(get(), get()) }
single { SearchRepository(get(), get()) }
single { VideoPlayRepository(get(), get()) }
single { RecommendVideoRepository(get(), get()) }
Expand All @@ -172,6 +175,7 @@ val appModule = module {
viewModel { PlayerViewModel(get()) }
viewModel { UserViewModel(get()) }
viewModel { HistoryViewModel(get(), get()) }
viewModel { ToViewViewModel(get(), get()) }
viewModel { FavoriteViewModel(get()) }
viewModel { UpInfoViewModel(get()) }
viewModel { FollowViewModel(get()) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.aaa1115910.bv.activities.user

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
// import dev.aaa1115910.bv.screen.user.HistoryScreen
import dev.aaa1115910.bv.screen.user.ToViewScreen
import dev.aaa1115910.bv.ui.theme.BVTheme

class ToViewActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
BVTheme {
// HistoryScreen()
ToViewScreen()
}
}
}
}
3 changes: 2 additions & 1 deletion app/src/main/kotlin/dev/aaa1115910/bv/screen/MainScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import dev.aaa1115910.bv.activities.settings.SettingsActivity
import dev.aaa1115910.bv.activities.user.FavoriteActivity
import dev.aaa1115910.bv.activities.user.FollowingSeasonActivity
import dev.aaa1115910.bv.activities.user.HistoryActivity
import dev.aaa1115910.bv.activities.user.ToViewActivity
import dev.aaa1115910.bv.activities.user.LoginActivity
import dev.aaa1115910.bv.activities.user.UserInfoActivity
import dev.aaa1115910.bv.component.UserPanel
Expand Down Expand Up @@ -199,7 +200,7 @@ fun MainScreen(
)
},
onGoLater = {
"按钮放在这只是拿来当摆设的!".toast(context)
context.startActivity(Intent(context, ToViewActivity::class.java))
}
)
}
Expand Down
124 changes: 124 additions & 0 deletions app/src/main/kotlin/dev/aaa1115910/bv/screen/user/ToViewScreen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package dev.aaa1115910.bv.screen.user

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.tv.foundation.lazy.grid.TvGridCells
import androidx.tv.foundation.lazy.grid.TvLazyVerticalGrid
import androidx.tv.foundation.lazy.grid.itemsIndexed
import androidx.tv.material3.ExperimentalTvMaterial3Api
import androidx.tv.material3.Text
import dev.aaa1115910.bv.R
import dev.aaa1115910.bv.activities.video.VideoInfoActivity
import dev.aaa1115910.bv.component.videocard.SmallVideoCard
import dev.aaa1115910.bv.entity.proxy.ProxyArea
import dev.aaa1115910.bv.viewmodel.user.HistoryViewModel
import dev.aaa1115910.bv.viewmodel.user.ToViewViewModel
import org.koin.androidx.compose.koinViewModel

@OptIn(ExperimentalTvMaterial3Api::class)
@Composable
fun ToViewScreen(
modifier: Modifier = Modifier,
ToViewViewModel: ToViewViewModel = koinViewModel()
) {
val context = LocalContext.current
var currentIndex by remember { mutableIntStateOf(0) }
val showLargeTitle by remember { derivedStateOf { currentIndex < 4 } }
val titleFontSize by animateFloatAsState(
targetValue = if (showLargeTitle) 48f else 24f,
label = "title font size"
)

LaunchedEffect(Unit) {
ToViewViewModel.update()
}

Scaffold(
modifier = modifier,
topBar = {
Box(
modifier = Modifier.padding(start = 48.dp, top = 24.dp, bottom = 8.dp, end = 48.dp)
) {
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.Bottom,
horizontalArrangement = Arrangement.SpaceBetween
) {
Text(
text = stringResource(R.string.title_activity_toview),
fontSize = titleFontSize.sp
)
if (ToViewViewModel.noMore) {
Text(
text = stringResource(
R.string.load_data_count_no_more,
ToViewViewModel.histories.size
),
color = Color.White.copy(alpha = 0.6f)
)
} else {
Text(
text = stringResource(
R.string.load_data_count,
ToViewViewModel.histories.size
),
color = Color.White.copy(alpha = 0.6f)
)
}
}
}
}
) { innerPadding ->
TvLazyVerticalGrid(
modifier = Modifier.padding(innerPadding),
columns = TvGridCells.Fixed(4),
contentPadding = PaddingValues(24.dp),
verticalArrangement = Arrangement.spacedBy(24.dp),
horizontalArrangement = Arrangement.spacedBy(24.dp)
) {
itemsIndexed(ToViewViewModel.histories) { index, item ->
Box(
contentAlignment = Alignment.Center
) {
SmallVideoCard(
data = item,
onClick = {
VideoInfoActivity.actionStart(
context = context,
aid = item.avid,
proxyArea = ProxyArea.checkProxyArea(item.title)
)
},
onFocus = {
currentIndex = index
//预加载
// if (index + 20 > ToViewViewModel.histories.size) {
// ToViewViewModel.update()
// }
}
)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package dev.aaa1115910.bv.viewmodel.user

import android.content.Context
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dev.aaa1115910.biliapi.http.entity.AuthFailureException
import dev.aaa1115910.biliapi.repositories.HistoryRepository
import dev.aaa1115910.biliapi.repositories.ToViewRepository
import dev.aaa1115910.bv.BVApp
import dev.aaa1115910.bv.BuildConfig
import dev.aaa1115910.bv.R
import dev.aaa1115910.bv.entity.carddata.VideoCardData
import dev.aaa1115910.bv.repository.UserRepository
import dev.aaa1115910.bv.util.Prefs
import dev.aaa1115910.bv.util.fInfo
import dev.aaa1115910.bv.util.fWarn
import dev.aaa1115910.bv.util.formatMinSec
import dev.aaa1115910.bv.util.toast
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class ToViewViewModel(
private val userRepository: UserRepository,
private val ToViewRepository: ToViewRepository
) : ViewModel() {
companion object {
private val logger = KotlinLogging.logger { }
}

var histories = mutableStateListOf<VideoCardData>()
var noMore by mutableStateOf(false)

private var cursor = 0L
private var updating = false

fun update() {
viewModelScope.launch(Dispatchers.IO) {
updateToView()
}
}

private suspend fun updateToView(context: Context = BVApp.context) {
if (updating || noMore) return
logger.fInfo { "Updating histories with params [cursor=$cursor, apiType=${Prefs.apiType}]" }
updating = true
runCatching {
val data = ToViewRepository.getToView(
cursor = cursor,
preferApiType = Prefs.apiType
)

data.data.forEach { ToViewItem ->
histories.add(
VideoCardData(
avid = ToViewItem.oid,
title = ToViewItem.title,
cover = ToViewItem.cover,
upName = ToViewItem.author,
timeString = if (ToViewItem.progress == -1) context.getString(R.string.play_time_finish)
else context.getString(
R.string.play_time_history,
(ToViewItem.progress * 1000L).formatMinSec(),
(ToViewItem.duration * 1000L).formatMinSec()
)
)
)
}
//update cursor
cursor = data.cursor
logger.fInfo { "Update toview cursor: [cursor=$cursor]" }
logger.fInfo { "Update histories success" }
if (cursor == 0L) {
noMore = true
logger.fInfo { "No more toview" }
}
}.onFailure {
logger.fWarn { "Update histories failed: ${it.stackTraceToString()}" }
when (it) {
is AuthFailureException -> {
withContext(Dispatchers.Main) {
BVApp.context.getString(R.string.exception_auth_failure)
.toast(BVApp.context)
}
logger.fInfo { "User auth failure" }
if (!BuildConfig.DEBUG) userRepository.logout()
}

else -> {}
}
}
updating = false
}
}
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 @@ -179,6 +179,7 @@
<string name="resolution_hdr_short">HDR</string>

<string name="search_input_history">历史记录</string>
<string name="title_activity_toview">现在不看</string>
<string name="search_input_hotword">bilibili 热搜</string>
<string name="search_input_soft_keybord_clear">清空</string>
<string name="search_input_soft_keybord_delete">删除</string>
Expand Down
Loading