put back sync network methods / avoid runBlocking

This commit is contained in:
Conny Duck 2024-04-01 20:32:20 +02:00
parent 5343766886
commit c1adc26dab
No known key found for this signature in database
3 changed files with 45 additions and 37 deletions

View File

@ -53,7 +53,6 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.shareIn import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class ComposeViewModel @Inject constructor( class ComposeViewModel @Inject constructor(
@ -431,28 +430,24 @@ class ComposeViewModel @Inject constructor(
fun searchAutocompleteSuggestions(token: String): List<AutocompleteResult> { fun searchAutocompleteSuggestions(token: String): List<AutocompleteResult> {
return when (token[0]) { return when (token[0]) {
'@' -> runBlocking { '@' -> api.searchAccountsSync(query = token.substring(1), limit = 10)
api.searchAccounts(query = token.substring(1), limit = 10) .fold({ accounts ->
.fold({ accounts -> accounts.map { AutocompleteResult.AccountResult(it) }
accounts.map { AutocompleteResult.AccountResult(it) } }, { e ->
}, { e -> Log.e(TAG, "Autocomplete search for $token failed.", e)
Log.e(TAG, "Autocomplete search for $token failed.", e) emptyList()
emptyList() })
}) '#' -> api.searchSync(
} query = token,
'#' -> runBlocking { type = SearchType.Hashtag.apiParameter,
api.search( limit = 10
query = token, )
type = SearchType.Hashtag.apiParameter, .fold({ searchResult ->
limit = 10 searchResult.hashtags.map { AutocompleteResult.HashtagResult(it.name) }
) }, { e ->
.fold({ searchResult -> Log.e(TAG, "Autocomplete search for $token failed.", e)
searchResult.hashtags.map { AutocompleteResult.HashtagResult(it.name) } emptyList()
}, { e -> })
Log.e(TAG, "Autocomplete search for $token failed.", e)
emptyList()
})
}
':' -> { ':' -> {
val emojiList = emoji.replayCache.firstOrNull() ?: return emptyList() val emojiList = emoji.replayCache.firstOrNull() ?: return emptyList()
val incomplete = token.substring(1) val incomplete = token.substring(1)

View File

@ -14,7 +14,6 @@ import com.keylesspalace.tusky.di.Injectable
import com.keylesspalace.tusky.entity.HashTag import com.keylesspalace.tusky.entity.HashTag
import com.keylesspalace.tusky.network.MastodonApi import com.keylesspalace.tusky.network.MastodonApi
import javax.inject.Inject import javax.inject.Inject
import kotlinx.coroutines.runBlocking
class FollowedTagsViewModel @Inject constructor( class FollowedTagsViewModel @Inject constructor(
private val api: MastodonApi private val api: MastodonApi
@ -39,19 +38,17 @@ class FollowedTagsViewModel @Inject constructor(
fun searchAutocompleteSuggestions( fun searchAutocompleteSuggestions(
token: String token: String
): List<ComposeAutoCompleteAdapter.AutocompleteResult> { ): List<ComposeAutoCompleteAdapter.AutocompleteResult> {
return runBlocking { return api.searchSync(query = token, type = SearchType.Hashtag.apiParameter, limit = 10)
api.search(query = token, type = SearchType.Hashtag.apiParameter, limit = 10) .fold({ searchResult ->
.fold({ searchResult -> searchResult.hashtags.map {
searchResult.hashtags.map { ComposeAutoCompleteAdapter.AutocompleteResult.HashtagResult(
ComposeAutoCompleteAdapter.AutocompleteResult.HashtagResult( it.name
it.name )
) }
} }, { e ->
}, { e -> Log.e(TAG, "Autocomplete search for $token failed.", e)
Log.e(TAG, "Autocomplete search for $token failed.", e) emptyList()
emptyList() })
})
}
} }
companion object { companion object {

View File

@ -314,6 +314,12 @@ interface MastodonApi {
@Query("following") following: Boolean? = null @Query("following") following: Boolean? = null
): NetworkResult<List<TimelineAccount>> ): NetworkResult<List<TimelineAccount>>
@GET("api/v1/accounts/search")
fun searchAccountsSync(
@Query("q") query: String,
@Query("limit") limit: Int? = null
): NetworkResult<List<TimelineAccount>>
@GET("api/v1/accounts/{id}") @GET("api/v1/accounts/{id}")
suspend fun account(@Path("id") accountId: String): NetworkResult<Account> suspend fun account(@Path("id") accountId: String): NetworkResult<Account>
@ -643,6 +649,16 @@ interface MastodonApi {
@Query("following") following: Boolean? = null @Query("following") following: Boolean? = null
): NetworkResult<SearchResult> ): 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 @FormUrlEncoded
@POST("api/v1/accounts/{id}/note") @POST("api/v1/accounts/{id}/note")
suspend fun updateAccountNote( suspend fun updateAccountNote(