-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from ckw1/develop
支持稍后再看/现在不看
- Loading branch information
Showing
11 changed files
with
528 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
app/src/main/kotlin/dev/aaa1115910/bv/activities/user/ToViewActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
app/src/main/kotlin/dev/aaa1115910/bv/screen/user/ToViewScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
// } | ||
} | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
app/src/main/kotlin/dev/aaa1115910/bv/viewmodel/user/ToViewViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.