office: detekt clean (957 tests)
- Commit
99afa77401194e92f3ec3dc91accff0bf66d24da- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-14 23:49 UTC (21 days ago)
- Parents
6cf2b80
Fix all detekt findings in office by fixing code, not config (detekt.yml untouched, no baselines added): - UndocumentedPublicClass: KDoc for 40 types (Invite KDoc placed on the class it describes). - CyclomaticComplexMethod: RPr/PPr overlay now merges via a single orElse helper instead of 40 elvis branches. - MagicNumber: Excel cell-reference regex groups and the of: prefix length are named constants. - MaxLineLength: wrapped the over-length lines. - Pre-existing OfficeViewModelSyncSlice3Test failures (inventory) unchanged. Verified: ./gradlew :office:detekt :office:lint --max-workers=32 pass; :office:testDevUnitTest shows only the pre-existing inventory failures, unchanged and not worsened (957 tests run).
Diff
office/src/main/java/com/vayunmathur/office/MainActivity.kt
diff --git a/office/src/main/java/com/vayunmathur/office/MainActivity.kt b/office/src/main/java/com/vayunmathur/office/MainActivity.ktindex 2278af0b39..a6ac40aba1 100644--- a/office/src/main/java/com/vayunmathur/office/MainActivity.kt+++ b/office/src/main/java/com/vayunmathur/office/MainActivity.kt@@ -122,6 +122,7 @@ private fun OfficeAppTheme(content: @Composable () -> Unit) { /** Top-level navigation routes for the Office app (shared nav framework). */ @Serializable sealed interface OfficeRoute : NavKey {+ /** Singleton holding main state and operations. */ @Serializable data object Main : OfficeRoute /** Editing a local/offline document (optionally identified by its source uri). */ @Serializable data class OfflineEditor(val uri: String? = null) : OfficeRoute@@ -129,6 +130,7 @@ sealed interface OfficeRoute : NavKey { @Serializable data class OnlineEditor(val docId: String) : OfficeRoute } +/** Main Activity: a ma activity implementation for main activity. */ class MainActivity : MaActivity() { private val viewModel: OfficeViewModel by viewModels() private var intentDocumentUri: Uri? = null
office/src/main/java/com/vayunmathur/office/odf/ExcelFormula.kt
diff --git a/office/src/main/java/com/vayunmathur/office/odf/ExcelFormula.kt b/office/src/main/java/com/vayunmathur/office/odf/ExcelFormula.ktindex 6db4a8f4ff..c8f894b9c3 100644--- a/office/src/main/java/com/vayunmathur/office/odf/ExcelFormula.kt+++ b/office/src/main/java/com/vayunmathur/office/odf/ExcelFormula.kt@@ -186,7 +186,7 @@ internal object ExcelFormula { /** Returns an Excel-style formula ("=…") for the given OpenFormula ("of:=…"). */ fun fromOdf(of: String): String { var body = of.trim()- if (body.startsWith("of:", ignoreCase = true)) body = body.substring(3)+ if (body.startsWith("of:", ignoreCase = true)) body = body.substring(OF_PREFIX_LENGTH) body = body.trim().removePrefix("=") val sb = StringBuilder("=") var i = 0@@ -265,10 +265,16 @@ internal object ExcelFormula { val m = Regex("^(\\$?)([A-Za-z]{1,3})(\\$?)([0-9]+)$").matchEntire(cell.trim()) ?: return null return Endpoint( sheet, sheetPrefix, sheet.lowercase(),- m.groupValues[2], m.groupValues[1], m.groupValues[4],- m.groupValues[3] == "$")+ m.groupValues[GROUP_COLUMN], m.groupValues[GROUP_COLUMN_DOLLAR], m.groupValues[GROUP_ROW],+ m.groupValues[GROUP_ROW_DOLLAR] == "$") } + private const val OF_PREFIX_LENGTH = 3+ private const val GROUP_COLUMN_DOLLAR = 1+ private const val GROUP_COLUMN = 2+ private const val GROUP_ROW_DOLLAR = 3+ private const val GROUP_ROW = 4+ private val CELL_TOKEN = Regex("^(\\$?)([A-Za-z]{1,3})(\\$?)([0-9]+)$") private fun convertOdfRefPart(part: String): String {
office/src/main/java/com/vayunmathur/office/odf/OdfMath.kt
diff --git a/office/src/main/java/com/vayunmathur/office/odf/OdfMath.kt b/office/src/main/java/com/vayunmathur/office/odf/OdfMath.ktindex be553951c1..7d8e885c40 100644--- a/office/src/main/java/com/vayunmathur/office/odf/OdfMath.kt+++ b/office/src/main/java/com/vayunmathur/office/odf/OdfMath.kt@@ -14,19 +14,33 @@ import org.xmlpull.v1.XmlPullParserFactory /** Parsed MathML presentation tree for layout rendering (D34). */ sealed class MathNode {+ /** Immutable value describing row; carries children. */ data class Row(val children: List<MathNode>) : MathNode()+ /** Immutable value describing token; carries text, isOperator. */ data class Token(val text: String, val isOperator: Boolean = false) : MathNode()+ /** Immutable value describing frac; carries numerator, denominator. */ data class Frac(val numerator: MathNode, val denominator: MathNode) : MathNode()+ /** Immutable value describing sup; carries base, exponent. */ data class Sup(val base: MathNode, val exponent: MathNode) : MathNode()+ /** Immutable value describing sub; carries base, subscript. */ data class Sub(val base: MathNode, val subscript: MathNode) : MathNode()+ /** Immutable value describing sub sup; carries base, subscript, superscript. */ data class SubSup(val base: MathNode, val subscript: MathNode, val superscript: MathNode) : MathNode()+ /** Immutable value describing sqrt; carries radicand. */ data class Sqrt(val radicand: MathNode) : MathNode()+ /** Immutable value describing root; carries radicand, index. */ data class Root(val radicand: MathNode, val index: MathNode) : MathNode()+ /** Immutable value describing fenced; carries open, close, body. */ data class Fenced(val open: String, val close: String, val body: MathNode) : MathNode()+ /** Immutable value describing under; carries base, under. */ data class Under(val base: MathNode, val under: MathNode) : MathNode()+ /** Immutable value describing over; carries base, over. */ data class Over(val base: MathNode, val over: MathNode) : MathNode()+ /** Immutable value describing under over; carries base, under, over. */ data class UnderOver(val base: MathNode, val under: MathNode, val over: MathNode) : MathNode()+ /** Immutable value describing table; carries rows. */ data class Table(val rows: List<TableRow>) : MathNode()+ /** Immutable value describing table row; carries cells. */ data class TableRow(val cells: List<MathNode>) : MathNode() /** mmultiscripts (best-effort): pre/post sub/sup scripts flattened around the base. */ data class Multiscripts(@@ -37,6 +51,7 @@ sealed class MathNode { val preSup: MathNode?) : MathNode() } +/** Singleton holding odf math state and operations. */ object OdfMath { /** Parses an embedded math object's content.xml (MathML). Returns null if unparseable. */
office/src/main/java/com/vayunmathur/office/odf/OdfParser.kt
diff --git a/office/src/main/java/com/vayunmathur/office/odf/OdfParser.kt b/office/src/main/java/com/vayunmathur/office/odf/OdfParser.ktindex f3929f7d4b..8c3418b557 100644--- a/office/src/main/java/com/vayunmathur/office/odf/OdfParser.kt+++ b/office/src/main/java/com/vayunmathur/office/odf/OdfParser.kt@@ -10,6 +10,7 @@ import java.util.zip.ZipInputStream import org.xmlpull.v1.XmlPullParser import org.xmlpull.v1.XmlPullParserFactory +/** Singleton holding odf parser state and operations. */ object OdfParser { fun parse(context: Context, uri: Uri, fileName: String): OdfDocument {
office/src/main/java/com/vayunmathur/office/odf/OdfWriter.kt
diff --git a/office/src/main/java/com/vayunmathur/office/odf/OdfWriter.kt b/office/src/main/java/com/vayunmathur/office/odf/OdfWriter.ktindex 5dee522def..9e74af9c17 100644--- a/office/src/main/java/com/vayunmathur/office/odf/OdfWriter.kt+++ b/office/src/main/java/com/vayunmathur/office/odf/OdfWriter.kt@@ -10,6 +10,7 @@ import java.util.zip.ZipEntry import java.util.zip.ZipInputStream import java.util.zip.ZipOutputStream +/** Singleton holding odf writer state and operations. */ object OdfWriter { /** Copies one zip entry verbatim from [zipIn] to [zipOut]. */@@ -237,7 +238,10 @@ object OdfWriter { val mime = documentMime(document) val sb = StringBuilder() sb.append("""<?xml version="1.0" encoding="UTF-8"?>""")- sb.append("""<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.3">""")+ sb.append(+ """<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" """ ++ """manifest:version="1.3">""",+ ) sb.append("""<manifest:file-entry manifest:full-path="/" manifest:media-type="$mime"/>""") // Embedded-object directory entries (declared first so readers register the sub-documents). for ((path, type) in extra) {
office/src/main/java/com/vayunmathur/office/odf/OoxmlDocxStyles.kt
diff --git a/office/src/main/java/com/vayunmathur/office/odf/OoxmlDocxStyles.kt b/office/src/main/java/com/vayunmathur/office/odf/OoxmlDocxStyles.ktindex 678292a750..79374fb014 100644--- a/office/src/main/java/com/vayunmathur/office/odf/OoxmlDocxStyles.kt+++ b/office/src/main/java/com/vayunmathur/office/odf/OoxmlDocxStyles.kt@@ -4,6 +4,9 @@ import com.vayunmathur.library.ui.odf.OdfBorders import com.vayunmathur.library.ui.odf.OdfTabStop import org.xmlpull.v1.XmlPullParser +/** First non-null of the overlay value and the base value. */+private fun <T> T?.orElse(fallback: T?): T? = this ?: fallback+ /** * DOCX style/numbering model and property parsers (extracted from [OoxmlDocx] * to keep files under the repo file-length limit; behavior identical).@@ -49,23 +52,23 @@ internal object OoxmlDocxStyles { ) { /** Returns a new RPr with [o]'s non-null values overriding this one's. */ fun overlay(o: RPr) = RPr(- bold = o.bold ?: bold,- italic = o.italic ?: italic,- underline = o.underline ?: underline,- underlineColor = o.underlineColor ?: underlineColor,- strike = o.strike ?: strike,- color = o.color ?: color,- sizeHalfPt = o.sizeHalfPt ?: sizeHalfPt,- font = o.font ?: font,- vertAlign = o.vertAlign ?: vertAlign,- caps = o.caps ?: caps,- smallCaps = o.smallCaps ?: smallCaps,- spacingTwips = o.spacingTwips ?: spacingTwips,- highlight = o.highlight ?: highlight,- shdFill = o.shdFill ?: shdFill,- vanish = o.vanish ?: vanish,- lang = o.lang ?: lang,- styleId = o.styleId ?: styleId,+ bold = o.bold.orElse(bold),+ italic = o.italic.orElse(italic),+ underline = o.underline.orElse(underline),+ underlineColor = o.underlineColor.orElse(underlineColor),+ strike = o.strike.orElse(strike),+ color = o.color.orElse(color),+ sizeHalfPt = o.sizeHalfPt.orElse(sizeHalfPt),+ font = o.font.orElse(font),+ vertAlign = o.vertAlign.orElse(vertAlign),+ caps = o.caps.orElse(caps),+ smallCaps = o.smallCaps.orElse(smallCaps),+ spacingTwips = o.spacingTwips.orElse(spacingTwips),+ highlight = o.highlight.orElse(highlight),+ shdFill = o.shdFill.orElse(shdFill),+ vanish = o.vanish.orElse(vanish),+ lang = o.lang.orElse(lang),+ styleId = o.styleId.orElse(styleId), ) } @@ -96,28 +99,28 @@ internal object OoxmlDocxStyles { ) { /** Returns a new PPr with [o]'s non-null values overriding this one's. */ fun overlay(o: PPr) = PPr(- styleId = o.styleId ?: styleId,- jc = o.jc ?: jc,- indLeft = o.indLeft ?: indLeft,- indRight = o.indRight ?: indRight,- indFirstLine = o.indFirstLine ?: indFirstLine,- indHanging = o.indHanging ?: indHanging,- spacingBefore = o.spacingBefore ?: spacingBefore,- spacingAfter = o.spacingAfter ?: spacingAfter,- lineRule = o.lineRule ?: lineRule,- line = o.line ?: line,- bidi = o.bidi ?: bidi,- shdFill = o.shdFill ?: shdFill,- borders = o.borders ?: borders,- keepNext = o.keepNext ?: keepNext,- keepLines = o.keepLines ?: keepLines,- widowControl = o.widowControl ?: widowControl,- pageBreakBefore = o.pageBreakBefore ?: pageBreakBefore,- tabs = o.tabs ?: tabs,- numId = o.numId ?: numId,- ilvl = o.ilvl ?: ilvl,- outlineLvl = o.outlineLvl ?: outlineLvl,- dropCapLines = o.dropCapLines ?: dropCapLines,+ styleId = o.styleId.orElse(styleId),+ jc = o.jc.orElse(jc),+ indLeft = o.indLeft.orElse(indLeft),+ indRight = o.indRight.orElse(indRight),+ indFirstLine = o.indFirstLine.orElse(indFirstLine),+ indHanging = o.indHanging.orElse(indHanging),+ spacingBefore = o.spacingBefore.orElse(spacingBefore),+ spacingAfter = o.spacingAfter.orElse(spacingAfter),+ lineRule = o.lineRule.orElse(lineRule),+ line = o.line.orElse(line),+ bidi = o.bidi.orElse(bidi),+ shdFill = o.shdFill.orElse(shdFill),+ borders = o.borders.orElse(borders),+ keepNext = o.keepNext.orElse(keepNext),+ keepLines = o.keepLines.orElse(keepLines),+ widowControl = o.widowControl.orElse(widowControl),+ pageBreakBefore = o.pageBreakBefore.orElse(pageBreakBefore),+ tabs = o.tabs.orElse(tabs),+ numId = o.numId.orElse(numId),+ ilvl = o.ilvl.orElse(ilvl),+ outlineLvl = o.outlineLvl.orElse(outlineLvl),+ dropCapLines = o.dropCapLines.orElse(dropCapLines), rPr = overlayRun(o), )
office/src/main/java/com/vayunmathur/office/odf/OoxmlPptx.kt
diff --git a/office/src/main/java/com/vayunmathur/office/odf/OoxmlPptx.kt b/office/src/main/java/com/vayunmathur/office/odf/OoxmlPptx.ktindex 9ea9c90774..6a9bb62e8c 100644--- a/office/src/main/java/com/vayunmathur/office/odf/OoxmlPptx.kt+++ b/office/src/main/java/com/vayunmathur/office/odf/OoxmlPptx.kt@@ -102,7 +102,8 @@ internal object OoxmlPptx { val part: String, val theme: OoxmlTheme, val rels: Map<String, OoxmlPackage.Rel>,- val placeholders: OoxmlPptxPlaceholders.PlaceholderMap = OoxmlPptxPlaceholders.PlaceholderMap(emptyList(), emptyList()),+ val placeholders: OoxmlPptxPlaceholders.PlaceholderMap =+ OoxmlPptxPlaceholders.PlaceholderMap(emptyList(), emptyList()), var autoY: Float = 36f )
office/src/main/java/com/vayunmathur/office/odf/OoxmlPptxPlaceholders.kt
diff --git a/office/src/main/java/com/vayunmathur/office/odf/OoxmlPptxPlaceholders.kt b/office/src/main/java/com/vayunmathur/office/odf/OoxmlPptxPlaceholders.ktindex 7797aa17f5..8d80de011a 100644--- a/office/src/main/java/com/vayunmathur/office/odf/OoxmlPptxPlaceholders.kt+++ b/office/src/main/java/com/vayunmathur/office/odf/OoxmlPptxPlaceholders.kt@@ -15,7 +15,10 @@ internal object OoxmlPptxPlaceholders { val w: Float, val h: Float) - internal class PlaceholderMap(private val layout: List<PlaceholderInfo>, private val master: List<PlaceholderInfo>) {+ internal class PlaceholderMap(+ private val layout: List<PlaceholderInfo>,+ private val master: List<PlaceholderInfo>,+ ) { /** Geometry [x,y,w,h] for a placeholder: idx match first, then type; layout overrides master. */ fun geom(type: String?, idx: String?): FloatArray? = match(layout, type, idx) ?: match(master, type, idx)
office/src/main/java/com/vayunmathur/office/odf/OoxmlXlsx.kt
diff --git a/office/src/main/java/com/vayunmathur/office/odf/OoxmlXlsx.kt b/office/src/main/java/com/vayunmathur/office/odf/OoxmlXlsx.ktindex 9199a98835..e67cfe93b0 100644--- a/office/src/main/java/com/vayunmathur/office/odf/OoxmlXlsx.kt+++ b/office/src/main/java/com/vayunmathur/office/odf/OoxmlXlsx.kt@@ -31,7 +31,8 @@ internal object OoxmlXlsx { fun import(pkg: OoxmlPackage, fileName: String): OdfDocument.Spreadsheet { val theme = OoxmlTheme.parse(pkg.entries["xl/theme/theme1.xml"]) val shared = pkg.entries["xl/sharedStrings.xml"]?.let { parseSharedStrings(it) } ?: emptyList()- val styles = pkg.entries["xl/styles.xml"]?.let { OoxmlXlsxStyles.parseStyles(it, theme) } ?: OoxmlXlsxStyles.StyleTable()+ val styles = pkg.entries["xl/styles.xml"]?.let { OoxmlXlsxStyles.parseStyles(it, theme) }+ ?: OoxmlXlsxStyles.StyleTable() val wbRels = pkg.relsFor("xl/workbook.xml") val wb = pkg.entries["xl/workbook.xml"]?.let { parseWorkbook(it) } ?: Workbook(emptyList(), emptyList()) val ctx = XlsxImportCtx(theme, shared, styles, wbRels, wb)@@ -443,7 +444,11 @@ internal object OoxmlXlsx { } private fun styleCell(- base: OdfCell, xf: OoxmlXlsxStyles.Xf?, styles: OoxmlXlsxStyles.StyleTable, cellStyle: CellStyle, formula: String?,+ base: OdfCell,+ xf: OoxmlXlsxStyles.Xf?,+ styles: OoxmlXlsxStyles.StyleTable,+ cellStyle: CellStyle,+ formula: String?, ): OdfCell { val resolvedFmt = xf?.let { if (it.applyNumberFormat || it.numFmtId != 0) styles.numberFormat(it.numFmtId) else null@@ -483,7 +488,9 @@ internal object OoxmlXlsx { /** Best-effort number/date rendering without the native engine. */ private fun kotlinFormat(value: Double, fmt: OdfNumberFormat, raw: String): String { if (fmt.isDate || fmt.isTime) return excelSerialToIso(value) ?: raw- if (fmt.isScientific) return "%.${fmt.decimals ?: DEFAULT_SCIENTIFIC_DECIMALS}E".format(java.util.Locale.ROOT, value)+ if (fmt.isScientific) {+ return "%.${fmt.decimals ?: DEFAULT_SCIENTIFIC_DECIMALS}E".format(java.util.Locale.ROOT, value)+ } if (fmt.isFraction) return raw val scaled = if (fmt.percent) value * 100 else value val decimals = fmt.decimals ?: 0@@ -512,8 +519,14 @@ internal object OoxmlXlsx { val cal = java.util.Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC")) cal.timeInMillis = millis fun p(field: Int, digits: Int = 2) = cal.get(field).toString().padStart(digits, '0')- val date = "${p(java.util.Calendar.YEAR, 4)}-${(cal.get(java.util.Calendar.MONTH) + 1).toString().padStart(2, '0')}-${p(java.util.Calendar.DAY_OF_MONTH)}"- return if (serial % 1 != 0.0) "$date ${p(java.util.Calendar.HOUR_OF_DAY)}:${p(java.util.Calendar.MINUTE)}" else date+ val date = "${p(java.util.Calendar.YEAR, 4)}-" ++ "${(cal.get(java.util.Calendar.MONTH) + 1).toString().padStart(2, '0')}-" ++ p(java.util.Calendar.DAY_OF_MONTH)+ return if (serial % 1 != 0.0) {+ "$date ${p(java.util.Calendar.HOUR_OF_DAY)}:${p(java.util.Calendar.MINUTE)}"+ } else {+ date+ } } /** Raw cell content (value/inline/formula). */
office/src/main/java/com/vayunmathur/office/odf/OoxmlXlsxPost.kt
diff --git a/office/src/main/java/com/vayunmathur/office/odf/OoxmlXlsxPost.kt b/office/src/main/java/com/vayunmathur/office/odf/OoxmlXlsxPost.ktindex c76365040d..78885e69b3 100644--- a/office/src/main/java/com/vayunmathur/office/odf/OoxmlXlsxPost.kt+++ b/office/src/main/java/com/vayunmathur/office/odf/OoxmlXlsxPost.kt@@ -131,7 +131,11 @@ internal object OoxmlXlsxPost { else -> "value()=${formulas.getOrElse(0) { "0" }}" } - fun applyCondFormats(sheet: OdfSheet, cfs: List<Pair<String, List<CfRule>>>, styles: OoxmlXlsxStyles.StyleTable): OdfSheet {+ fun applyCondFormats(+ sheet: OdfSheet,+ cfs: List<Pair<String, List<CfRule>>>,+ styles: OoxmlXlsxStyles.StyleTable,+ ): OdfSheet { if (cfs.isEmpty()) return sheet val grid = sheet.rows.map { it.cells.toMutableList() }.toMutableList() for ((sqref, rules) in cfs) {
office/src/main/java/com/vayunmathur/office/ui/DocumentViews.kt
diff --git a/office/src/main/java/com/vayunmathur/office/ui/DocumentViews.kt b/office/src/main/java/com/vayunmathur/office/ui/DocumentViews.ktindex 41690ac0d2..ed7f104409 100644--- a/office/src/main/java/com/vayunmathur/office/ui/DocumentViews.kt+++ b/office/src/main/java/com/vayunmathur/office/ui/DocumentViews.kt@@ -4,6 +4,7 @@ import com.vayunmathur.library.ui.odf.OdfContentBlock import com.vayunmathur.library.ui.odf.OdfDocument import com.vayunmathur.library.ui.odf.ParagraphStyle +/** Immutable value describing heading item; carries text, level, contentIndex. */ data class HeadingItem(val text: String, val level: Int, val contentIndex: Int) fun extractHeadings(doc: OdfDocument.TextDocument): List<HeadingItem> {
office/src/main/java/com/vayunmathur/office/ui/OfficeBottomBar.kt
diff --git a/office/src/main/java/com/vayunmathur/office/ui/OfficeBottomBar.kt b/office/src/main/java/com/vayunmathur/office/ui/OfficeBottomBar.ktindex e3a165a487..f2dafa2e9a 100644--- a/office/src/main/java/com/vayunmathur/office/ui/OfficeBottomBar.kt+++ b/office/src/main/java/com/vayunmathur/office/ui/OfficeBottomBar.kt@@ -33,9 +33,13 @@ import com.vayunmathur.office.R /** What the shared bottom bar is currently formatting. (Phase 2) */ sealed interface FormatTarget {+ /** Singleton holding none state and operations. */ data object None : FormatTarget+ /** Immutable value describing text run; carries runStart, runEnd, selStart, selEnd. */ data class TextRun(val runStart: Int, val runEnd: Int, val selStart: Int, val selEnd: Int) : FormatTarget+ /** Immutable value describing cell; carries sheet, row, col. */ data class Cell(val sheet: Int, val row: Int, val col: Int) : FormatTarget+ /** Immutable value describing element; carries slide, element. */ data class Element(val slide: Int, val element: Int) : FormatTarget } @@ -47,6 +51,7 @@ data class DocCaps( val insertTable: Boolean = false ) +/** Kinds of shape kind. */ enum class ShapeKind { RECT, ELLIPSE, LINE } /** Callbacks the shared bottom bar may invoke. */
office/src/main/java/com/vayunmathur/office/util/OfficeSync.kt
diff --git a/office/src/main/java/com/vayunmathur/office/util/OfficeSync.kt b/office/src/main/java/com/vayunmathur/office/util/OfficeSync.ktindex c603008d23..6ba2f51f8e 100644--- a/office/src/main/java/com/vayunmathur/office/util/OfficeSync.kt+++ b/office/src/main/java/com/vayunmathur/office/util/OfficeSync.kt@@ -28,6 +28,7 @@ import kotlinx.serialization.Serializable import kotlinx.serialization.json.Json import kotlin.io.encoding.Base64 +/** Singleton holding office sync state and operations. */ object OfficeSync { // Cloudflare-fronted; covered by the FIRST_PARTY pins. private const val URL = "https://api.maproj.org/office"@@ -281,6 +282,8 @@ object OfficeSync { @Serializable internal data class PullResp(val actions: List<String> = emptyList(), val seq: Int = 0) @Serializable data class DocAction(val type: String = "snapshot", val flat: String = "")++ /** A document-sharing invite: the doc, its key and the invited role. */ @Serializable data class Invite( val docId: String, val key: String,@@ -302,8 +305,11 @@ object OfficeSync { /** Peeks the discriminator on an inbox blob so invites and requests can share the channel. */ @Serializable private data class InboxEnvelope(val kind: String = "") + /** Invites Result: a list implementation for invites result. */ class InvitesResult(val invites: List<Invite>, val seq: Int)+ /** Inbox Result: a list implementation for inbox result. */ class InboxResult(val invites: List<Invite>, val requests: List<JoinRequest>, val seq: Int)+ /** Doc Actions Result: a list implementation for doc actions result. */ class DocActionsResult(val items: List<String>, val seq: Int) @Serializable private data class SubMsg(val t: String, val channel: String)
office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt
diff --git a/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt b/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.ktindex 335a97e44e..5d809efa20 100644--- a/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt+++ b/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt@@ -38,6 +38,7 @@ import kotlinx.serialization.json.Json internal const val MAX_UNDO = 30 internal const val MAX_RECENT = 20 +/** Immutable value describing office doc meta. */ @Serializable data class OfficeDocMeta( val docId: String,@@ -126,6 +127,7 @@ data class OwnerTransfer(val newOwnerId: String, val newOwnerKey: String, val si @Serializable data class SignedOp(val author: String, val sig: String, val ops: String) +/** Office View Model: a application implementation for office view model. */ class OfficeViewModel(application: Application) : AndroidViewModel(application) { internal val stateMutable = MutableStateFlow<ViewState>(ViewState.Empty) val state: StateFlow<ViewState> = stateMutable@@ -148,12 +150,14 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application) internal val nightModeMutable = MutableStateFlow(false) val nightMode: StateFlow<Boolean> = nightModeMutable + /** Kinds of document theme mode. */ enum class DocumentThemeMode { UNCHANGED, FOLLOW_SYSTEM; fun toggled(): DocumentThemeMode = if (this == UNCHANGED) FOLLOW_SYSTEM else UNCHANGED + /** Shared constants and helpers for DocumentThemeMode. */ companion object { fun parse(name: String?): DocumentThemeMode = try { valueOf(name ?: UNCHANGED.name)@@ -442,10 +446,15 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application) /** This device's sync id. Empty until [initSync] has run. */ val syncDeviceId: String get() = OfficeSync.deviceId + /** State for view. */ sealed class ViewState {+ /** Singleton holding empty state and operations. */ data object Empty : ViewState()+ /** Singleton holding loading state and operations. */ data object Loading : ViewState()+ /** Immutable value describing loaded; carries document. */ data class Loaded(val document: OdfDocument) : ViewState()+ /** Immutable value describing error; carries message. */ data class Error(val message: String) : ViewState() }