ibutton_scene_add_value.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "../ibutton_i.h"
  2. static void ibutton_scene_add_type_byte_input_callback(void* context) {
  3. iButton* ibutton = context;
  4. view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventByteEditResult);
  5. }
  6. static void ibutton_scene_add_type_byte_changed_callback(void* context) {
  7. iButton* ibutton = context;
  8. view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventByteEditChanged);
  9. }
  10. void ibutton_scene_add_value_on_enter(void* context) {
  11. iButton* ibutton = context;
  12. byte_input_set_header_text(ibutton->byte_input, "Enter the key");
  13. iButtonEditableData editable_data;
  14. ibutton_protocols_get_editable_data(ibutton->protocols, ibutton->key, &editable_data);
  15. byte_input_set_result_callback(
  16. ibutton->byte_input,
  17. ibutton_scene_add_type_byte_input_callback,
  18. ibutton_scene_add_type_byte_changed_callback,
  19. context,
  20. editable_data.ptr,
  21. editable_data.size);
  22. view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewByteInput);
  23. }
  24. bool ibutton_scene_add_value_on_event(void* context, SceneManagerEvent event) {
  25. iButton* ibutton = context;
  26. SceneManager* scene_manager = ibutton->scene_manager;
  27. bool consumed = false;
  28. if(event.type == SceneManagerEventTypeCustom) {
  29. consumed = true;
  30. if(event.event == iButtonCustomEventByteEditResult) {
  31. scene_manager_next_scene(scene_manager, iButtonSceneSaveName);
  32. } else if(event.event == iButtonCustomEventByteEditChanged) {
  33. ibutton_protocols_apply_edits(ibutton->protocols, ibutton->key);
  34. }
  35. } else if(event.type == SceneManagerEventTypeBack) {
  36. // User cancelled editing, reload the key from storage
  37. if(scene_manager_has_previous_scene(scene_manager, iButtonSceneSavedKeyMenu)) {
  38. if(!ibutton_load_key(ibutton)) {
  39. consumed = scene_manager_search_and_switch_to_previous_scene(
  40. scene_manager, iButtonSceneStart);
  41. }
  42. }
  43. }
  44. return consumed;
  45. }
  46. void ibutton_scene_add_value_on_exit(void* context) {
  47. iButton* ibutton = context;
  48. byte_input_set_result_callback(ibutton->byte_input, NULL, NULL, NULL, NULL, 0);
  49. byte_input_set_header_text(ibutton->byte_input, NULL);
  50. }