1 /* 2 * Copyright (C) 2024 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 package com.android.wallpaper.picker.customization.ui.viewmodel 18 19 import com.android.wallpaper.picker.customization.ui.util.CustomizationOptionUtil 20 import dagger.assisted.Assisted 21 import dagger.assisted.AssistedFactory 22 import dagger.assisted.AssistedInject 23 import dagger.hilt.android.scopes.ViewModelScoped 24 import kotlinx.coroutines.CoroutineScope 25 import kotlinx.coroutines.flow.Flow 26 import kotlinx.coroutines.flow.MutableStateFlow 27 import kotlinx.coroutines.flow.asStateFlow 28 29 class DefaultCustomizationOptionsViewModel 30 @AssistedInject 31 constructor( 32 wallpaperCarouselViewModelFactory: WallpaperCarouselViewModel.Factory, 33 @Assisted viewModelScope: CoroutineScope, 34 ) : CustomizationOptionsViewModel { 35 36 override val wallpaperCarouselViewModel = 37 wallpaperCarouselViewModelFactory.create(viewModelScope) 38 39 private val _selectedOptionState = 40 MutableStateFlow<CustomizationOptionUtil.CustomizationOption?>(null) 41 override val selectedOption = _selectedOptionState.asStateFlow() 42 43 private val _discardChangesDialogViewModel: MutableStateFlow<DiscardChangesDialogViewModel?> = 44 MutableStateFlow(null) 45 override val discardChangesDialogViewModel: Flow<DiscardChangesDialogViewModel?> = 46 _discardChangesDialogViewModel.asStateFlow() 47 showDiscardChangesDialogViewModelnull48 fun showDiscardChangesDialogViewModel() { 49 _discardChangesDialogViewModel.value = 50 DiscardChangesDialogViewModel( 51 onDismiss = { _discardChangesDialogViewModel.value = null }, 52 onKeepEditing = { _discardChangesDialogViewModel.value = null }, 53 onDiscard = { 54 _discardChangesDialogViewModel.value = null 55 unselectOption() 56 }, 57 ) 58 } 59 handleBackPressednull60 override fun handleBackPressed(): Boolean { 61 return unselectOption() 62 } 63 64 /** 65 * Unselect the currently selected option. If the option is already unselected, do nothing and 66 * return false; otherwise, unselect and return true. 67 */ unselectOptionnull68 private fun unselectOption(): Boolean { 69 return if (_selectedOptionState.value != null) { 70 _selectedOptionState.value = null 71 true 72 } else { 73 false 74 } 75 } 76 resetPreviewnull77 override fun resetPreview() {} 78 selectOptionnull79 fun selectOption(option: CustomizationOptionUtil.CustomizationOption) { 80 _selectedOptionState.value = option 81 } 82 83 @ViewModelScoped 84 @AssistedFactory 85 interface Factory : CustomizationOptionsViewModelFactory { createnull86 override fun create(viewModelScope: CoroutineScope): DefaultCustomizationOptionsViewModel 87 } 88 } 89