From 654024e860948194389851074a3b9157d604ce12 Mon Sep 17 00:00:00 2001 From: Romain Goyet Date: Mon, 3 Oct 2016 11:24:20 +0200 Subject: [PATCH] [apps] Use the Home app Change-Id: I25d881f237ce4b7db0b92afe68596416d3704781 --- apps/apps_container.cpp | 41 ++++++++++++++++++++-------------------- apps/apps_container.h | 20 +++++++------------- apps/home/app.cpp | 9 +++++++-- apps/home/app.h | 4 +++- apps/home/controller.cpp | 20 +++++++++++++++----- apps/home/controller.h | 6 +++++- apps/main.cpp | 2 +- 7 files changed, 58 insertions(+), 44 deletions(-) diff --git a/apps/apps_container.cpp b/apps/apps_container.cpp index 5aa4624bc..e56c16e78 100644 --- a/apps/apps_container.cpp +++ b/apps/apps_container.cpp @@ -1,34 +1,33 @@ #include "apps_container.h" +extern "C" { +#include +} AppsContainer::AppsContainer() : Container(), - m_activeAppIndex(0) + m_homeApp(this) { } +int AppsContainer::numberOfApps() { + return k_numberOfApps; +} + +App * AppsContainer::appAtIndex(int index) { + static App * apps[] = { + &m_homeApp, + &m_graphApp, + &m_probabilityApp, + }; + assert(sizeof(apps)/sizeof(apps[0]) == k_numberOfApps); + assert(index >= 0 && index < k_numberOfApps); + return apps[index]; +} + bool AppsContainer::handleEvent(Ion::Events::Event event) { if (event == Ion::Events::Event::F1) { - m_activeAppIndex++; - if (m_activeAppIndex >= (int)(AppId::Count)) { - m_activeAppIndex = 0; - } - switchTo((AppId)m_activeAppIndex); + switchTo(appAtIndex(0)); return true; } return false; } - -void AppsContainer::switchTo(AppId appId) { - Container::switchTo(appWithId(appId)); -} - -App * AppsContainer::appWithId(AppId appId) { - App * apps[] = { - &m_graphApp, - &m_probabilityApp, -#if USE_PIC_VIEW_APP - &m_picViewApp, -#endif - }; - return apps[(int)appId]; -}; diff --git a/apps/apps_container.h b/apps/apps_container.h index feafc9314..4cca3d81e 100644 --- a/apps/apps_container.h +++ b/apps/apps_container.h @@ -1,6 +1,7 @@ #ifndef APPS_CONTAINER_H #define APPS_CONTAINER_H +#include "home/app.h" #include "graph/graph_app.h" #include "probability/app.h" @@ -12,21 +13,14 @@ class AppsContainer : public Container { public: AppsContainer(); - enum class AppId { - Graph = 0, - Probability = 1, -#if USE_PIC_VIEW_APP - PicView = 2, - Count = 3 -#else - Count = 2 -#endif - }; - void switchTo(AppId appId); + + int numberOfApps(); + App * appAtIndex(int index); + bool handleEvent(Ion::Events::Event event) override; private: - App * appWithId(AppId appId); - int m_activeAppIndex; + static constexpr int k_numberOfApps = 3; + Home::App m_homeApp; GraphApp m_graphApp; Probability::App m_probabilityApp; #if USE_PIC_VIEW_APP diff --git a/apps/home/app.cpp b/apps/home/app.cpp index cde6768e9..19456bb5c 100644 --- a/apps/home/app.cpp +++ b/apps/home/app.cpp @@ -1,11 +1,16 @@ #include "app.h" +#include "../apps_container.h" +extern "C" { +#include +} namespace Home { -App::App() : +App::App(AppsContainer * container) : ::App(), - m_controller(Controller(this)) + m_controller(Controller(this, container)) { + assert(container->appAtIndex(0) == this); } ViewController * App::rootViewController() { diff --git a/apps/home/app.h b/apps/home/app.h index 44a6965b3..89c85f7bd 100644 --- a/apps/home/app.h +++ b/apps/home/app.h @@ -4,11 +4,13 @@ #include #include "controller.h" +class AppsContainer; + namespace Home { class App : public ::App { public: - App(); + App(AppsContainer * container); protected: ViewController * rootViewController() override; private: diff --git a/apps/home/controller.cpp b/apps/home/controller.cpp index 9d83267b6..ebe303840 100644 --- a/apps/home/controller.cpp +++ b/apps/home/controller.cpp @@ -1,12 +1,14 @@ #include "controller.h" +#include "../apps_container.h" extern "C" { #include } namespace Home { -Controller::Controller(Responder * parentResponder) : +Controller::Controller(Responder * parentResponder, ::AppsContainer * container) : ViewController(parentResponder), + m_container(container), m_tableView(TableView(this)), m_activeIndex(0) { @@ -15,6 +17,9 @@ Controller::Controller(Responder * parentResponder) : bool Controller::handleEvent(Ion::Events::Event event) { int nextActiveIndex = 0; switch (event) { + case Ion::Events::Event::ENTER: + m_container->switchTo(m_container->appAtIndex(m_activeIndex+1)); + return true; case Ion::Events::Event::DOWN_ARROW: nextActiveIndex = m_activeIndex+k_numberOfColumns; break; @@ -33,8 +38,8 @@ bool Controller::handleEvent(Ion::Events::Event event) { if (nextActiveIndex < 0) { nextActiveIndex = 0; } - if (nextActiveIndex >= k_numberOfApps) { - nextActiveIndex = k_numberOfApps-1; + if (nextActiveIndex >= numberOfIcons()) { + nextActiveIndex = numberOfIcons()-1; } setActiveIndex(nextActiveIndex); return true; @@ -49,7 +54,7 @@ View * Controller::view() { } int Controller::numberOfRows() { - return ((k_numberOfApps-1)/k_numberOfColumns)+1; + return ((numberOfIcons()-1)/k_numberOfColumns)+1; } int Controller::numberOfColumns() { @@ -72,8 +77,13 @@ int Controller::reusableCellCount() { return k_maxNumberOfCells; } +int Controller::numberOfIcons() { + assert(m_container->numberOfApps() > 0); + return m_container->numberOfApps() - 1; +} + void Controller::setActiveIndex(int index) { - if (m_activeIndex < 0 && m_activeIndex >= k_numberOfApps) { + if (m_activeIndex < 0 && m_activeIndex >= numberOfIcons()) { return; } diff --git a/apps/home/controller.h b/apps/home/controller.h index 7cbef8a58..ff93fbdb9 100644 --- a/apps/home/controller.h +++ b/apps/home/controller.h @@ -4,11 +4,13 @@ #include #include "app_cell.h" +class AppsContainer; + namespace Home { class Controller : public ViewController, public SimpleTableViewDataSource { public: - Controller(Responder * parentResponder); + Controller(Responder * parentResponder, ::AppsContainer * container); View * view() override; @@ -22,7 +24,9 @@ public: virtual View * reusableCell(int index) override; virtual int reusableCellCount() override; private: + int numberOfIcons(); void setActiveIndex(int index); + AppsContainer * m_container; TableView m_tableView; static constexpr int k_numberOfColumns = 3; static constexpr int k_numberOfApps = 10; diff --git a/apps/main.cpp b/apps/main.cpp index a253a4426..69a5097c7 100644 --- a/apps/main.cpp +++ b/apps/main.cpp @@ -2,6 +2,6 @@ void ion_app() { AppsContainer container; - container.switchTo(AppsContainer::AppId::Graph); + container.switchTo(container.appAtIndex(0)); container.run(); }