pokemon_ot_id.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <ctype.h>
  2. #include <furi.h>
  3. #include <gui/modules/text_input.h>
  4. #include <gui/view_dispatcher.h>
  5. #include <stdlib.h>
  6. #include "../pokemon_app.h"
  7. #include "pokemon_menu.h"
  8. static char ot_id_buf[6];
  9. static bool select_ot_id_input_validator(const char* text, FuriString* error, void* context) {
  10. PokemonFap* pokemon_fap = (PokemonFap*)context;
  11. int ot_id;
  12. uint16_t ot_id_16;
  13. bool rc = true;
  14. unsigned int i;
  15. /* Need to check each byte to ensure is not alpha. atoi returns 0 which is
  16. * technically a valid ID, so we need to separately check for alpha chars.
  17. */
  18. for(i = 0; i < sizeof(ot_id_buf); i++) {
  19. if(!isdigit((unsigned int)text[i])) {
  20. if(text[i] == '\0') break;
  21. rc = false;
  22. break;
  23. }
  24. }
  25. ot_id = atoi(text);
  26. if(ot_id < 0 || ot_id > 65535 || rc == false) {
  27. furi_string_printf(error, "OT ID must\nbe between\n0-65535!");
  28. rc = false;
  29. } else {
  30. ot_id_16 = __builtin_bswap16((uint16_t)ot_id);
  31. pokemon_fap->trade_block->party[0].ot_id = ot_id_16;
  32. }
  33. return rc;
  34. }
  35. static void select_ot_id_input_callback(void* context) {
  36. PokemonFap* pokemon_fap = (PokemonFap*)context;
  37. scene_manager_previous_scene(pokemon_fap->scene_manager);
  38. }
  39. void select_ot_id_scene_on_exit(void* context) {
  40. PokemonFap* pokemon_fap = (PokemonFap*)context;
  41. view_dispatcher_switch_to_view(pokemon_fap->view_dispatcher, AppViewMainMenu);
  42. view_dispatcher_remove_view(pokemon_fap->view_dispatcher, AppViewOpts);
  43. }
  44. void select_ot_id_scene_on_enter(void* context) {
  45. PokemonFap* pokemon_fap = (PokemonFap*)context;
  46. text_input_reset(pokemon_fap->text_input);
  47. text_input_set_validator(pokemon_fap->text_input, select_ot_id_input_validator, pokemon_fap);
  48. text_input_set_result_callback(
  49. pokemon_fap->text_input,
  50. select_ot_id_input_callback,
  51. pokemon_fap,
  52. ot_id_buf,
  53. sizeof(ot_id_buf),
  54. true);
  55. text_input_set_header_text(pokemon_fap->text_input, "Enter OT ID (numbers only):");
  56. view_dispatcher_add_view(
  57. pokemon_fap->view_dispatcher, AppViewOpts, text_input_get_view(pokemon_fap->text_input));
  58. view_dispatcher_switch_to_view(pokemon_fap->view_dispatcher, AppViewOpts);
  59. }