From a9437d90014c2a38ecde3170a5c304ed8cfb85a0 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Tue, 21 May 2024 15:56:33 -0400 Subject: [PATCH] Refactors CreatedAt Comparator --- .../model/observables/CreatedAtComparator.kt | 43 +++++++++++++++++++ .../model/observables/LatestByKindWithETag.kt | 16 +------ .../NIP90ContentDiscoveryResponseFilter.kt | 18 +------- 3 files changed, 46 insertions(+), 31 deletions(-) create mode 100644 app/src/main/java/com/vitorpamplona/amethyst/model/observables/CreatedAtComparator.kt diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/observables/CreatedAtComparator.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/observables/CreatedAtComparator.kt new file mode 100644 index 000000000..fb6ba16f4 --- /dev/null +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/observables/CreatedAtComparator.kt @@ -0,0 +1,43 @@ +/** + * Copyright (c) 2024 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.model.observables + +import com.vitorpamplona.amethyst.model.Note + +object CreatedAtComparator : Comparator { + override fun compare( + first: Note?, + second: Note?, + ): Int { + val firstEvent = first?.event + val secondEvent = second?.event + + return if (firstEvent == null && secondEvent == null) { + 0 + } else if (firstEvent == null) { + 1 + } else if (secondEvent == null) { + -1 + } else { + firstEvent.createdAt().compareTo(secondEvent.createdAt()) + } + } +} diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/observables/LatestByKindWithETag.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/observables/LatestByKindWithETag.kt index 6952b1a3f..c1ebd5a1f 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/observables/LatestByKindWithETag.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/observables/LatestByKindWithETag.kt @@ -50,21 +50,7 @@ class LatestByKindWithETag(private val kind: Int, private val eTag: S it.kind() == kind && it.isTaggedEvent(eTag) } == true }, - comparator = { first: Note?, second: Note? -> - println("Comparator $first $second") - val firstEvent = first?.event - val secondEvent = second?.event - - if (firstEvent == null && secondEvent == null) { - 0 - } else if (firstEvent == null) { - 1 - } else if (secondEvent == null) { - -1 - } else { - firstEvent.createdAt().compareTo(secondEvent.createdAt()) - } - }, + comparator = CreatedAtComparator, )?.event as? T _latest.tryEmit(latestNote) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/dal/NIP90ContentDiscoveryResponseFilter.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/dal/NIP90ContentDiscoveryResponseFilter.kt index 408803dc9..fef70a0b5 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/dal/NIP90ContentDiscoveryResponseFilter.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/dal/NIP90ContentDiscoveryResponseFilter.kt @@ -23,6 +23,7 @@ package com.vitorpamplona.amethyst.ui.dal import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.Note +import com.vitorpamplona.amethyst.model.observables.CreatedAtComparator import com.vitorpamplona.quartz.events.MuteListEvent import com.vitorpamplona.quartz.events.NIP90ContentDiscoveryResponseEvent import com.vitorpamplona.quartz.events.PeopleListEvent @@ -52,21 +53,6 @@ open class NIP90ContentDiscoveryResponseFilter( return noteEvent is NIP90ContentDiscoveryResponseEvent && noteEvent.isTaggedEvent(request) } - val createAtComparator = { first: Note?, second: Note? -> - val firstEvent = first?.event - val secondEvent = second?.event - - if (firstEvent == null && secondEvent == null) { - 0 - } else if (firstEvent == null) { - 1 - } else if (secondEvent == null) { - -1 - } else { - firstEvent.createdAt().compareTo(secondEvent.createdAt()) - } - } - override fun feed(): List { val params = buildFilterParams(account) @@ -75,7 +61,7 @@ open class NIP90ContentDiscoveryResponseFilter( filter = { idHex: String, note: Note -> acceptableEvent(note) }, - comparator = createAtComparator, + comparator = CreatedAtComparator, ) val noteEvent = latestNote?.event as? NIP90ContentDiscoveryResponseEvent ?: return listOf()