[apps/shared] Factorize tab table controller in a class

Change-Id: I0af0da6b9a1216c57c8a4b669d14d8a3473ac48b
This commit is contained in:
Émilie Feral
2017-03-08 11:18:55 +01:00
parent 72d898e73e
commit b4b2e284e1
9 changed files with 97 additions and 119 deletions

View File

@@ -7,19 +7,18 @@
#include <assert.h>
using namespace Poincare;
using namespace Shared;
namespace Regression {
CalculationController::CalculationController(Responder * parentResponder, ButtonRowController * header, Store * store) :
ViewController(parentResponder),
TabTableController(parentResponder, Metric::CommonTopMargin, Metric::CommonRightMargin, Metric::CommonBottomMargin, Metric::CommonLeftMargin, this, true),
ButtonRowDelegate(header, nullptr),
m_titleCells{EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small),
EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small)},
m_r2TitleCell(1.0f, 0.5f),
m_columnTitleCell(EvenOddDoubleBufferTextCell(&m_selectableTableView)),
m_calculationCells{EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small)},
m_selectableTableView(SelectableTableView(this, this, Metric::CommonTopMargin, Metric::CommonRightMargin,
Metric::CommonBottomMargin, Metric::CommonLeftMargin, this, true, true, Palette::WallScreenDark)),
m_store(store)
{
for (int k = 0; k < k_maxNumberOfDisplayableRows/2; k++) {
@@ -41,10 +40,6 @@ const char * CalculationController::title() const {
return "Statistiques";
}
View * CalculationController::view() {
return &m_selectableTableView;
}
bool CalculationController::handleEvent(Ion::Events::Event event) {
if (event == Ion::Events::Up) {
m_selectableTableView.deselectTable();
@@ -60,7 +55,7 @@ void CalculationController::didBecomeFirstResponder() {
} else {
m_selectableTableView.selectCellAtLocation(m_selectableTableView.selectedColumn(), m_selectableTableView.selectedRow());
}
app()->setFirstResponder(&m_selectableTableView);
TabTableController::didBecomeFirstResponder();
}
void CalculationController::tableViewDidChangeSelection(SelectableTableView * t, int previousSelectedCellX, int previousSelectedCellY) {
@@ -181,22 +176,6 @@ KDCoordinate CalculationController::rowHeight(int j) {
return k_cellHeight;
}
KDCoordinate CalculationController::cumulatedWidthFromIndex(int i) {
return i*k_cellWidth;
}
KDCoordinate CalculationController::cumulatedHeightFromIndex(int j) {
return j*k_cellHeight;
}
int CalculationController::indexFromCumulatedWidth(KDCoordinate offsetX) {
return (offsetX-1) / k_cellWidth;
}
int CalculationController::indexFromCumulatedHeight(KDCoordinate offsetY) {
return (offsetY-1) / k_cellHeight;
}
HighlightCell * CalculationController::reusableCell(int index, int type) {
if (type == 0) {
assert(index < k_maxNumberOfDisplayableRows);
@@ -250,16 +229,6 @@ int CalculationController::typeAtLocation(int i, int j) {
return 4;
}
void CalculationController::viewWillAppear() {
m_selectableTableView.reloadData();
}
void CalculationController::willExitResponderChain(Responder * nextFirstResponder) {
if (nextFirstResponder == tabController()) {
m_selectableTableView.deselectTable();
}
}
Responder * CalculationController::tabController() const {
return (parentResponder()->parentResponder()->parentResponder());
}

View File

@@ -4,16 +4,16 @@
#include <escher.h>
#include "store.h"
#include "even_odd_double_buffer_text_cell.h"
#include "../shared/tab_table_controller.h"
namespace Regression {
class CalculationController : public ViewController, public ButtonRowDelegate, public AlternateEmptyViewDelegate, public TableViewDataSource, public SelectableTableViewDelegate {
class CalculationController : public Shared::TabTableController, public ButtonRowDelegate, public AlternateEmptyViewDelegate, public SelectableTableViewDelegate {
public:
CalculationController(Responder * parentResponder, ButtonRowController * header, Store * store);
~CalculationController();
const char * title() const override;
View * view() override;
bool handleEvent(Ion::Events::Event event) override;
void didBecomeFirstResponder() override;
void tableViewDidChangeSelection(SelectableTableView * t, int previousSelectedCellX, int previousSelectedCellY) override;
@@ -27,17 +27,11 @@ public:
void willDisplayCellAtLocation(HighlightCell * cell, int i, int j) override;
KDCoordinate columnWidth(int i) override;
KDCoordinate rowHeight(int j) override;
KDCoordinate cumulatedWidthFromIndex(int i) override;
KDCoordinate cumulatedHeightFromIndex(int j) override;
int indexFromCumulatedWidth(KDCoordinate offsetX) override;
int indexFromCumulatedHeight(KDCoordinate offsetY) override;
HighlightCell * reusableCell(int index, int type) override;
int reusableCellCount(int type) override;
int typeAtLocation(int i, int j) override;
void viewWillAppear() override;
void willExitResponderChain(Responder * nextFirstResponder) override;
private:
Responder * tabController() const;
Responder * tabController() const override;
constexpr static int k_totalNumberOfRows = 11;
constexpr static int k_totalNumberOfColumns = 2;
constexpr static int k_maxNumberOfDisplayableRows = 10;
@@ -49,7 +43,6 @@ private:
EvenOddDoubleBufferTextCell m_columnTitleCell;
EvenOddDoubleBufferTextCell m_doubleCalculationCells[k_maxNumberOfDisplayableRows/2];
EvenOddBufferTextCell m_calculationCells[k_maxNumberOfDisplayableRows/2];
SelectableTableView m_selectableTableView;
Store * m_store;
};

View File

@@ -28,6 +28,7 @@ app_objs += $(addprefix apps/shared/,\
range_parameter_controller.o\
store_controller.o\
store_parameter_controller.o\
tab_table_controller.o\
text_field_delegate.o\
text_field_delegate_app.o\
values_function_parameter_controller.o\

View File

@@ -10,16 +10,10 @@ namespace Shared {
EditableCellTableViewController::EditableCellTableViewController(Responder * parentResponder, KDCoordinate topMargin,
KDCoordinate rightMargin, KDCoordinate bottomMargin, KDCoordinate leftMargin) :
ViewController(parentResponder),
m_selectableTableView(SelectableTableView(this, this, topMargin, rightMargin, bottomMargin, leftMargin, this,
false, true, Palette::WallScreenDark))
TabTableController(parentResponder, topMargin, rightMargin, bottomMargin, leftMargin, this, true)
{
}
View * EditableCellTableViewController::view() {
return &m_selectableTableView;
}
bool EditableCellTableViewController::textFieldDidFinishEditing(TextField * textField, const char * text) {
AppsContainer * appsContainer = ((TextFieldDelegateApp *)app())->container();
Context * globalContext = appsContainer->globalContext();
@@ -64,14 +58,6 @@ KDCoordinate EditableCellTableViewController::rowHeight(int j) {
return k_cellHeight;
}
KDCoordinate EditableCellTableViewController::cumulatedHeightFromIndex(int j) {
return j*k_cellHeight;
}
int EditableCellTableViewController::indexFromCumulatedHeight(KDCoordinate offsetY) {
return (offsetY-1) / k_cellHeight;
}
void EditableCellTableViewController::willDisplayCellAtLocationWithDisplayMode(HighlightCell * cell, int i, int j, Expression::FloatDisplayMode floatDisplayMode) {
EvenOddCell * myCell = (EvenOddCell *)cell;
myCell->setEven(j%2 == 0);
@@ -104,12 +90,12 @@ void EditableCellTableViewController::didBecomeFirstResponder() {
int selectedColumn = m_selectableTableView.selectedColumn();
selectedColumn = selectedColumn >= numberOfColumns() ? numberOfColumns() - 1 : selectedColumn;
m_selectableTableView.selectCellAtLocation(selectedColumn, selectedRow);
app()->setFirstResponder(&m_selectableTableView);
TabTableController::didBecomeFirstResponder();
}
}
void EditableCellTableViewController::viewWillAppear() {
m_selectableTableView.reloadData();
TabTableController::viewWillAppear();
if (m_selectableTableView.selectedRow() == -1) {
m_selectableTableView.selectCellAtLocation(0, 1);
} else {
@@ -121,12 +107,6 @@ void EditableCellTableViewController::viewWillAppear() {
}
}
void EditableCellTableViewController::willExitResponderChain(Responder * nextFirstResponder) {
if (nextFirstResponder == tabController()) {
m_selectableTableView.deselectTable();
}
}
TextFieldDelegateApp * EditableCellTableViewController::textFieldDelegateApp() {
return (TextFieldDelegateApp *)app();
}

View File

@@ -4,14 +4,14 @@
#include <escher.h>
#include <poincare.h>
#include "text_field_delegate.h"
#include "tab_table_controller.h"
namespace Shared {
class EditableCellTableViewController : public ViewController, public TableViewDataSource, public SelectableTableViewDelegate, public TextFieldDelegate {
class EditableCellTableViewController : public TabTableController , public SelectableTableViewDelegate, public TextFieldDelegate {
public:
EditableCellTableViewController(Responder * parentResponder, KDCoordinate topMargin,
KDCoordinate rightMargin, KDCoordinate bottomMargin, KDCoordinate leftMargin);
virtual View * view() override;
bool textFieldDidFinishEditing(TextField * textField, const char * text) override;
void tableViewDidChangeSelection(SelectableTableView * t, int previousSelectedCellX, int previousSelectedCellY) override;
@@ -19,14 +19,9 @@ public:
int numberOfRows() override;
void willDisplayCellAtLocationWithDisplayMode(HighlightCell * cell, int i, int j, Poincare::Expression::FloatDisplayMode FloatDisplayMode);
KDCoordinate rowHeight(int j) override;
KDCoordinate cumulatedHeightFromIndex(int j) override;
int indexFromCumulatedHeight(KDCoordinate offsetY) override;
void didBecomeFirstResponder() override;
void viewWillAppear() override;
void willExitResponderChain(Responder * nextFirstResponder) override;
protected:
SelectableTableView m_selectableTableView;
private:
TextFieldDelegateApp * textFieldDelegateApp() override;
static constexpr KDCoordinate k_cellHeight = 20;
@@ -35,7 +30,6 @@ private:
virtual float dataAtLocation(int columnIndex, int rowIndex) = 0;
virtual int numberOfElements() = 0;
virtual int maxNumberOfElements() const = 0;
virtual Responder * tabController() const = 0;
};
}

View File

@@ -0,0 +1,49 @@
#include "tab_table_controller.h"
namespace Shared {
TabTableController::TabTableController(Responder * parentResponder, KDCoordinate topMargin,
KDCoordinate rightMargin, KDCoordinate bottomMargin, KDCoordinate leftMargin, SelectableTableViewDelegate * delegate, bool showIndicators) :
ViewController(parentResponder),
m_selectableTableView(SelectableTableView(this, this, topMargin, rightMargin, bottomMargin, leftMargin,
delegate, showIndicators, true, Palette::WallScreenDark))
{
}
View * TabTableController::view() {
return &m_selectableTableView;
}
void TabTableController::didBecomeFirstResponder() {
app()->setFirstResponder(&m_selectableTableView);
}
KDCoordinate TabTableController::cumulatedWidthFromIndex(int i) {
return i*columnWidth(0);
}
KDCoordinate TabTableController::cumulatedHeightFromIndex(int j) {
return j*rowHeight(0);
}
int TabTableController::indexFromCumulatedWidth(KDCoordinate offsetX) {
return (offsetX-1) / columnWidth(0);
}
int TabTableController::indexFromCumulatedHeight(KDCoordinate offsetY) {
return (offsetY-1) / rowHeight(0);
}
void TabTableController::viewWillAppear() {
m_selectableTableView.reloadData();
}
void TabTableController::willExitResponderChain(Responder * nextFirstResponder) {
if (nextFirstResponder == tabController()) {
m_selectableTableView.deselectTable();
m_selectableTableView.scrollToCell(0,0);
}
}
}

View File

@@ -0,0 +1,29 @@
#ifndef SHARED_TAB_TABLE_CONTROLLER_H
#define SHARED_TAB_TABLE_CONTROLLER_H
#include <escher.h>
namespace Shared {
class TabTableController : public ViewController, public TableViewDataSource {
public:
TabTableController(Responder * parentResponder, KDCoordinate topMargin,
KDCoordinate rightMargin, KDCoordinate bottomMargin, KDCoordinate leftMargin, SelectableTableViewDelegate * delegate, bool showIndicators);
virtual View * view() override;
KDCoordinate cumulatedHeightFromIndex(int j) override;
int indexFromCumulatedHeight(KDCoordinate offsetY) override;
KDCoordinate cumulatedWidthFromIndex(int i) override;
int indexFromCumulatedWidth(KDCoordinate offsetX) override;
void didBecomeFirstResponder() override;
void viewWillAppear() override;
void willExitResponderChain(Responder * nextFirstResponder) override;
protected:
SelectableTableView m_selectableTableView;
virtual Responder * tabController() const = 0;
};
}
#endif

View File

@@ -5,19 +5,18 @@
#include <poincare.h>
#include <assert.h>
using namespace Shared;
using namespace Poincare;
namespace Statistics {
CalculationController::CalculationController(Responder * parentResponder, ButtonRowController * header, Store * store) :
ViewController(parentResponder),
TabTableController(parentResponder, Metric::CommonTopMargin, Metric::CommonRightMargin, Metric::CommonBottomMargin, Metric::CommonLeftMargin, nullptr, true),
ButtonRowDelegate(header, nullptr),
m_titleCells{EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small),
EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small)},
m_calculationCells{EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small),
EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small)},
m_selectableTableView(SelectableTableView(this, this, Metric::CommonTopMargin, Metric::CommonRightMargin, Metric::CommonBottomMargin, Metric::CommonLeftMargin,
nullptr, true, true, Palette::WallScreenDark)),
m_store(store)
{
for (int k = 0; k < k_maxNumberOfDisplayableRows; k++) {
@@ -30,10 +29,6 @@ const char * CalculationController::title() const {
return "Stats";
}
View * CalculationController::view() {
return &m_selectableTableView;
}
bool CalculationController::handleEvent(Ion::Events::Event event) {
if (event == Ion::Events::Up) {
m_selectableTableView.deselectTable();
@@ -49,7 +44,7 @@ void CalculationController::didBecomeFirstResponder() {
} else {
m_selectableTableView.selectCellAtLocation(m_selectableTableView.selectedColumn(), m_selectableTableView.selectedRow());
}
app()->setFirstResponder(&m_selectableTableView);
TabTableController::didBecomeFirstResponder();
}
bool CalculationController::isEmpty() const {
@@ -105,22 +100,6 @@ KDCoordinate CalculationController::rowHeight(int j) {
return k_cellHeight;
}
KDCoordinate CalculationController::cumulatedWidthFromIndex(int i) {
return i*k_cellWidth;
}
KDCoordinate CalculationController::cumulatedHeightFromIndex(int j) {
return j*k_cellHeight;
}
int CalculationController::indexFromCumulatedWidth(KDCoordinate offsetX) {
return (offsetX-1) / k_cellWidth;
}
int CalculationController::indexFromCumulatedHeight(KDCoordinate offsetY) {
return (offsetY-1) / k_cellHeight;
}
HighlightCell * CalculationController::reusableCell(int index, int type) {
assert(index < k_totalNumberOfRows);
if (type == 0) {
@@ -137,16 +116,6 @@ int CalculationController::typeAtLocation(int i, int j) {
return i;
}
void CalculationController::viewWillAppear() {
m_selectableTableView.reloadData();
}
void CalculationController::willExitResponderChain(Responder * nextFirstResponder) {
if (nextFirstResponder == tabController()) {
m_selectableTableView.deselectTable();
}
}
Responder * CalculationController::tabController() const {
return (parentResponder()->parentResponder()->parentResponder());
}

View File

@@ -3,15 +3,16 @@
#include <escher.h>
#include "store.h"
#include "../shared/tab_table_controller.h"
namespace Statistics {
class CalculationController : public ViewController, public ButtonRowDelegate, public AlternateEmptyViewDelegate, public TableViewDataSource {
class CalculationController : public Shared::TabTableController, public ButtonRowDelegate, public AlternateEmptyViewDelegate {
public:
CalculationController(Responder * parentResponder, ButtonRowController * header, Store * store);
const char * title() const override;
View * view() override;
bool handleEvent(Ion::Events::Event event) override;
void didBecomeFirstResponder() override;
@@ -24,24 +25,17 @@ public:
void willDisplayCellAtLocation(HighlightCell * cell, int i, int j) override;
KDCoordinate columnWidth(int i) override;
KDCoordinate rowHeight(int j) override;
KDCoordinate cumulatedWidthFromIndex(int i) override;
KDCoordinate cumulatedHeightFromIndex(int j) override;
int indexFromCumulatedWidth(KDCoordinate offsetX) override;
int indexFromCumulatedHeight(KDCoordinate offsetY) override;
HighlightCell * reusableCell(int index, int type) override;
int reusableCellCount(int type) override;
int typeAtLocation(int i, int j) override;
void viewWillAppear() override;
void willExitResponderChain(Responder * nextFirstResponder) override;
private:
Responder * tabController() const;
Responder * tabController() const override;
constexpr static int k_totalNumberOfRows = 13;
constexpr static int k_maxNumberOfDisplayableRows = 11;
static constexpr KDCoordinate k_cellHeight = 20;
static constexpr KDCoordinate k_cellWidth = Ion::Display::Width/2 - Metric::CommonRightMargin/2 - Metric::CommonLeftMargin/2;
EvenOddPointerTextCell m_titleCells[k_maxNumberOfDisplayableRows];
EvenOddBufferTextCell m_calculationCells[k_maxNumberOfDisplayableRows];
SelectableTableView m_selectableTableView;
Store * m_store;
};