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.panel 18 19 import javax.inject.Inject 20 import javax.inject.Provider 21 22 /** 23 * The Controller for all the possible panels in the tool. 24 * 25 * It receives [PanelAction] from the main state machine and then processes it or redirects 26 * it to a specific [PanelActionReducer]. 27 * 28 * @property panelActionReducers a map of all of the [PanelActionReducer] available in the system. 29 */ 30 internal class PanelController @Inject constructor( 31 private val panelActionReducers: Map< 32 Class<out PanelActionReducer>, 33 @JvmSuppressWildcards Provider<PanelActionReducer> 34 >, 35 ) { 36 37 private var currentPanelActionReducer: PanelActionReducer? = null 38 handleActionnull39 fun handleAction(panel: Panel?, action: PanelAction): Panel? = when (action) { 40 is OpenPanelAction -> { 41 currentPanelActionReducer = panelActionReducers[action.panelClass.java]?.get() 42 currentPanelActionReducer?.bundle = action.bundle 43 currentPanelActionReducer?.build() 44 } 45 46 ClosePanelAction -> { 47 currentPanelActionReducer = null 48 null 49 } 50 51 ReloadPanelAction -> { 52 currentPanelActionReducer?.build() 53 } 54 55 else -> { 56 // Here there has to be a valid panel and a valid panelReducer, otherwise it should crash. 57 requireNotNull(panel) 58 currentPanelActionReducer?.reduce(panel, action) 59 } 60 } 61 } 62