Implement status() without rxjava (#2999)

* Implement status() without rxjava

* Use lambda param named `throwable`

* Update DraftsActivity.kt
This commit is contained in:
Nik Clayton 2022-12-06 20:23:48 +01:00 committed by GitHub
parent 0f11e5c062
commit 0d962c7cc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 16 deletions

View File

@ -25,8 +25,7 @@ import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.androidx.lifecycle.AndroidLifecycleScopeProvider.from
import autodispose2.autoDispose
import at.connyduck.calladapter.networkresult.fold
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.snackbar.Snackbar
import com.keylesspalace.tusky.BaseActivity
@ -37,7 +36,6 @@ import com.keylesspalace.tusky.db.DraftEntity
import com.keylesspalace.tusky.di.ViewModelFactory
import com.keylesspalace.tusky.util.parseAsMastodonHtml
import com.keylesspalace.tusky.util.visible
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import retrofit2.HttpException
@ -88,13 +86,17 @@ class DraftsActivity : BaseActivity(), DraftActionListener {
}
override fun onOpenDraft(draft: DraftEntity) {
if (draft.inReplyToId == null) {
openDraftWithoutReply(draft)
return
}
if (draft.inReplyToId != null) {
val context = this as Context
lifecycleScope.launch {
bottomSheet.state = BottomSheetBehavior.STATE_COLLAPSED
viewModel.getStatus(draft.inReplyToId)
.observeOn(AndroidSchedulers.mainThread())
.autoDispose(from(this))
.subscribe(
.fold(
{ status ->
val composeOptions = ComposeActivity.ComposeOptions(
draftId = draft.id,
@ -113,10 +115,9 @@ class DraftsActivity : BaseActivity(), DraftActionListener {
bottomSheet.state = BottomSheetBehavior.STATE_HIDDEN
startActivity(ComposeActivity.startIntent(this, composeOptions))
startActivity(ComposeActivity.startIntent(context, composeOptions))
},
{ throwable ->
bottomSheet.state = BottomSheetBehavior.STATE_HIDDEN
Log.w(TAG, "failed loading reply information", throwable)
@ -124,7 +125,7 @@ class DraftsActivity : BaseActivity(), DraftActionListener {
if (throwable is HttpException && throwable.code() == 404) {
// the original status to which a reply was drafted has been deleted
// let's open the ComposeActivity without reply information
Toast.makeText(this, getString(R.string.drafts_post_reply_removed), Toast.LENGTH_LONG).show()
Toast.makeText(context, getString(R.string.drafts_post_reply_removed), Toast.LENGTH_LONG).show()
openDraftWithoutReply(draft)
} else {
Snackbar.make(binding.root, getString(R.string.drafts_failed_loading_reply), Snackbar.LENGTH_SHORT)
@ -132,8 +133,6 @@ class DraftsActivity : BaseActivity(), DraftActionListener {
}
}
)
} else {
openDraftWithoutReply(draft)
}
}

View File

@ -20,12 +20,12 @@ import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.cachedIn
import at.connyduck.calladapter.networkresult.NetworkResult
import com.keylesspalace.tusky.db.AccountManager
import com.keylesspalace.tusky.db.AppDatabase
import com.keylesspalace.tusky.db.DraftEntity
import com.keylesspalace.tusky.entity.Status
import com.keylesspalace.tusky.network.MastodonApi
import io.reactivex.rxjava3.core.Single
import kotlinx.coroutines.launch
import javax.inject.Inject
@ -60,7 +60,7 @@ class DraftsViewModel @Inject constructor(
}
}
fun getStatus(statusId: String): Single<Status> {
suspend fun getStatus(statusId: String): NetworkResult<Status> {
return api.status(statusId)
}

View File

@ -165,9 +165,9 @@ interface MastodonApi {
): NetworkResult<Status>
@GET("api/v1/statuses/{id}")
fun status(
suspend fun status(
@Path("id") statusId: String
): Single<Status>
): NetworkResult<Status>
@GET("api/v1/statuses/{id}")
suspend fun statusAsync(