pokemon_ot_id.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. FURI_LOG_D(TAG, "[ot_id] Set OT ID to %05d", (uint16_t)ot_id);
  34. return rc;
  35. }
  36. static void select_ot_id_input_callback(void* context) {
  37. PokemonFap* pokemon_fap = (PokemonFap*)context;
  38. scene_manager_previous_scene(pokemon_fap->scene_manager);
  39. }
  40. void select_ot_id_scene_on_exit(void* context) {
  41. PokemonFap* pokemon_fap = (PokemonFap*)context;
  42. view_dispatcher_switch_to_view(pokemon_fap->view_dispatcher, AppViewMainMenu);
  43. view_dispatcher_remove_view(pokemon_fap->view_dispatcher, AppViewOpts);
  44. }
  45. void select_ot_id_scene_on_enter(void* context) {
  46. PokemonFap* pokemon_fap = (PokemonFap*)context;
  47. text_input_reset(pokemon_fap->text_input);
  48. text_input_set_validator(pokemon_fap->text_input, select_ot_id_input_validator, pokemon_fap);
  49. text_input_set_result_callback(
  50. pokemon_fap->text_input,
  51. select_ot_id_input_callback,
  52. pokemon_fap,
  53. ot_id_buf,
  54. sizeof(ot_id_buf),
  55. true);
  56. text_input_set_header_text(pokemon_fap->text_input, "Enter OT ID (numbers only):");
  57. view_dispatcher_add_view(
  58. pokemon_fap->view_dispatcher, AppViewOpts, text_input_get_view(pokemon_fap->text_input));
  59. view_dispatcher_switch_to_view(pokemon_fap->view_dispatcher, AppViewOpts);
  60. }