From 39a6fdaf1c38e41876011bbb9f2035f2b107811b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Fri, 4 Oct 2019 10:40:51 +0200 Subject: [PATCH] [apps/settings] MainController: get rid of magic numbers --- apps/settings/main_controller.cpp | 26 +++++++++++++------------- apps/settings/main_controller.h | 13 +++++++++++++ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/apps/settings/main_controller.cpp b/apps/settings/main_controller.cpp index 74ba5787c..e479266fe 100644 --- a/apps/settings/main_controller.cpp +++ b/apps/settings/main_controller.cpp @@ -66,13 +66,13 @@ bool MainController::handleEvent(Ion::Events::Event event) { if (event == Ion::Events::OK || event == Ion::Events::EXE || event == Ion::Events::Right) { GenericSubController * subController = nullptr; int rowIndex = selectedRow(); - if (rowIndex == 1) { + if (rowIndex == k_indexOfDisplayModeCell) { subController = &m_displayModeController; - } else if (rowIndex == 4 || rowIndex == 5) { + } else if (rowIndex == k_indexOfBrightnessCell || rowIndex == k_indexOfLanguageCell) { assert(false); - } else if (rowIndex == 6) { + } else if (rowIndex == k_indexOfExamModeCell) { subController = &m_examModeController; - } else if (rowIndex == 7 + hasPrompt()) { + } else if (rowIndex == k_indexOfAboutCell + hasPrompt()) { subController = &m_aboutController; } else { subController = &m_preferencesController; @@ -123,10 +123,10 @@ int MainController::reusableCellCount(int type) { } int MainController::typeAtLocation(int i, int j) { - if (j == 4) { + if (j == k_indexOfBrightnessCell) { return 1; } - if (hasPrompt() && j == 7) { + if (hasPrompt() && j == k_indexOfPopUpCell) { return 2; } return 0; @@ -137,18 +137,18 @@ void MainController::willDisplayCellForIndex(HighlightCell * cell, int index) { Preferences * preferences = Preferences::sharedPreferences(); MessageTableCell * myCell = (MessageTableCell *)cell; myCell->setMessage(model()->children(index)->label()); - if (index == 4) { + if (index == k_indexOfBrightnessCell) { MessageTableCellWithGauge * myGaugeCell = (MessageTableCellWithGauge *)cell; GaugeView * myGauge = (GaugeView *)myGaugeCell->accessoryView(); myGauge->setLevel((float)globalPreferences->brightnessLevel()/(float)Ion::Backlight::MaxBrightness); return; } - if (index == 5) { + if (index == k_indexOfLanguageCell) { int index = (int)globalPreferences->language()-1; static_cast(cell)->setSubtitle(I18n::LanguageNames[index]); return; } - if (hasPrompt() && index == 7) { + if (hasPrompt() && index == k_indexOfPopUpCell) { MessageTableCellWithSwitch * mySwitchCell = (MessageTableCellWithSwitch *)cell; SwitchView * mySwitch = (SwitchView *)mySwitchCell->accessoryView(); mySwitch->setState(globalPreferences->showPopUp()); @@ -157,16 +157,16 @@ void MainController::willDisplayCellForIndex(HighlightCell * cell, int index) { MessageTableCellWithChevronAndMessage * myTextCell = (MessageTableCellWithChevronAndMessage *)cell; int childIndex = -1; switch (index) { - case 0: + case k_indexOfAngleUnitCell: childIndex = (int)preferences->angleUnit(); break; - case 1: + case k_indexOfDisplayModeCell: childIndex = (int)preferences->displayMode(); break; - case 2: + case k_indexOfEditionModeCell: childIndex = (int)preferences->editionMode(); break; - case 3: + case k_indexOfComplexFormatCell: childIndex = (int)preferences->complexFormat(); break; } diff --git a/apps/settings/main_controller.h b/apps/settings/main_controller.h index 32db50c66..91187be6f 100644 --- a/apps/settings/main_controller.h +++ b/apps/settings/main_controller.h @@ -27,6 +27,19 @@ public: void willDisplayCellForIndex(HighlightCell * cell, int index) override; void viewWillAppear() override; private: + constexpr static int k_indexOfAngleUnitCell = 0; + constexpr static int k_indexOfDisplayModeCell = k_indexOfAngleUnitCell + 1; + constexpr static int k_indexOfEditionModeCell = k_indexOfDisplayModeCell + 1; + constexpr static int k_indexOfComplexFormatCell = k_indexOfEditionModeCell + 1; + constexpr static int k_indexOfBrightnessCell = k_indexOfComplexFormatCell + 1; + constexpr static int k_indexOfLanguageCell = k_indexOfBrightnessCell + 1; + constexpr static int k_indexOfExamModeCell = k_indexOfLanguageCell + 1; + /* Pop-up cell and About cell are located at the same index because pop-up + * cell is optional. We must always correct k_indexOfAboutCell with + * hasPrompt() (TODO: make hasPrompt() constexpr and correct + * k_indexOfAboutCell) */ + constexpr static int k_indexOfPopUpCell = k_indexOfExamModeCell + 1; + constexpr static int k_indexOfAboutCell = k_indexOfExamModeCell + 1; static const SettingsMessageTree * model(); StackViewController * stackController() const; I18n::Message promptMessage() const;