nfc_maker_text_input.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. #include "nfc_maker_text_input.h"
  2. #include <gui/elements.h>
  3. #include "nfc_maker.h"
  4. #include <furi.h>
  5. struct NFCMaker_TextInput {
  6. View* view;
  7. FuriTimer* timer;
  8. };
  9. typedef struct {
  10. const char text;
  11. const uint8_t x;
  12. const uint8_t y;
  13. } NFCMaker_TextInputKey;
  14. typedef struct {
  15. const NFCMaker_TextInputKey* rows[3];
  16. const uint8_t keyboard_index;
  17. } Keyboard;
  18. typedef struct {
  19. const char* header;
  20. char* text_buffer;
  21. size_t text_buffer_size;
  22. size_t minimum_length;
  23. bool clear_default_text;
  24. bool cursor_select;
  25. size_t cursor_pos;
  26. NFCMaker_TextInputCallback callback;
  27. void* callback_context;
  28. uint8_t selected_row;
  29. uint8_t selected_column;
  30. uint8_t selected_keyboard;
  31. NFCMaker_TextInputValidatorCallback validator_callback;
  32. void* validator_callback_context;
  33. FuriString* validator_text;
  34. bool validator_message_visible;
  35. } NFCMaker_TextInputModel;
  36. static const uint8_t keyboard_origin_x = 1;
  37. static const uint8_t keyboard_origin_y = 29;
  38. static const uint8_t keyboard_row_count = 3;
  39. static const uint8_t keyboard_count = 2;
  40. #define ENTER_KEY '\r'
  41. #define BACKSPACE_KEY '\b'
  42. #define SWITCH_KEYBOARD_KEY 0xfe
  43. static const NFCMaker_TextInputKey keyboard_keys_row_1[] = {
  44. {'q', 1, 8},
  45. {'w', 10, 8},
  46. {'e', 19, 8},
  47. {'r', 28, 8},
  48. {'t', 37, 8},
  49. {'y', 46, 8},
  50. {'u', 55, 8},
  51. {'i', 64, 8},
  52. {'o', 73, 8},
  53. {'p', 82, 8},
  54. {'0', 91, 8},
  55. {'1', 100, 8},
  56. {'2', 110, 8},
  57. {'3', 120, 8},
  58. };
  59. static const NFCMaker_TextInputKey keyboard_keys_row_2[] = {
  60. {'a', 1, 20},
  61. {'s', 10, 20},
  62. {'d', 19, 20},
  63. {'f', 28, 20},
  64. {'g', 37, 20},
  65. {'h', 46, 20},
  66. {'j', 55, 20},
  67. {'k', 64, 20},
  68. {'l', 73, 20},
  69. {BACKSPACE_KEY, 82, 12},
  70. {'4', 100, 20},
  71. {'5', 110, 20},
  72. {'6', 120, 20},
  73. };
  74. static const NFCMaker_TextInputKey keyboard_keys_row_3[] = {
  75. {SWITCH_KEYBOARD_KEY, 1, 23},
  76. {'z', 13, 32},
  77. {'x', 21, 32},
  78. {'c', 28, 32},
  79. {'v', 36, 32},
  80. {'b', 44, 32},
  81. {'n', 52, 32},
  82. {'m', 59, 32},
  83. {'_', 67, 32},
  84. {ENTER_KEY, 74, 23},
  85. {'7', 100, 32},
  86. {'8', 110, 32},
  87. {'9', 120, 32},
  88. };
  89. static const NFCMaker_TextInputKey symbol_keyboard_keys_row_1[] = {
  90. {'!', 2, 8},
  91. {'@', 12, 8},
  92. {'#', 22, 8},
  93. {'$', 32, 8},
  94. {'%', 42, 8},
  95. {'^', 52, 8},
  96. {'&', 62, 8},
  97. {'(', 71, 8},
  98. {')', 81, 8},
  99. {'0', 91, 8},
  100. {'1', 100, 8},
  101. {'2', 110, 8},
  102. {'3', 120, 8},
  103. };
  104. static const NFCMaker_TextInputKey symbol_keyboard_keys_row_2[] = {
  105. {'~', 2, 20},
  106. {'+', 12, 20},
  107. {'-', 22, 20},
  108. {'=', 32, 20},
  109. {'[', 42, 20},
  110. {']', 52, 20},
  111. {'{', 62, 20},
  112. {'}', 72, 20},
  113. {BACKSPACE_KEY, 82, 12},
  114. {'4', 100, 20},
  115. {'5', 110, 20},
  116. {'6', 120, 20},
  117. };
  118. static const NFCMaker_TextInputKey symbol_keyboard_keys_row_3[] = {
  119. {SWITCH_KEYBOARD_KEY, 1, 23},
  120. {'.', 15, 32},
  121. {',', 29, 32},
  122. {':', 41, 32},
  123. {'/', 53, 32},
  124. {'\'', 65, 32},
  125. {ENTER_KEY, 74, 23},
  126. {'7', 100, 32},
  127. {'8', 110, 32},
  128. {'9', 120, 32},
  129. };
  130. static const Keyboard keyboard = {
  131. .rows =
  132. {
  133. keyboard_keys_row_1,
  134. keyboard_keys_row_2,
  135. keyboard_keys_row_3,
  136. },
  137. .keyboard_index = 0,
  138. };
  139. static const Keyboard symbol_keyboard = {
  140. .rows =
  141. {
  142. symbol_keyboard_keys_row_1,
  143. symbol_keyboard_keys_row_2,
  144. symbol_keyboard_keys_row_3,
  145. },
  146. .keyboard_index = 1,
  147. };
  148. static const Keyboard* keyboards[] = {
  149. &keyboard,
  150. &symbol_keyboard,
  151. };
  152. static void switch_keyboard(NFCMaker_TextInputModel* model) {
  153. model->selected_keyboard = (model->selected_keyboard + 1) % keyboard_count;
  154. }
  155. static uint8_t get_row_size(const Keyboard* keyboard, uint8_t row_index) {
  156. uint8_t row_size = 0;
  157. if(keyboard == &symbol_keyboard) {
  158. switch(row_index + 1) {
  159. case 1:
  160. row_size = COUNT_OF(symbol_keyboard_keys_row_1);
  161. break;
  162. case 2:
  163. row_size = COUNT_OF(symbol_keyboard_keys_row_2);
  164. break;
  165. case 3:
  166. row_size = COUNT_OF(symbol_keyboard_keys_row_3);
  167. break;
  168. default:
  169. furi_crash(NULL);
  170. }
  171. } else {
  172. switch(row_index + 1) {
  173. case 1:
  174. row_size = COUNT_OF(keyboard_keys_row_1);
  175. break;
  176. case 2:
  177. row_size = COUNT_OF(keyboard_keys_row_2);
  178. break;
  179. case 3:
  180. row_size = COUNT_OF(keyboard_keys_row_3);
  181. break;
  182. default:
  183. furi_crash(NULL);
  184. }
  185. }
  186. return row_size;
  187. }
  188. static const NFCMaker_TextInputKey* get_row(const Keyboard* keyboard, uint8_t row_index) {
  189. const NFCMaker_TextInputKey* row = NULL;
  190. if(row_index < 3) {
  191. row = keyboard->rows[row_index];
  192. } else {
  193. furi_crash(NULL);
  194. }
  195. return row;
  196. }
  197. static char get_selected_char(NFCMaker_TextInputModel* model) {
  198. return get_row(
  199. keyboards[model->selected_keyboard], model->selected_row)[model->selected_column]
  200. .text;
  201. }
  202. static bool char_is_lowercase(char letter) {
  203. return (letter >= 0x61 && letter <= 0x7A);
  204. }
  205. static char char_to_uppercase(const char letter) {
  206. if(letter == '_') {
  207. return 0x20;
  208. } else if(letter == ':') {
  209. return 0x3B;
  210. } else if(letter == '/') {
  211. return 0x5C;
  212. } else if(letter == '\'') {
  213. return 0x60;
  214. } else if(letter == '.') {
  215. return 0x2A;
  216. } else if(char_is_lowercase(letter)) {
  217. return (letter - 0x20);
  218. } else {
  219. return letter;
  220. }
  221. }
  222. static void nfc_maker_text_input_backspace_cb(NFCMaker_TextInputModel* model) {
  223. if(model->clear_default_text) {
  224. model->text_buffer[0] = 0;
  225. model->cursor_pos = 0;
  226. } else if(model->cursor_pos > 0) {
  227. char* move = model->text_buffer + model->cursor_pos;
  228. memmove(move - 1, move, strlen(move) + 1);
  229. model->cursor_pos--;
  230. }
  231. }
  232. static void nfc_maker_text_input_view_draw_callback(Canvas* canvas, void* _model) {
  233. NFCMaker_TextInputModel* model = _model;
  234. uint8_t text_length = model->text_buffer ? strlen(model->text_buffer) : 0;
  235. uint8_t needed_string_width = canvas_width(canvas) - 8;
  236. uint8_t start_pos = 4;
  237. model->cursor_pos = model->cursor_pos > text_length ? text_length : model->cursor_pos;
  238. size_t cursor_pos = model->cursor_pos;
  239. canvas_clear(canvas);
  240. canvas_set_color(canvas, ColorBlack);
  241. canvas_draw_str(canvas, 2, 8, model->header);
  242. elements_slightly_rounded_frame(canvas, 1, 12, 126, 15);
  243. char buf[model->text_buffer_size + 1];
  244. if(model->text_buffer) {
  245. strlcpy(buf, model->text_buffer, sizeof(buf));
  246. }
  247. char* str = buf;
  248. if(model->clear_default_text) {
  249. elements_slightly_rounded_box(
  250. canvas, start_pos - 1, 14, canvas_string_width(canvas, str) + 2, 10);
  251. canvas_set_color(canvas, ColorWhite);
  252. } else {
  253. char* move = str + cursor_pos;
  254. memmove(move + 1, move, strlen(move) + 1);
  255. str[cursor_pos] = '|';
  256. }
  257. if(cursor_pos > 0 && canvas_string_width(canvas, str) > needed_string_width) {
  258. canvas_draw_str(canvas, start_pos, 22, "...");
  259. start_pos += 6;
  260. needed_string_width -= 8;
  261. for(uint32_t off = 0;
  262. strlen(str) && canvas_string_width(canvas, str) > needed_string_width &&
  263. off < cursor_pos;
  264. off++) {
  265. str++;
  266. }
  267. }
  268. if(canvas_string_width(canvas, str) > needed_string_width) {
  269. needed_string_width -= 4;
  270. size_t len = strlen(str);
  271. while(len && canvas_string_width(canvas, str) > needed_string_width) {
  272. str[len--] = '\0';
  273. }
  274. strcat(str, "...");
  275. }
  276. canvas_draw_str(canvas, start_pos, 22, str);
  277. canvas_set_font(canvas, FontKeyboard);
  278. for(uint8_t row = 0; row < keyboard_row_count; row++) {
  279. const uint8_t column_count = get_row_size(keyboards[model->selected_keyboard], row);
  280. const NFCMaker_TextInputKey* keys = get_row(keyboards[model->selected_keyboard], row);
  281. for(size_t column = 0; column < column_count; column++) {
  282. bool selected = !model->cursor_select && model->selected_row == row &&
  283. model->selected_column == column;
  284. const Icon* icon = NULL;
  285. if(keys[column].text == ENTER_KEY) {
  286. icon = selected ? &I_KeySaveSelected_24x11 : &I_KeySave_24x11;
  287. } else if(keys[column].text == SWITCH_KEYBOARD_KEY) {
  288. icon = selected ? &I_KeyKeyboardSelected_10x11 : &I_KeyKeyboard_10x11;
  289. } else if(keys[column].text == BACKSPACE_KEY) {
  290. icon = selected ? &I_KeyBackspaceSelected_16x9 : &I_KeyBackspace_16x9;
  291. }
  292. canvas_set_color(canvas, ColorBlack);
  293. if(icon != NULL) {
  294. canvas_draw_icon(
  295. canvas,
  296. keyboard_origin_x + keys[column].x,
  297. keyboard_origin_y + keys[column].y,
  298. icon);
  299. } else {
  300. if(selected) {
  301. canvas_draw_box(
  302. canvas,
  303. keyboard_origin_x + keys[column].x - 1,
  304. keyboard_origin_y + keys[column].y - 8,
  305. 7,
  306. 10);
  307. canvas_set_color(canvas, ColorWhite);
  308. }
  309. if(model->clear_default_text || text_length == 0) {
  310. canvas_draw_glyph(
  311. canvas,
  312. keyboard_origin_x + keys[column].x,
  313. keyboard_origin_y + keys[column].y,
  314. char_to_uppercase(keys[column].text));
  315. } else {
  316. canvas_draw_glyph(
  317. canvas,
  318. keyboard_origin_x + keys[column].x,
  319. keyboard_origin_y + keys[column].y,
  320. keys[column].text);
  321. }
  322. }
  323. }
  324. }
  325. if(model->validator_message_visible) {
  326. canvas_set_font(canvas, FontSecondary);
  327. canvas_set_color(canvas, ColorWhite);
  328. canvas_draw_box(canvas, 8, 10, 110, 48);
  329. canvas_set_color(canvas, ColorBlack);
  330. canvas_draw_icon(canvas, 10, 14, &I_WarningDolphin_45x42);
  331. canvas_draw_rframe(canvas, 8, 8, 112, 50, 3);
  332. canvas_draw_rframe(canvas, 9, 9, 110, 48, 2);
  333. elements_multiline_text(canvas, 62, 20, furi_string_get_cstr(model->validator_text));
  334. canvas_set_font(canvas, FontKeyboard);
  335. }
  336. }
  337. static void nfc_maker_text_input_handle_up(
  338. NFCMaker_TextInput* nfc_maker_text_input,
  339. NFCMaker_TextInputModel* model) {
  340. UNUSED(nfc_maker_text_input);
  341. if(model->selected_row > 0) {
  342. model->selected_row--;
  343. if(model->selected_row == 0 &&
  344. model->selected_column >
  345. get_row_size(keyboards[model->selected_keyboard], model->selected_row) - 6) {
  346. model->selected_column = model->selected_column + 1;
  347. }
  348. if(model->selected_row == 1 &&
  349. model->selected_keyboard == symbol_keyboard.keyboard_index) {
  350. if(model->selected_column > 5)
  351. model->selected_column += 2;
  352. else if(model->selected_column > 1)
  353. model->selected_column += 1;
  354. }
  355. } else {
  356. model->cursor_select = true;
  357. model->clear_default_text = false;
  358. }
  359. }
  360. static void nfc_maker_text_input_handle_down(
  361. NFCMaker_TextInput* nfc_maker_text_input,
  362. NFCMaker_TextInputModel* model) {
  363. UNUSED(nfc_maker_text_input);
  364. if(model->cursor_select) {
  365. model->cursor_select = false;
  366. } else if(model->selected_row < keyboard_row_count - 1) {
  367. model->selected_row++;
  368. if(model->selected_row == 1 &&
  369. model->selected_column >
  370. get_row_size(keyboards[model->selected_keyboard], model->selected_row) - 4) {
  371. model->selected_column = model->selected_column - 1;
  372. }
  373. if(model->selected_row == 2 &&
  374. model->selected_keyboard == symbol_keyboard.keyboard_index) {
  375. if(model->selected_column > 7)
  376. model->selected_column -= 2;
  377. else if(model->selected_column > 1)
  378. model->selected_column -= 1;
  379. }
  380. }
  381. }
  382. static void nfc_maker_text_input_handle_left(
  383. NFCMaker_TextInput* nfc_maker_text_input,
  384. NFCMaker_TextInputModel* model) {
  385. UNUSED(nfc_maker_text_input);
  386. if(model->cursor_select) {
  387. if(model->cursor_pos > 0) {
  388. model->cursor_pos = CLAMP(model->cursor_pos - 1, strlen(model->text_buffer), 0u);
  389. }
  390. } else if(model->selected_column > 0) {
  391. model->selected_column--;
  392. } else {
  393. model->selected_column =
  394. get_row_size(keyboards[model->selected_keyboard], model->selected_row) - 1;
  395. }
  396. }
  397. static void nfc_maker_text_input_handle_right(
  398. NFCMaker_TextInput* nfc_maker_text_input,
  399. NFCMaker_TextInputModel* model) {
  400. UNUSED(nfc_maker_text_input);
  401. if(model->cursor_select) {
  402. model->cursor_pos = CLAMP(model->cursor_pos + 1, strlen(model->text_buffer), 0u);
  403. } else if(
  404. model->selected_column <
  405. get_row_size(keyboards[model->selected_keyboard], model->selected_row) - 1) {
  406. model->selected_column++;
  407. } else {
  408. model->selected_column = 0;
  409. }
  410. }
  411. static void nfc_maker_text_input_handle_ok(
  412. NFCMaker_TextInput* nfc_maker_text_input,
  413. NFCMaker_TextInputModel* model,
  414. InputType type) {
  415. if(model->cursor_select) return;
  416. bool shift = type == InputTypeLong;
  417. bool repeat = type == InputTypeRepeat;
  418. char selected = get_selected_char(model);
  419. size_t text_length = strlen(model->text_buffer);
  420. if(selected == ENTER_KEY) {
  421. if(model->validator_callback &&
  422. (!model->validator_callback(
  423. model->text_buffer, model->validator_text, model->validator_callback_context))) {
  424. model->validator_message_visible = true;
  425. furi_timer_start(nfc_maker_text_input->timer, furi_kernel_get_tick_frequency() * 4);
  426. } else if(model->callback != 0 && text_length >= model->minimum_length) {
  427. model->callback(model->callback_context);
  428. }
  429. } else if(selected == SWITCH_KEYBOARD_KEY) {
  430. switch_keyboard(model);
  431. } else {
  432. if(selected == BACKSPACE_KEY) {
  433. nfc_maker_text_input_backspace_cb(model);
  434. } else if(!repeat) {
  435. if(model->clear_default_text) {
  436. text_length = 0;
  437. }
  438. if(text_length < (model->text_buffer_size - 1)) {
  439. if(shift != (text_length == 0)) {
  440. selected = char_to_uppercase(selected);
  441. }
  442. if(model->clear_default_text) {
  443. model->text_buffer[0] = selected;
  444. model->text_buffer[1] = '\0';
  445. model->cursor_pos = 1;
  446. } else {
  447. char* move = model->text_buffer + model->cursor_pos;
  448. memmove(move + 1, move, strlen(move) + 1);
  449. model->text_buffer[model->cursor_pos] = selected;
  450. model->cursor_pos++;
  451. }
  452. }
  453. }
  454. model->clear_default_text = false;
  455. }
  456. }
  457. static bool nfc_maker_text_input_view_input_callback(InputEvent* event, void* context) {
  458. NFCMaker_TextInput* nfc_maker_text_input = context;
  459. furi_assert(nfc_maker_text_input);
  460. bool consumed = false;
  461. // Acquire model
  462. NFCMaker_TextInputModel* model = view_get_model(nfc_maker_text_input->view);
  463. if((!(event->type == InputTypePress) && !(event->type == InputTypeRelease)) &&
  464. model->validator_message_visible) {
  465. model->validator_message_visible = false;
  466. consumed = true;
  467. } else if(event->type == InputTypeShort) {
  468. consumed = true;
  469. switch(event->key) {
  470. case InputKeyUp:
  471. nfc_maker_text_input_handle_up(nfc_maker_text_input, model);
  472. break;
  473. case InputKeyDown:
  474. nfc_maker_text_input_handle_down(nfc_maker_text_input, model);
  475. break;
  476. case InputKeyLeft:
  477. nfc_maker_text_input_handle_left(nfc_maker_text_input, model);
  478. break;
  479. case InputKeyRight:
  480. nfc_maker_text_input_handle_right(nfc_maker_text_input, model);
  481. break;
  482. case InputKeyOk:
  483. nfc_maker_text_input_handle_ok(nfc_maker_text_input, model, event->type);
  484. break;
  485. default:
  486. consumed = false;
  487. break;
  488. }
  489. } else if(event->type == InputTypeLong) {
  490. consumed = true;
  491. switch(event->key) {
  492. case InputKeyUp:
  493. nfc_maker_text_input_handle_up(nfc_maker_text_input, model);
  494. break;
  495. case InputKeyDown:
  496. nfc_maker_text_input_handle_down(nfc_maker_text_input, model);
  497. break;
  498. case InputKeyLeft:
  499. nfc_maker_text_input_handle_left(nfc_maker_text_input, model);
  500. break;
  501. case InputKeyRight:
  502. nfc_maker_text_input_handle_right(nfc_maker_text_input, model);
  503. break;
  504. case InputKeyOk:
  505. nfc_maker_text_input_handle_ok(nfc_maker_text_input, model, event->type);
  506. break;
  507. case InputKeyBack:
  508. nfc_maker_text_input_backspace_cb(model);
  509. break;
  510. default:
  511. consumed = false;
  512. break;
  513. }
  514. } else if(event->type == InputTypeRepeat) {
  515. consumed = true;
  516. switch(event->key) {
  517. case InputKeyUp:
  518. nfc_maker_text_input_handle_up(nfc_maker_text_input, model);
  519. break;
  520. case InputKeyDown:
  521. nfc_maker_text_input_handle_down(nfc_maker_text_input, model);
  522. break;
  523. case InputKeyLeft:
  524. nfc_maker_text_input_handle_left(nfc_maker_text_input, model);
  525. break;
  526. case InputKeyRight:
  527. nfc_maker_text_input_handle_right(nfc_maker_text_input, model);
  528. break;
  529. case InputKeyOk:
  530. nfc_maker_text_input_handle_ok(nfc_maker_text_input, model, event->type);
  531. break;
  532. case InputKeyBack:
  533. nfc_maker_text_input_backspace_cb(model);
  534. break;
  535. default:
  536. consumed = false;
  537. break;
  538. }
  539. }
  540. // Commit model
  541. view_commit_model(nfc_maker_text_input->view, consumed);
  542. return consumed;
  543. }
  544. void nfc_maker_text_input_timer_callback(void* context) {
  545. furi_assert(context);
  546. NFCMaker_TextInput* nfc_maker_text_input = context;
  547. with_view_model(
  548. nfc_maker_text_input->view,
  549. NFCMaker_TextInputModel * model,
  550. { model->validator_message_visible = false; },
  551. true);
  552. }
  553. NFCMaker_TextInput* nfc_maker_text_input_alloc() {
  554. NFCMaker_TextInput* nfc_maker_text_input = malloc(sizeof(NFCMaker_TextInput));
  555. nfc_maker_text_input->view = view_alloc();
  556. view_set_context(nfc_maker_text_input->view, nfc_maker_text_input);
  557. view_allocate_model(
  558. nfc_maker_text_input->view, ViewModelTypeLocking, sizeof(NFCMaker_TextInputModel));
  559. view_set_draw_callback(nfc_maker_text_input->view, nfc_maker_text_input_view_draw_callback);
  560. view_set_input_callback(nfc_maker_text_input->view, nfc_maker_text_input_view_input_callback);
  561. nfc_maker_text_input->timer = furi_timer_alloc(
  562. nfc_maker_text_input_timer_callback, FuriTimerTypeOnce, nfc_maker_text_input);
  563. with_view_model(
  564. nfc_maker_text_input->view,
  565. NFCMaker_TextInputModel * model,
  566. {
  567. model->validator_text = furi_string_alloc();
  568. model->minimum_length = 1;
  569. model->cursor_pos = 0;
  570. model->cursor_select = false;
  571. },
  572. false);
  573. nfc_maker_text_input_reset(nfc_maker_text_input);
  574. return nfc_maker_text_input;
  575. }
  576. void nfc_maker_text_input_free(NFCMaker_TextInput* nfc_maker_text_input) {
  577. furi_assert(nfc_maker_text_input);
  578. with_view_model(
  579. nfc_maker_text_input->view,
  580. NFCMaker_TextInputModel * model,
  581. { furi_string_free(model->validator_text); },
  582. false);
  583. // Send stop command
  584. furi_timer_stop(nfc_maker_text_input->timer);
  585. // Release allocated memory
  586. furi_timer_free(nfc_maker_text_input->timer);
  587. view_free(nfc_maker_text_input->view);
  588. free(nfc_maker_text_input);
  589. }
  590. void nfc_maker_text_input_reset(NFCMaker_TextInput* nfc_maker_text_input) {
  591. furi_assert(nfc_maker_text_input);
  592. with_view_model(
  593. nfc_maker_text_input->view,
  594. NFCMaker_TextInputModel * model,
  595. {
  596. model->header = "";
  597. model->selected_row = 0;
  598. model->selected_column = 0;
  599. model->selected_keyboard = 0;
  600. model->minimum_length = 1;
  601. model->clear_default_text = false;
  602. model->cursor_pos = 0;
  603. model->cursor_select = false;
  604. model->text_buffer = NULL;
  605. model->text_buffer_size = 0;
  606. model->callback = NULL;
  607. model->callback_context = NULL;
  608. model->validator_callback = NULL;
  609. model->validator_callback_context = NULL;
  610. furi_string_reset(model->validator_text);
  611. model->validator_message_visible = false;
  612. },
  613. true);
  614. }
  615. View* nfc_maker_text_input_get_view(NFCMaker_TextInput* nfc_maker_text_input) {
  616. furi_assert(nfc_maker_text_input);
  617. return nfc_maker_text_input->view;
  618. }
  619. void nfc_maker_text_input_set_result_callback(
  620. NFCMaker_TextInput* nfc_maker_text_input,
  621. NFCMaker_TextInputCallback callback,
  622. void* callback_context,
  623. char* text_buffer,
  624. size_t text_buffer_size,
  625. bool clear_default_text) {
  626. with_view_model(
  627. nfc_maker_text_input->view,
  628. NFCMaker_TextInputModel * model,
  629. {
  630. model->callback = callback;
  631. model->callback_context = callback_context;
  632. model->text_buffer = text_buffer;
  633. model->text_buffer_size = text_buffer_size;
  634. model->clear_default_text = clear_default_text;
  635. model->cursor_select = false;
  636. if(text_buffer && text_buffer[0] != '\0') {
  637. model->cursor_pos = strlen(text_buffer);
  638. // Set focus on Save
  639. model->selected_row = 2;
  640. model->selected_column = 9;
  641. model->selected_keyboard = 0;
  642. } else {
  643. model->cursor_pos = 0;
  644. }
  645. },
  646. true);
  647. }
  648. void nfc_maker_text_input_set_minimum_length(
  649. NFCMaker_TextInput* nfc_maker_text_input,
  650. size_t minimum_length) {
  651. with_view_model(
  652. nfc_maker_text_input->view,
  653. NFCMaker_TextInputModel * model,
  654. { model->minimum_length = minimum_length; },
  655. true);
  656. }
  657. void nfc_maker_text_input_set_validator(
  658. NFCMaker_TextInput* nfc_maker_text_input,
  659. NFCMaker_TextInputValidatorCallback callback,
  660. void* callback_context) {
  661. with_view_model(
  662. nfc_maker_text_input->view,
  663. NFCMaker_TextInputModel * model,
  664. {
  665. model->validator_callback = callback;
  666. model->validator_callback_context = callback_context;
  667. },
  668. true);
  669. }
  670. NFCMaker_TextInputValidatorCallback
  671. nfc_maker_text_input_get_validator_callback(NFCMaker_TextInput* nfc_maker_text_input) {
  672. NFCMaker_TextInputValidatorCallback validator_callback = NULL;
  673. with_view_model(
  674. nfc_maker_text_input->view,
  675. NFCMaker_TextInputModel * model,
  676. { validator_callback = model->validator_callback; },
  677. false);
  678. return validator_callback;
  679. }
  680. void* nfc_maker_text_input_get_validator_callback_context(
  681. NFCMaker_TextInput* nfc_maker_text_input) {
  682. void* validator_callback_context = NULL;
  683. with_view_model(
  684. nfc_maker_text_input->view,
  685. NFCMaker_TextInputModel * model,
  686. { validator_callback_context = model->validator_callback_context; },
  687. false);
  688. return validator_callback_context;
  689. }
  690. void nfc_maker_text_input_set_header_text(
  691. NFCMaker_TextInput* nfc_maker_text_input,
  692. const char* text) {
  693. with_view_model(
  694. nfc_maker_text_input->view,
  695. NFCMaker_TextInputModel * model,
  696. { model->header = text; },
  697. true);
  698. }