Skip to content

Commit

Permalink
to be played
Browse files Browse the repository at this point in the history
  • Loading branch information
storytellerF committed Apr 12, 2023
1 parent 357f9e2 commit 49e8c4d
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 85 deletions.
2 changes: 1 addition & 1 deletion .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions app/src/main/java/com/storyteller_f/bi/Common.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.paging.LoadState

sealed class LoadingState {
class Loading(val state: String) : LoadingState()
Expand Down Expand Up @@ -36,3 +37,33 @@ fun StateView(state: LoadingState?, content: @Composable () -> Unit) {
is LoadingState.Done -> content()
}
}


@Composable
fun StateView(state: LoadState?, content: @Composable () -> Unit) {
when (state) {
null -> OneCenter {
Text(text = "waiting")
}
is LoadState.Loading -> OneCenter {
Text(text = "loading")
}
is LoadState.Error -> OneCenter {
Text(text = state.error.localizedMessage.orEmpty())
}
is LoadState.NotLoading -> content()
}
}

@Composable
fun ErrorStateView(state: LoadState?, content: @Composable () -> Unit) {
when (state) {
null -> OneCenter {
Text(text = "waiting")
}
is LoadState.Error -> OneCenter {
Text(text = state.error.localizedMessage.orEmpty())
}
else -> content()
}
}
30 changes: 26 additions & 4 deletions app/src/main/java/com/storyteller_f/bi/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.ExperimentalMaterial3Api
Expand All @@ -28,9 +30,10 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.NavHost
Expand All @@ -44,6 +47,7 @@ import com.storyteller_f.bi.components.HistoryPage
import com.storyteller_f.bi.components.HomeTopBar
import com.storyteller_f.bi.components.MomentsPage
import com.storyteller_f.bi.components.Screen
import com.storyteller_f.bi.components.ToBePlayedPage
import com.storyteller_f.bi.components.UserCenterDrawer
import com.storyteller_f.bi.ui.theme.BiTheme
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -73,6 +77,7 @@ class MainActivity : ComponentActivity() {
val items = listOf(
Screen.History,
Screen.Moments,
Screen.ToBePlay
)
BiTheme {
ModalNavigationDrawer(
Expand Down Expand Up @@ -107,14 +112,14 @@ class MainActivity : ComponentActivity() {
restoreState = true
}
}, {
Log.i(TAG, "onCreate: ${screen.icon}")
when {
screen.icon != null -> {
Icon(
ImageVector.vectorResource(id = screen.icon),
contentDescription = screen.route
)
}

screen.vector != null -> Icon(
screen.vector,
contentDescription = screen.route
Expand Down Expand Up @@ -146,6 +151,11 @@ class MainActivity : ComponentActivity() {
MomentsPage()
}
}
composable(Screen.ToBePlay.route) {
UserAware {
ToBePlayedPage()
}
}
}

}
Expand Down Expand Up @@ -175,6 +185,18 @@ class MainActivity : ComponentActivity() {
}
}

@Composable
fun StandBy(width: Int, height: Int, me: @Composable () -> Unit) {
val view = LocalView.current
if (view.isInEditMode) {
Box(modifier = Modifier
.width(width.dp)
.height(height.dp))
} else {
me()
}
}

fun String.createQRImage(width: Int, height: Int): Bitmap {
val bitMatrix = QRCodeWriter().encode(
this,
Expand All @@ -192,4 +214,4 @@ fun String.createQRImage(width: Int, height: Int): Bitmap {
}.toArray(),
width, height, Bitmap.Config.ARGB_8888
)
}
}
21 changes: 10 additions & 11 deletions app/src/main/java/com/storyteller_f/bi/components/History.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package com.storyteller_f.bi.components

import android.content.Intent
import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
Expand All @@ -33,7 +31,11 @@ import com.a10miaomiao.bilimiao.comm.network.request
import com.a10miaomiao.bilimiao.comm.utils.UrlUtil
import com.bumptech.glide.integration.compose.ExperimentalGlideComposeApi
import com.bumptech.glide.integration.compose.GlideImage
import com.storyteller_f.bi.ErrorStateView
import com.storyteller_f.bi.LoadingState
import com.storyteller_f.bi.OneCenter
import com.storyteller_f.bi.StandBy
import com.storyteller_f.bi.StateView
import com.storyteller_f.bi.VideoActivity

