Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Commit

Permalink
Add support for searching threads and nodes
Browse files Browse the repository at this point in the history
Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
  • Loading branch information
theimpulson committed Mar 26, 2024
1 parent 8444d54 commit 789f2dc
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 0 deletions.
25 changes: 25 additions & 0 deletions app/src/main/java/io/aayush/relabs/network/XDAInterface.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import io.aayush.relabs.network.data.node.Nodes
import io.aayush.relabs.network.data.post.PostInfo
import io.aayush.relabs.network.data.post.PostReply
import io.aayush.relabs.network.data.react.PostReact
import io.aayush.relabs.network.data.search.Order
import io.aayush.relabs.network.data.search.PostSearch
import io.aayush.relabs.network.data.search.SearchResultNode
import io.aayush.relabs.network.data.search.SearchResultThread
import io.aayush.relabs.network.data.thread.ThreadInfo
import io.aayush.relabs.network.data.thread.Threads
import io.aayush.relabs.network.data.user.Me
Expand Down Expand Up @@ -105,4 +109,25 @@ interface XDAInterface {

@POST("audapp-push-subscriptions")
suspend fun postExpoPushToken(@Body body: RequestBody): Response<Success>

@POST("audapp-search")
suspend fun postSearch(
@Query("keywords") query: String,
@Query("search_type") type: String,
@Query("c[container_only]") searchThreadConstraint: Int? = null,
@Query("c[title_only]") searchTitleConstraint: Int? = null,
@Query("order") order: String = Order.RELEVANCE.value,
): Response<PostSearch>

@POST("audapp-search/{id}")
suspend fun getSearchResultsForThread(
@Path("id") searchID: Int,
@Query("page") page: Int? = null,
): Response<SearchResultThread>

@POST("audapp-search/{id}")
suspend fun getSearchResultsForNode(
@Path("id") searchID: Int,
@Query("page") page: Int? = null,
): Response<SearchResultNode>
}
30 changes: 30 additions & 0 deletions app/src/main/java/io/aayush/relabs/network/XDARepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import io.aayush.relabs.network.data.post.PostInfo
import io.aayush.relabs.network.data.post.PostReply
import io.aayush.relabs.network.data.react.PostReact
import io.aayush.relabs.network.data.react.React
import io.aayush.relabs.network.data.search.Type
import io.aayush.relabs.network.data.thread.Thread
import io.aayush.relabs.network.data.thread.ThreadInfo
import io.aayush.relabs.network.data.thread.Threads
import io.aayush.relabs.network.data.user.Me
import io.aayush.relabs.network.paging.GenericPagingSource.Companion.createPager
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emptyFlow
import java.util.UUID
import okhttp3.MultipartBody
import retrofit2.Response
Expand Down Expand Up @@ -136,6 +138,34 @@ class XDARepository @Inject constructor(
return safeExecute { xdaInterface.getWatchedNodes() }?.nodes
}

fun getSearchResultsForThreads(query: String): Flow<PagingData<Thread>> {
return createPager { page ->
val search = safeExecute { xdaInterface.postSearch(query, Type.THREAD.value) }?.search

if (search != null && search.id != 0) {
safeExecute {
xdaInterface.getSearchResultsForThread(search.id, page)
}?.results.orEmpty()
} else {
emptyList()
}
}.flow
}

fun getSearchResultsForNodes(query: String): Flow<PagingData<Node>> {
return createPager { page ->
val search = safeExecute { xdaInterface.postSearch(query, Type.NODE.value) }?.search

if (search != null && search.id != 0) {
safeExecute {
xdaInterface.getSearchResultsForNode(search.id, page)
}?.results.orEmpty()
} else {
emptyList()
}
}.flow
}

private inline fun <T> safeExecute(block: () -> Response<T>): T? {
return try {
val response = block()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.aayush.relabs.network.data.search

enum class Order(val value: String) {
RELEVANCE("relevance"),
DATE("date"),
MOST_RECENT("last_update"),
MOST_REPLIES("replies")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.aayush.relabs.network.data.search

data class PostSearch(
val search: Search = Search()
)
12 changes: 12 additions & 0 deletions app/src/main/java/io/aayush/relabs/network/data/search/Search.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.aayush.relabs.network.data.search

import io.aayush.relabs.network.data.common.DateTime

data class Search(
val created_at: DateTime = DateTime(),
val id: Int = 0,
val result_count: Int = 0,
val search_constraints: SearchConstraints = SearchConstraints(),
val search_order: String = String(),
val search_type: String = String()
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.aayush.relabs.network.data.search

data class SearchConstraints(
val container_only: Int = 0,
val title_only: Int = 0
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.aayush.relabs.network.data.search

import io.aayush.relabs.network.data.common.Pagination
import io.aayush.relabs.network.data.node.Node

data class SearchResultNode(
val search: Search = Search(),
val results: List<Node> = emptyList(),
val pagination: Pagination = Pagination()
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.aayush.relabs.network.data.search

import io.aayush.relabs.network.data.common.Pagination
import io.aayush.relabs.network.data.thread.Thread

data class SearchResultThread(
val search: Search = Search(),
val results: List<Thread> = emptyList(),
val pagination: Pagination = Pagination()
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.aayush.relabs.network.data.search

enum class Type(val value: String) {
NODE("node"),
THREAD("post")
}

0 comments on commit 789f2dc

Please sign in to comment.