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,17 +430,14 @@ 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(
'#' -> runBlocking {
api.search(
query = token, query = token,
type = SearchType.Hashtag.apiParameter, type = SearchType.Hashtag.apiParameter,
limit = 10 limit = 10
@ -452,7 +448,6 @@ class ComposeViewModel @Inject constructor(
Log.e(TAG, "Autocomplete search for $token failed.", e) Log.e(TAG, "Autocomplete search for $token failed.", e)
emptyList() 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,8 +38,7 @@ 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(
@ -52,7 +50,6 @@ class FollowedTagsViewModel @Inject constructor(
emptyList() emptyList()
}) })
} }
}
companion object { companion object {
private const val TAG = "FollowedTagsViewModel" private const val TAG = "FollowedTagsViewModel"

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(