From cfd518283f2a5ae129e83cc7e1d356f995ad2ebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Fri, 17 Feb 2017 14:37:54 +0100 Subject: [PATCH] [apps/shared] In list controller, factorize handle event Change-Id: I0e00f1cf3b217782ec6f858d60954733e2be5276 --- apps/graph/list/list_controller.cpp | 48 ----------- apps/graph/list/list_controller.h | 4 +- apps/sequence/list/list_controller.cpp | 111 ++++++++++--------------- apps/sequence/list/list_controller.h | 6 +- apps/shared/list_controller.cpp | 45 ++++++++++ apps/shared/list_controller.h | 3 + 6 files changed, 94 insertions(+), 123 deletions(-) diff --git a/apps/graph/list/list_controller.cpp b/apps/graph/list/list_controller.cpp index 434573b5d..c174b5db0 100644 --- a/apps/graph/list/list_controller.cpp +++ b/apps/graph/list/list_controller.cpp @@ -37,54 +37,6 @@ KDCoordinate ListController::rowHeight(int j) { return functionSize + k_emptyRowHeight - KDText::stringSize(" ").height(); } -bool ListController::handleEvent(Ion::Events::Event event) { - if (Shared::ListController::handleEvent(event)) { - return true; - } - if (event == Ion::Events::OK) { - return handleEnter(); - } - if ((!event.hasText() && event != Ion::Events::XNT) - || m_selectableTableView.selectedColumn() == 0 - || (m_selectableTableView.selectedRow() == numberOfRows() - 1 - && m_functionStore->numberOfFunctions() < m_functionStore->maxNumberOfFunctions())) { - return false; - } - Shared::Function * function = m_functionStore->functionAtIndex(m_selectableTableView.selectedRow()); - editExpression(function, event); - return true; -} - -bool ListController::handleEnter() { - switch (m_selectableTableView.selectedColumn()) { - case 0: - { - if (m_functionStore->numberOfFunctions() < m_functionStore->maxNumberOfFunctions() && - m_selectableTableView.selectedRow() == numberOfRows() - 1) { - return true; - } - configureFunction(m_functionStore->functionAtIndex(m_selectableTableView.selectedRow())); - return true; - } - case 1: - { - if (m_functionStore->numberOfFunctions() < m_functionStore->maxNumberOfFunctions() && - m_selectableTableView.selectedRow() == numberOfRows() - 1) { - m_functionStore->addEmptyFunction(); - m_selectableTableView.reloadData(); - return true; - } - Shared::Function * function = m_functionStore->functionAtIndex(m_selectableTableView.selectedRow()); - editExpression(function, Ion::Events::OK); - return true; - } - default: - { - return false; - } - } -} - void ListController::editExpression(Function * function, Ion::Events::Event event) { char * initialText = nullptr; char initialTextContent[255]; diff --git a/apps/graph/list/list_controller.h b/apps/graph/list/list_controller.h index 9fb0ed026..ea90d3e22 100644 --- a/apps/graph/list/list_controller.h +++ b/apps/graph/list/list_controller.h @@ -17,10 +17,8 @@ public: const char * title() const override; int numberOfRows() override; KDCoordinate rowHeight(int j) override; - bool handleEvent(Ion::Events::Event event) override; private: - bool handleEnter(); - void editExpression(Shared::Function * function, Ion::Events::Event event); + void editExpression(Shared::Function * function, Ion::Events::Event event) override; Shared::ListParameterController * parameterController() override; int maxNumberOfRows() override; HighlightCell * titleCells(int index) override; diff --git a/apps/sequence/list/list_controller.cpp b/apps/sequence/list/list_controller.cpp index ccc2a7f64..00c48ebda 100644 --- a/apps/sequence/list/list_controller.cpp +++ b/apps/sequence/list/list_controller.cpp @@ -27,7 +27,7 @@ const char * ListController::title() const { Toolbox * ListController::toolboxForTextField(TextField * textField) { int recurrenceDepth = 0; int sequenceDefinition = sequenceDefinitionForRow(m_selectableTableView.selectedRow()); - Sequence * sequence = m_sequenceStore->functionAtIndex(sequenceIndexForRow(m_selectableTableView.selectedRow())); + Sequence * sequence = m_sequenceStore->functionAtIndex(functionIndexForRow(m_selectableTableView.selectedRow())); if (sequenceDefinition == 0) { recurrenceDepth = sequence->numberOfElements()-1; } @@ -55,7 +55,7 @@ KDCoordinate ListController::rowHeight(int j) { if (m_sequenceStore->numberOfFunctions() < m_sequenceStore->maxNumberOfFunctions() && j == numberOfRows() - 1) { return k_emptyRowHeight; } - Sequence * sequence = m_sequenceStore->functionAtIndex(sequenceIndexForRow(j)); + Sequence * sequence = m_sequenceStore->functionAtIndex(functionIndexForRow(j)); KDCoordinate defaultHeight = sequence->type() == Sequence::Type::Explicite ? k_emptyRowHeight : k_emptySubRowHeight; ExpressionLayout * layout = sequence->layout(); if (sequenceDefinitionForRow(j) == 1) { @@ -74,78 +74,42 @@ KDCoordinate ListController::rowHeight(int j) { void ListController::willDisplayCellAtLocation(HighlightCell * cell, int i, int j) { Shared::ListController::willDisplayCellAtLocation(cell, i, j); EvenOddCell * myCell = (EvenOddCell *)cell; - myCell->setEven(sequenceIndexForRow(j)%2 == 0); -} - -bool ListController::handleEvent(Ion::Events::Event event) { - if (Shared::ListController::handleEvent(event)) { - return true; - } - if (event == Ion::Events::OK) { - return handleEnter(); - } - if ((!event.hasText() && event != Ion::Events::XNT) - || m_selectableTableView.selectedColumn() == 0 - || (m_selectableTableView.selectedRow() == numberOfRows() - 1 - && m_sequenceStore->numberOfFunctions() < m_sequenceStore->maxNumberOfFunctions())) { - return false; - } - Sequence * sequence = m_sequenceStore->functionAtIndex(sequenceIndexForRow(m_selectableTableView.selectedRow())); - editExpression(sequence, sequenceDefinitionForRow(m_selectableTableView.selectedRow()), event); - return true; -} - -bool ListController::handleEnter() { - switch (m_selectableTableView.selectedColumn()) { - case 0: - { - if (m_sequenceStore->numberOfFunctions() < m_sequenceStore->maxNumberOfFunctions() && - m_selectableTableView.selectedRow() == numberOfRows() - 1) { - return true; - } - configureFunction(m_functionStore->functionAtIndex(sequenceIndexForRow(m_selectableTableView.selectedRow()))); - return true; - } - case 1: - { - if (m_sequenceStore->numberOfFunctions() < m_sequenceStore->maxNumberOfFunctions() && - m_selectableTableView.selectedRow() == numberOfRows() - 1) { - app()->displayModalViewController(&m_typeStackController, 0.f, 0.f, 32, 20, 20, 20); - return true; - } - Sequence * sequence = m_sequenceStore->functionAtIndex(sequenceIndexForRow(m_selectableTableView.selectedRow())); - editExpression(sequence, sequenceDefinitionForRow(m_selectableTableView.selectedRow()), Ion::Events::OK); - return true; - } - default: - { - return false; - } - } + myCell->setEven(functionIndexForRow(j)%2 == 0); } void ListController::editExpression(Sequence * sequence, int sequenceDefinition, Ion::Events::Event event) { char * initialText = nullptr; char initialTextContent[255]; if (event == Ion::Events::OK) { - strlcpy(initialTextContent, sequence->text(), sizeof(initialTextContent)); + switch (sequenceDefinition) { + case 0: + strlcpy(initialTextContent, sequence->text(), sizeof(initialTextContent)); + break; + case 1: + strlcpy(initialTextContent, sequence->firstInitialConditionText(), sizeof(initialTextContent)); + break; + default: + strlcpy(initialTextContent, sequence->secondInitialConditionText(), sizeof(initialTextContent)); + break; + } initialText = initialTextContent; } App * myApp = (App *)app(); InputViewController * inputController = myApp->inputViewController(); inputController->setTextFieldDelegate(this); - if (sequenceDefinition == 0) { - inputController->edit(this, event, sequence, initialText, - [](void * context, void * sender){ - Sequence * mySequence = (Sequence *)context; - InputViewController * myInputViewController = (InputViewController *)sender; - const char * textBody = myInputViewController->textBody(); - mySequence->setContent(textBody); - }, - [](void * context, void * sender){ - }); - } - if (sequenceDefinition == 1) { + switch (sequenceDefinition) { + case 0: + inputController->edit(this, event, sequence, initialText, + [](void * context, void * sender){ + Sequence * mySequence = (Sequence *)context; + InputViewController * myInputViewController = (InputViewController *)sender; + const char * textBody = myInputViewController->textBody(); + mySequence->setContent(textBody); + }, + [](void * context, void * sender){ + }); + break; + case 1: inputController->edit(this, event, sequence, initialText, [](void * context, void * sender){ Sequence * mySequence = (Sequence *)context; @@ -155,8 +119,8 @@ void ListController::editExpression(Sequence * sequence, int sequenceDefinition, }, [](void * context, void * sender){ }); - } - if (sequenceDefinition == 2) { + break; + default: inputController->edit(this, event, sequence, initialText, [](void * context, void * sender){ Sequence * mySequence = (Sequence *)context; @@ -190,7 +154,7 @@ HighlightCell * ListController::expressionCells(int index) { void ListController::willDisplayTitleCellAtIndex(HighlightCell * cell, int j) { SequenceTitleCell * myCell = (SequenceTitleCell *)cell; - Sequence * sequence = m_sequenceStore->functionAtIndex(sequenceIndexForRow(j)); + Sequence * sequence = m_sequenceStore->functionAtIndex(functionIndexForRow(j)); if (sequenceDefinitionForRow(j) == 0) { myCell->setExpression(sequence->definitionName()); } @@ -206,7 +170,7 @@ void ListController::willDisplayTitleCellAtIndex(HighlightCell * cell, int j) { void ListController::willDisplayExpressionCellAtIndex(HighlightCell * cell, int j) { FunctionExpressionCell * myCell = (FunctionExpressionCell *)cell; - Sequence * sequence = m_sequenceStore->functionAtIndex(sequenceIndexForRow(j)); + Sequence * sequence = m_sequenceStore->functionAtIndex(functionIndexForRow(j)); if (sequenceDefinitionForRow(j) == 0) { myCell->setExpression(sequence->layout()); } @@ -221,13 +185,13 @@ void ListController::willDisplayExpressionCellAtIndex(HighlightCell * cell, int myCell->setTextColor(textColor); } -int ListController::sequenceIndexForRow(int j) { +int ListController::functionIndexForRow(int j) { if (j < 0) { return j; } if (m_sequenceStore->numberOfFunctions() < m_sequenceStore->maxNumberOfFunctions() && j == numberOfRows() - 1) { - return sequenceIndexForRow(j-1)+1; + return functionIndexForRow(j-1)+1; } int rowIndex = 0; int sequenceIndex = -1; @@ -258,4 +222,13 @@ int ListController::sequenceDefinitionForRow(int j) { return sequence->numberOfElements()-rowIndex+j; } +void ListController::addEmptyFunction() { + app()->displayModalViewController(&m_typeStackController, 0.f, 0.f, 32, 20, 20, 20); +} + +void ListController::editExpression(Shared::Function * function, Ion::Events::Event event) { + Sequence * sequence = (Sequence *)function; + editExpression(sequence, sequenceDefinitionForRow(m_selectableTableView.selectedRow()), event); +} + } diff --git a/apps/sequence/list/list_controller.h b/apps/sequence/list/list_controller.h index ece4a97bd..af897c037 100644 --- a/apps/sequence/list/list_controller.h +++ b/apps/sequence/list/list_controller.h @@ -21,11 +21,9 @@ public: int numberOfRows() override; virtual KDCoordinate rowHeight(int j) override; void willDisplayCellAtLocation(HighlightCell * cell, int i, int j) override; - bool handleEvent(Ion::Events::Event event) override; Toolbox * toolboxForTextField(TextField * textField) override; private: Shared::TextFieldDelegateApp * textFieldDelegateApp() override; - bool handleEnter(); void editExpression(Sequence * sequence, int sequenceDefinitionIndex, Ion::Events::Event event); ListParameterController * parameterController() override; int maxNumberOfRows() override; @@ -33,8 +31,10 @@ private: HighlightCell * expressionCells(int index) override; void willDisplayTitleCellAtIndex(HighlightCell * cell, int j) override; void willDisplayExpressionCellAtIndex(HighlightCell * cell, int j) override; - int sequenceIndexForRow(int j); + int functionIndexForRow(int j) override; int sequenceDefinitionForRow(int j); + void addEmptyFunction() override; + void editExpression(Shared::Function * function, Ion::Events::Event event) override; static constexpr KDCoordinate k_emptySubRowHeight = 30; constexpr static int k_maxNumberOfRows = 9; SequenceStore * m_sequenceStore; diff --git a/apps/shared/list_controller.cpp b/apps/shared/list_controller.cpp index 4a86da756..77e18368e 100644 --- a/apps/shared/list_controller.cpp +++ b/apps/shared/list_controller.cpp @@ -140,6 +140,42 @@ bool ListController::handleEvent(Ion::Events::Event event) { app()->setFirstResponder(tabController()); return true; } + if (event == Ion::Events::OK) { + switch (m_selectableTableView.selectedColumn()) { + case 0: + { + if (m_functionStore->numberOfFunctions() < m_functionStore->maxNumberOfFunctions() && + m_selectableTableView.selectedRow() == numberOfRows() - 1) { + return true; + } + configureFunction(m_functionStore->functionAtIndex(functionIndexForRow(m_selectableTableView.selectedRow()))); + return true; + } + case 1: + { + if (m_functionStore->numberOfFunctions() < m_functionStore->maxNumberOfFunctions() && + m_selectableTableView.selectedRow() == numberOfRows() - 1) { + addEmptyFunction(); + return true; + } + Shared::Function * function = m_functionStore->functionAtIndex(functionIndexForRow(m_selectableTableView.selectedRow())); + editExpression(function, Ion::Events::OK); + return true; + } + default: + { + return false; + } + } + } + if ((event.hasText() || event == Ion::Events::XNT) + && m_selectableTableView.selectedColumn() == 1 + && (m_selectableTableView.selectedRow() != numberOfRows() - 1 + || m_functionStore->numberOfFunctions() == m_functionStore->maxNumberOfFunctions())) { + Shared::Function * function = m_functionStore->functionAtIndex(functionIndexForRow(m_selectableTableView.selectedRow())); + editExpression(function, event); + return true; + } return false; } @@ -161,4 +197,13 @@ Responder * ListController::tabController() const{ return (parentResponder()->parentResponder()->parentResponder()); } +int ListController::functionIndexForRow(int j) { + return j; +} + +void ListController::addEmptyFunction() { + m_functionStore->addEmptyFunction(); + m_selectableTableView.reloadData(); +} + } diff --git a/apps/shared/list_controller.h b/apps/shared/list_controller.h index c9bc4577a..89c9ed2fa 100644 --- a/apps/shared/list_controller.h +++ b/apps/shared/list_controller.h @@ -34,6 +34,9 @@ protected: private: static constexpr KDCoordinate k_functionNameWidth = 65; Responder * tabController() const; + virtual int functionIndexForRow(int j); + virtual void addEmptyFunction(); + virtual void editExpression(Function * function, Ion::Events::Event event) = 0; virtual ListParameterController * parameterController() = 0; virtual int maxNumberOfRows() = 0; virtual HighlightCell * titleCells(int index) = 0;