Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PickContactGroupFragment: Show error in snackbar if query fails #624

Merged
merged 1 commit into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/src/main/java/com/chiller3/bcr/rule/PickContactGroupAlert.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* SPDX-FileCopyrightText: 2024 Andrew Gunnerson
* SPDX-License-Identifier: GPL-3.0-only
*/

package com.chiller3.bcr.rule

sealed interface PickContactGroupAlert {
data class QueryFailed(val error: String) : PickContactGroupAlert
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.preference.size
import com.chiller3.bcr.ContactGroupInfo
import com.chiller3.bcr.PreferenceBaseFragment
import com.chiller3.bcr.R
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.launch

class PickContactGroupFragment : PreferenceBaseFragment(), Preference.OnPreferenceClickListener {
Expand All @@ -26,6 +27,16 @@ class PickContactGroupFragment : PreferenceBaseFragment(), Preference.OnPreferen
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.record_rules_preferences, rootKey)

lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.alerts.collect {
it.firstOrNull()?.let { alert ->
onAlert(alert)
}
}
}
}

lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.groups.collect {
Expand Down Expand Up @@ -74,6 +85,21 @@ class PickContactGroupFragment : PreferenceBaseFragment(), Preference.OnPreferen
return false
}

private fun onAlert(alert: PickContactGroupAlert) {
val msg = when (alert) {
is PickContactGroupAlert.QueryFailed ->
getString(R.string.alert_contact_group_query_failure, alert.error)
}

Snackbar.make(requireView(), msg, Snackbar.LENGTH_LONG)
.addCallback(object : Snackbar.Callback() {
override fun onDismissed(transientBottomBar: Snackbar?, event: Int) {
viewModel.acknowledgeFirstAlert()
}
})
.show()
}

companion object {
private const val PREF_GROUP_PREFIX = "group_"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ import com.chiller3.bcr.ContactGroupInfo
import com.chiller3.bcr.withContactGroups
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class PickContactGroupViewModel(application: Application) : AndroidViewModel(application) {
private val _alerts = MutableStateFlow<List<PickContactGroupAlert>>(emptyList())
val alerts = _alerts.asStateFlow()

private val _groups = MutableStateFlow<List<ContactGroupInfo>>(emptyList())
val groups: StateFlow<List<ContactGroupInfo>> = _groups
val groups = _groups.asStateFlow()

init {
refreshGroups()
Expand All @@ -29,8 +32,8 @@ class PickContactGroupViewModel(application: Application) : AndroidViewModel(app
private fun refreshGroups() {
viewModelScope.launch {
withContext(Dispatchers.IO) {
val groups = try {
withContactGroups(getApplication()) { contactGroups ->
try {
val groups = withContactGroups(getApplication()) { contactGroups ->
contactGroups
.sortedWith { o1, o2 ->
compareValuesBy(
Expand All @@ -43,16 +46,20 @@ class PickContactGroupViewModel(application: Application) : AndroidViewModel(app
}
.toList()
}

_groups.update { groups }
} catch (e: Exception) {
Log.w(TAG, "Failed to list all contact groups", e)
return@withContext
_alerts.update { it + PickContactGroupAlert.QueryFailed(e.toString()) }
}

_groups.update { groups }
}
}
}

fun acknowledgeFirstAlert() {
_alerts.update { it.drop(1) }
}

companion object {
private val TAG = PickContactGroupViewModel::class.java.simpleName
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import com.chiller3.bcr.getContactGroupById
import com.chiller3.bcr.withContactsByUri
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
Expand Down Expand Up @@ -104,10 +104,10 @@ class RecordRulesViewModel(application: Application) : AndroidViewModel(applicat
private val prefs = Preferences(getApplication())

private val _messages = MutableStateFlow<List<Message>>(emptyList())
val messages: StateFlow<List<Message>> = _messages
val messages = _messages.asStateFlow()

private val _rules = MutableStateFlow<List<DisplayedRecordRule>>(emptyList())
val rules: StateFlow<List<DisplayedRecordRule>> = _rules
val rules = _rules.asStateFlow()

private val rulesMutex = Mutex()

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
<!-- Snackbar alerts -->
<string name="alert_logcat_success">Successfully saved logs to %1$s</string>
<string name="alert_logcat_failure">Failed to save logs to %1$s: %2$s</string>
<string name="alert_contact_group_query_failure">Failed to query contact groups: %1$s</string>

<!-- Notifications -->
<string name="notification_channel_persistent_name">Background services</string>
Expand Down