Skip to content

Commit e8bcd90

Browse files
authored
Refactoring (#17)
1 parent 70f84f0 commit e8bcd90

22 files changed

+534
-464
lines changed

application.fam

+1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ App(
1515
stack_size=2 * 1024,
1616
order=20,
1717
fap_category="Misc",
18+
fap_icon_assets="images",
1819
fap_icon="totp_10px.png"
1920
)

images/DolphinCommon_56x48.png

1.38 KB
Loading

images/totp_arrow_left_8x9.png

149 Bytes
Loading

images/totp_arrow_right_8x9.png

149 Bytes
Loading

scenes/add_new_token/totp_scene_add_new_token.c

+142-134
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "../../services/base32/base32.h"
99
#include "../../services/config/config.h"
1010
#include "../../services/ui/ui_controls.h"
11+
#include "../../services/roll_value/roll_value.h"
12+
#include "../../services/nullable/nullable.h"
1113
#include "../generate_token/totp_scene_generate_token.h"
1214

1315
#define TOKEN_ALGO_LIST_LENGTH 3
@@ -34,7 +36,7 @@ typedef struct {
3436
InputTextSceneContext* token_secret_input_context;
3537
InputTextSceneState* input_state;
3638
uint32_t input_started_at;
37-
int16_t current_token_index;
39+
TotpNullable_uint16_t current_token_index;
3840
int16_t screen_y_offset;
3941
TokenHashAlgo algo;
4042
TokenDigitsCount digits_count;
@@ -87,9 +89,9 @@ void totp_scene_add_new_token_activate(
8789
scene_state->input_state = NULL;
8890

8991
if(context == NULL) {
90-
scene_state->current_token_index = -1;
92+
TOTP_NULLABLE_NULL(scene_state->current_token_index);
9193
} else {
92-
scene_state->current_token_index = context->current_token_index;
94+
TOTP_NULLABLE_VALUE(scene_state->current_token_index, context->current_token_index);
9395
}
9496
}
9597

@@ -150,144 +152,150 @@ void update_screen_y_offset(SceneState* scene_state) {
150152
}
151153

152154
bool totp_scene_add_new_token_handle_event(PluginEvent* const event, PluginState* plugin_state) {
153-
if(event->type == EventTypeKey) {
154-
SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
155-
if(scene_state->input_started_at > 0 &&
156-
furi_get_tick() - scene_state->input_started_at > 300) {
157-
return totp_input_text_handle_event(event, scene_state->input_state);
155+
if(event->type != EventTypeKey) {
156+
return true;
157+
}
158+
159+
SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
160+
if(scene_state->input_started_at > 0 &&
161+
furi_get_tick() - scene_state->input_started_at > 300) {
162+
return totp_input_text_handle_event(event, scene_state->input_state);
163+
}
164+
165+
if(event->input.type == InputTypeLong && event->input.key == InputKeyBack) {
166+
return false;
167+
}
168+
169+
if(event->input.type != InputTypePress) {
170+
return true;
171+
}
172+
173+
switch(event->input.key) {
174+
case InputKeyUp:
175+
totp_roll_value_uint8_t(
176+
&scene_state->selected_control,
177+
-1,
178+
TokenNameTextBox,
179+
ConfirmButton,
180+
RollOverflowBehaviorStop);
181+
update_screen_y_offset(scene_state);
182+
break;
183+
case InputKeyDown:
184+
totp_roll_value_uint8_t(
185+
&scene_state->selected_control,
186+
1,
187+
TokenNameTextBox,
188+
ConfirmButton,
189+
RollOverflowBehaviorStop);
190+
update_screen_y_offset(scene_state);
191+
break;
192+
case InputKeyRight:
193+
if(scene_state->selected_control == TokenAlgoSelect) {
194+
totp_roll_value_uint8_t(&scene_state->algo, 1, SHA1, SHA512, RollOverflowBehaviorRoll);
195+
} else if(scene_state->selected_control == TokenLengthSelect) {
196+
totp_roll_value_uint8_t(
197+
&scene_state->digits_count,
198+
1,
199+
TOTP_6_DIGITS,
200+
TOTP_8_DIGITS,
201+
RollOverflowBehaviorRoll);
158202
}
203+
break;
204+
case InputKeyLeft:
205+
if(scene_state->selected_control == TokenAlgoSelect) {
206+
totp_roll_value_uint8_t(
207+
&scene_state->algo, -1, SHA1, SHA512, RollOverflowBehaviorRoll);
208+
} else if(scene_state->selected_control == TokenLengthSelect) {
209+
totp_roll_value_uint8_t(
210+
&scene_state->digits_count,
211+
-1,
212+
TOTP_6_DIGITS,
213+
TOTP_8_DIGITS,
214+
RollOverflowBehaviorRoll);
215+
}
216+
break;
217+
case InputKeyOk:
218+
switch(scene_state->selected_control) {
219+
case TokenNameTextBox:
220+
if(scene_state->input_state != NULL) {
221+
totp_input_text_free(scene_state->input_state);
222+
}
223+
scene_state->input_state =
224+
totp_input_text_activate(scene_state->token_name_input_context);
225+
scene_state->input_started_at = furi_get_tick();
226+
break;
227+
case TokenSecretTextBox:
228+
if(scene_state->input_state != NULL) {
229+
totp_input_text_free(scene_state->input_state);
230+
}
231+
scene_state->input_state =
232+
totp_input_text_activate(scene_state->token_secret_input_context);
233+
scene_state->input_started_at = furi_get_tick();
234+
break;
235+
case TokenAlgoSelect:
236+
break;
237+
case TokenLengthSelect:
238+
break;
239+
case ConfirmButton: {
240+
TokenInfo* tokenInfo = token_info_alloc();
241+
bool token_secret_set = token_info_set_secret(
242+
tokenInfo,
243+
scene_state->token_secret,
244+
scene_state->token_secret_length,
245+
&plugin_state->iv[0]);
159246

160-
if(event->input.type == InputTypeLong && event->input.key == InputKeyBack) {
161-
return false;
162-
} else if(event->input.type == InputTypePress) {
163-
switch(event->input.key) {
164-
case InputKeyUp:
165-
if(scene_state->selected_control > TokenNameTextBox) {
166-
scene_state->selected_control--;
167-
update_screen_y_offset(scene_state);
168-
}
169-
break;
170-
case InputKeyDown:
171-
if(scene_state->selected_control < ConfirmButton) {
172-
scene_state->selected_control++;
173-
update_screen_y_offset(scene_state);
174-
}
175-
break;
176-
case InputKeyRight:
177-
if(scene_state->selected_control == TokenAlgoSelect) {
178-
if(scene_state->algo < SHA512) {
179-
scene_state->algo++;
180-
} else {
181-
scene_state->algo = SHA1;
182-
}
183-
} else if(scene_state->selected_control == TokenLengthSelect) {
184-
if(scene_state->digits_count < TOTP_8_DIGITS) {
185-
scene_state->digits_count++;
186-
} else {
187-
scene_state->digits_count = TOTP_6_DIGITS;
188-
}
189-
}
190-
break;
191-
case InputKeyLeft:
192-
if(scene_state->selected_control == TokenAlgoSelect) {
193-
if(scene_state->algo > SHA1) {
194-
scene_state->algo--;
195-
} else {
196-
scene_state->algo = SHA512;
197-
}
198-
} else if(scene_state->selected_control == TokenLengthSelect) {
199-
if(scene_state->digits_count > TOTP_6_DIGITS) {
200-
scene_state->digits_count--;
201-
} else {
202-
scene_state->digits_count = TOTP_8_DIGITS;
203-
}
204-
}
205-
break;
206-
case InputKeyOk:
207-
switch(scene_state->selected_control) {
208-
case TokenNameTextBox:
209-
if(scene_state->input_state != NULL) {
210-
totp_input_text_free(scene_state->input_state);
211-
}
212-
scene_state->input_state =
213-
totp_input_text_activate(scene_state->token_name_input_context);
214-
scene_state->input_started_at = furi_get_tick();
215-
break;
216-
case TokenSecretTextBox:
217-
if(scene_state->input_state != NULL) {
218-
totp_input_text_free(scene_state->input_state);
219-
}
220-
scene_state->input_state =
221-
totp_input_text_activate(scene_state->token_secret_input_context);
222-
scene_state->input_started_at = furi_get_tick();
223-
break;
224-
case TokenAlgoSelect:
225-
break;
226-
case TokenLengthSelect:
227-
break;
228-
case ConfirmButton: {
229-
TokenInfo* tokenInfo = token_info_alloc();
230-
bool token_secret_set = token_info_set_secret(
231-
tokenInfo,
232-
scene_state->token_secret,
233-
scene_state->token_secret_length,
234-
&plugin_state->iv[0]);
235-
236-
if(token_secret_set) {
237-
tokenInfo->name = malloc(scene_state->token_name_length + 1);
238-
strlcpy(
239-
tokenInfo->name,
240-
scene_state->token_name,
241-
scene_state->token_name_length + 1);
242-
tokenInfo->algo = scene_state->algo;
243-
tokenInfo->digits = scene_state->digits_count;
244-
245-
if(plugin_state->tokens_list == NULL) {
246-
plugin_state->tokens_list = list_init_head(tokenInfo);
247-
} else {
248-
list_add(plugin_state->tokens_list, tokenInfo);
249-
}
250-
plugin_state->tokens_count++;
251-
252-
totp_config_file_save_new_token(tokenInfo);
253-
254-
GenerateTokenSceneContext generate_scene_context = {
255-
.current_token_index = plugin_state->tokens_count - 1};
256-
totp_scene_director_activate_scene(
257-
plugin_state, TotpSceneGenerateToken, &generate_scene_context);
258-
} else {
259-
token_info_free(tokenInfo);
260-
DialogMessage* message = dialog_message_alloc();
261-
dialog_message_set_buttons(message, "Back", NULL, NULL);
262-
dialog_message_set_text(
263-
message,
264-
"Token secret is invalid",
265-
SCREEN_WIDTH_CENTER,
266-
SCREEN_HEIGHT_CENTER,
267-
AlignCenter,
268-
AlignCenter);
269-
dialog_message_show(plugin_state->dialogs, message);
270-
dialog_message_free(message);
271-
scene_state->selected_control = TokenSecretTextBox;
272-
update_screen_y_offset(scene_state);
273-
}
274-
break;
275-
}
276-
}
277-
break;
278-
case InputKeyBack:
279-
if(scene_state->current_token_index >= 0) {
280-
GenerateTokenSceneContext generate_scene_context = {
281-
.current_token_index = scene_state->current_token_index};
282-
totp_scene_director_activate_scene(
283-
plugin_state, TotpSceneGenerateToken, &generate_scene_context);
247+
if(token_secret_set) {
248+
tokenInfo->name = malloc(scene_state->token_name_length + 1);
249+
strlcpy(
250+
tokenInfo->name, scene_state->token_name, scene_state->token_name_length + 1);
251+
tokenInfo->algo = scene_state->algo;
252+
tokenInfo->digits = scene_state->digits_count;
253+
254+
if(plugin_state->tokens_list == NULL) {
255+
plugin_state->tokens_list = list_init_head(tokenInfo);
284256
} else {
285-
totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
257+
list_add(plugin_state->tokens_list, tokenInfo);
286258
}
287-
break;
259+
plugin_state->tokens_count++;
260+
261+
totp_config_file_save_new_token(tokenInfo);
262+
263+
GenerateTokenSceneContext generate_scene_context = {
264+
.current_token_index = plugin_state->tokens_count - 1};
265+
totp_scene_director_activate_scene(
266+
plugin_state, TotpSceneGenerateToken, &generate_scene_context);
267+
} else {
268+
token_info_free(tokenInfo);
269+
DialogMessage* message = dialog_message_alloc();
270+
dialog_message_set_buttons(message, "Back", NULL, NULL);
271+
dialog_message_set_text(
272+
message,
273+
"Token secret is invalid",
274+
SCREEN_WIDTH_CENTER,
275+
SCREEN_HEIGHT_CENTER,
276+
AlignCenter,
277+
AlignCenter);
278+
dialog_message_show(plugin_state->dialogs, message);
279+
dialog_message_free(message);
280+
scene_state->selected_control = TokenSecretTextBox;
281+
update_screen_y_offset(scene_state);
288282
}
283+
break;
289284
}
285+
}
286+
break;
287+
case InputKeyBack:
288+
if(!scene_state->current_token_index.is_null) {
289+
GenerateTokenSceneContext generate_scene_context = {
290+
.current_token_index = scene_state->current_token_index.value};
291+
totp_scene_director_activate_scene(
292+
plugin_state, TotpSceneGenerateToken, &generate_scene_context);
293+
} else {
294+
totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
295+
}
296+
break;
290297
}
298+
291299
return true;
292300
}
293301

scenes/add_new_token/totp_scene_add_new_token.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "../../types/plugin_event.h"
88

99
typedef struct {
10-
uint8_t current_token_index;
10+
uint16_t current_token_index;
1111
} TokenAddEditSceneContext;
1212

1313
void totp_scene_add_new_token_init(const PluginState* plugin_state);

0 commit comments

Comments
 (0)