Skip to content

Commit

Permalink
build: update compose.
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyroid committed Jan 15, 2024
1 parent 8804570 commit 17230cd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 113 deletions.
26 changes: 13 additions & 13 deletions androidApp/src/main/java/com/m3u/androidApp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import com.m3u.core.unspecified.unspecifiable
import com.m3u.core.util.basic.rational
import com.m3u.core.util.context.isDarkMode
import com.m3u.core.util.context.isPortraitMode
import com.m3u.core.util.coroutine.getValue
import com.m3u.core.util.coroutine.setValue
import com.m3u.core.wrapper.Message
import com.m3u.data.service.PlayerService
import com.m3u.ui.Action
Expand All @@ -50,10 +52,10 @@ import com.m3u.ui.OnUserLeaveHint
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.collections.immutable.ImmutableList
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.reflect.KMutableProperty0
import kotlin.time.Duration

@AndroidEntryPoint
Expand All @@ -66,10 +68,10 @@ class MainActivity : AppCompatActivity() {
private val viewModel: AppViewModel by viewModels()
private val helper by lazy {
helper(
title = viewModel.title::value,
actions = viewModel.actions::value,
fob = viewModel.fob::value,
deep = viewModel.deep::value
title = viewModel.title,
actions = viewModel.actions,
fob = viewModel.fob,
deep = viewModel.deep
)
}

Expand Down Expand Up @@ -115,10 +117,10 @@ class MainActivity : AppCompatActivity() {
}

private fun helper(
title: Method<String>,
actions: Method<ImmutableList<Action>>,
fob: Method<Fob?>,
deep: Method<Int>
title: MutableStateFlow<String>,
actions: MutableStateFlow<ImmutableList<Action>>,
fob: MutableStateFlow<Fob?>,
deep: MutableStateFlow<Int>
): Helper = object : Helper {
init {
addOnPictureInPictureModeChangedListener { info ->
Expand Down Expand Up @@ -152,9 +154,9 @@ class MainActivity : AppCompatActivity() {
}

override var deep: Int
get() = deep.get()
get() = deep.value
set(value) {
deep.set(value.coerceAtLeast(0))
deep.value = value.coerceAtLeast(0)
}

override val message: StateFlow<Message> = viewModel.message
Expand Down Expand Up @@ -287,5 +289,3 @@ class MainActivity : AppCompatActivity() {
}
}
}

private typealias Method<E> = KMutableProperty0<E>
14 changes: 14 additions & 0 deletions core/src/main/java/com/m3u/core/util/coroutine/Flows.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.m3u.core.util.coroutine

import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlin.reflect.KProperty


operator fun <T> MutableStateFlow<T>.setValue(ref: Any, property: KProperty<*>, t: T) {
value = t
}

operator fun <T> StateFlow<T>.getValue(ref: Any, property: KProperty<*>): T {
return value
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,32 @@ package com.m3u.features.setting
import android.net.Uri
import androidx.activity.compose.BackHandler
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.adaptive.AnimatedPane
import androidx.compose.material3.adaptive.HingePolicy
import androidx.compose.material3.adaptive.ListDetailPaneScaffold
import androidx.compose.material3.adaptive.ListDetailPaneScaffoldRole
import androidx.compose.material3.adaptive.PaneScaffoldDirective
import androidx.compose.material3.adaptive.Posture
import androidx.compose.material3.adaptive.WindowAdaptiveInfo
import androidx.compose.material3.adaptive.allVerticalHingeBounds
import androidx.compose.material3.adaptive.calculateListDetailPaneScaffoldState
import androidx.compose.material3.adaptive.calculatePosture
import androidx.compose.material3.adaptive.collectFoldingFeaturesAsState
import androidx.compose.material3.adaptive.currentWindowSize
import androidx.compose.material3.adaptive.occludingVerticalHingeBounds
import androidx.compose.material3.adaptive.separatingVerticalHingeBounds
import androidx.compose.material3.windowsizeclass.WindowSizeClass
import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.toSize
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.m3u.core.architecture.pref.LocalPref
import com.m3u.core.util.basic.title
import com.m3u.core.util.compose.observableStateOf
import com.m3u.data.database.model.Stream
import com.m3u.features.setting.fragments.AppearanceFragment
import com.m3u.features.setting.fragments.ColorPack
import com.m3u.features.setting.fragments.ScriptsFragment
import com.m3u.features.setting.fragments.SubscriptionsFragment
import com.m3u.features.setting.fragments.AppearanceFragment
import com.m3u.features.setting.fragments.preferences.PreferencesFragment
import com.m3u.i18n.R.string
import com.m3u.ui.Destination.Root.Setting.SettingFragment
Expand Down Expand Up @@ -138,18 +123,20 @@ private fun SettingScreen(

val colorArgb = pref.colorArgb

var fragment: SettingFragment by remember(targetFragment) {
observableStateOf(targetFragment) {
helper.title = when (it) {
SettingFragment.Root -> rootTitle
SettingFragment.Playlists -> playlistTitle
SettingFragment.Scripts -> scriptTitle
SettingFragment.Appearance -> appearanceTitle
}.title()
helper.deep += when (it) {
SettingFragment.Root -> -1
else -> 1
}
var fragment: SettingFragment by rememberSaveable(targetFragment) {
mutableStateOf(targetFragment)
}

LaunchedEffect(fragment) {
helper.title = when (fragment) {
SettingFragment.Root -> rootTitle
SettingFragment.Playlists -> playlistTitle
SettingFragment.Scripts -> scriptTitle
SettingFragment.Appearance -> appearanceTitle
}.title()
helper.deep += when (fragment) {
SettingFragment.Root -> -1
else -> 1
}
}

Expand All @@ -162,22 +149,12 @@ private fun SettingScreen(
)
}
val scaffoldState = calculateListDetailPaneScaffoldState(
currentPaneDestination = currentPaneDestination,
scaffoldDirective = calculateStandardPaneScaffoldDirective(
WindowAdaptiveInfo(
WindowSizeClass.calculateFromSize(
with(LocalDensity.current) {
currentWindowSize().toSize().toDpSize()
}
),
calculatePosture(collectFoldingFeaturesAsState().value)
),
HingePolicy.NeverAvoid
)
currentPaneDestination = currentPaneDestination
)

ListDetailPaneScaffold(
scaffoldState = scaffoldState,
windowInsets = WindowInsets(0),
listPane = {
PreferencesFragment(
fragment = fragment,
Expand Down Expand Up @@ -255,59 +232,3 @@ private fun SettingScreen(
currentPaneDestination = ListDetailPaneScaffoldRole.List
}
}

private fun calculateStandardPaneScaffoldDirective(
windowAdaptiveInfo: WindowAdaptiveInfo,
verticalHingePolicy: HingePolicy = HingePolicy.AvoidSeparating
): PaneScaffoldDirective {
val maxHorizontalPartitions: Int
val contentPadding: PaddingValues
val verticalSpacerSize: Dp
when (windowAdaptiveInfo.windowSizeClass.widthSizeClass) {
WindowWidthSizeClass.Compact -> {
maxHorizontalPartitions = 1
contentPadding = PaddingValues(0.dp)
verticalSpacerSize = 0.dp
}

WindowWidthSizeClass.Medium -> {
maxHorizontalPartitions = 1
contentPadding = PaddingValues(0.dp)
verticalSpacerSize = 0.dp
}

else -> {
maxHorizontalPartitions = 2
contentPadding = PaddingValues(0.dp)
verticalSpacerSize = 0.dp
}
}
val maxVerticalPartitions: Int
val horizontalSpacerSize: Dp

if (windowAdaptiveInfo.windowPosture.isTabletop) {
maxVerticalPartitions = 2
horizontalSpacerSize = 24.dp
} else {
maxVerticalPartitions = 1
horizontalSpacerSize = 0.dp
}

return PaneScaffoldDirective(
contentPadding,
maxHorizontalPartitions,
verticalSpacerSize,
maxVerticalPartitions,
horizontalSpacerSize,
getExcludedVerticalBounds(windowAdaptiveInfo.windowPosture, verticalHingePolicy)
)
}

private fun getExcludedVerticalBounds(posture: Posture, hingePolicy: HingePolicy): List<Rect> {
return when (hingePolicy) {
HingePolicy.AvoidSeparating -> posture.separatingVerticalHingeBounds
HingePolicy.AvoidOccluding -> posture.occludingVerticalHingeBounds
HingePolicy.AlwaysAvoid -> posture.allVerticalHingeBounds
else -> emptyList()
}
}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ androidx-core-splashscreen = "1.0.1"
androidx-appcompat = "1.6.1"
androidx-activity = "1.8.2"
androidx-lifecycle = "2.7.0"
androidx-compose = "1.5.4"
androidx-compose = "1.6.0-rc01"
androidx-compose-material3 = "1.2.0-beta02"
androidx-compose-material3-adaptive = "1.0.0-alpha04"
androidx-compose-material3-adaptive-navigation-suite = "1.0.0-alpha02"
Expand Down

0 comments on commit 17230cd

Please sign in to comment.