Move SSD detection truth selection to typed Kotlin

ober

a20f349aa629727642baa2a95cfd0f9bb1dc2a08

diff --git a/templates/ssd-review.ss b/templates/ssd-review.ss
index 3e59813..e5e26b3 100644
--- a/templates/ssd-review.ss
+++ b/templates/ssd-review.ss
@@ -4447,6 +4447,7 @@
                 selectModeStatus selectionResultStatus detectStartStatus
                 detectorSourceWithTruth detectionTruthNote detectionOcrNote
                 detectionLearnedNote shouldLoadLocalTruth
+                localTruthForDetection existingTruthForDetection
                 shouldUseExistingTruth shouldApplyExistingTruth
                 shouldUseExistingTruthObject shouldApplyExistingTruthObject
                 shouldApplyLearnedGuesses shouldMergeRemoteTruth
@@ -4473,6 +4474,7 @@
         (type DocumentFile)
         (type JSONObject)
         (type SsdSession)
+        (type TruthStore)
         (type ViewportState)
         (extern (fileIsFile (file : File)) : Bool
           (kotlin-member-get isFile))
@@ -4501,6 +4503,9 @@
                                   (green : Int32)
                                   (blue : Int32)) : Int32
           (kotlin-call android graphics Color argb))
+        (extern (truthStoreLoadTruth (store : TruthStore)
+                                     (session : SsdSession)) : (Nullable JSONObject)
+          (kotlin-member-call loadTruth))
         (def (normalizeFiringArc (value : String)) : String
           (string-uppercase (string-trim value)))
         (def (normalizeLabel (value : String)) : String
@@ -5142,6 +5147,18 @@
             ""))
         (def (shouldLoadLocalTruth (forceDetect : Bool)) : Bool
           (not forceDetect))
+        (def (localTruthForDetection (store : TruthStore)
+                                     (session : SsdSession)
+                                     (forceDetect : Bool)) : (Nullable JSONObject)
+          (if (shouldLoadLocalTruth forceDetect)
+            (truthStoreLoadTruth store session)
+            (nullable-none JSONObject)))
+        (def (existingTruthForDetection (store : TruthStore)
+                                        (session : SsdSession)
+                                        (localTruth : (Nullable JSONObject))) : (Nullable JSONObject)
+          (if (truthPresent localTruth)
+            localTruth
+            (truthStoreLoadTruth store session)))
         (def (shouldUseExistingTruth (existingTruthGroupCount : Int32)
                                      (forceDetect : Bool)) : Bool
           (and (> existingTruthGroupCount (int32 0))
@@ -6922,7 +6939,7 @@
        "                    ssdArea = null"
        "                )"
        ""
-       "                val localTruth = if (shouldLoadLocalTruth(forceDetect)) truthStore.loadTruth(session) else null"
+       "                val localTruth = localTruthForDetection(truthStore, session, forceDetect)"
 	       "                val localGroupCount = nullableTruthGroupCount(localTruth)"
 	       "                if (truthGroupCountAtLeast(localGroupCount, FAST_TRUTH_GROUP_THRESHOLD)) {"
 	       "                    truthStore.applyTruth(session)"
@@ -6942,7 +6959,7 @@
        "                }"
        ""
        "                val remoteMatches = 0"
-       "                val existingTruth = if (truthPresent(localTruth)) localTruth else truthStore.loadTruth(session)"
+       "                val existingTruth = existingTruthForDetection(truthStore, session, localTruth)"
        "                val existingTruthGroupCount = nullableTruthGroupCount(existingTruth)"
        "                var ocrGuesses = 0"
        "                var detectorNote = \"\""