1package com.parseable.android.ui.screens.alerts
3import androidx.lifecycle.ViewModel
4import androidx.lifecycle.viewModelScope
5import com.parseable.android.data.model.Alert
6import com.parseable.android.data.model.ApiResult
7import com.parseable.android.data.repository.ParseableRepository
8import dagger.hilt.android.lifecycle.HiltViewModel
9import kotlinx.coroutines.flow.MutableStateFlow
10import kotlinx.coroutines.flow.StateFlow
11import kotlinx.coroutines.flow.asStateFlow
12import kotlinx.coroutines.flow.update
13import kotlinx.coroutines.launch
14import javax.inject.Inject
16data class AlertsState(
17 val alerts: List<Alert> = emptyList(),
18 val isLoading: Boolean = false,
19 val error: String? = null,
20 val alertToDelete: Alert? = null,
24class AlertsViewModel @Inject constructor(
25 private val repository: ParseableRepository,
28 private val _state = MutableStateFlow(AlertsState())
29 val state: StateFlow<AlertsState> = _state.asStateFlow()
32 viewModelScope.launch {
33 _state.update { it.copy(isLoading = true, error = null) }
34 when (val result = repository.listAlerts()) {
35 is ApiResult.Success -> {
36 val sorted = result.data.sortedBy { a ->
37 (a.name ?: a.title ?: "").lowercase()
39 _state.update { it.copy(alerts = sorted, isLoading = false) }
41 is ApiResult.Error -> {
42 _state.update { it.copy(isLoading = false, error = result.userMessage) }
48 fun requestDelete(alert: Alert) {
49 _state.update { it.copy(alertToDelete = alert) }
53 _state.update { it.copy(alertToDelete = null) }
56 fun deleteAlert(alertId: String) {
57 viewModelScope.launch {
58 _state.update { it.copy(alertToDelete = null, isLoading = true, error = null) }
59 when (val result = repository.deleteAlert(alertId)) {
60 is ApiResult.Success -> refresh()
61 is ApiResult.Error -> {
62 _state.update { it.copy(isLoading = false, error = result.userMessage) }
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list