diff --git a/apps/calculation/history_controller.cpp b/apps/calculation/history_controller.cpp index a1f557e81..d86c63f85 100644 --- a/apps/calculation/history_controller.cpp +++ b/apps/calculation/history_controller.cpp @@ -8,30 +8,34 @@ using namespace Shared; namespace Calculation { HistoryController::HistoryController(Responder * parentResponder, CalculationStore * calculationStore) : - DynamicViewController(parentResponder), + ViewController(parentResponder), + m_selectableTableView(this, this, this, this), m_calculationHistory{}, m_calculationStore(calculationStore) { + for (int i = 0; i < k_maxNumberOfDisplayedRows; i++) { + m_calculationHistory[i].setParentResponder(&m_selectableTableView); + } } void HistoryController::reload() { - selectableTableView()->reloadData(); + m_selectableTableView.reloadData(); } void HistoryController::didBecomeFirstResponder() { selectCellAtLocation(0, numberOfRows()-1); - app()->setFirstResponder(selectableTableView()); + app()->setFirstResponder(&m_selectableTableView); } void HistoryController::willExitResponderChain(Responder * nextFirstResponder) { if (nextFirstResponder == parentResponder()) { - selectableTableView()->deselectTable(); + m_selectableTableView.deselectTable(); } } bool HistoryController::handleEvent(Ion::Events::Event event) { if (event == Ion::Events::Down) { - selectableTableView()->deselectTable(); + m_selectableTableView.deselectTable(); app()->setFirstResponder(parentResponder()); return true; } @@ -40,10 +44,10 @@ bool HistoryController::handleEvent(Ion::Events::Event event) { } if (event == Ion::Events::OK || event == Ion::Events::EXE) { int focusRow = selectedRow(); - HistoryViewCell * selectedCell = (HistoryViewCell *)selectableTableView()->selectedCell(); + HistoryViewCell * selectedCell = (HistoryViewCell *)m_selectableTableView.selectedCell(); HistoryViewCell::SubviewType subviewType = selectedCell->selectedSubviewType(); EditExpressionController * editController = (EditExpressionController *)parentResponder(); - selectableTableView()->deselectTable(); + m_selectableTableView.deselectTable(); app()->setFirstResponder(editController); Calculation * calculation = m_calculationStore->calculationAtIndex(focusRow); if (subviewType == HistoryViewCell::SubviewType::Input) { @@ -60,9 +64,9 @@ bool HistoryController::handleEvent(Ion::Events::Event event) { } if (event == Ion::Events::Backspace) { int focusRow = selectedRow(); - HistoryViewCell * selectedCell = (HistoryViewCell *)selectableTableView()->selectedCell(); + HistoryViewCell * selectedCell = (HistoryViewCell *)m_selectableTableView.selectedCell(); HistoryViewCell::SubviewType subviewType = selectedCell->selectedSubviewType(); - selectableTableView()->deselectTable(); + m_selectableTableView.deselectTable(); EditExpressionController * editController = (EditExpressionController *)parentResponder(); m_calculationStore->deleteCalculationAtIndex(focusRow); reload(); @@ -71,20 +75,20 @@ bool HistoryController::handleEvent(Ion::Events::Event event) { return true; } if (focusRow > 0) { - selectableTableView()->selectCellAtLocation(0, focusRow-1); + m_selectableTableView.selectCellAtLocation(0, focusRow-1); } else { - selectableTableView()->selectCellAtLocation(0, 0); + m_selectableTableView.selectCellAtLocation(0, 0); } if (subviewType == HistoryViewCell::SubviewType::Input) { - tableViewDidChangeSelection(selectableTableView(), 0, selectedRow()); + tableViewDidChangeSelection(&m_selectableTableView, 0, selectedRow()); } else { - tableViewDidChangeSelection(selectableTableView(), 0, -1); + tableViewDidChangeSelection(&m_selectableTableView, 0, -1); } - selectableTableView()->scrollToCell(0, selectedRow()); + m_selectableTableView.scrollToCell(0, selectedRow()); return true; } if (event == Ion::Events::Clear) { - selectableTableView()->deselectTable(); + m_selectableTableView.deselectTable(); m_calculationStore->deleteAll(); reload(); app()->setFirstResponder(parentResponder()); @@ -92,7 +96,7 @@ bool HistoryController::handleEvent(Ion::Events::Event event) { } if (event == Ion::Events::Back) { EditExpressionController * editController = (EditExpressionController *)parentResponder(); - selectableTableView()->deselectTable(); + m_selectableTableView.deselectTable(); app()->setFirstResponder(editController); return true; } @@ -123,7 +127,7 @@ HighlightCell * HistoryController::reusableCell(int index, int type) { assert(type == 0); assert(index >= 0); assert(index < k_maxNumberOfDisplayedRows); - return m_calculationHistory[index]; + return &m_calculationHistory[index]; } int HistoryController::reusableCellCount(int type) { @@ -152,27 +156,7 @@ int HistoryController::typeAtLocation(int i, int j) { } void HistoryController::scrollToCell(int i, int j) { - selectableTableView()->scrollToCell(i, j); -} - -CalculationSelectableTableView * HistoryController::selectableTableView() { - return (CalculationSelectableTableView *)view(); -} - -View * HistoryController::loadView() { - CalculationSelectableTableView * tableView = new CalculationSelectableTableView(this, this, this, this); -for (int i = 0; i < k_maxNumberOfDisplayedRows; i++) { - m_calculationHistory[i] = new HistoryViewCell(tableView); - } - return tableView; -} - -void HistoryController::unloadView(View * view) { - for (int i = 0; i < k_maxNumberOfDisplayedRows; i++) { - delete m_calculationHistory[i]; - m_calculationHistory[i] = nullptr; - } - delete view; + m_selectableTableView.scrollToCell(i, j); } } diff --git a/apps/calculation/history_controller.h b/apps/calculation/history_controller.h index 3f88fc95e..9e4461c39 100644 --- a/apps/calculation/history_controller.h +++ b/apps/calculation/history_controller.h @@ -11,10 +11,10 @@ namespace Calculation { class App; -class HistoryController : public DynamicViewController, public ListViewDataSource, public SelectableTableViewDataSource, public SelectableTableViewDelegate { +class HistoryController : public ViewController, public ListViewDataSource, public SelectableTableViewDataSource, public SelectableTableViewDelegate { public: HistoryController(Responder * parentResponder, CalculationStore * calculationStore); - + View * view() override { return &m_selectableTableView; } bool handleEvent(Ion::Events::Event event) override; void didBecomeFirstResponder() override; void willExitResponderChain(Responder * nextFirstResponder) override; @@ -27,12 +27,11 @@ public: int typeAtLocation(int i, int j) override; void tableViewDidChangeSelection(SelectableTableView * t, int previousSelectedCellX, int previousSelectedCellY) override; void scrollToCell(int i, int j); - View * loadView() override; - void unloadView(View * view) override; private: CalculationSelectableTableView * selectableTableView(); constexpr static int k_maxNumberOfDisplayedRows = 5; - HistoryViewCell * m_calculationHistory[k_maxNumberOfDisplayedRows]; + CalculationSelectableTableView m_selectableTableView; + HistoryViewCell m_calculationHistory[k_maxNumberOfDisplayedRows]; CalculationStore * m_calculationStore; }; diff --git a/apps/calculation/history_view_cell.h b/apps/calculation/history_view_cell.h index 4f7e060e6..b50fe6769 100644 --- a/apps/calculation/history_view_cell.h +++ b/apps/calculation/history_view_cell.h @@ -14,7 +14,7 @@ public: Input, Output }; - HistoryViewCell(Responder * parentResponder); + HistoryViewCell(Responder * parentResponder = nullptr); void reloadCell() override; void reloadScroll(); void setEven(bool even) override;