Skip to content

Commit 49bb0af

Browse files
committed
perf: 优化栈顶判断逻辑
1 parent 78e6de5 commit 49bb0af

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

insets-systembar/src/main/kotlin/com/xiaocydx/insets/systembar/SystemBarWindowEnforcer.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,17 +278,16 @@ internal class WindowStateHolder(savaStateHandle: SavedStateHandle) : ViewModel(
278278
}
279279

280280
fun applyState(who: String): WindowState? {
281-
val index = backStack.indexOf(who)
282281
val state = stateStore[who]
283282
state?.isApplied = true
284-
return if (isLastApply(index)) state else null
283+
return if (isLastState(who)) state else null
285284
}
286285

287286
fun applyPrevState(who: String): WindowState? {
288-
val index = backStack.indexOf(who)
289287
var prevState: WindowState? = null
290-
if (isLastApply(index)) {
291-
val prevWho = backStack.getOrNull(index - 1)
288+
if (isLastState(who)) {
289+
val prevIndex = backStack.lastIndex - 1
290+
val prevWho = backStack.getOrNull(prevIndex)
292291
if (prevWho != null) prevState = stateStore[prevWho]
293292
}
294293
return prevState?.takeIf { it.isApplied }
@@ -304,8 +303,8 @@ internal class WindowStateHolder(savaStateHandle: SavedStateHandle) : ViewModel(
304303
check(!isStateSaved) { "SavedStateHandle已保存,不允许再修改" }
305304
}
306305

307-
private fun isLastApply(index: Int): Boolean {
308-
return index >= 0 && index == backStack.lastIndex
306+
private fun isLastState(who: String): Boolean {
307+
return backStack.lastOrNull() == who
309308
}
310309

311310
private companion object {

0 commit comments

Comments
 (0)