From 9b775c6a91b91531edda67892b93041e5fb3f882 Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Thu, 16 Jun 2022 22:41:54 +0300 Subject: [PATCH] Automatically create screens for applist and settings (#1153) Apps and settings are now stored in a single array (two arrays in total). Replace magic values with appsPerScreen and entriesPerScreen. --- src/displayapp/screens/ApplicationList.cpp | 72 +++++------------ src/displayapp/screens/ApplicationList.h | 31 +++++++- src/displayapp/screens/settings/Settings.cpp | 83 +++++--------------- src/displayapp/screens/settings/Settings.h | 39 +++++++-- 4 files changed, 100 insertions(+), 125 deletions(-) diff --git a/src/displayapp/screens/ApplicationList.cpp b/src/displayapp/screens/ApplicationList.cpp index 9798a861..9fd408e1 100644 --- a/src/displayapp/screens/ApplicationList.cpp +++ b/src/displayapp/screens/ApplicationList.cpp @@ -1,13 +1,23 @@ #include "displayapp/screens/ApplicationList.h" #include -#include -#include "displayapp/screens/Symbols.h" -#include "displayapp/screens/Tile.h" +#include #include "displayapp/Apps.h" #include "displayapp/DisplayApp.h" using namespace Pinetime::Applications::Screens; +constexpr std::array ApplicationList::applications; + +auto ApplicationList::CreateScreenList() const { + std::array()>, nScreens> screens; + for (size_t i = 0; i < screens.size(); i++) { + screens[i] = [this, i]() -> std::unique_ptr { + return CreateScreen(i); + }; + } + return screens; +} + ApplicationList::ApplicationList(Pinetime::Applications::DisplayApp* app, Pinetime::Controllers::Settings& settingsController, Pinetime::Controllers::Battery& batteryController, @@ -16,18 +26,7 @@ ApplicationList::ApplicationList(Pinetime::Applications::DisplayApp* app, settingsController {settingsController}, batteryController {batteryController}, dateTimeController {dateTimeController}, - screens {app, - settingsController.GetAppMenu(), - { - [this]() -> std::unique_ptr { - return CreateScreen1(); - }, - [this]() -> std::unique_ptr { - return CreateScreen2(); - }, - //[this]() -> std::unique_ptr { return CreateScreen3(); } - }, - Screens::ScreenListModes::UpDown} { + screens {app, settingsController.GetAppMenu(), CreateScreenList(), Screens::ScreenListModes::UpDown} { } ApplicationList::~ApplicationList() { @@ -38,42 +37,11 @@ bool ApplicationList::OnTouchEvent(Pinetime::Applications::TouchEvents event) { return screens.OnTouchEvent(event); } -std::unique_ptr ApplicationList::CreateScreen1() { - std::array applications {{ - {Symbols::stopWatch, Apps::StopWatch}, - {Symbols::clock, Apps::Alarm}, - {Symbols::hourGlass, Apps::Timer}, - {Symbols::shoe, Apps::Steps}, - {Symbols::heartBeat, Apps::HeartRate}, - {Symbols::music, Apps::Music}, - }}; +std::unique_ptr ApplicationList::CreateScreen(unsigned int screenNum) const { + std::array apps; + for (int i = 0; i < appsPerScreen; i++) { + apps[i] = applications[screenNum * appsPerScreen + i]; + } - return std::make_unique(0, 2, app, settingsController, batteryController, dateTimeController, applications); + return std::make_unique(screenNum, nScreens, app, settingsController, batteryController, dateTimeController, apps); } - -std::unique_ptr ApplicationList::CreateScreen2() { - std::array applications {{ - {Symbols::paintbrush, Apps::Paint}, - {Symbols::paddle, Apps::Paddle}, - {"2", Apps::Twos}, - {Symbols::chartLine, Apps::Motion}, - {Symbols::drum, Apps::Metronome}, - {Symbols::map, Apps::Navigation}, - }}; - - return std::make_unique(1, 2, app, settingsController, batteryController, dateTimeController, applications); -} - -/*std::unique_ptr ApplicationList::CreateScreen3() { - std::array applications { - {{"A", Apps::Meter}, - {"B", Apps::Navigation}, - {"C", Apps::Clock}, - {"D", Apps::Music}, - {"E", Apps::SysInfo}, - {"F", Apps::Brightness} - } - }; - - return std::make_unique(2, 3, app, settingsController, batteryController, dateTimeController, applications); -}*/ diff --git a/src/displayapp/screens/ApplicationList.h b/src/displayapp/screens/ApplicationList.h index f430a89e..14d7623c 100644 --- a/src/displayapp/screens/ApplicationList.h +++ b/src/displayapp/screens/ApplicationList.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include "displayapp/screens/Screen.h" @@ -7,6 +8,8 @@ #include "components/datetime/DateTimeController.h" #include "components/settings/Settings.h" #include "components/battery/BatteryController.h" +#include "displayapp/screens/Symbols.h" +#include "displayapp/screens/Tile.h" namespace Pinetime { namespace Applications { @@ -21,14 +24,34 @@ namespace Pinetime { bool OnTouchEvent(TouchEvents event) override; private: + auto CreateScreenList() const; + std::unique_ptr CreateScreen(unsigned int screenNum) const; + Controllers::Settings& settingsController; Pinetime::Controllers::Battery& batteryController; Controllers::DateTime& dateTimeController; - ScreenList<2> screens; - std::unique_ptr CreateScreen1(); - std::unique_ptr CreateScreen2(); - // std::unique_ptr CreateScreen3(); + static constexpr int appsPerScreen = 6; + + // Increment this when more space is needed + static constexpr int nScreens = 2; + + static constexpr std::array applications {{ + {Symbols::stopWatch, Apps::StopWatch}, + {Symbols::clock, Apps::Alarm}, + {Symbols::hourGlass, Apps::Timer}, + {Symbols::shoe, Apps::Steps}, + {Symbols::heartBeat, Apps::HeartRate}, + {Symbols::music, Apps::Music}, + + {Symbols::paintbrush, Apps::Paint}, + {Symbols::paddle, Apps::Paddle}, + {"2", Apps::Twos}, + {Symbols::chartLine, Apps::Motion}, + {Symbols::drum, Apps::Metronome}, + {Symbols::map, Apps::Navigation}, + }}; + ScreenList screens; }; } } diff --git a/src/displayapp/screens/settings/Settings.cpp b/src/displayapp/screens/settings/Settings.cpp index a91b8f77..ffa01d18 100644 --- a/src/displayapp/screens/settings/Settings.cpp +++ b/src/displayapp/screens/settings/Settings.cpp @@ -1,33 +1,27 @@ #include "displayapp/screens/settings/Settings.h" #include -#include -#include "displayapp/screens/List.h" +#include #include "displayapp/Apps.h" #include "displayapp/DisplayApp.h" -#include "displayapp/screens/Symbols.h" using namespace Pinetime::Applications::Screens; +constexpr std::array Settings::entries; + +auto Settings::CreateScreenList() const { + std::array()>, nScreens> screens; + for (size_t i = 0; i < screens.size(); i++) { + screens[i] = [this, i]() -> std::unique_ptr { + return CreateScreen(i); + }; + } + return screens; +} + Settings::Settings(Pinetime::Applications::DisplayApp* app, Pinetime::Controllers::Settings& settingsController) : Screen(app), settingsController {settingsController}, - screens {app, - settingsController.GetSettingsMenu(), - { - [this]() -> std::unique_ptr { - return CreateScreen1(); - }, - [this]() -> std::unique_ptr { - return CreateScreen2(); - }, - [this]() -> std::unique_ptr { - return CreateScreen3(); - }, - [this]() -> std::unique_ptr { - return CreateScreen4(); - }, - }, - Screens::ScreenListModes::UpDown} { + screens {app, settingsController.GetSettingsMenu(), CreateScreenList(), Screens::ScreenListModes::UpDown} { } Settings::~Settings() { @@ -38,48 +32,11 @@ bool Settings::OnTouchEvent(Pinetime::Applications::TouchEvents event) { return screens.OnTouchEvent(event); } -std::unique_ptr Settings::CreateScreen1() { - std::array applications {{ - {Symbols::sun, "Display", Apps::SettingDisplay}, - {Symbols::eye, "Wake Up", Apps::SettingWakeUp}, - {Symbols::clock, "Time format", Apps::SettingTimeFormat}, - {Symbols::home, "Watch face", Apps::SettingWatchFace}, - }}; +std::unique_ptr Settings::CreateScreen(unsigned int screenNum) const { + std::array screens; + for (int i = 0; i < entriesPerScreen; i++) { + screens[i] = entries[screenNum * entriesPerScreen + i]; + } - return std::make_unique(0, 4, app, settingsController, applications); -} - -std::unique_ptr Settings::CreateScreen2() { - std::array applications {{ - {Symbols::shoe, "Steps", Apps::SettingSteps}, - {Symbols::clock, "Set date", Apps::SettingSetDate}, - {Symbols::clock, "Set time", Apps::SettingSetTime}, - {Symbols::batteryHalf, "Battery", Apps::BatteryInfo}, - }}; - - return std::make_unique(1, 4, app, settingsController, applications); -} - -std::unique_ptr Settings::CreateScreen3() { - - std::array applications {{ - {Symbols::clock, "Chimes", Apps::SettingChimes}, - {Symbols::tachometer, "Shake Calib.", Apps::SettingShakeThreshold}, - {Symbols::check, "Firmware", Apps::FirmwareValidation}, - {Symbols::bluetooth, "Bluetooth", Apps::SettingBluetooth}, - }}; - - return std::make_unique(2, 4, app, settingsController, applications); -} - -std::unique_ptr Settings::CreateScreen4() { - - std::array applications {{ - {Symbols::list, "About", Apps::SysInfo}, - {Symbols::none, "None", Apps::None}, - {Symbols::none, "None", Apps::None}, - {Symbols::none, "None", Apps::None}, - }}; - - return std::make_unique(3, 4, app, settingsController, applications); + return std::make_unique(screenNum, nScreens, app, settingsController, screens); } diff --git a/src/displayapp/screens/settings/Settings.h b/src/displayapp/screens/settings/Settings.h index be090075..a86db44f 100644 --- a/src/displayapp/screens/settings/Settings.h +++ b/src/displayapp/screens/settings/Settings.h @@ -1,8 +1,11 @@ #pragma once -#include +#include +#include #include "displayapp/screens/Screen.h" #include "displayapp/screens/ScreenList.h" +#include "displayapp/screens/Symbols.h" +#include "displayapp/screens/List.h" namespace Pinetime { @@ -17,14 +20,38 @@ namespace Pinetime { bool OnTouchEvent(Pinetime::Applications::TouchEvents event) override; private: + auto CreateScreenList() const; + std::unique_ptr CreateScreen(unsigned int screenNum) const; + Controllers::Settings& settingsController; - ScreenList<4> screens; + static constexpr int entriesPerScreen = 4; - std::unique_ptr CreateScreen1(); - std::unique_ptr CreateScreen2(); - std::unique_ptr CreateScreen3(); - std::unique_ptr CreateScreen4(); + // Increment this when more space is needed + static constexpr int nScreens = 4; + + static constexpr std::array entries {{ + {Symbols::sun, "Display", Apps::SettingDisplay}, + {Symbols::eye, "Wake Up", Apps::SettingWakeUp}, + {Symbols::clock, "Time format", Apps::SettingTimeFormat}, + {Symbols::home, "Watch face", Apps::SettingWatchFace}, + + {Symbols::shoe, "Steps", Apps::SettingSteps}, + {Symbols::clock, "Set date", Apps::SettingSetDate}, + {Symbols::clock, "Set time", Apps::SettingSetTime}, + {Symbols::batteryHalf, "Battery", Apps::BatteryInfo}, + + {Symbols::clock, "Chimes", Apps::SettingChimes}, + {Symbols::tachometer, "Shake Calib.", Apps::SettingShakeThreshold}, + {Symbols::check, "Firmware", Apps::FirmwareValidation}, + {Symbols::bluetooth, "Bluetooth", Apps::SettingBluetooth}, + + {Symbols::list, "About", Apps::SysInfo}, + {Symbols::none, "None", Apps::None}, + {Symbols::none, "None", Apps::None}, + {Symbols::none, "None", Apps::None}, + }}; + ScreenList screens; }; } }