1 /*
2  * Copyright (C) 2021 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 package com.android.customization.model.grid;
17 
18 import androidx.lifecycle.SavedStateHandle;
19 import androidx.lifecycle.ViewModel;
20 
21 /** The class to store status of the grid fragment view. */
22 public class GridOptionViewModel extends ViewModel {
23     private static final String SELECTED_OPTION_KEY = "selected_option";
24     private static final String BOTTOM_ACTION_BAR_VISIBLE_KEY = "bottom_action_bar_visible";
25 
26     private SavedStateHandle mState;
27 
GridOptionViewModel(SavedStateHandle savedStateHandle)28     public GridOptionViewModel(SavedStateHandle savedStateHandle) {
29         mState = savedStateHandle;
30     }
31 
32     /** Gets selected {@link GridOption} from {@link SavedStateHandle} */
getSelectedOption()33     public GridOption getSelectedOption() {
34         return mState.get(SELECTED_OPTION_KEY);
35     }
36 
37     /** Sets selected {@link GridOption} to {@link SavedStateHandle} */
setSelectedOption(GridOption selectedOption)38     public void setSelectedOption(GridOption selectedOption) {
39         mState.set(SELECTED_OPTION_KEY, selectedOption);
40     }
41 
42     /** Gets bottom action bar visible from {@link SavedStateHandle} */
getBottomActionBarVisible()43     public boolean getBottomActionBarVisible() {
44         return mState.contains(BOTTOM_ACTION_BAR_VISIBLE_KEY)
45                 ? mState.get(BOTTOM_ACTION_BAR_VISIBLE_KEY)
46                 : false;
47     }
48 
49     /** Sets bottom action bar visible to {@link SavedStateHandle} */
setBottomActionBarVisible(boolean bottomActionBarVisible)50     public void setBottomActionBarVisible(boolean bottomActionBarVisible) {
51         mState.set(BOTTOM_ACTION_BAR_VISIBLE_KEY, bottomActionBarVisible);
52     }
53 }
54