diff --git a/apps/graph/app.cpp b/apps/graph/app.cpp index 8c8bd9188..d7b347aeb 100644 --- a/apps/graph/app.cpp +++ b/apps/graph/app.cpp @@ -70,7 +70,7 @@ App::App(Container * container, Snapshot * snapshot) : m_valuesAlternateEmptyViewController(&m_valuesHeader, &m_valuesController, &m_valuesController), m_valuesHeader(&m_valuesStackViewController, &m_valuesAlternateEmptyViewController, &m_valuesController), m_valuesStackViewController(&m_tabViewController, &m_valuesHeader), - m_tabViewController(&m_inputViewController, &m_listStackViewController, &m_graphStackViewController, &m_valuesStackViewController), + m_tabViewController(&m_inputViewController, snapshot, &m_listStackViewController, &m_graphStackViewController, &m_valuesStackViewController), m_inputViewController(&m_modalViewController, &m_tabViewController, this) { } diff --git a/apps/regression/app.cpp b/apps/regression/app.cpp index e378caf2f..ae4276bdf 100644 --- a/apps/regression/app.cpp +++ b/apps/regression/app.cpp @@ -70,7 +70,7 @@ App::App(Container * container, Snapshot * snapshot) : m_storeController(&m_storeHeader, snapshot->store(), &m_storeHeader), m_storeHeader(&m_storeStackViewController, &m_storeController, &m_storeController), m_storeStackViewController(&m_tabViewController, &m_storeHeader), - m_tabViewController(&m_modalViewController, &m_storeStackViewController, &m_graphStackViewController, &m_calculationHeader) + m_tabViewController(&m_modalViewController, snapshot, &m_storeStackViewController, &m_graphStackViewController, &m_calculationHeader) { } diff --git a/apps/regression/app.h b/apps/regression/app.h index 521a66ae9..78288fae9 100644 --- a/apps/regression/app.h +++ b/apps/regression/app.h @@ -18,7 +18,7 @@ public: I18n::Message upperName() override; const Image * icon() override; }; - class Snapshot : public ::App::Snapshot { + class Snapshot : public ::App::Snapshot, public TabViewDataSource { public: Snapshot(); App * unpack(Container * container) override; diff --git a/apps/sequence/app.cpp b/apps/sequence/app.cpp index 1dfb024b5..eab2e392c 100644 --- a/apps/sequence/app.cpp +++ b/apps/sequence/app.cpp @@ -68,7 +68,7 @@ App::App(Container * container, Snapshot * snapshot) : m_valuesAlternateEmptyViewController(&m_valuesHeader, &m_valuesController, &m_valuesController), m_valuesHeader(nullptr, &m_valuesAlternateEmptyViewController, &m_valuesController), m_valuesStackViewController(&m_tabViewController, &m_valuesHeader), - m_tabViewController(&m_inputViewController, &m_listStackViewController, &m_graphStackViewController, &m_valuesStackViewController), + m_tabViewController(&m_inputViewController, snapshot, &m_listStackViewController, &m_graphStackViewController, &m_valuesStackViewController), m_inputViewController(&m_modalViewController, &m_tabViewController, &m_listController) { } diff --git a/apps/shared/function_app.h b/apps/shared/function_app.h index 68a7e6a4a..b4d1a63ca 100644 --- a/apps/shared/function_app.h +++ b/apps/shared/function_app.h @@ -12,7 +12,7 @@ namespace Shared { class FunctionApp : public TextFieldDelegateApp { public: - class Snapshot : public ::App::Snapshot { + class Snapshot : public ::App::Snapshot, public TabViewDataSource { public: Snapshot(); CurveViewCursor * cursor(); diff --git a/apps/statistics/app.cpp b/apps/statistics/app.cpp index 8b487457c..a73742354 100644 --- a/apps/statistics/app.cpp +++ b/apps/statistics/app.cpp @@ -85,7 +85,7 @@ App::App(Container * container, Snapshot * snapshot) : m_storeController(&m_storeHeader, snapshot->store(), &m_storeHeader), m_storeHeader(&m_storeStackViewController, &m_storeController, &m_storeController), m_storeStackViewController(&m_tabViewController, &m_storeHeader), - m_tabViewController(&m_modalViewController, &m_storeStackViewController, &m_histogramStackViewController, &m_boxHeader, &m_calculationHeader) + m_tabViewController(&m_modalViewController, snapshot, &m_storeStackViewController, &m_histogramStackViewController, &m_boxHeader, &m_calculationHeader) { } diff --git a/apps/statistics/app.h b/apps/statistics/app.h index 9cd2f84cc..ee6e46e02 100644 --- a/apps/statistics/app.h +++ b/apps/statistics/app.h @@ -19,7 +19,7 @@ public: I18n::Message upperName() override; const Image * icon() override; }; - class Snapshot : public ::App::Snapshot { + class Snapshot : public ::App::Snapshot, public TabViewDataSource { public: Snapshot(); App * unpack(Container * container) override; diff --git a/escher/Makefile b/escher/Makefile index 5d4e188d6..0da0c885a 100644 --- a/escher/Makefile +++ b/escher/Makefile @@ -55,6 +55,7 @@ objs += $(addprefix escher/src/,\ tab_view.o\ tab_view_cell.o\ tab_view_controller.o\ + tab_view_data_source.o\ table_cell.o\ table_view.o\ table_view_data_source.o\ diff --git a/escher/include/escher.h b/escher/include/escher.h index e2d74acb4..35df10e91 100644 --- a/escher/include/escher.h +++ b/escher/include/escher.h @@ -58,6 +58,7 @@ #include #include #include +#include #include #include #include diff --git a/escher/include/escher/tab_view_controller.h b/escher/include/escher/tab_view_controller.h index 79131b0b6..d3a581a23 100644 --- a/escher/include/escher/tab_view_controller.h +++ b/escher/include/escher/tab_view_controller.h @@ -2,11 +2,12 @@ #define ESCHER_TAB_VIEW_CONTROLLER_H #include +#include #include class TabViewController : public ViewController { public: - TabViewController(Responder * parentResponder, ViewController * one, ViewController * two, ViewController * three, ViewController * four = nullptr); + TabViewController(Responder * parentResponder, TabViewDataSource * dataSource, ViewController * one, ViewController * two, ViewController * three, ViewController * four = nullptr); View * view() override; int activeTab() const; void setSelectedTab(int8_t index); @@ -45,8 +46,7 @@ private: static constexpr uint8_t k_maxNumberOfChildren = 4; ViewController * m_children[k_maxNumberOfChildren]; uint8_t m_numberOfChildren; - int8_t m_activeChildIndex; - int8_t m_selectedChildIndex; + TabViewDataSource * m_dataSource; }; #endif diff --git a/escher/include/escher/tab_view_data_source.h b/escher/include/escher/tab_view_data_source.h new file mode 100644 index 000000000..2dde629f4 --- /dev/null +++ b/escher/include/escher/tab_view_data_source.h @@ -0,0 +1,20 @@ +#ifndef ESCHER_TAB_VIEW_DATA_SOURCE_H +#define ESCHER_TAB_VIEW_DATA_SOURCE_H + +extern "C" { +#include +} + +class TabViewDataSource { +public: + TabViewDataSource(); + int activeTab() const; + int selectedTab() const; + void setSelectedTab(int index); + void setActiveTab(int index); +private: + int8_t m_activeChildIndex; + int8_t m_selectedChildIndex; +}; + +#endif diff --git a/escher/src/tab_view_controller.cpp b/escher/src/tab_view_controller.cpp index 4df33fb37..0ac87489a 100644 --- a/escher/src/tab_view_controller.cpp +++ b/escher/src/tab_view_controller.cpp @@ -52,10 +52,9 @@ const char * TabViewController::ContentView::className() const { } #endif -TabViewController::TabViewController(Responder * parentResponder, ViewController * one, ViewController * two, ViewController * three, ViewController * four) : +TabViewController::TabViewController(Responder * parentResponder, TabViewDataSource * dataSource, ViewController * one, ViewController * two, ViewController * three, ViewController * four) : ViewController(parentResponder), - m_activeChildIndex(-1), - m_selectedChildIndex(-1) + m_dataSource(dataSource) { assert(one != nullptr); assert(two != nullptr || (three == nullptr && four == nullptr)); @@ -72,7 +71,7 @@ TabViewController::TabViewController(Responder * parentResponder, ViewController } int TabViewController::activeTab() const { - return m_activeChildIndex; + return m_dataSource->activeTab(); } bool TabViewController::handleEvent(Ion::Events::Event event) { @@ -87,23 +86,23 @@ bool TabViewController::handleEvent(Ion::Events::Event event) { return false; } if (event == Ion::Events::Left) { - if (m_selectedChildIndex > 0) { - setSelectedTab(m_selectedChildIndex-1); + if (m_dataSource->selectedTab() > 0) { + setSelectedTab(m_dataSource->selectedTab()-1); } return true; } if (event == Ion::Events::Right) { - if (m_selectedChildIndex < m_numberOfChildren-1) { - setSelectedTab(m_selectedChildIndex+1); + if (m_dataSource->selectedTab() < m_numberOfChildren-1) { + setSelectedTab(m_dataSource->selectedTab()+1); } return true; } if (event == Ion::Events::Down) { - setActiveTab(m_activeChildIndex, false); + setActiveTab(m_dataSource->activeTab(), false); return true; } if (event == Ion::Events::OK || event == Ion::Events::EXE) { - setActiveTab(m_selectedChildIndex, true); + setActiveTab(m_dataSource->selectedTab(), true); return true; } return false; @@ -112,28 +111,28 @@ bool TabViewController::handleEvent(Ion::Events::Event event) { void TabViewController::setActiveTab(int8_t i, bool forceReactive) { assert(i >= 0 && i < m_numberOfChildren); ViewController * activeVC = m_children[i]; - if (i != m_activeChildIndex || forceReactive) { - if (i != m_activeChildIndex) { + if (i != m_dataSource->activeTab() || forceReactive) { + if (i != m_dataSource->activeTab()) { m_view.setActiveView(activeVC->view()); m_children[i]->viewWillAppear(); } m_view.m_tabView.setActiveIndex(i); } app()->setFirstResponder(activeVC); - if (i != m_activeChildIndex || forceReactive) { - if (m_activeChildIndex >= 0 && m_activeChildIndex != i) { - m_children[m_activeChildIndex]->viewDidDisappear(); + if (i != m_dataSource->activeTab() || forceReactive) { + if (m_dataSource->activeTab() >= 0 && m_dataSource->activeTab() != i) { + m_children[m_dataSource->activeTab()]->viewDidDisappear(); } - m_activeChildIndex = i; + m_dataSource->setActiveTab(i); } } void TabViewController::setSelectedTab(int8_t i) { - if (i == m_selectedChildIndex) { + if (i == m_dataSource->selectedTab()) { return; } m_view.m_tabView.setSelectedIndex(i); - m_selectedChildIndex = i; + m_dataSource->setSelectedTab(i); } void TabViewController::didEnterResponderChain(Responder * previousResponder) { @@ -141,7 +140,7 @@ void TabViewController::didEnterResponderChain(Responder * previousResponder) { } void TabViewController::didBecomeFirstResponder() { - setSelectedTab(m_activeChildIndex); + setSelectedTab(m_dataSource->activeTab()); } void TabViewController::willResignFirstResponder() { @@ -167,12 +166,12 @@ void TabViewController::viewWillAppear() { m_view.m_tabView.addTab(m_children[i]); } } - if (m_activeChildIndex < 0) { - m_activeChildIndex = 0; + if (m_dataSource->activeTab() < 0) { + m_dataSource->setActiveTab(0); } - m_view.setActiveView(m_children[m_activeChildIndex]->view()); + m_view.setActiveView(m_children[m_dataSource->activeTab()]->view()); activeViewController()->viewWillAppear(); - m_view.m_tabView.setActiveIndex(m_activeChildIndex); + m_view.m_tabView.setActiveIndex(m_dataSource->activeTab()); } void TabViewController::viewDidDisappear() { @@ -180,6 +179,6 @@ void TabViewController::viewDidDisappear() { } ViewController * TabViewController::activeViewController() { - assert(m_activeChildIndex >= 0 && m_activeChildIndex < m_numberOfChildren); - return m_children[m_activeChildIndex]; + assert(m_dataSource->activeTab() >= 0 && m_dataSource->activeTab() < m_numberOfChildren); + return m_children[m_dataSource->activeTab()]; } diff --git a/escher/src/tab_view_data_source.cpp b/escher/src/tab_view_data_source.cpp new file mode 100644 index 000000000..7aee60d00 --- /dev/null +++ b/escher/src/tab_view_data_source.cpp @@ -0,0 +1,23 @@ +#include + +TabViewDataSource::TabViewDataSource() : + m_activeChildIndex(-1), + m_selectedChildIndex(-1) +{ +} + +int TabViewDataSource::activeTab() const { + return m_activeChildIndex; +} + +int TabViewDataSource::selectedTab() const { + return m_selectedChildIndex; +} +void TabViewDataSource::setActiveTab(int i) { + m_activeChildIndex = i; +} + +void TabViewDataSource::setSelectedTab(int i) { + m_selectedChildIndex = i; +} +