1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18 package com.android.wallpaper.picker.undo.ui.viewmodel 19 20 import com.android.wallpaper.R 21 import com.android.wallpaper.module.logging.UserEventLogger 22 import com.android.wallpaper.picker.common.button.ui.viewmodel.ButtonStyle 23 import com.android.wallpaper.picker.common.button.ui.viewmodel.ButtonViewModel 24 import com.android.wallpaper.picker.common.dialog.ui.viewmodel.DialogViewModel 25 import com.android.wallpaper.picker.common.icon.ui.viewmodel.Icon 26 import com.android.wallpaper.picker.common.text.ui.viewmodel.Text 27 import com.android.wallpaper.picker.undo.domain.interactor.UndoInteractor 28 import kotlinx.coroutines.flow.Flow 29 import kotlinx.coroutines.flow.MutableStateFlow 30 import kotlinx.coroutines.flow.asStateFlow 31 32 /** Models the UI state of the undo system. */ 33 class UndoViewModel( 34 private val interactor: UndoInteractor, 35 private val logger: UserEventLogger, 36 ) { 37 /** Whether the "revert" button should be visible. */ 38 val isRevertButtonVisible: Flow<Boolean> = interactor.isUndoable 39 private val _dialog = MutableStateFlow<DialogViewModel?>(null) 40 /** 41 * A view-model of the undo confirmation dialog that should be shown, or `null` when no dialog 42 * should be shown. 43 */ 44 val dialog: Flow<DialogViewModel?> = _dialog.asStateFlow() 45 46 /** Notifies that the "revert" button has been clicked by the user. */ onRevertButtonClickednull47 fun onRevertButtonClicked() { 48 _dialog.value = 49 DialogViewModel( 50 icon = 51 Icon.Resource( 52 res = R.drawable.ic_device_reset, 53 contentDescription = null, 54 ), 55 headline = Text.Resource(R.string.reset_confirmation_dialog_title), 56 message = Text.Resource(R.string.reset_confirmation_dialog_message), 57 buttons = 58 listOf( 59 ButtonViewModel( 60 text = Text.Resource(R.string.cancel), 61 style = ButtonStyle.Secondary, 62 ), 63 ButtonViewModel( 64 text = Text.Resource(R.string.reset), 65 style = ButtonStyle.Primary, 66 onClicked = { 67 interactor.revertAll() 68 logger.logResetApplied() 69 _dialog.value = null 70 }, 71 ), 72 ), 73 onDismissed = { _dialog.value = null }, 74 ) 75 } 76 } 77