introduce SnackbarEvent instead of DomainBlockEvent

This commit is contained in:
Conny Duck 2023-07-05 19:42:30 +02:00
parent 2393ff1e4d
commit 8e56b5429b
3 changed files with 50 additions and 45 deletions

View File

@ -41,11 +41,7 @@ class DomainBlocksFragment : Fragment(R.layout.fragment_domain_blocks), Injectab
viewLifecycleOwner.lifecycleScope.launch {
viewModel.uiEvents.collect { event ->
when (event) {
is DomainBlockEvent.UnblockError -> showUnmuteError(event.domain)
is DomainBlockEvent.BlockError -> showMuteError(event.domain)
is DomainBlockEvent.BlockSuccess -> showUnmuteSuccess(event.domain)
}
showSnackbar(event)
}
}
@ -75,30 +71,17 @@ class DomainBlocksFragment : Fragment(R.layout.fragment_domain_blocks), Injectab
}
}
private fun showUnmuteError(domain: String) {
showSnackbar(
getString(R.string.error_unblocking_domain, domain),
R.string.action_retry
) { viewModel.unblock(domain) }
}
private fun showSnackbar(event: SnackbarEvent) {
val message = if (event.throwable == null) {
getString(event.message, event.domain)
} else {
Log.w(TAG, event.throwable)
val error = event.throwable.localizedMessage ?: getString(R.string.ui_error_unknown)
getString(event.message, event.domain, error)
}
private fun showMuteError(domain: String) {
showSnackbar(
getString(R.string.error_blocking_domain, domain),
R.string.action_retry
) { viewModel.block(domain) }
}
private fun showUnmuteSuccess(domain: String) {
showSnackbar(
getString(R.string.confirmation_domain_unmuted, domain),
R.string.action_undo
) { viewModel.block(domain) }
}
private fun showSnackbar(message: String, actionText: Int, action: (View) -> Unit) {
Snackbar.make(binding.recyclerView, message, Snackbar.LENGTH_LONG)
.setAction(actionText, action)
.setAction(event.actionText, event.action)
.show()
}

View File

@ -1,6 +1,7 @@
package com.keylesspalace.tusky.components.domainblocks
import android.util.Log
import android.view.View
import androidx.annotation.StringRes
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.ExperimentalPagingApi
@ -8,6 +9,7 @@ import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.cachedIn
import at.connyduck.calladapter.networkresult.fold
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.network.MastodonApi
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch
@ -17,7 +19,7 @@ class DomainBlocksViewModel @Inject constructor(
private val api: MastodonApi
) : ViewModel() {
val domains: MutableList<String> = mutableListOf()
val uiEvents = MutableSharedFlow<DomainBlockEvent>()
val uiEvents = MutableSharedFlow<SnackbarEvent>()
var nextKey: String? = null
var currentSource: DomainBlocksPagingSource? = null
@ -40,8 +42,15 @@ class DomainBlocksViewModel @Inject constructor(
domains.add(domain)
currentSource?.invalidate()
}, { e ->
Log.w(TAG, "Error blocking domain $domain", e)
uiEvents.emit(DomainBlockEvent.BlockError(domain))
uiEvents.emit(
SnackbarEvent(
message = R.string.error_blocking_domain,
domain = domain,
throwable = e,
actionText = R.string.action_retry,
action = { block(domain) }
)
)
})
}
}
@ -51,21 +60,34 @@ class DomainBlocksViewModel @Inject constructor(
api.unblockDomain(domain).fold({
domains.remove(domain)
currentSource?.invalidate()
uiEvents.emit(DomainBlockEvent.BlockSuccess(domain))
uiEvents.emit(
SnackbarEvent(
message = R.string.confirmation_domain_unmuted,
domain = domain,
throwable = null,
actionText = R.string.action_undo,
action = { block(domain) }
)
)
}, { e ->
Log.w(TAG, "Error unblocking domain $domain", e)
uiEvents.emit(DomainBlockEvent.UnblockError(domain))
uiEvents.emit(
SnackbarEvent(
message = R.string.error_unblocking_domain,
domain = domain,
throwable = e,
actionText = R.string.action_retry,
action = { unblock(domain) }
)
)
})
}
}
companion object {
private const val TAG = "DomainBlocksViewModel"
}
}
sealed class DomainBlockEvent {
data class BlockSuccess(val domain: String) : DomainBlockEvent()
data class UnblockError(val domain: String) : DomainBlockEvent()
data class BlockError(val domain: String) : DomainBlockEvent()
}
class SnackbarEvent(
@StringRes val message: Int,
val domain: String,
val throwable: Throwable?,
@StringRes val actionText: Int,
val action: (View) -> Unit
)

View File

@ -44,8 +44,8 @@
<string name="error_following_hashtags_unsupported">This instance does not support following hashtags.</string>
<string name="error_muting_hashtag_format">Error muting #%s</string>
<string name="error_unmuting_hashtag_format">Error unmuting #%s</string>
<string name="error_blocking_domain">Failed to mute %s</string>
<string name="error_unblocking_domain">Failed to unmute %s</string>
<string name="error_blocking_domain">Failed to mute %1$s: %2$s</string>
<string name="error_unblocking_domain">Failed to unmute %1$s: %2$s</string>
<string name="error_status_source_load">Failed to load the status source from the server.</string>
<string name="title_login">Login</string>