Skip to content

Commit

Permalink
refactor: Remove synchronous search functions (#481)
Browse files Browse the repository at this point in the history
The previous code used synchronous (i.e., non-suspending) functions to
call the /api/v2/search and /api/v2/accounts/search endpoints.

This is not necessary as the search was always performed in a separate
thread.

Remove, and replace their usage with the equivalent functions that
suspend.
  • Loading branch information
nikclayton authored Mar 1, 2024
1 parent fb66293 commit fcae441
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,7 @@ class ComposeActivity :
}
}

override fun search(token: String): List<ComposeAutoCompleteAdapter.AutocompleteResult> {
override suspend fun search(token: String): List<ComposeAutoCompleteAdapter.AutocompleteResult> {
return viewModel.searchAutocompleteSuggestions(token)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import app.pachli.core.network.model.TimelineAccount
import app.pachli.databinding.ItemAutocompleteEmojiBinding
import app.pachli.databinding.ItemAutocompleteHashtagBinding
import com.bumptech.glide.Glide
import kotlinx.coroutines.runBlocking

class ComposeAutoCompleteAdapter(
private val autocompletionProvider: AutocompletionProvider,
Expand Down Expand Up @@ -70,7 +71,10 @@ class ComposeAutoCompleteAdapter(
override fun performFiltering(constraint: CharSequence?): FilterResults {
val filterResults = FilterResults()
if (constraint != null) {
val results = autocompletionProvider.search(constraint.toString())
// runBlocking here is OK because this happens in a worker thread
val results = runBlocking {
autocompletionProvider.search(constraint.toString())
}
filterResults.values = results
filterResults.count = results.size
}
Expand Down Expand Up @@ -154,7 +158,7 @@ class ComposeAutoCompleteAdapter(
}

interface AutocompletionProvider {
fun search(token: String): List<AutocompleteResult>
suspend fun search(token: String): List<AutocompleteResult>
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,10 @@ class ComposeViewModel @Inject constructor(
}
}

fun searchAutocompleteSuggestions(token: String): List<AutocompleteResult> {
suspend fun searchAutocompleteSuggestions(token: String): List<AutocompleteResult> {
when (token[0]) {
'@' -> {
return api.searchAccountsSync(query = token.substring(1), limit = 10)
return api.searchAccounts(query = token.substring(1), limit = 10)
.fold({ accounts ->
accounts.map { AutocompleteResult.AccountResult(it) }
}, { e ->
Expand All @@ -390,7 +390,7 @@ class ComposeViewModel @Inject constructor(
})
}
'#' -> {
return api.searchSync(query = token, type = SearchType.Hashtag.apiParameter, limit = 10)
return api.search(query = token, type = SearchType.Hashtag.apiParameter, limit = 10)
.fold({ searchResult ->
searchResult.hashtags.map { AutocompleteResult.HashtagResult(it.name) }
}, { e ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class FollowedTagsActivity :
}
}

override fun search(token: String): List<ComposeAutoCompleteAdapter.AutocompleteResult> {
override suspend fun search(token: String): List<ComposeAutoCompleteAdapter.AutocompleteResult> {
return viewModel.searchAutocompleteSuggestions(token)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class FollowedTagsViewModel @Inject constructor(
},
).flow.cachedIn(viewModelScope)

fun searchAutocompleteSuggestions(token: String): List<ComposeAutoCompleteAdapter.AutocompleteResult> {
return api.searchSync(query = token, type = SearchType.Hashtag.apiParameter, limit = 10)
suspend fun searchAutocompleteSuggestions(token: String): List<ComposeAutoCompleteAdapter.AutocompleteResult> {
return api.search(query = token, type = SearchType.Hashtag.apiParameter, limit = 10)
.fold({ searchResult ->
searchResult.hashtags.map { ComposeAutoCompleteAdapter.AutocompleteResult.HashtagResult(it.name) }
}, { e ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class ComposeActivityTest {
}
}
}
onBlocking { searchSync(any(), anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull()) } doReturn NetworkResult.success(
onBlocking { search(any(), anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull()) } doReturn NetworkResult.success(
SearchResult(emptyList(), emptyList(), emptyList()),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,6 @@ interface MastodonApi {
@Query("following") following: Boolean? = null,
): NetworkResult<List<TimelineAccount>>

@GET("api/v1/accounts/search")
fun searchAccountsSync(
@Query("q") query: String,
@Query("resolve") resolve: Boolean? = null,
@Query("limit") limit: Int? = null,
@Query("following") following: Boolean? = null,
): NetworkResult<List<TimelineAccount>>

@GET("api/v1/accounts/{id}")
suspend fun account(
@Path("id") accountId: String,
Expand Down Expand Up @@ -730,16 +722,6 @@ interface MastodonApi {
@Query("following") following: Boolean? = null,
): NetworkResult<SearchResult>

@GET("api/v2/search")
fun searchSync(
@Query("q") query: String,
@Query("type") type: String? = null,
@Query("resolve") resolve: Boolean? = null,
@Query("limit") limit: Int? = null,
@Query("offset") offset: Int? = null,
@Query("following") following: Boolean? = null,
): NetworkResult<SearchResult>

@FormUrlEncoded
@POST("api/v1/accounts/{id}/note")
suspend fun updateAccountNote(
Expand Down

0 comments on commit fcae441

Please sign in to comment.