Explorar o código

Fixed #212 (#213)

* Fixed #212. Jusat a bit of refactoring

* CLang format changes

---------

Co-authored-by: akopachov <akopachov@users.noreply.github.com>
Alexander Kopachov %!s(int64=2) %!d(string=hai) anos
pai
achega
5e9db3fbde

+ 26 - 7
ui/scenes/add_new_token/totp_scene_add_new_token.c

@@ -62,7 +62,8 @@ static void update_duration_text(SceneState* scene_state) {
 
 static TotpIteratorUpdateTokenResult add_token_handler(TokenInfo* tokenInfo, const void* context) {
     const struct TotpAddContext* context_t = context;
-    if(sscanf(context_t->scene_state->initial_counter, "%" PRIu64, &tokenInfo->counter) != 1) {
+    if(context_t->scene_state->type == TokenTypeHOTP &&
+       sscanf(context_t->scene_state->initial_counter, "%" PRIu64, &tokenInfo->counter) != 1) {
         return TotpIteratorUpdateTokenResultInvalidCounter;
     }
 
@@ -264,15 +265,24 @@ bool totp_scene_add_new_token_handle_event(
                     RollOverflowBehaviorRoll);
             } else if(scene_state->selected_control == TokenLengthSelect) {
                 totp_roll_value_uint8_t(
-                    &scene_state->digits_count_index, 1, 0, 2, RollOverflowBehaviorRoll);
+                    &scene_state->digits_count_index,
+                    1,
+                    0,
+                    COUNT_OF(TOKEN_DIGITS_TEXT_LIST) - 1,
+                    RollOverflowBehaviorRoll);
             } else if(
                 scene_state->selected_control == TokenDurationOrCounterSelect &&
                 scene_state->type == TokenTypeTOTP) {
                 totp_roll_value_uint8_t(
-                    &scene_state->duration, 15, 15, 255, RollOverflowBehaviorStop);
+                    &scene_state->duration, 15, 15, UINT8_MAX, RollOverflowBehaviorStop);
                 update_duration_text(scene_state);
             } else if(scene_state->selected_control == TokenTypeSelect) {
-                totp_roll_value_uint8_t(&scene_state->type, 1, 0, 1, RollOverflowBehaviorRoll);
+                totp_roll_value_uint8_t(
+                    &scene_state->type,
+                    1,
+                    0,
+                    COUNT_OF(TOKEN_TYPE_LIST) - 1,
+                    RollOverflowBehaviorRoll);
             }
             break;
         case InputKeyLeft:
@@ -285,15 +295,24 @@ bool totp_scene_add_new_token_handle_event(
                     RollOverflowBehaviorRoll);
             } else if(scene_state->selected_control == TokenLengthSelect) {
                 totp_roll_value_uint8_t(
-                    &scene_state->digits_count_index, -1, 0, 2, RollOverflowBehaviorRoll);
+                    &scene_state->digits_count_index,
+                    -1,
+                    0,
+                    COUNT_OF(TOKEN_DIGITS_TEXT_LIST) - 1,
+                    RollOverflowBehaviorRoll);
             } else if(
                 scene_state->selected_control == TokenDurationOrCounterSelect &&
                 scene_state->type == TokenTypeTOTP) {
                 totp_roll_value_uint8_t(
-                    &scene_state->duration, -15, 15, 255, RollOverflowBehaviorStop);
+                    &scene_state->duration, -15, 15, UINT8_MAX, RollOverflowBehaviorStop);
                 update_duration_text(scene_state);
             } else if(scene_state->selected_control == TokenTypeSelect) {
-                totp_roll_value_uint8_t(&scene_state->type, -1, 0, 1, RollOverflowBehaviorRoll);
+                totp_roll_value_uint8_t(
+                    &scene_state->type,
+                    -1,
+                    0,
+                    COUNT_OF(TOKEN_TYPE_LIST) - 1,
+                    RollOverflowBehaviorRoll);
             }
             break;
         case InputKeyOk:

+ 6 - 12
ui/scenes/app_settings/totp_app_settings.c

@@ -15,12 +15,6 @@
 #include "../../../workers/bt_type_code/bt_type_code.h"
 #endif
 
-#ifdef TOTP_BADBT_AUTOMATION_ENABLED
-#define AUTOMATION_LIST_MAX_INDEX (3)
-#else
-#define AUTOMATION_LIST_MAX_INDEX (1)
-#endif
-#define BAD_KB_LAYOUT_LIST_MAX_INDEX (2)
 #define FONT_TEST_STR_LENGTH (7)
 
 static const char* YES_NO_LIST[] = {"NO", "YES"};
@@ -73,9 +67,9 @@ void totp_scene_app_settings_activate(PluginState* plugin_state) {
     scene_state->notification_sound = plugin_state->notification_method & NotificationMethodSound;
     scene_state->notification_vibro = plugin_state->notification_method & NotificationMethodVibro;
     scene_state->automation_method =
-        MIN(plugin_state->automation_method, AUTOMATION_LIST_MAX_INDEX);
+        MIN(plugin_state->automation_method, COUNT_OF(AUTOMATION_LIST) - 1);
     scene_state->automation_kb_layout =
-        MIN(plugin_state->automation_kb_layout, BAD_KB_LAYOUT_LIST_MAX_INDEX);
+        MIN(plugin_state->automation_kb_layout, COUNT_OF(BAD_KB_LAYOUT_LIST) - 1);
 
     scene_state->total_fonts_count = totp_font_provider_get_fonts_count();
     scene_state->active_font_index = plugin_state->active_font_index;
@@ -288,14 +282,14 @@ bool totp_scene_app_settings_handle_event(
                     &scene_state->automation_method,
                     1,
                     0,
-                    AUTOMATION_LIST_MAX_INDEX,
+                    COUNT_OF(AUTOMATION_LIST) - 1,
                     RollOverflowBehaviorRoll);
             } else if(scene_state->selected_control == BadKeyboardLayoutSelect) {
                 totp_roll_value_uint8_t(
                     &scene_state->automation_kb_layout,
                     1,
                     0,
-                    BAD_KB_LAYOUT_LIST_MAX_INDEX,
+                    COUNT_OF(BAD_KB_LAYOUT_LIST) - 1,
                     RollOverflowBehaviorRoll);
             }
             break;
@@ -324,14 +318,14 @@ bool totp_scene_app_settings_handle_event(
                     &scene_state->automation_method,
                     -1,
                     0,
-                    AUTOMATION_LIST_MAX_INDEX,
+                    COUNT_OF(AUTOMATION_LIST) - 1,
                     RollOverflowBehaviorRoll);
             } else if(scene_state->selected_control == BadKeyboardLayoutSelect) {
                 totp_roll_value_uint8_t(
                     &scene_state->automation_kb_layout,
                     -1,
                     0,
-                    BAD_KB_LAYOUT_LIST_MAX_INDEX,
+                    COUNT_OF(BAD_KB_LAYOUT_LIST) - 1,
                     RollOverflowBehaviorRoll);
             }
             break;