SettingsViewModel.feedback.snippet.kt

1.4 KB · raw

1/*2 * D8 — additions to `ui/settings/SettingsViewModel.kt`.3 *4 * 1. Add `private val feedback: FeedbackRepository` to the constructor's5 *    @Inject parameter list (Hilt provides it automatically — see INTEGRATION.md6 *    for why no module binding is required).7 * 2. Paste the state flow + methods below into the class body.8 * 3. Add the imports listed in INTEGRATION.md.9 *10 * Constructor becomes (illustrative — keep existing params):11 *12 *   @HiltViewModel13 *   class SettingsViewModel @Inject constructor(14 *       private val vault: VaultRepository,15 *       private val license: LicenseRepository,16 *       private val feedback: FeedbackRepository,17 *       @ApplicationContext private val appContext: Context,18 *   ) : ViewModel() {19 */2021private val _feedbackState = MutableStateFlow<FeedbackState>(FeedbackState.Idle)22val feedbackState: StateFlow<FeedbackState> = _feedbackState.asStateFlow()2324fun sendFeedback(type: String, message: String) {25    if (_feedbackState.value is FeedbackState.Sending) return26    _feedbackState.value = FeedbackState.Sending27    viewModelScope.launch {28        _feedbackState.value = try {29            feedback.submit(type, message)30            FeedbackState.Sent31        } catch (e: Exception) {32            FeedbackState.Error("Couldn't send feedback — check your connection")33        }34    }35}3637/** Reset to Idle when the Feedback card leaves the screen. */38fun resetFeedback() {39    _feedbackState.value = FeedbackState.Idle40}