fun MutableLiveData<LoadingState>.loaded() {
Expand Down Expand Up @@ -68,15 +70,14 @@ class HistoryViewModel : ViewModel() {
fun HistoryPage() {
val viewModel = viewModel<HistoryViewModel>()
val lazyItems = viewModel.flow.collectAsLazyPagingItems()
when (val state = lazyItems.loadState.refresh) {
is LoadState.Loading -> Text(text = "loading")
is LoadState.Error -> Text(text = state.error.localizedMessage ?: "")
is LoadState.NotLoading -> LazyColumn {
StateView(state = lazyItems.loadState.refresh) {
LazyColumn {
items(lazyItems, {
it.oid.toString() + "" + it.kid.toString()
}) { item ->
HistoryItem(item ?: HistoryOuterClass.CursorItem.getDefaultInstance())
}
bottomAppending(lazyItems)
}
}
}
Expand Down Expand Up @@ -108,7 +109,7 @@ fun HistoryItem(
@Composable
@OptIn(ExperimentalGlideComposeApi::class)
fun VideoItem(
url: String? = null,
pic: String = "",
text: String = "text",
label: String = "label",
watchVideo: () -> Unit = {}
Expand All @@ -121,10 +122,8 @@ fun VideoItem(
val coverModifier = Modifier
.width((16 * 8).dp)
.height((8 * 8).dp)
if (url == null) {
Box(coverModifier.background(Color.Blue))
} else {
val u = "${UrlUtil.autoHttps(url)}@672w_378h_1c_"
StandBy(width = 16 * 8, height = 8 * 8) {
val u = "${UrlUtil.autoHttps(pic)}@672w_378h_1c_"
GlideImage(u, contentDescription = null, modifier = coverModifier)
}
Column(modifier = Modifier.padding(start = 8.dp)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.PlayArrow
import androidx.compose.ui.graphics.vector.ImageVector
import com.storyteller_f.bi.R

sealed class Screen(val route: String, @StringRes val resourceId: Int, val vector: ImageVector? = null, @DrawableRes val icon: Int? = null) {
object History : Screen("history", R.string.history, icon = R.drawable.baseline_history_24)
object Moments : Screen("moments", R.string.moments, vector = Icons.Filled.Menu)
object ToBePlay: Screen("to be play", R.string.to_be_play, vector = Icons.Filled.PlayArrow)
}
14 changes: 7 additions & 7 deletions app/src/main/java/com/storyteller_f/bi/components/Login.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import com.storyteller_f.bi.userInfo
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class QrcodeLoginViewModel(val context: Application) : AndroidViewModel(context) {
class QrcodeLoginViewModel(private val context: Application) : AndroidViewModel(context) {
val state = MutableLiveData<LoadingState>()
val qrcodeUrl = MutableLiveData<String>()
val checkState = MutableLiveData<LoadingState>()
Expand Down Expand Up @@ -86,26 +86,26 @@ class QrcodeLoginViewModel(val context: Application) : AndroidViewModel(context)
.gson<ResultInfo<LoginInfo.QrLoginInfo>>()
when (res.code) {
86039 -> {
checkState.value = LoadingState.Loading("未确认")
checkState.loading("未确认")
// 未确认
delay(3000)
checkQr(authCode)
}

86090 -> {
checkState.value = LoadingState.Loading("扫描成功,请点击确认")
checkState.loading("扫描成功,请点击确认")
// 已扫码未确认
delay(2000)
checkQr(authCode)
}

86038, -3 -> {
// 过期、失效
checkState.value = LoadingState.Loading("二维码已过期,请刷新")
checkState.loading("二维码已过期,请刷新")
}

0 -> {
checkState.value = LoadingState.Loading("扫码成功,正在读取信息")
checkState.loading("扫码成功,正在读取信息")
// 成功
val loginInfo = res.data.toLoginInfo()
BilimiaoCommApp.commApp.saveAuthInfo(loginInfo)
Expand All @@ -114,7 +114,7 @@ class QrcodeLoginViewModel(val context: Application) : AndroidViewModel(context)

else -> {
// 发生错误
checkState.value = LoadingState.Loading("登录失败,请稍后重试\n" + res.message)
checkState.loading("登录失败,请稍后重试\n" + res.message)
}
}
}
Expand All @@ -131,7 +131,7 @@ class QrcodeLoginViewModel(val context: Application) : AndroidViewModel(context)
context.saveUserInfo(res.data)
LoadingState.Done
} else {
LoadingState.Error(java.lang.Exception(res.message))
LoadingState.Error(Exception(res.message))
}

}
Expand Down
Loading

0 comments on commit 49e8c4d

Please sign in to comment.