-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deduplicate posts in the feed (#1613)
* Deduplicate posts in the feed * Fix formatting
- Loading branch information
Showing
5 changed files
with
132 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.jerboa.feed | ||
|
||
import it.vercruysse.lemmyapi.Identity | ||
|
||
open class UniqueFeedController<T : Identity> : FeedController<T>() { | ||
private val ids = mutableSetOf<Long>() | ||
|
||
override fun add(item: T): Boolean { | ||
if (ids.add(item.id)) { | ||
items.add(item) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
override fun addAll(newItems: List<T>) { | ||
newItems.forEach { | ||
if (ids.add(it.id)) { | ||
items.add(it) | ||
} | ||
} | ||
} | ||
|
||
override fun clear() { | ||
super.clear() | ||
ids.clear() | ||
} | ||
|
||
override fun remove(item: T): Boolean { | ||
ids.remove(item.id) | ||
return super.remove(item) | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
app/src/test/java/com/jerboa/feed/UniqueFeedControllerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.jerboa.feed | ||
|
||
import it.vercruysse.lemmyapi.Identity | ||
import org.junit.Assert.* | ||
import org.junit.Test | ||
|
||
class UniqueFeedControllerTest { | ||
private data class PostView( | ||
override val id: Long, | ||
) : Identity | ||
|
||
@Test | ||
fun `Should not add duplicate posts`() { | ||
val controller = UniqueFeedController<PostView>() | ||
controller.add(PostView(1)) | ||
assertEquals(1, controller.feed.size) | ||
controller.add(PostView(1)) | ||
assertEquals(1, controller.feed.size) | ||
controller.add(PostView(2)) | ||
assertEquals(2, controller.feed.size) | ||
} | ||
|
||
@Test | ||
fun `Should remove post`() { | ||
val controller = UniqueFeedController<PostView>() | ||
controller.add(PostView(1)) | ||
assertEquals(1, controller.feed.size) | ||
controller.remove(PostView(1)) | ||
assertEquals(0, controller.feed.size) | ||
} | ||
|
||
@Test | ||
fun `Post removal should clear id`() { | ||
val controller = UniqueFeedController<PostView>() | ||
controller.add(PostView(1)) | ||
assertEquals(1, controller.feed.size) | ||
controller.remove(PostView(1)) | ||
assertTrue(controller.feed.isEmpty()) | ||
controller.add(PostView(1)) | ||
assertEquals(1, controller.feed.size) | ||
} | ||
|
||
@Test | ||
fun `Should clear all posts`() { | ||
val controller = UniqueFeedController<PostView>() | ||
controller.add(PostView(1)) | ||
controller.add(PostView(2)) | ||
assertEquals(2, controller.feed.size) | ||
controller.clear() | ||
assertTrue(controller.feed.isEmpty()) | ||
} | ||
|
||
@Test | ||
fun `Clear should clear ids`() { | ||
val controller = UniqueFeedController<PostView>() | ||
controller.add(PostView(1)) | ||
controller.add(PostView(2)) | ||
assertEquals(2, controller.feed.size) | ||
controller.clear() | ||
assertTrue(controller.feed.isEmpty()) | ||
controller.add(PostView(1)) | ||
controller.add(PostView(2)) | ||
assertEquals(2, controller.feed.size) | ||
} | ||
|
||
@Test | ||
fun `Add all should not add duplicates`() { | ||
val controller = UniqueFeedController<PostView>() | ||
controller.addAll(listOf(PostView(1), PostView(2), PostView(1))) | ||
assertEquals(2, controller.feed.size) | ||
} | ||
|
||
@Test | ||
fun `Init should clear ids`() { | ||
val controller = UniqueFeedController<PostView>() | ||
controller.add(PostView(1)) | ||
controller.add(PostView(2)) | ||
assertEquals(2, controller.feed.size) | ||
controller.init(listOf(PostView(1), PostView(2))) | ||
assertEquals(2, controller.feed.size) | ||
} | ||
} |