refactor(search): extract shared code to commons, remove dead code

- Extract DateUtils (isLeapYear, dateToUnix, timestampToDate) to deduplicate
  identical implementations in QueryParser and QuerySerializer
- Move SavedSearch data class from desktopApp to commons/search
- Replace local formatTimestamp with existing toTimeAgo from commons/util
- Remove dead code: resolveAuthorName, _authorSuggestions (never called)
- Remove dead code: createOrFilters (never wired into SearchScreen)
- Remove unused ICacheProvider dependency from AdvancedSearchBarState

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
nrobi144
2026-03-11 07:21:17 +02:00
parent b3952ce9db
commit e8e2650fa9
9 changed files with 106 additions and 119 deletions
@@ -21,7 +21,6 @@
package com.vitorpamplona.amethyst.commons.search
import com.vitorpamplona.amethyst.commons.model.User
import com.vitorpamplona.amethyst.commons.model.cache.ICacheProvider
import com.vitorpamplona.quartz.nip01Core.core.Event
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
@@ -44,7 +43,6 @@ enum class ChangeSource {
@OptIn(FlowPreview::class)
class AdvancedSearchBarState(
private val cache: ICacheProvider,
private val scope: CoroutineScope,
private val debounceMs: Long = 300L,
) {
@@ -82,10 +80,6 @@ class AdvancedSearchBarState(
private val _isSearching = MutableStateFlow(false)
val isSearching: StateFlow<Boolean> = _isSearching.asStateFlow()
// Author name suggestions for autocomplete
private val _authorSuggestions = MutableStateFlow<ImmutableList<User>>(persistentListOf())
val authorSuggestions: StateFlow<ImmutableList<User>> = _authorSuggestions.asStateFlow()
// Expanded panel state
private val _panelExpanded = MutableStateFlow(false)
val panelExpanded: StateFlow<Boolean> = _panelExpanded.asStateFlow()
@@ -214,15 +208,4 @@ class AdvancedSearchBarState(
_noteResults.value = (current + newEvents).sortedByDescending { it.createdAt }.toImmutableList()
}
}
// Name resolution for autocomplete
@Suppress("UNCHECKED_CAST")
fun resolveAuthorName(name: String) {
if (name.length < 2) {
_authorSuggestions.value = persistentListOf()
return
}
val results = cache.findUsersStartingWith(name, 5) as List<User>
_authorSuggestions.value = results.toImmutableList()
}
}
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.amethyst.commons.search
object DateUtils {
fun isLeapYear(year: Int): Boolean = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
fun dateToUnix(
year: Int,
month: Int,
day: Int,
): Long {
var totalDays = 0L
for (y in 1970 until year) {
totalDays += if (isLeapYear(y)) 366 else 365
}
val daysInMonth = intArrayOf(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
if (isLeapYear(year)) daysInMonth[2] = 29
for (m in 1 until month) {
totalDays += daysInMonth[m]
}
totalDays += (day - 1)
return totalDays * 86400L
}
fun timestampToDate(timestamp: Long): String {
var remaining = timestamp
var year = 1970
while (true) {
val daysInYear = if (isLeapYear(year)) 366L else 365L
val secondsInYear = daysInYear * 86400L
if (remaining < secondsInYear) break
remaining -= secondsInYear
year++
}
val daysInMonth = intArrayOf(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
if (isLeapYear(year)) daysInMonth[2] = 29
var dayOfYear = (remaining / 86400).toInt() + 1
var month = 1
while (month <= 12 && dayOfYear > daysInMonth[month]) {
dayOfYear -= daysInMonth[month]
month++
}
return "$year-${month.toString().padStart(2, '0')}-${dayOfYear.toString().padStart(2, '0')}"
}
}
@@ -319,28 +319,5 @@ object QueryParser {
year: Int,
month: Int,
day: Int,
): Long {
// Simple UTC date to unix timestamp calculation
// Days from epoch (1970-01-01) to the given date
var totalDays = 0L
// Add days for full years
for (y in 1970 until year) {
totalDays += if (isLeapYear(y)) 366 else 365
}
// Add days for full months in target year
val daysInMonth = intArrayOf(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
if (isLeapYear(year)) daysInMonth[2] = 29
for (m in 1 until month) {
totalDays += daysInMonth[m]
}
// Add remaining days
totalDays += (day - 1)
return totalDays * 86400L
}
private fun isLeapYear(year: Int): Boolean = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
): Long = DateUtils.dateToUnix(year, month, day)
}
@@ -84,30 +84,5 @@ object QuerySerializer {
return parts.joinToString(" ")
}
fun timestampToDate(timestamp: Long): String {
// Convert unix timestamp to YYYY-MM-DD
var remaining = timestamp
var year = 1970
while (true) {
val daysInYear = if (isLeapYear(year)) 366L else 365L
val secondsInYear = daysInYear * 86400L
if (remaining < secondsInYear) break
remaining -= secondsInYear
year++
}
val daysInMonth = intArrayOf(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
if (isLeapYear(year)) daysInMonth[2] = 29
var dayOfYear = (remaining / 86400).toInt() + 1
var month = 1
while (month <= 12 && dayOfYear > daysInMonth[month]) {
dayOfYear -= daysInMonth[month]
month++
}
return "$year-${month.toString().padStart(2, '0')}-${dayOfYear.toString().padStart(2, '0')}"
}
private fun isLeapYear(year: Int): Boolean = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
fun timestampToDate(timestamp: Long): String = DateUtils.timestampToDate(timestamp)
}
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.amethyst.commons.search
data class SavedSearch(
val id: String,
val label: String,
val query: SearchQuery,
val createdAt: Long,
)
@@ -22,6 +22,7 @@ package com.vitorpamplona.amethyst.desktop
import com.vitorpamplona.amethyst.commons.search.QueryParser
import com.vitorpamplona.amethyst.commons.search.QuerySerializer
import com.vitorpamplona.amethyst.commons.search.SavedSearch
import com.vitorpamplona.amethyst.commons.search.SearchQuery
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
@@ -134,10 +135,3 @@ object SearchHistoryStore {
prefs.put(KEY_SAVED, raw)
}
}
data class SavedSearch(
val id: String,
val label: String,
val query: SearchQuery,
val createdAt: Long,
)
@@ -128,19 +128,6 @@ object SearchFilterFactory {
}
}
fun createOrFilters(
query: SearchQuery,
limit: Int = 100,
): List<List<Filter>> {
if (query.orTerms.isEmpty()) return listOf(createFilters(query, limit))
// Each OR term gets its own set of filters
return query.orTerms.take(3).map { term ->
val termQuery = query.copy(text = term, orTerms = kotlinx.collections.immutable.persistentListOf())
createFilters(termQuery, limit)
}
}
private fun buildSearchString(query: SearchQuery): String? {
val parts = mutableListOf<String>()
@@ -111,7 +111,7 @@ fun SearchScreen(
modifier: Modifier = Modifier,
) {
val scope = rememberCoroutineScope()
val state = remember { AdvancedSearchBarState(localCache, scope) }
val state = remember { AdvancedSearchBarState(scope) }
val focusRequester = remember { FocusRequester() }
// Pre-fill initial query
@@ -408,7 +408,7 @@ fun SearchScreen(
@Composable
private fun SearchEmptyState(
historyItems: List<com.vitorpamplona.amethyst.commons.search.SearchQuery>,
savedSearches: List<com.vitorpamplona.amethyst.desktop.SavedSearch>,
savedSearches: List<com.vitorpamplona.amethyst.commons.search.SavedSearch>,
onLoadQuery: (com.vitorpamplona.amethyst.commons.search.SearchQuery) -> Unit,
onDeleteSaved: (String) -> Unit,
onClearHistory: () -> Unit,
@@ -62,6 +62,7 @@ import androidx.compose.ui.unit.dp
import com.vitorpamplona.amethyst.commons.search.AdvancedSearchBarState
import com.vitorpamplona.amethyst.commons.search.KindRegistry
import com.vitorpamplona.amethyst.commons.ui.components.UserSearchCard
import com.vitorpamplona.amethyst.commons.util.toTimeAgo
import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip23LongContent.LongTextNoteEvent
@@ -255,7 +256,7 @@ private fun NotePreviewCard(
Spacer(Modifier.weight(1f))
// Timestamp
Text(
formatTimestamp(event.createdAt),
event.createdAt.toTimeAgo(withDot = false),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
@@ -296,32 +297,3 @@ private fun <T> ExpandableSection(
}
}
}
private fun formatTimestamp(ts: Long): String {
val now = System.currentTimeMillis() / 1000
val diff = now - ts
return when {
diff < 60 -> {
"just now"
}
diff < 3600 -> {
"${diff / 60}m ago"
}
diff < 86400 -> {
"${diff / 3600}h ago"
}
diff < 604800 -> {
"${diff / 86400}d ago"
}
else -> {
// Simple date format
val days = ts / 86400
val year = 1970 + (days / 365).toInt()
"$year"
}
}
}