chore: 修复预览不可用

This commit is contained in:
HuanCheng65 2023-07-23 16:40:12 +08:00
parent b224836d0f
commit 18ec754df5
No known key found for this signature in database
GPG Key ID: 5EC9DD60A32C7360
9 changed files with 90 additions and 28 deletions

View File

@ -284,6 +284,9 @@ class App : Application(), IApp, SketchFactory {
lateinit var INSTANCE: App lateinit var INSTANCE: App
private set private set
val isInitialized: Boolean
get() = this::INSTANCE.isInitialized
val isSystemNight: Boolean val isSystemNight: Boolean
get() = nightMode == Configuration.UI_MODE_NIGHT_YES get() = nightMode == Configuration.UI_MODE_NIGHT_YES
@ -669,6 +672,9 @@ class App : Application(), IApp, SketchFactory {
} }
override fun getColorById(context: Context, colorId: Int): Int { override fun getColorById(context: Context, colorId: Int): Int {
// if (!isInitialized) {
// return context.getColorCompat(colorId)
// }
when (colorId) { when (colorId) {
R.color.default_color_primary -> return getColorByAttr(context, R.attr.colorPrimary) R.color.default_color_primary -> return getColorByAttr(context, R.attr.colorPrimary)
R.color.default_color_accent -> return getColorByAttr(context, R.attr.colorAccent) R.color.default_color_accent -> return getColorByAttr(context, R.attr.colorAccent)

View File

@ -26,6 +26,8 @@ import kotlinx.collections.immutable.toImmutableList
import java.io.File import java.io.File
import kotlin.math.roundToInt import kotlin.math.roundToInt
private val Context.scaledDensity: Float
get() = resources.displayMetrics.scaledDensity
fun Float.dpToPx(): Int = fun Float.dpToPx(): Int =
dpToPxFloat().roundToInt() dpToPxFloat().roundToInt()
@ -33,11 +35,11 @@ fun Float.dpToPx(): Int =
fun Float.dpToPxFloat(): Float = fun Float.dpToPxFloat(): Float =
this * App.ScreenInfo.DENSITY + 0.5f this * App.ScreenInfo.DENSITY + 0.5f
fun Float.spToPx(): Int = fun Float.spToPx(context: Context = App.INSTANCE): Int =
(this * App.INSTANCE.resources.displayMetrics.scaledDensity + 0.5f).roundToInt() (this * context.scaledDensity + 0.5f).roundToInt()
fun Float.spToPxFloat(): Float = fun Float.spToPxFloat(context: Context = App.INSTANCE): Float =
this * App.INSTANCE.resources.displayMetrics.scaledDensity + 0.5f this * context.scaledDensity + 0.5f
fun Float.pxToDp(): Int = fun Float.pxToDp(): Int =
(this / App.ScreenInfo.DENSITY + 0.5f).roundToInt() (this / App.ScreenInfo.DENSITY + 0.5f).roundToInt()
@ -45,8 +47,8 @@ fun Float.pxToDp(): Int =
fun Float.pxToDpFloat(): Float = fun Float.pxToDpFloat(): Float =
this / App.ScreenInfo.DENSITY + 0.5f this / App.ScreenInfo.DENSITY + 0.5f
fun Float.pxToSp(): Int = fun Float.pxToSp(context: Context = App.INSTANCE): Int =
(this / App.INSTANCE.resources.displayMetrics.scaledDensity + 0.5f).roundToInt() (this / context.scaledDensity + 0.5f).roundToInt()
fun Int.dpToPx(): Int = this.toFloat().dpToPx() fun Int.dpToPx(): Int = this.toFloat().dpToPx()
@ -54,7 +56,7 @@ fun Int.spToPx(): Int = this.toFloat().spToPx()
fun Int.pxToDp(): Int = this.toFloat().pxToDp() fun Int.pxToDp(): Int = this.toFloat().pxToDp()
fun Int.pxToSp(): Int = this.toFloat().pxToSp() fun Int.pxToSp(context: Context = App.INSTANCE): Int = this.toFloat().pxToSp(context)
fun Float.pxToSpFloat(): Float = this / App.INSTANCE.resources.displayMetrics.scaledDensity + 0.5f fun Float.pxToSpFloat(): Float = this / App.INSTANCE.resources.displayMetrics.scaledDensity + 0.5f

View File

@ -12,6 +12,8 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.staticCompositionLocalOf import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.core.view.WindowCompat import androidx.core.view.WindowCompat
import com.google.accompanist.systemuicontroller.SystemUiController import com.google.accompanist.systemuicontroller.SystemUiController
import com.google.accompanist.systemuicontroller.rememberSystemUiController import com.google.accompanist.systemuicontroller.rememberSystemUiController
@ -114,7 +116,9 @@ abstract class BaseComposeActivity : BaseActivity() {
companion object { companion object {
val LocalWindowSizeClass = val LocalWindowSizeClass =
staticCompositionLocalOf<WindowSizeClass> { error("not initialized") } staticCompositionLocalOf<WindowSizeClass> {
WindowSizeClass.calculateFromSize(DpSize(0.dp, 0.dp))
}
} }
} }

View File

@ -14,12 +14,32 @@ import androidx.activity.viewModels
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll import androidx.compose.foundation.verticalScroll
import androidx.compose.material.* import androidx.compose.material.Card
import androidx.compose.runtime.* import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
@ -53,12 +73,31 @@ import com.huanchengfly.tieba.post.ui.page.editprofile.EditProfileEvent
import com.huanchengfly.tieba.post.ui.page.editprofile.EditProfileIntent import com.huanchengfly.tieba.post.ui.page.editprofile.EditProfileIntent
import com.huanchengfly.tieba.post.ui.page.editprofile.EditProfileState import com.huanchengfly.tieba.post.ui.page.editprofile.EditProfileState
import com.huanchengfly.tieba.post.ui.page.editprofile.viewmodel.EditProfileViewModel import com.huanchengfly.tieba.post.ui.page.editprofile.viewmodel.EditProfileViewModel
import com.huanchengfly.tieba.post.ui.widgets.compose.* import com.huanchengfly.tieba.post.ui.widgets.compose.ActionItem
import com.huanchengfly.tieba.post.ui.widgets.compose.BackNavigationIcon
import com.huanchengfly.tieba.post.ui.widgets.compose.CounterTextField
import com.huanchengfly.tieba.post.ui.widgets.compose.Dialog
import com.huanchengfly.tieba.post.ui.widgets.compose.DialogNegativeButton
import com.huanchengfly.tieba.post.ui.widgets.compose.Toolbar
import com.huanchengfly.tieba.post.ui.widgets.compose.picker.ListSinglePicker import com.huanchengfly.tieba.post.ui.widgets.compose.picker.ListSinglePicker
import com.huanchengfly.tieba.post.utils.* import com.huanchengfly.tieba.post.ui.widgets.compose.rememberDialogState
import com.huanchengfly.tieba.post.utils.AccountUtil
import com.huanchengfly.tieba.post.utils.ColorUtils
import com.huanchengfly.tieba.post.utils.PermissionUtils
import com.huanchengfly.tieba.post.utils.PickMediasRequest
import com.huanchengfly.tieba.post.utils.StringUtil
import com.huanchengfly.tieba.post.utils.ThemeUtil
import com.huanchengfly.tieba.post.utils.isPhotoPickerAvailable
import com.huanchengfly.tieba.post.utils.registerPickMediasLauncher
import com.huanchengfly.tieba.post.utils.requestPermission
import com.yalantis.ucrop.UCrop import com.yalantis.ucrop.UCrop
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.* import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.onEach
import java.io.File import java.io.File
@AndroidEntryPoint @AndroidEntryPoint
@ -458,11 +497,11 @@ fun PageEditProfile(
} }
) { ) {
ListSinglePicker( ListSinglePicker(
itemTitles = listOf( itemTitles = persistentListOf(
stringResource(id = R.string.profile_sex_male), stringResource(id = R.string.profile_sex_male),
stringResource(id = R.string.profile_sex_female) stringResource(id = R.string.profile_sex_female)
), ),
itemValues = listOf(1, 2), itemValues = persistentListOf(1, 2),
selectedPosition = if (sex == 1) 0 else if (sex == 2) 1 else -1, selectedPosition = if (sex == 1) 0 else if (sex == 2) 1 else -1,
onItemSelected = { _, _, value, _ -> onItemSelected = { _, _, value, _ ->
sex = value sex = value

View File

@ -739,11 +739,11 @@ fun ForumPage(
menuState = menuState, menuState = menuState,
menuContent = { menuContent = {
ListSinglePicker( ListSinglePicker(
itemTitles = listOf( itemTitles = persistentListOf(
stringResource(id = R.string.title_sort_by_reply), stringResource(id = R.string.title_sort_by_reply),
stringResource(id = R.string.title_sort_by_send) stringResource(id = R.string.title_sort_by_send)
), ),
itemValues = listOf(0, 1), itemValues = persistentListOf(0, 1),
selectedPosition = currentSortType, selectedPosition = currentSortType,
onItemSelected = { _, _, value, changed -> onItemSelected = { _, _, value, changed ->
if (changed) { if (changed) {

View File

@ -98,11 +98,13 @@ private val ImmutableHolder<Media>.url: String
@Composable @Composable
private fun DefaultUserHeader( private fun DefaultUserHeader(
user: ImmutableHolder<User>, userProvider: () -> ImmutableHolder<User>,
time: Int, timeProvider: () -> Int,
content: @Composable RowScope.() -> Unit content: @Composable RowScope.() -> Unit
) { ) {
val context = LocalContext.current val context = LocalContext.current
val user = remember(userProvider) { userProvider() }
val time = remember(timeProvider) { timeProvider() }
UserHeader( UserHeader(
avatar = { avatar = {
Avatar( Avatar(
@ -144,6 +146,7 @@ private fun DefaultUserHeader(
@Composable @Composable
fun Card( fun Card(
modifier: Modifier = Modifier,
header: @Composable ColumnScope.() -> Unit = {}, header: @Composable ColumnScope.() -> Unit = {},
content: @Composable ColumnScope.() -> Unit = {}, content: @Composable ColumnScope.() -> Unit = {},
action: @Composable (ColumnScope.() -> Unit)? = null, action: @Composable (ColumnScope.() -> Unit)? = null,
@ -156,6 +159,7 @@ fun Card(
Column( Column(
modifier = cardModifier modifier = cardModifier
.then(modifier)
.then(paddingModifier) .then(paddingModifier)
.padding(horizontal = 16.dp) .padding(horizontal = 16.dp)
) { ) {
@ -509,6 +513,7 @@ fun FeedCard(
item: ImmutableHolder<ThreadInfo>, item: ImmutableHolder<ThreadInfo>,
onClick: (ThreadInfo) -> Unit, onClick: (ThreadInfo) -> Unit,
onAgree: (ThreadInfo) -> Unit, onAgree: (ThreadInfo) -> Unit,
modifier: Modifier = Modifier,
onReplyClick: (ThreadInfo) -> Unit = {}, onReplyClick: (ThreadInfo) -> Unit = {},
onClickForum: (SimpleForum) -> Unit = {}, onClickForum: (SimpleForum) -> Unit = {},
dislikeAction: @Composable () -> Unit = {}, dislikeAction: @Composable () -> Unit = {},
@ -517,11 +522,9 @@ fun FeedCard(
header = { header = {
val hasAuthor = remember(item) { item.isNotNull { author } } val hasAuthor = remember(item) { item.isNotNull { author } }
if (hasAuthor) { if (hasAuthor) {
val author = remember(item) { item.getImmutable { author!! } }
val time = remember(item) { item.get { lastTimeInt } }
DefaultUserHeader( DefaultUserHeader(
user = author, userProvider = { item.getImmutable { author!! } },
time = time timeProvider = { item.get { lastTimeInt } }
) { dislikeAction() } ) { dislikeAction() }
} }
}, },
@ -562,6 +565,7 @@ fun FeedCard(
} }
}, },
onClick = { onClick(item.get()) }, onClick = { onClick(item.get()) },
modifier = modifier
) )
} }
@ -683,6 +687,7 @@ fun FeedCardPreview() {
) )
), ),
onClick = {}, onClick = {},
onAgree = {} onAgree = {},
modifier = Modifier.background(ExtendedTheme.colors.card)
) )
} }

View File

@ -28,6 +28,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.takeOrElse import androidx.compose.ui.graphics.takeOrElse
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLayoutDirection import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.text.AnnotatedString
@ -207,7 +208,7 @@ fun IconText(
) )
) )
val sizePx = getEmoticonHeightPx(mergedStyle) * 9 / 10 val sizePx = getEmoticonHeightPx(mergedStyle) * 9 / 10
val sizeSp = sizePx.pxToSp().sp val sizeSp = sizePx.pxToSp(LocalContext.current).sp
val sizeDp = sizePx.pxToDp().dp val sizeDp = sizePx.pxToDp().dp
val iconInlineContent = val iconInlineContent =
remember(sizeSp) { remember(sizeSp) {

View File

@ -14,6 +14,7 @@ import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.withStyle import androidx.compose.ui.text.withStyle
import com.huanchengfly.tieba.post.App
import com.huanchengfly.tieba.post.R import com.huanchengfly.tieba.post.R
import com.huanchengfly.tieba.post.components.spans.EmoticonSpanV2 import com.huanchengfly.tieba.post.components.spans.EmoticonSpanV2
import com.huanchengfly.tieba.post.ui.common.theme.utils.ThemeUtils import com.huanchengfly.tieba.post.ui.common.theme.utils.ThemeUtils
@ -98,7 +99,7 @@ object StringUtil {
nickname: String?, nickname: String?,
color: Color = Color.Unspecified color: Color = Color.Unspecified
): AnnotatedString { ): AnnotatedString {
val showBoth = context.appPreferences.showBothUsernameAndNickname val showBoth = App.isInitialized && context.appPreferences.showBothUsernameAndNickname
return buildAnnotatedString { return buildAnnotatedString {
if (showBoth && !nickname.isNullOrBlank() && username != nickname && username.isNotBlank()) { if (showBoth && !nickname.isNullOrBlank() && username != nickname && username.isNotBlank()) {
append(nickname) append(nickname)

View File

@ -25,6 +25,7 @@ import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.CustomViewTarget import com.bumptech.glide.request.target.CustomViewTarget
import com.bumptech.glide.request.transition.Transition import com.bumptech.glide.request.transition.Transition
import com.google.android.material.appbar.AppBarLayout import com.google.android.material.appbar.AppBarLayout
import com.huanchengfly.tieba.post.App
import com.huanchengfly.tieba.post.App.Companion.INSTANCE import com.huanchengfly.tieba.post.App.Companion.INSTANCE
import com.huanchengfly.tieba.post.App.Companion.translucentBackground import com.huanchengfly.tieba.post.App.Companion.translucentBackground
import com.huanchengfly.tieba.post.R import com.huanchengfly.tieba.post.R
@ -44,7 +45,10 @@ import java.util.Locale
object ThemeUtil { object ThemeUtil {
val themeState: MutableState<String> by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { val themeState: MutableState<String> by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
mutableStateOf(dataStore.getString(KEY_THEME, THEME_DEFAULT)) mutableStateOf(
if (App.isInitialized) dataStore.getString(KEY_THEME, THEME_DEFAULT)
else THEME_DEFAULT
)
} }
const val TAG = "ThemeUtil" const val TAG = "ThemeUtil"