Move SSD tree PDF selection to typed Kotlin

ober

03abb313e9b2ce82911393c4fc81e13e4273ca15

diff --git a/templates/ssd-review.ss b/templates/ssd-review.ss
index 06ee3a9..79730dd 100644
--- a/templates/ssd-review.ss
+++ b/templates/ssd-review.ss
@@ -3867,13 +3867,16 @@
                       (json-object-opt-json-array root "groups")))))))))
 
     (typed-kotlin-file "com/sfb/ssdreview/UiTextHelpers.kt"
-      (kotlin-imports (java io File))
+      (kotlin-imports (java io File)
+                      (androidx documentfile provider DocumentFile))
       (typed-library (com sfb ssdreview)
         (export normalizeFiringArc normalizeLabel shouldReplaceGuessLabel
                 shouldReplaceBoxTypeLabel resolvedBoxTypeLabel
                 boxTypeFallbackName boxTypeLabelMatches defaultBoxTypeLabel
                 isGenericGuessLabel isSsdPdfName
                 shouldIncludeSsdPdfChoiceName shouldIncludeSsdPdfFile
+                shouldStopSsdPdfTreeScan ssdPdfDocumentIsDirectory
+                shouldIncludeSsdPdfDocument
                 sourceBaseName canonicalSourceName isUsefulGuessLabel isErrorStatus
                 formatActivityDuration groupDisplayLabel appendModeStatus
                 selectedGroupStatus sessionCountsStatus rotatedPageStatus
@@ -3935,10 +3938,15 @@
         (type Int32)
         (type Float32)
         (type File)
+        (type DocumentFile)
         (extern (fileIsFile (file : File)) : Bool
           (kotlin-member-get isFile))
         (extern (fileName (file : File)) : String
           (kotlin-member-get name))
+        (extern (documentFileIsDirectoryRaw (file : DocumentFile)) : Bool
+          (kotlin-member-get isDirectory))
+        (extern (documentFileIsFile (file : DocumentFile)) : Bool
+          (kotlin-member-get isFile))
         (extern (androidColorRgb (red : Int32)
                                  (green : Int32)
                                  (blue : Int32)) : Int32
@@ -4041,6 +4049,20 @@
                  (fileName file)
                  currentChoices
                  limit)))
+        (def (shouldStopSsdPdfTreeScan (depth : Int32)
+                                       (currentChoices : Int32)
+                                       (limit : Int32)
+                                       (maxDepth : Int32)) : Bool
+          (or (> depth maxDepth)
+              (>= currentChoices limit)))
+        (def (ssdPdfDocumentIsDirectory (file : DocumentFile)) : Bool
+          (documentFileIsDirectoryRaw file))
+        (def (shouldIncludeSsdPdfDocument (file : DocumentFile)
+                                          (name : String)
+                                          (currentChoices : Int32)
+                                          (limit : Int32)) : Bool
+          (and (documentFileIsFile file)
+               (shouldIncludeSsdPdfChoiceName name currentChoices limit)))
         (def (sourceBaseName (value : String)) : String
           (string-lowercase
             (string-replace-regex
@@ -6640,19 +6662,16 @@
        "        choices: MutableList<SsdPdfChoice>,"
        "        depth: Int"
        "    ) {"
-       "        if (depth > MAX_SSD_PDF_SCAN_DEPTH || choices.size >= MAX_SSD_PDF_CHOICES) return"
+       "        if (shouldStopSsdPdfTreeScan(depth, choices.size, MAX_SSD_PDF_CHOICES, MAX_SSD_PDF_SCAN_DEPTH)) return"
        "        dir.listFiles()"
-       "            .sortedWith(compareBy<DocumentFile>({ !it.isDirectory }, { nullableLowercaseOrEmpty(it.name) }))"
+       "            .sortedWith(compareBy<DocumentFile>({ !ssdPdfDocumentIsDirectory(it) }, { nullableLowercaseOrEmpty(it.name) }))"
        "            .forEach { child ->"
-       "                if (choices.size >= MAX_SSD_PDF_CHOICES) return"
+       "                if (shouldStopSsdPdfTreeScan(depth, choices.size, MAX_SSD_PDF_CHOICES, MAX_SSD_PDF_SCAN_DEPTH)) return"
        "                val name = child.name ?: return@forEach"
        "                val label = ssdPdfTreeLabel(prefix, name)"
-       "                if (child.isDirectory) {"
+       "                if (ssdPdfDocumentIsDirectory(child)) {"
        "                    collectSsdPdfs(child, label, choices, depth + 1)"
-       "                } else if ("
-       "                    child.isFile &&"
-       "                    isSsdPdfName(name)"
-       "                ) {"
+       "                } else if (shouldIncludeSsdPdfDocument(child, name, choices.size, MAX_SSD_PDF_CHOICES)) {"
        "                    choices.add(SsdPdfChoice(label, child.uri))"
        "                }"
        "            }"