From 1642276678814ff474c4e7b3089bb067306bf558 Mon Sep 17 00:00:00 2001 From: Gabriel Ozouf Date: Thu, 18 Jun 2020 17:25:08 +0200 Subject: [PATCH] [apps/shared] CountryController Created a CountryController class to chose the country in the settings and at onboarding. Change-Id: I6787e8b45744a8a98e83f59e5d2c4760364635cb --- apps/shared/Makefile | 1 + apps/shared/country_controller.cpp | 51 ++++++++++++++++++++++++++++++ apps/shared/country_controller.h | 37 ++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 apps/shared/country_controller.cpp create mode 100644 apps/shared/country_controller.h diff --git a/apps/shared/Makefile b/apps/shared/Makefile index b525a9033..481c962e0 100644 --- a/apps/shared/Makefile +++ b/apps/shared/Makefile @@ -26,6 +26,7 @@ app_shared_src = $(addprefix apps/shared/,\ buffer_function_title_cell.cpp \ buffer_text_view_with_text_field.cpp \ button_with_separator.cpp \ + country_controller.cpp \ cursor_view.cpp \ editable_cell_table_view_controller.cpp \ expression_field_delegate_app.cpp \ diff --git a/apps/shared/country_controller.cpp b/apps/shared/country_controller.cpp new file mode 100644 index 000000000..5efce8a6f --- /dev/null +++ b/apps/shared/country_controller.cpp @@ -0,0 +1,51 @@ +#include "country_controller.h" +#include "../global_preferences.h" +#include "../apps_container.h" + +namespace Shared { + +CountryController::CountryController(Responder * parentResponder, KDCoordinate verticalMargin) : + ViewController(parentResponder), + m_selectableTableView(this, this, this) +{ + m_selectableTableView.setTopMargin(verticalMargin); + m_selectableTableView.setBottomMargin(verticalMargin); + for (int i = 0; i < I18n::NumberOfCountries; i++) { + m_cells[i].setMessageFont(KDFont::LargeFont); + } +} + +void CountryController::resetSelection() { + m_selectableTableView.deselectTable(); + selectCellAtLocation(0, static_cast(GlobalPreferences::sharedGlobalPreferences()->country())); +} + + +void CountryController::viewWillAppear() { + ViewController::viewWillAppear(); + resetSelection(); + /* FIXME : When selecting a country, changing the language, then coming back + * to select a country, some countries' names will be cropped. We force the + * TableView to refresh to prevent that. */ + m_selectableTableView.reloadData(); +} + +bool CountryController::handleEvent(Ion::Events::Event event) { + if (event == Ion::Events::OK || event == Ion::Events::EXE) { + /* TODO : Since the order in which the countries are displayed changes with + * the language, we need something more sophisticated + * than (I18n::Country)selectedRow() */ + GlobalPreferences::sharedGlobalPreferences()->setCountry(static_cast(selectedRow())); + return true; + } + return false; +} + +void CountryController::willDisplayCellForIndex(HighlightCell * cell, int index) { + /* TODO : We want the countries to always appear in alphabetical order. The + * index from which the country's name is fetched must take that into + * account.*/ + static_cast(cell)->setMessage(I18n::CountryNames[index]); +} + +} diff --git a/apps/shared/country_controller.h b/apps/shared/country_controller.h new file mode 100644 index 000000000..8cf355e07 --- /dev/null +++ b/apps/shared/country_controller.h @@ -0,0 +1,37 @@ +#ifndef SHARED_COUNTRY_CONTROLLER_H +#define SHARED_COUNTRY_CONTROLLER_H + +#include +#include + +namespace Shared { + +class CountryController : public ViewController, public SimpleListViewDataSource, public SelectableTableViewDataSource { +public: + CountryController(Responder * parentResponder, KDCoordinate verticalMargin); + void resetSelection(); + + View * view() override { return &m_selectableTableView; } + const char * title() override {return I18n::translate(I18n::Message::Country); } + void didBecomeFirstResponder() override {Container::activeApp()->setFirstResponder(&m_selectableTableView); } + void viewWillAppear() override; + bool handleEvent(Ion::Events::Event event) override; + + int numberOfRows() const override { return I18n::NumberOfCountries; } + KDCoordinate cellHeight() override { return Metric::ParameterCellHeight; } + HighlightCell * reusableCell(int index) override { return &m_cells[index]; } + int reusableCellCount() const override { return I18n::NumberOfCountries; } + + void willDisplayCellForIndex(HighlightCell * cell, int index) override; + +protected: + SelectableTableView m_selectableTableView; + +private: + MessageTableCell m_cells[I18n::NumberOfCountries]; + // TODO : Add variables for static text +}; + +} + +#endif