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 package com.android.car.customization.tool.domain 18 19 import com.android.car.customization.tool.domain.menu.MenuAction 20 import com.android.car.customization.tool.domain.menu.MenuController 21 import com.android.car.customization.tool.domain.menu.ReloadMenuAction 22 import com.android.car.customization.tool.domain.panel.PanelAction 23 import com.android.car.customization.tool.domain.panel.PanelController 24 import com.android.car.customization.tool.domain.panel.ReloadPanelAction 25 import javax.inject.Inject 26 27 /** 28 * Main StateMachine, accepts [Action] and creates new [PageState]. 29 * 30 * This state machine is directly responsible for actions that change the state of the whole tool 31 * (like [Action.ToggleToolAction]) and redirects all of the other actions to the [MenuController] 32 * and the [PanelController]. 33 * 34 * @property menuController the [MenuController]. 35 * @property panelController the [PanelController]. 36 */ 37 internal class CustomizationToolStateMachine @Inject constructor( 38 private val menuController: MenuController, 39 private val panelController: PanelController, 40 ) : StateMachine<PageState, Action>(allowDuplicates = false) { 41 42 override var currentState: PageState = PageState( 43 isOpen = false, 44 menu = menuController.buildMenu(), 45 panel = null 46 ) 47 reducernull48 override fun reducer(state: PageState, action: Action): PageState = when (action) { 49 ToggleUiAction -> state.copy(isOpen = !state.isOpen) 50 51 is ReloadStateAction -> { 52 state.copy( 53 menu = menuController.handleAction(state.menu, ReloadMenuAction), 54 panel = panelController.handleAction(state.panel, ReloadPanelAction) 55 ) 56 } 57 58 is MenuAction -> { 59 state.copy( 60 menu = menuController.handleAction(state.menu, action) 61 ) 62 } 63 64 is PanelAction -> { 65 state.copy( 66 panel = panelController.handleAction(state.panel, action) 67 ) 68 } 69 70 else -> throw IllegalArgumentException("Action $action has not been implemented") 71 } 72 } 73