Skip to content

Commit 215b039

Browse files
committed
fix coding style
1 parent 0d913df commit 215b039

File tree

9 files changed

+167
-189
lines changed

9 files changed

+167
-189
lines changed

.editorconfig

+2
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ ktlint_standard_function-naming = disabled
1414
ktlint_standard_string-template-indent = disabled # due to multiline-expression-wrapping dependency
1515
ktlint_standard_class-signature = disabled # due to discouraged-comment-location
1616
ktlint_standard_if-else-wrapping = disabled # due to discouraged-comment-location
17+
ktlint_standard_no-empty-first-line-in-class-body = disabled # I don't like it
18+
ktlint_standard_function-expression-body = disabled # I don't like it

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ plugins {
99
id("org.jetbrains.kotlin.android") version "1.9.23" apply false
1010
id("org.jetbrains.kotlin.plugin.serialization") version "1.9.23" apply false
1111
id("org.jetbrains.kotlinx.binary-compatibility-validator") version "0.14.0" apply false
12-
id("org.jmailen.kotlinter") version "4.1.1" apply false
12+
id("org.jmailen.kotlinter") version "4.3.0" apply false
1313
id("com.android.application") version "8.3.0" apply false
1414
id("com.vanniktech.maven.publish.base") version "0.27.0" apply false
1515
}

demo/src/main/kotlin/dev/hrach/navigation/demo/App.kt

