Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package de.markusressel.mkdocseditor.feature.editor.ui

import de.markusressel.mkdocseditor.auth.api.ServerFeature
import de.markusressel.mkdocseditor.feature.common.data.ServerFeatures
import de.markusressel.mkdocseditor.feature.history.domain.LineAuthorUseCase
import de.markusressel.mkdocseditor.feature.history.domain.SeenChangesUseCase
import de.markusressel.mkdocseditor.feature.history.domain.changeLocation
import de.markusressel.mkdocsrestclient.history.BlameBlockModel
import de.markusressel.mkdocsrestclient.history.DiffHunkModel
import de.markusressel.mkdocsrestclient.history.DocumentChangesModel
import kotlinx.coroutines.NonCancellable
Expand Down Expand Up @@ -141,6 +143,7 @@ internal class CodeEditorViewModel @Inject constructor(
private val pendingQrKey: PendingQrKey,
private val resolveQrCode: ResolveQrCodeUseCase,
private val seenChanges: SeenChangesUseCase,
private val lineAuthor: LineAuthorUseCase,
private val serverFeatures: ServerFeatures,
savedStateHandle: SavedStateHandle,
clientInstanceId: ClientInstanceId,
Expand Down Expand Up @@ -324,7 +327,8 @@ internal class CodeEditorViewModel @Inject constructor(
launch {
val history = serverFeatures.has(ServerFeature.HISTORY)
val revert = serverFeatures.has(ServerFeature.REVERT)
_uiState.update { it.copy(historyAvailable = history, revertAvailable = revert) }
val blame = serverFeatures.has(ServerFeature.BLAME)
_uiState.update { it.copy(historyAvailable = history, revertAvailable = revert, blameAvailable = blame) }
}

// a key of a sticker without a page, to put on this one
Expand Down Expand Up @@ -517,6 +521,11 @@ internal class CodeEditorViewModel @Inject constructor(
is UiEvent.DismissDialog -> dismissCurrentDialog()
is UiEvent.ChangesSinceSeenClicked -> uiState.value.changesSinceSeen?.let { showDialog(DialogState.ChangesSinceSeen(it)) }
is UiEvent.ChangesSinceSeenDismissed -> _uiState.update { it.copy(changesSinceSeen = null) }
is UiEvent.AuthorLineClicked -> showLineAuthor(event.line)
is UiEvent.LineAuthorHistoryClicked -> {
dismissCurrentDialog()
openHistory()
}
is UiEvent.ChangeHunkClicked -> {
dismissCurrentDialog()
jumpToChange(event.hunk.changeLocation())
Expand Down Expand Up @@ -682,15 +691,29 @@ internal class CodeEditorViewModel @Inject constructor(
is TopAppBarAction.CodeEditor.ConnectionAction -> onConnectionIconClicked(action.indicator)
is TopAppBarAction.CodeEditor.PageReferencesAction -> showPageReferences()
is TopAppBarAction.CodeEditor.QrCodesAction -> showQrCodes()
is TopAppBarAction.CodeEditor.HistoryAction -> uiState.value.documentId?.let {
_navigationEvents.send(
NavigationRequest.OpenHistory(
documentId = it,
canRestore = uiState.value.canEdit,
canRevert = uiState.value.canEdit && uiState.value.revertAvailable,
)
)
}
is TopAppBarAction.CodeEditor.HistoryAction -> openHistory()
}
}

private suspend fun openHistory() {
val documentId = uiState.value.documentId ?: return
_navigationEvents.send(
NavigationRequest.OpenHistory(
documentId = documentId,
canRestore = uiState.value.canEdit,
canRevert = uiState.value.canEdit && uiState.value.revertAvailable,
canRestoreSection = uiState.value.canEdit && uiState.value.blameAvailable,
)
)
}

/** @param line 0-based */
private suspend fun showLineAuthor(line: Int) {
val documentId = uiState.value.documentId ?: return
when (val answer = lineAuthor(documentId, line)) {
is LineAuthorUseCase.Answer.Found -> showDialog(DialogState.LineAuthor(answer.block))
LineAuthorUseCase.Answer.Unknown -> showMessage(context.getString(R.string.line_author_none))
is LineAuthorUseCase.Answer.Failed -> showMessage(context.getString(answer.message))
}
}

Expand Down Expand Up @@ -1278,6 +1301,9 @@ internal class CodeEditorViewModel @Inject constructor(
/** whether the server can undo a single change of the history */
val revertAvailable: Boolean = false,

/** whether the server tells who last changed each line, and can restore a single section */
val blameAvailable: Boolean = false,

/** what others changed since the user last looked at the page, null if nothing; see [SeenChangesUseCase] */
val changesSinceSeen: DocumentChangesModel? = null,
) {
Expand All @@ -1297,6 +1323,10 @@ internal class CodeEditorViewModel @Inject constructor(
data object ChangesSinceSeenDismissed : UiEvent()
data class ChangeHunkClicked(val hunk: DiffHunkModel) : UiEvent()

/** a line of the author strip, 0-based */
data class AuthorLineClicked(val line: Int) : UiEvent()
data object LineAuthorHistoryClicked : UiEvent()

data class ItemActionClicked(val item: WikiItem, val action: ItemAction) : UiEvent()
data class ItemMovePreviewRequested(val parentId: String?, val name: String?) : UiEvent()
data object ItemMoveBackClicked : UiEvent()
Expand Down Expand Up @@ -1380,7 +1410,12 @@ internal class CodeEditorViewModel @Inject constructor(
sealed interface NavigationRequest {
data class Login(val backendId: Long) : NavigationRequest
data class OpenDocument(val documentId: String) : NavigationRequest
data class OpenHistory(val documentId: String, val canRestore: Boolean, val canRevert: Boolean) : NavigationRequest
data class OpenHistory(
val documentId: String,
val canRestore: Boolean,
val canRevert: Boolean,
val canRestoreSection: Boolean,
) : NavigationRequest
/** leave the editor, e.g. to show a folder in the file browser */
data object Close : NavigationRequest
data class ShareLink(val link: String) : NavigationRequest {
Expand Down Expand Up @@ -1414,6 +1449,9 @@ internal class CodeEditorViewModel @Inject constructor(
/** what changed since the last visit, hunk by hunk */
data class ChangesSinceSeen(val changes: DocumentChangesModel) : DialogState

/** who last changed a block of lines, and when */
data class LineAuthor(val block: BlameBlockModel) : DialogState

/**
* The QR codes of the page, and adding or removing them
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,19 @@ internal fun authorColor(author: String, knownColors: Map<String, String?>): Col
*
* @param lines the shares of each line
* @param widthPx the width of the strip
* @param onLineTapped gets the 0-based line; false places the cursor like any tap on the bar
*/
internal class AuthorStripColumn(
private val lines: List<List<LineShare>>,
private val widthPx: Int,
private val colorOf: (String) -> Color,
private val onLineTapped: (Int) -> Boolean = { false },
) : KodeEditorGutterColumn {

override fun width(lineCount: Int) = widthPx

override fun onTap(line: Int): Boolean = onLineTapped(line)

override fun DrawScope.drawCell(cell: GutterCell) {
var top = 0f
lines.getOrNull(cell.line)?.forEach { share ->
Expand All @@ -67,6 +71,14 @@ internal class AuthorStripColumn(
}
}

/** [column] as it is, with [onLineTapped] for taps on it */
internal class TappableGutterColumn(
private val column: KodeEditorGutterColumn,
private val onLineTapped: (Int) -> Boolean,
) : KodeEditorGutterColumn by column {
override fun onTap(line: Int): Boolean = onLineTapped(line)
}

/** A light marker on the text of each author */
@Composable
internal fun BoxScope.AuthorMarkers(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ internal fun CodeEditorLayout(
restoreToCursor: Boolean = false,
changeJump: CodeEditorViewModel.ChangeJump? = null,
onChangeJumpShown: (CodeEditorViewModel.ChangeJump) -> Unit = {},
onAuthorLineClicked: ((Int) -> Unit)? = null,
) {
val viewState = rememberKodeEditorViewState()
val currentOnVisibleRangeChanged by rememberUpdatedState(onVisibleRangeChanged)
Expand Down Expand Up @@ -238,13 +239,19 @@ internal fun CodeEditorLayout(
val colorOf: (String) -> Color = { authorColor(it, authorColors()) }
val stripWidth = with(LocalDensity.current) { AUTHOR_STRIP_WIDTH.roundToPx() }
val runs = authorRuns.value
val currentOnAuthorLineClicked by rememberUpdatedState(onAuthorLineClicked)
val onGutterTapped: (Int) -> Boolean = { line -> currentOnAuthorLineClicked?.let { it(line); true } ?: false }
val authorStrip = if (settings.authorHighlight != AuthorHighlightMode.Off && runs.runs.isNotEmpty()) {
remember(runs, stripWidth) {
AuthorStripColumn(lineShares(state.text, runs.runs), stripWidth, colorOf)
AuthorStripColumn(lineShares(state.text, runs.runs), stripWidth, colorOf, onGutterTapped)
}
} else {
null
}
// the strip is too narrow to hit, so the line numbers next to it tell who wrote a line as well
val numbers = lineNumbers.takeIf { settings.lineNumbers }?.let { numbers ->
if (authorStrip == null) numbers else remember(numbers) { TappableGutterColumn(numbers, onGutterTapped) }
}

CompositionLocalProvider(
LocalTextSelectionColors provides TextSelectionColors(
Expand Down Expand Up @@ -275,7 +282,7 @@ internal fun CodeEditorLayout(
enabled = true,
readOnly = readOnly,
lineWrapping = settings.lineWrapping,
gutterColumns = listOfNotNull(lineNumbers.takeIf { settings.lineNumbers }, authorStrip),
gutterColumns = listOfNotNull(numbers, authorStrip),
overlay = { layout ->
changeHighlight?.let { ChangeHighlight(layout, it, highlightColor, { changeHighlightAlpha.value }) }
if (settings.authorHighlight == AuthorHighlightMode.StripAndMarker) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import androidx.compose.ui.res.stringResource
import de.markusressel.mkdocseditor.R
import de.markusressel.mkdocseditor.feature.qr.domain.QrLink
import de.markusressel.mkdocseditor.feature.history.ui.compose.ChangesSinceSeenSheet
import de.markusressel.mkdocseditor.feature.history.ui.compose.LineAuthorSheet
import de.markusressel.mkdocseditor.feature.history.ui.compose.HistoryScreen
import de.markusressel.mkdocseditor.feature.qr.ui.compose.PageQrCodesDialog

Expand Down Expand Up @@ -107,7 +108,12 @@ internal data class CodeEditorScreen(

is CodeEditorViewModel.NavigationRequest.OpenHistory ->
navigator.push(
HistoryScreen(documentId = request.documentId, canRestore = request.canRestore, canRevert = request.canRevert)
HistoryScreen(
documentId = request.documentId,
canRestore = request.canRestore,
canRevert = request.canRevert,
canRestoreSection = request.canRestoreSection,
)
)

is CodeEditorViewModel.NavigationRequest.Close -> navigator.pop()
Expand Down Expand Up @@ -200,6 +206,13 @@ internal data class CodeEditorScreen(
onDismiss = { viewModel.onUiEvent(CodeEditorViewModel.UiEvent.DismissDialog) },
)

is CodeEditorViewModel.DialogState.LineAuthor -> LineAuthorSheet(
block = dialogState.block,
onOpenHistory = { viewModel.onUiEvent(CodeEditorViewModel.UiEvent.LineAuthorHistoryClicked) }
.takeIf { uiState.historyAvailable },
onDismiss = { viewModel.onUiEvent(CodeEditorViewModel.UiEvent.DismissDialog) },
)

is CodeEditorViewModel.DialogState.PageReferencesPanel -> if (tree != null && documentId != null) {
PageReferencesDialog(
tree = tree!!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ internal fun CodeEditorScreenContent(
// the editor goes to a change while it is shown, else the page while reading
changeJump = uiState.changeJump,
onChangeJumpShown = { onUiEvent(CodeEditorViewModel.UiEvent.ChangeJumpShown(it)) },
onAuthorLineClicked = { line: Int -> onUiEvent(CodeEditorViewModel.UiEvent.AuthorLineClicked(line)) }
.takeIf { uiState.blameAvailable && uiState.documentId != null },
)
},
preview = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package de.markusressel.mkdocseditor.feature.history.domain

import androidx.annotation.StringRes
import com.github.kittinunf.result.Result
import de.markusressel.mkdocseditor.feature.common.ui.userMessage
import de.markusressel.mkdocsrestclient.IMkDocsRestClient
import de.markusressel.mkdocsrestclient.history.BlameBlockModel
import timber.log.Timber
import javax.inject.Inject

/**
* Who last changed a line of a page and when, from the blame of the server (`GET /document/{id}/blame/`). It is asked
* for each time, because the page changes while it is open.
*/
internal class LineAuthorUseCase @Inject constructor(
private val restClient: IMkDocsRestClient,
) {
sealed interface Answer {
/** the lines around [line] that were changed together */
data class Found(val block: BlameBlockModel) : Answer

/** the line is newer than the text the server knows, or nobody known wrote it */
data object Unknown : Answer

data class Failed(@param:StringRes val message: Int) : Answer
}

/** @param line 0-based, as in the editor */
suspend operator fun invoke(documentId: String, line: Int): Answer =
when (val result = restClient.getDocumentBlame(documentId)) {
is Result.Success -> result.value.blockAt(line + 1)
?.takeIf { it.author != null }
?.let { Answer.Found(it) }
?: Answer.Unknown
is Result.Failure -> {
Timber.w(result.error, "blame of %s", documentId)
Answer.Failed(result.error.userMessage())
}
}
}
Loading
Loading