Gitnuro/src/main/kotlin/com/jetpackduba/gitnuro/ui/Stashes.kt
2022-10-22 15:35:32 +02:00

70 lines
2.2 KiB
Kotlin

package com.jetpackduba.gitnuro.ui
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.res.painterResource
import com.jetpackduba.gitnuro.ui.components.SideMenuPanel
import com.jetpackduba.gitnuro.ui.components.SideMenuSubentry
import com.jetpackduba.gitnuro.ui.components.gitnuroViewModel
import com.jetpackduba.gitnuro.ui.context_menu.ContextMenu
import com.jetpackduba.gitnuro.ui.context_menu.ContextMenuElement
import com.jetpackduba.gitnuro.ui.context_menu.stashesContextMenuItems
import com.jetpackduba.gitnuro.viewmodels.StashStatus
import com.jetpackduba.gitnuro.viewmodels.StashesViewModel
import org.eclipse.jgit.revwalk.RevCommit
@Composable
fun Stashes(
stashesViewModel: StashesViewModel = gitnuroViewModel(),
) {
val stashStatusState = stashesViewModel.stashStatus.collectAsState()
val stashStatus = stashStatusState.value
val isExpanded by stashesViewModel.isExpanded.collectAsState()
val stashList = if (stashStatus is StashStatus.Loaded)
stashStatus.stashes
else
listOf()
SideMenuPanel(
title = "Stashes",
icon = painterResource("stash.svg"),
items = stashList,
isExpanded = isExpanded,
onExpand = { stashesViewModel.onExpand() },
itemContent = { stash ->
StashRow(
stash = stash,
onClick = { stashesViewModel.selectTab(stash) },
contextItems = stashesContextMenuItems(
onApply = { stashesViewModel.applyStash(stash) },
onPop = {
stashesViewModel.popStash(stash)
},
onDelete = {
stashesViewModel.deleteStash(stash)
},
)
)
}
)
}
@Composable
private fun StashRow(
stash: RevCommit,
onClick: () -> Unit,
contextItems: List<ContextMenuElement>,
) {
ContextMenu(
items = { contextItems }
) {
SideMenuSubentry(
text = stash.shortMessage,
iconResourcePath = "stash.svg",
onClick = onClick,
)
}
}