+1-11
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ import androidx.compose.material3.NavigationBarItem
1313
import androidx.compose.material3.Scaffold
1414
import androidx.compose.material3.Text
1515
import androidx.compose.runtime.Composable
16-
import androidx.compose.runtime.SideEffect
1716
import androidx.compose.runtime.getValue
1817
import androidx.compose.runtime.remember
1918
import androidx.compose.ui.Modifier
20-
import androidx.compose.ui.graphics.luminance
2119
import androidx.compose.ui.graphics.vector.ImageVector
2220
import androidx.navigation.NavGraph.Companion.findStartDestination
2321
import androidx.navigation.NavHostController
@@ -29,15 +27,6 @@ import dev.hrach.navigation.modalsheet.ModalSheetNavigator
2927
@Composable
3028
public fun App() {
3129
MaterialTheme {
32-
@Suppress("UNUSED_VARIABLE")
33-
val isLight = MaterialTheme.colorScheme.background.luminance() > 0.5
34-
SideEffect {
35-
// systemUiController.setSystemBarsColor(
36-
// color = Color.Transparent,
37-
// darkIcons = isLight,
38-
// )
39-
}
40-
4130
val modalSheetNavigator = remember { ModalSheetNavigator() }
4231
val bottomSheetNavigator = remember { BottomSheetNavigator() }
4332
val navController = rememberNavController(modalSheetNavigator, bottomSheetNavigator)
@@ -84,6 +73,7 @@ private val Items = listOf(
8473
@Composable
8574
private fun BottomBar(navController: NavHostController) {
8675
val navBackStackEntry by navController.currentBackStackEntryAsState()
76+
8777
@Suppress("UNUSED_VARIABLE")
8878
val currentDestination = navBackStackEntry?.destination
8979

demo/src/main/kotlin/dev/hrach/navigation/demo/NavHost.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import androidx.compose.runtime.Composable
44
import androidx.navigation.NavHostController
55
import androidx.navigation.compose.NavHost
66
import androidx.navigation.compose.composable
7-
import dev.hrach.navigation.bottomsheet.BottomSheetNavigator
87
import dev.hrach.navigation.bottomsheet.BottomSheetHost
8+
import dev.hrach.navigation.bottomsheet.BottomSheetNavigator
99
import dev.hrach.navigation.bottomsheet.bottomSheet
10-
import dev.hrach.navigation.modalsheet.ModalSheetHost
11-
import dev.hrach.navigation.modalsheet.ModalSheetNavigator
1210
import dev.hrach.navigation.demo.screens.BottomSheet
1311
import dev.hrach.navigation.demo.screens.Home
1412
import dev.hrach.navigation.demo.screens.List
1513
import dev.hrach.navigation.demo.screens.Modal1
1614
import dev.hrach.navigation.demo.screens.Modal2
1715
import dev.hrach.navigation.demo.screens.Profile
16+
import dev.hrach.navigation.modalsheet.ModalSheetHost
17+
import dev.hrach.navigation.modalsheet.ModalSheetNavigator
1818
import dev.hrach.navigation.modalsheet.modalSheet
1919

2020
@Composable

modalsheet/src/main/kotlin/dev/hrach/navigation/modalsheet/ModalSheetDialog.kt

+110-114
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ internal fun ModalSheetDialog(
6161
val currentContent by rememberUpdatedState(content)
6262
val dialogId = rememberSaveable { UUID.randomUUID() }
6363
val dialog = remember(view, density) {
64-
ModalSheetDialogLayout.ModalSheetDialogWrapper(
64+
ModalSheetDialogWrapper(
6565
onPredictiveBack,
6666
view,
6767
securePolicy,
@@ -120,132 +120,128 @@ private class ModalSheetDialogLayout(
120120
PredictiveBackHandler { onPredictiveBack(it) }
121121
content()
122122
}
123+
}
123124

124-
// Fork of androidx.compose.ui.window.DialogWrapper.
125-
// predictiveBackProgress and scope params added for predictive back implementation.
126-
// EdgeToEdgeFloatingDialogWindowTheme provided to allow theme to extend into status bar.
127-
class ModalSheetDialogWrapper(
128-
private var onPredictiveBack: suspend (Flow<BackEventCompat>) -> Unit,
129-
private val composeView: View,
130-
securePolicy: SecureFlagPolicy,
131-
layoutDirection: LayoutDirection,
132-
density: Density,
133-
dialogId: UUID,
134-
) : ComponentDialog(
135-
ContextThemeWrapper(
136-
composeView.context,
137-
R.style.EdgeToEdgeFloatingDialogWindowTheme,
138-
),
139-
), ViewRootForInspector {
140-
private val dialogLayout: ModalSheetDialogLayout
125+
// Fork of androidx.compose.ui.window.DialogWrapper.
126+
// predictiveBackProgress and scope params added for predictive back implementation.
127+
// EdgeToEdgeFloatingDialogWindowTheme provided to allow theme to extend into status bar.
128+
internal class ModalSheetDialogWrapper(
129+
private var onPredictiveBack: suspend (Flow<BackEventCompat>) -> Unit,
130+
private val composeView: View,
131+
securePolicy: SecureFlagPolicy,
132+
layoutDirection: LayoutDirection,
133+
density: Density,
134+
dialogId: UUID,
135+
) : ComponentDialog(ContextThemeWrapper(composeView.context, R.style.EdgeToEdgeFloatingDialogWindowTheme)),
136+
ViewRootForInspector {
137+
private val dialogLayout: ModalSheetDialogLayout
141138

142-
// On systems older than Android S, there is a bug in the surface insets matrix math used by
143-
// elevation, so high values of maxSupportedElevation break accessibility services: b/232788477.
144-
private val maxSupportedElevation = 8.dp
145-
override val subCompositionView: AbstractComposeView get() = dialogLayout
139+
// On systems older than Android S, there is a bug in the surface insets matrix math used by
140+
// elevation, so high values of maxSupportedElevation break accessibility services: b/232788477.
141+
private val maxSupportedElevation = 8.dp
142+
override val subCompositionView: AbstractComposeView get() = dialogLayout
146143

147-
init {
148-
val window = window ?: error("Dialog has no window")
149-
window.requestFeature(Window.FEATURE_NO_TITLE)
150-
window.setBackgroundDrawableResource(android.R.color.transparent)
151-
WindowCompat.setDecorFitsSystemWindows(window, false)
152-
dialogLayout = ModalSheetDialogLayout(
153-
context,
154-
window,
155-
onPredictiveBack,
156-
).apply {
157-
// Set unique id for AbstractComposeView. This allows state restoration for the state
158-
// defined inside the Dialog via rememberSaveable()
159-
setTag(R.id.compose_view_saveable_id_tag, "Dialog:$dialogId")
160-
// Enable children to draw their shadow by not clipping them
161-
clipChildren = false
162-
// Allocate space for elevation
163-
with(density) { elevation = maxSupportedElevation.toPx() }
164-
// Simple outline to force window manager to allocate space for shadow.
165-
// Note that the outline affects clickable area for the dismiss listener. In case of
166-
// shapes like circle the area for dismiss might be to small (rectangular outline
167-
// consuming clicks outside of the circle).
168-
outlineProvider = object : ViewOutlineProvider() {
169-
override fun getOutline(view: View, result: Outline) {
170-
result.setRect(0, 0, view.width, view.height)
171-
// We set alpha to 0 to hide the view's shadow and let the composable to draw
172-
// its own shadow. This still enables us to get the extra space needed in the
173-
// surface.
174-
result.alpha = 0f
175-
}
144+
init {
145+
val window = window ?: error("Dialog has no window")
146+
window.requestFeature(Window.FEATURE_NO_TITLE)
147+
window.setBackgroundDrawableResource(android.R.color.transparent)
148+
WindowCompat.setDecorFitsSystemWindows(window, false)
149+
dialogLayout = ModalSheetDialogLayout(
150+
context,
151+
window,
152+
onPredictiveBack,
153+
).apply {
154+
// Set unique id for AbstractComposeView. This allows state restoration for the state
155+
// defined inside the Dialog via rememberSaveable()
156+
setTag(R.id.compose_view_saveable_id_tag, "Dialog:$dialogId")
157+
// Enable children to draw their shadow by not clipping them
158+
clipChildren = false
159+
// Allocate space for elevation
160+
with(density) { elevation = maxSupportedElevation.toPx() }
161+
// Simple outline to force window manager to allocate space for shadow.
162+
// Note that the outline affects clickable area for the dismiss listener. In case of
163+
// shapes like circle the area for dismiss might be to small (rectangular outline
164+
// consuming clicks outside of the circle).
165+
outlineProvider = object : ViewOutlineProvider() {
166+
override fun getOutline(view: View, result: Outline) {
167+
result.setRect(0, 0, view.width, view.height)
168+
// We set alpha to 0 to hide the view's shadow and let the composable to draw
169+
// its own shadow. This still enables us to get the extra space needed in the
170+
// surface.
171+
result.alpha = 0f
176172
}
177173
}
178-
// Clipping logic removed because we are spanning edge to edge.
179-
setContentView(dialogLayout)
180-
dialogLayout.setViewTreeLifecycleOwner(composeView.findViewTreeLifecycleOwner())
181-
dialogLayout.setViewTreeViewModelStoreOwner(composeView.findViewTreeViewModelStoreOwner())
182-
dialogLayout.setViewTreeSavedStateRegistryOwner(
183-
composeView.findViewTreeSavedStateRegistryOwner(),
184-
)
185-
dialogLayout.setViewTreeOnBackPressedDispatcherOwner(this)
186-
// Initial setup
187-
updateParameters(onPredictiveBack, securePolicy, layoutDirection)
188-
WindowCompat.getInsetsController(window, window.decorView).apply {
189-
isAppearanceLightStatusBars = true
190-
isAppearanceLightNavigationBars = true
191-
}
192174
}
193-
194-
private fun setLayoutDirection(layoutDirection: LayoutDirection) {
195-
dialogLayout.layoutDirection = when (layoutDirection) {
196-
LayoutDirection.Ltr -> android.util.LayoutDirection.LTR
197-
LayoutDirection.Rtl -> android.util.LayoutDirection.RTL
198-
}
175+
// Clipping logic removed because we are spanning edge to edge.
176+
setContentView(dialogLayout)
177+
dialogLayout.setViewTreeLifecycleOwner(composeView.findViewTreeLifecycleOwner())
178+
dialogLayout.setViewTreeViewModelStoreOwner(composeView.findViewTreeViewModelStoreOwner())
179+
dialogLayout.setViewTreeSavedStateRegistryOwner(
180+
composeView.findViewTreeSavedStateRegistryOwner(),
181+
)
182+
dialogLayout.setViewTreeOnBackPressedDispatcherOwner(this)
183+
// Initial setup
184+
updateParameters(onPredictiveBack, securePolicy, layoutDirection)
185+
WindowCompat.getInsetsController(window, window.decorView).apply {
186+
isAppearanceLightStatusBars = true
187+
isAppearanceLightNavigationBars = true
199188
}
189+
}
200190

201-
fun setContent(parentComposition: CompositionContext, children: @Composable () -> Unit) {
202-
dialogLayout.setContent(parentComposition, children)
191+
private fun setLayoutDirection(layoutDirection: LayoutDirection) {
192+
dialogLayout.layoutDirection = when (layoutDirection) {
193+
LayoutDirection.Ltr -> android.util.LayoutDirection.LTR
194+
LayoutDirection.Rtl -> android.util.LayoutDirection.RTL
203195
}
196+
}
204197

205-
private fun setSecurePolicy(securePolicy: SecureFlagPolicy) {
206-
val secureFlagEnabled =
207-
securePolicy.shouldApplySecureFlag(composeView.isFlagSecureEnabled())
208-
window!!.setFlags(
209-
if (secureFlagEnabled) {
210-
WindowManager.LayoutParams.FLAG_SECURE
211-
} else {
212-
WindowManager.LayoutParams.FLAG_SECURE.inv()
213-
},
214-
WindowManager.LayoutParams.FLAG_SECURE,
215-
)
216-
}
198+
fun setContent(parentComposition: CompositionContext, children: @Composable () -> Unit) {
199+
dialogLayout.setContent(parentComposition, children)
200+
}
217201

218-
fun updateParameters(
219-
onPredictiveBack: suspend (Flow<BackEventCompat>) -> Unit,
220-
securePolicy: SecureFlagPolicy,
221-
layoutDirection: LayoutDirection,
222-
) {
223-
this.onPredictiveBack = onPredictiveBack
224-
setSecurePolicy(securePolicy)
225-
setLayoutDirection(layoutDirection)
226-
// Window flags to span parent window.
227-
window?.setLayout(
228-
WindowManager.LayoutParams.MATCH_PARENT,
229-
WindowManager.LayoutParams.MATCH_PARENT,
230-
)
231-
window?.setSoftInputMode(
232-
if (Build.VERSION.SDK_INT >= 30) {
233-
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING
234-
} else {
235-
@Suppress("DEPRECATION")
236-
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
237-
},
238-
)
239-
}
202+
private fun setSecurePolicy(securePolicy: SecureFlagPolicy) {
203+
val secureFlagEnabled =
204+
securePolicy.shouldApplySecureFlag(composeView.isFlagSecureEnabled())
205+
window!!.setFlags(
206+
if (secureFlagEnabled) {
207+
WindowManager.LayoutParams.FLAG_SECURE
208+
} else {
209+
WindowManager.LayoutParams.FLAG_SECURE.inv()
210+
},
211+
WindowManager.LayoutParams.FLAG_SECURE,
212+
)
213+
}
240214

241-
fun disposeComposition() {
242-
dialogLayout.disposeComposition()
243-
}
215+
fun updateParameters(
216+
onPredictiveBack: suspend (Flow<BackEventCompat>) -> Unit,
217+
securePolicy: SecureFlagPolicy,
218+
layoutDirection: LayoutDirection,
219+
) {
220+
this.onPredictiveBack = onPredictiveBack
221+
setSecurePolicy(securePolicy)
222+
setLayoutDirection(layoutDirection)
223+
// Window flags to span parent window.
224+
window?.setLayout(
225+
WindowManager.LayoutParams.MATCH_PARENT,
226+
WindowManager.LayoutParams.MATCH_PARENT,
227+
)
228+
window?.setSoftInputMode(
229+
if (Build.VERSION.SDK_INT >= 30) {
230+
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING
231+
} else {
232+
@Suppress("DEPRECATION")
233+
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
234+
},
235+
)
236+
}
244237

245-
override fun cancel() {
246-
// Prevents the dialog from dismissing itself
247-
return
248-
}
238+
fun disposeComposition() {
239+
dialogLayout.disposeComposition()
240+
}
241+
242+
override fun cancel() {
243+
// Prevents the dialog from dismissing itself
244+
return
249245
}
250246
}
251247

modalsheet/src/main/kotlin/dev/hrach/navigation/modalsheet/ModalSheetHost.kt

+19-19
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ import kotlinx.coroutines.CancellationException
4040
public fun ModalSheetHost(
4141
modalSheetNavigator: ModalSheetNavigator,
4242
modifier: Modifier = Modifier,
43-
enterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() ->
44-
@JvmSuppressWildcards EnterTransition) = { fadeIn(animationSpec = tween(700)) },
45-
exitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() ->
46-
@JvmSuppressWildcards ExitTransition) = { fadeOut(animationSpec = tween(700)) },
47-
popEnterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() ->
48-
@JvmSuppressWildcards EnterTransition) = enterTransition,
49-
popExitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() ->
50-
@JvmSuppressWildcards ExitTransition) = exitTransition,
51-
sizeTransform: (AnimatedContentTransitionScope<NavBackStackEntry>.() ->
52-
@JvmSuppressWildcards SizeTransform?)? = null,
43+
enterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> @JvmSuppressWildcards EnterTransition) =
44+
{ fadeIn(animationSpec = tween(700)) },
45+
exitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> @JvmSuppressWildcards ExitTransition) =
46+
{ fadeOut(animationSpec = tween(700)) },
47+
popEnterTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> @JvmSuppressWildcards EnterTransition) =
48+
enterTransition,
49+
popExitTransition: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> @JvmSuppressWildcards ExitTransition) =
50+
exitTransition,
51+
sizeTransform: (AnimatedContentTransitionScope<NavBackStackEntry>.() -> @JvmSuppressWildcards SizeTransform?)? =
52+
null,
5353
) {
5454
val modalBackStack by modalSheetNavigator.backStack.collectAsState(listOf())
5555

@@ -94,8 +94,7 @@ public fun ModalSheetHost(
9494
} ?: exitTransition.invoke(this)
9595
}
9696
}
97-
val finalSizeTransform:
98-
AnimatedContentTransitionScope<NavBackStackEntry>.() -> SizeTransform? = {
97+
val finalSizeTransform: AnimatedContentTransitionScope<NavBackStackEntry>.() -> SizeTransform? = {
9998
val targetDestination = targetState.destination as ModalSheetNavigator.Destination
10099

101100
targetDestination.hierarchy.firstNotNullOfOrNull { destination ->
@@ -104,12 +103,10 @@ public fun ModalSheetHost(
104103
}
105104

106105
val transition = updateTransition(backStackEntry, label = "entry")
107-
if (!(
108-
transition.currentState == transition.targetState
109-
&& transition.currentState == null
110-
&& backStackEntry == null
111-
)
112-
) {
106+
val nothingToShow = transition.currentState == transition.targetState &&
107+
transition.currentState == null &&
108+
backStackEntry == null
109+
if (!nothingToShow) {
113110
val securePolicy = (backStackEntry?.destination as? ModalSheetNavigator.Destination)
114111
?.securePolicy
115112
?: SecureFlagPolicy.Inherit
@@ -163,7 +160,10 @@ public fun ModalSheetHost(
163160
@Suppress("UNCHECKED_CAST")
164161
this as AnimatedContentTransitionScope<NavBackStackEntry>
165162
ContentTransform(
166-
finalEnter(this), finalExit(this), targetZIndex, finalSizeTransform(this),
163+
targetContentEnter = finalEnter(this),
164+
initialContentExit = finalExit(this),
165+
targetContentZIndex = targetZIndex,
166+
sizeTransform = finalSizeTransform(this),
167167
)
168168
},
169169
) { currentEntry ->

0 commit comments

Comments
 (0)