diff --git a/Incido Development Plan.txt b/Incido Development Plan.txt index d7c8777..ca0550e 100644 --- a/Incido Development Plan.txt +++ b/Incido Development Plan.txt @@ -376,6 +376,7 @@ A short reference for AI assistants: | 0.3.0.0 | 2026-08-14 | Hashtags (separate entity + cross-ref, 5-tag cap, autocomplete, persistent chips on editor, search() also matches tag names) + bolder colour accent (14 dp gradient stripe + tinted card background + corner chip) + nav drawer (Home / Favorites / Untagged + recent tags) + centred app-bar title + new version scheme adopted (validator in app/build.gradle.kts). | | 0.3.0.1 | 2026-08-14 | Urgent UI polish: editor hashtag row moved to the bottom (and hidden when empty), `+ add` chip removed (use `#` icon in top bar), colour picker centred horizontally, list-card dog-ear colour accent replaces the small circle chip, list cards show a hashtag row (with `+N` abbreviation when capped). | | 0.3.0.2 | 2026-08-14 | Hotfix: dropped the redundant leading `#` from the AddTagSheet placeholder (was `#tag-name`, now `hashtag-name` — the leading `#` icon in the field already signals the format). | +| 0.3.0.3 | 2026-08-14 | Hotfix: `NoteRepositoryImpl.hashtagUsageSnapshot()` batched note/tag lookups (one `Box.get` per distinct noteId and tagId, not per xref) and the manual `trySend(snapshot())` in `observeHashtagsByRecentUse()` dropped — the ObjectBox observer's first fire is now the sole source. Result: the drawer's Recent Tags list fills in well under a second on real data instead of taking minutes. | --- diff --git a/README.md b/README.md index 76795bb..e31c1dc 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,7 @@ cross-platform ports, end-to-end sync, and security features. | 0.3.0.0 | 2026-08-14 | Hashtags (visible chips on editor, 5-tag cap, autocomplete) + bolder colour accent + nav drawer (Home / Favorites / Untagged + recent tags) + centred app-bar title + new version scheme | | 0.3.0.1 | 2026-08-14 | Urgent UI polish: editor hashtag row moved to the bottom (and hidden when empty), `+ add` chip removed (use `#` icon in top bar), colour picker centred horizontally, list-card dog-ear colour accent replaces the small circle chip, list cards show a hashtag row (with `+N` abbreviation when capped). | | 0.3.0.2 | 2026-08-14 | Hotfix: dropped the redundant leading `#` from the AddTagSheet placeholder (was `#tag-name`, now `hashtag-name` — the leading `#` icon in the field already signals the format). | +| 0.3.0.3 | 2026-08-14 | Hotfix: batched hashtag-usage lookups in `NoteRepositoryImpl.hashtagUsageSnapshot()` (one `Box.get` per distinct noteId and tagId, not per xref) and dropped the manual `trySend(snapshot())` in `observeHashtagsByRecentUse()` — the ObjectBox observer's first fire is now the sole source. The drawer's Recent Tags list fills in well under a second instead of taking minutes. | --- diff --git a/app/build.gradle.kts b/app/build.gradle.kts index b70c225..5c72971 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -28,8 +28,8 @@ android { applicationId = "org.incido.app" minSdk = 26 targetSdk = 35 - versionCode = 7 - versionName = "0.3.0.2" + versionCode = 8 + versionName = "0.3.0.3" } // --- Signing --- diff --git a/core/data/src/main/kotlin/org/incido/data/repository/NoteRepositoryImpl.kt b/core/data/src/main/kotlin/org/incido/data/repository/NoteRepositoryImpl.kt index 19221fa..1130398 100644 --- a/core/data/src/main/kotlin/org/incido/data/repository/NoteRepositoryImpl.kt +++ b/core/data/src/main/kotlin/org/incido/data/repository/NoteRepositoryImpl.kt @@ -78,8 +78,13 @@ class NoteRepositoryImpl( }.flowOn(Dispatchers.Default) override fun observeHashtagsByRecentUse(): Flow> = callbackFlow { - trySend(hashtagUsageSnapshot()) - val sub = noteBox.query().build().subscribe().observer { trySend(hashtagUsageSnapshot()) } + // ObjectBox fires its observer with the full current query state + // immediately on subscribe, so the observer alone is the source + // of truth — no manual trySend that would race the subscription + // and double the work of the snapshot. + val sub = noteBox.query().build().subscribe().observer { _ -> + trySend(hashtagUsageSnapshot()) + } awaitClose { sub.cancel() } }.flowOn(Dispatchers.Default) @@ -281,22 +286,39 @@ class NoteRepositoryImpl( val xrefs: List = xrefBox.all if (xrefs.isEmpty()) return emptyList() - // For each hashtagId, accumulate (lastUsedAt, count). - data class Agg(var lastUsedAt: Long, var count: Int) - val aggregate = HashMap(xrefs.size) + // 1) Batch-load each unique noteId once (was once per xref, which + // was O(M) Box.get calls for a notes-with-multiple-tags world). + val noteIds: Set = xrefs.mapTo(HashSet(xrefs.size)) { it.noteId } + val noteById: Map = noteIds.mapNotNull { id -> + noteBox.get(id)?.let { id to it } + }.toMap() + + // 2) Aggregate per hashtagId. + val aggregate: HashMap = HashMap(xrefs.size) for (x in xrefs) { - val note: NoteEntity = noteBox.get(x.noteId) ?: continue + val note: NoteEntity = noteById[x.noteId] ?: continue val cur = aggregate[x.hashtagId] if (cur == null) { - aggregate[x.hashtagId] = Agg(note.updatedAt, 1) + aggregate[x.hashtagId] = longArrayOf(note.updatedAt, 1L) } else { - if (note.updatedAt > cur.lastUsedAt) cur.lastUsedAt = note.updatedAt - cur.count += 1 + if (note.updatedAt > cur[0]) cur[0] = note.updatedAt + cur[1] = cur[1] + 1 } } - return aggregate.mapNotNull { (tagId, agg) -> - val tag: HashtagEntity = tagBox.get(tagId) ?: return@mapNotNull null - HashtagUsage(name = tag.name, noteCount = agg.count, lastUsedAt = agg.lastUsedAt) + + // 3) Batch-load each unique tagId once. + val tagIds: List = aggregate.keys.toList() + val tagById: Map = tagIds.mapNotNull { id -> + tagBox.get(id)?.let { id to it } + }.toMap() + + return aggregate.mapNotNull { (tagId, arr) -> + val tag: HashtagEntity = tagById[tagId] ?: return@mapNotNull null + HashtagUsage( + name = tag.name, + noteCount = arr[1].toInt(), + lastUsedAt = arr[0], + ) }.sortedWith( compareByDescending { it.lastUsedAt } .thenByDescending { it.noteCount }