InfiniTime.git

commit c853681d753c7ca8e2fe7bb22ca046f46ec47460

Author: Riku Isokoski <riksu9000@gmail.com>

Reduce duplication in Twos (#1274)

 src/displayapp/screens/Twos.cpp | 71 ++++++++++++----------------------
 src/displayapp/screens/Twos.h | 7 --


diff --git a/src/displayapp/screens/Twos.cpp b/src/displayapp/screens/Twos.cpp
index 5d1f49804df38ecc87987c8a406a6f2c743193f5..9e7418c8dc747d2873fd53505b8f3502e09ff120 100644
--- a/src/displayapp/screens/Twos.cpp
+++ b/src/displayapp/screens/Twos.cpp
@@ -7,53 +7,34 @@ using namespace Pinetime::Applications::Screens;
 
 Twos::Twos(Pinetime::Applications::DisplayApp* app) : Screen(app) {
 
-  // create styles to apply to different valued tiles
-  lv_style_init(&style_cell1);
-  lv_style_init(&style_cell2);
-  lv_style_init(&style_cell3);
-  lv_style_init(&style_cell4);
-  lv_style_init(&style_cell5);
+  struct colorPair {
+    lv_color_t bg;
+    lv_color_t fg;
+  };
 
-  lv_style_set_border_color(&style_cell1, LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
-  lv_style_set_border_width(&style_cell1, LV_STATE_DEFAULT, 3);
-  lv_style_set_bg_opa(&style_cell1, LV_STATE_DEFAULT, LV_OPA_COVER);
-  lv_style_set_bg_color(&style_cell1, LV_STATE_DEFAULT, lv_color_hex(0xcdc0b4));
-  lv_style_set_pad_top(&style_cell1, LV_STATE_DEFAULT, 29);
-  lv_style_set_text_color(&style_cell1, LV_STATE_DEFAULT, LV_COLOR_BLACK);
+  static constexpr colorPair colors[nColors] = {
+    {LV_COLOR_MAKE(0xcd, 0xc0, 0xb4), LV_COLOR_BLACK},
+    {LV_COLOR_MAKE(0xef, 0xdf, 0xc6), LV_COLOR_BLACK},
+    {LV_COLOR_MAKE(0xef, 0x92, 0x63), LV_COLOR_WHITE},
+    {LV_COLOR_MAKE(0xf7, 0x61, 0x42), LV_COLOR_WHITE},
+    {LV_COLOR_MAKE(0x00, 0x7d, 0xc5), LV_COLOR_WHITE},
+  };
 
-  lv_style_set_border_color(&style_cell2, LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
-  lv_style_set_border_width(&style_cell2, LV_STATE_DEFAULT, 3);
-  lv_style_set_bg_opa(&style_cell2, LV_STATE_DEFAULT, LV_OPA_COVER);
-  lv_style_set_bg_color(&style_cell2, LV_STATE_DEFAULT, lv_color_hex(0xefdfc6));
-  lv_style_set_pad_top(&style_cell2, LV_STATE_DEFAULT, 29);
-  lv_style_set_text_color(&style_cell2, LV_STATE_DEFAULT, LV_COLOR_BLACK);
+  gridDisplay = lv_table_create(lv_scr_act(), nullptr);
 
-  lv_style_set_border_color(&style_cell3, LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
-  lv_style_set_border_width(&style_cell3, LV_STATE_DEFAULT, 3);
-  lv_style_set_bg_opa(&style_cell3, LV_STATE_DEFAULT, LV_OPA_COVER);
-  lv_style_set_bg_color(&style_cell3, LV_STATE_DEFAULT, lv_color_hex(0xef9263));
-  lv_style_set_pad_top(&style_cell3, LV_STATE_DEFAULT, 29);
+  for (size_t i = 0; i < nColors; i++) {
+    lv_style_init(&cellStyles[i]);
 
-  lv_style_set_border_color(&style_cell4, LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
-  lv_style_set_border_width(&style_cell4, LV_STATE_DEFAULT, 3);
-  lv_style_set_bg_opa(&style_cell4, LV_STATE_DEFAULT, LV_OPA_COVER);
-  lv_style_set_bg_color(&style_cell4, LV_STATE_DEFAULT, lv_color_hex(0xf76142));
-  lv_style_set_pad_top(&style_cell4, LV_STATE_DEFAULT, 29);
+    lv_style_set_border_color(&cellStyles[i], LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
+    lv_style_set_border_width(&cellStyles[i], LV_STATE_DEFAULT, 3);
+    lv_style_set_bg_opa(&cellStyles[i], LV_STATE_DEFAULT, LV_OPA_COVER);
+    lv_style_set_bg_color(&cellStyles[i], LV_STATE_DEFAULT, colors[i].bg);
+    lv_style_set_pad_top(&cellStyles[i], LV_STATE_DEFAULT, 29);
+    lv_style_set_text_color(&cellStyles[i], LV_STATE_DEFAULT, colors[i].fg);
 
-  lv_style_set_border_color(&style_cell5, LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
-  lv_style_set_border_width(&style_cell5, LV_STATE_DEFAULT, 3);
-  lv_style_set_bg_opa(&style_cell5, LV_STATE_DEFAULT, LV_OPA_COVER);
-  lv_style_set_bg_color(&style_cell5, LV_STATE_DEFAULT, lv_color_hex(0x007dc5));
-  lv_style_set_pad_top(&style_cell5, LV_STATE_DEFAULT, 29);
+    lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL1 + i, &cellStyles[i]);
+  }
 
-  // format grid display
-
-  gridDisplay = lv_table_create(lv_scr_act(), nullptr);
-  lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL1, &style_cell1);
-  lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL2, &style_cell2);
-  lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL3, &style_cell3);
-  lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL4, &style_cell4);
-  lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL4 + 1, &style_cell5);
   lv_table_set_col_cnt(gridDisplay, nCols);
   lv_table_set_row_cnt(gridDisplay, nRows);
   for (int col = 0; col < nCols; col++) {
@@ -83,11 +64,9 @@   lv_label_set_text_fmt(scoreText, "Score #FFFF00 %i#", score);
 }
 
 Twos::~Twos() {
-  lv_style_reset(&style_cell1);
-  lv_style_reset(&style_cell2);
-  lv_style_reset(&style_cell3);
-  lv_style_reset(&style_cell4);
-  lv_style_reset(&style_cell5);
+  for (lv_style_t cellStyle : cellStyles) {
+    lv_style_reset(&cellStyle);
+  }
   lv_obj_clean(lv_scr_act());
 }
 




diff --git a/src/displayapp/screens/Twos.h b/src/displayapp/screens/Twos.h
index 4a6ada0b7de16bb87667553a3bb12423a5d2dee2..da935724e5d9e98a9487f8b8d62835bb4b9f9e73 100644
--- a/src/displayapp/screens/Twos.h
+++ b/src/displayapp/screens/Twos.h
@@ -18,11 +18,8 @@
         bool OnTouchEvent(TouchEvents event) override;
 
       private:
-        lv_style_t style_cell1;
-        lv_style_t style_cell2;
-        lv_style_t style_cell3;
-        lv_style_t style_cell4;
-        lv_style_t style_cell5;
+        static constexpr int nColors = 5;
+        lv_style_t cellStyles[nColors];
 
         lv_obj_t* scoreText;
         lv_obj_t* gridDisplay;