• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.option.ui.viewmodel
19 
20 import com.android.wallpaper.picker.common.text.ui.viewmodel.Text
21 import kotlinx.coroutines.flow.Flow
22 import kotlinx.coroutines.flow.StateFlow
23 
24 /** Models UI state for an item in a list of selectable options. */
25 data class OptionItemViewModel<Payload>(
26     /**
27      * A stable key that uniquely identifies this option amongst all other options in the same list
28      * of options.
29      */
30     val key: StateFlow<String>,
31 
32     /**
33      * The view model representing additional details needed for binding the icon of an option item
34      */
35     val payload: Payload? = null,
36 
37     /**
38      * A text to show to the user (or attach as content description on the icon, if there's no
39      * dedicated view for it).
40      */
41     val text: Text,
42 
43     /** Whether this option is selected. */
44     val isSelected: StateFlow<Boolean>,
45 
46     /** Whether this option is enabled. */
47     val isEnabled: Boolean = true,
48 
49     /** Notifies that the option has been clicked by the user. */
50     val onClicked: Flow<(() -> Unit)?>,
51 
52     /** Notifies that the option has been long-clicked by the user. */
53     val onLongClicked: (() -> Unit)? = null,
54 ) {
equalsnull55     override fun equals(other: Any?): Boolean {
56         val otherItem = other as? OptionItemViewModel<*> ?: return false
57         // skipping comparison of onClicked because it is correlated with
58         // changes on isSelected
59         return this.payload == otherItem.payload &&
60             this.text == otherItem.text &&
61             this.isSelected.value == otherItem.isSelected.value &&
62             this.isEnabled == otherItem.isEnabled &&
63             this.onLongClicked == otherItem.onLongClicked
64     }
65 }
66