• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.picker.common.button.ui.viewmodel.ButtonStyle
22 import com.android.wallpaper.picker.common.button.ui.viewmodel.ButtonViewModel
23 import com.android.wallpaper.picker.common.dialog.ui.viewmodel.DialogViewModel
24 import com.android.wallpaper.picker.common.icon.ui.viewmodel.Icon
25 import com.android.wallpaper.picker.common.text.ui.viewmodel.Text
26 import com.android.wallpaper.picker.undo.domain.interactor.UndoInteractor
27 import kotlinx.coroutines.flow.Flow
28 import kotlinx.coroutines.flow.MutableStateFlow
29 import kotlinx.coroutines.flow.asStateFlow
30 
31 /** Models the UI state of the undo system. */
32 class UndoViewModel(
33     private val interactor: UndoInteractor,
34 ) {
35     /** Whether the "revert" button should be visible. */
36     val isRevertButtonVisible: Flow<Boolean> = interactor.isUndoable
37     private val _dialog = MutableStateFlow<DialogViewModel?>(null)
38     /**
39      * A view-model of the undo confirmation dialog that should be shown, or `null` when no dialog
40      * should be shown.
41      */
42     val dialog: Flow<DialogViewModel?> = _dialog.asStateFlow()
43 
44     /** Notifies that the "revert" button has been clicked by the user. */
onRevertButtonClickednull45     fun onRevertButtonClicked() {
46         _dialog.value =
47             DialogViewModel(
48                 icon =
49                     Icon.Resource(
50                         res = R.drawable.ic_device_reset,
51                         contentDescription = null,
52                     ),
53                 title = Text.Resource(R.string.reset_confirmation_dialog_title),
54                 message = Text.Resource(R.string.reset_confirmation_dialog_message),
55                 buttons =
56                     listOf(
57                         ButtonViewModel(
58                             text = Text.Resource(R.string.cancel),
59                             style = ButtonStyle.Secondary,
60                         ),
61                         ButtonViewModel(
62                             text = Text.Resource(R.string.reset),
63                             style = ButtonStyle.Primary,
64                             onClicked = {
65                                 interactor.revertAll()
66                                 _dialog.value = null
67                             },
68                         ),
69                     ),
70                 onDismissed = { _dialog.value = null },
71             )
72     }
73 }
74