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 /** Hides text and places the provided text in the content description instead */ 44 val isTextUserVisible: Boolean = true, 45 46 /** Whether this option is selected. */ 47 val isSelected: StateFlow<Boolean>, 48 49 /** Whether this option is enabled. */ 50 val isEnabled: Boolean = true, 51 52 /** Notifies that the option has been clicked by the user. */ 53 val onClicked: Flow<(() -> Unit)?>, 54 55 /** Notifies that the option has been long-clicked by the user. */ 56 val onLongClicked: (() -> Unit)? = null, 57 ) { equalsnull58 override fun equals(other: Any?): Boolean { 59 val otherItem = other as? OptionItemViewModel<*> ?: return false 60 // skipping comparison of onClicked because it is correlated with 61 // changes on isSelected 62 return this.payload == otherItem.payload && 63 this.text == otherItem.text && 64 this.isSelected.value == otherItem.isSelected.value && 65 this.isEnabled == otherItem.isEnabled && 66 this.onLongClicked == otherItem.onLongClicked 67 } 68 } 69