shapshup.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <gui/view_dispatcher.h>
  4. #include <gui/view_stack.h>
  5. #include <gui/modules/text_input.h>
  6. #include <gui/modules/popup.h>
  7. #include <gui/modules/widget.h>
  8. #include <gui/modules/loading.h>
  9. #include <dialogs/dialogs.h>
  10. #include "shapshup_i.h"
  11. #include "shapshup_custom_event.h"
  12. #define TAG "ShapShupApp"
  13. #define TICK_PERIOD 500
  14. static bool shapshup_custom_event_callback(void* context, uint32_t event) {
  15. furi_assert(context);
  16. ShapShupState* instance = context;
  17. return scene_manager_handle_custom_event(instance->scene_manager, event);
  18. }
  19. static bool shapshup_back_event_callback(void* context) {
  20. furi_assert(context);
  21. ShapShupState* instance = context;
  22. return scene_manager_handle_back_event(instance->scene_manager);
  23. }
  24. static void shapshup_tick_event_callback(void* context) {
  25. furi_assert(context);
  26. ShapShupState* instance = context;
  27. scene_manager_handle_tick_event(instance->scene_manager);
  28. }
  29. ShapShupState* shapshup_alloc() {
  30. ShapShupState* instance = malloc(sizeof(ShapShupState));
  31. instance->file_path = furi_string_alloc();
  32. instance->scene_manager = scene_manager_alloc(&shapshup_scene_handlers, instance);
  33. instance->view_dispatcher = view_dispatcher_alloc();
  34. instance->gui = furi_record_open(RECORD_GUI);
  35. instance->storage = furi_record_open(RECORD_STORAGE);
  36. view_dispatcher_enable_queue(instance->view_dispatcher);
  37. view_dispatcher_set_event_callback_context(instance->view_dispatcher, instance);
  38. view_dispatcher_set_custom_event_callback(
  39. instance->view_dispatcher, shapshup_custom_event_callback);
  40. view_dispatcher_set_navigation_event_callback(
  41. instance->view_dispatcher, shapshup_back_event_callback);
  42. view_dispatcher_set_tick_event_callback(
  43. instance->view_dispatcher, shapshup_tick_event_callback, TICK_PERIOD);
  44. //Dialog
  45. instance->dialogs = furi_record_open(RECORD_DIALOGS);
  46. // Notifications
  47. instance->notifications = furi_record_open(RECORD_NOTIFICATION);
  48. // TextInput
  49. instance->text_input = text_input_alloc();
  50. view_dispatcher_add_view(
  51. instance->view_dispatcher,
  52. ShapShupViewTextInput,
  53. text_input_get_view(instance->text_input));
  54. // Custom Widget
  55. instance->widget = widget_alloc();
  56. view_dispatcher_add_view(
  57. instance->view_dispatcher, ShapShupViewWidget, widget_get_view(instance->widget));
  58. // Popup
  59. instance->popup = popup_alloc();
  60. view_dispatcher_add_view(
  61. instance->view_dispatcher, ShapShupViewPopup, popup_get_view(instance->popup));
  62. // Loading
  63. instance->loading = loading_alloc();
  64. view_dispatcher_add_view(
  65. instance->view_dispatcher, ShapShupViewLoading, loading_get_view(instance->loading));
  66. // ViewStack
  67. instance->view_stack = view_stack_alloc();
  68. view_dispatcher_add_view(
  69. instance->view_dispatcher, ShapShupViewStack, view_stack_get_view(instance->view_stack));
  70. // ShapShupMainView
  71. instance->view_main = shapshup_main_view_alloc();
  72. view_dispatcher_add_view(
  73. instance->view_dispatcher,
  74. ShapShupViewMain,
  75. shapshup_main_view_get_view(instance->view_main));
  76. // Loading
  77. instance->loading = loading_alloc();
  78. return instance;
  79. }
  80. void shapshup_free(ShapShupState* instance) {
  81. furi_assert(instance);
  82. // Notifications
  83. notification_message(instance->notifications, &sequence_blink_stop);
  84. furi_record_close(RECORD_NOTIFICATION);
  85. instance->notifications = NULL;
  86. // Loading
  87. // Wtf is going on here, 2 times free?
  88. //loading_free(instance->loading);
  89. // View Main
  90. view_dispatcher_remove_view(instance->view_dispatcher, ShapShupViewMain);
  91. shapshup_main_view_free(instance->view_main);
  92. // TextInput
  93. view_dispatcher_remove_view(instance->view_dispatcher, ShapShupViewTextInput);
  94. text_input_free(instance->text_input);
  95. // Custom Widget
  96. view_dispatcher_remove_view(instance->view_dispatcher, ShapShupViewWidget);
  97. widget_free(instance->widget);
  98. // Popup
  99. view_dispatcher_remove_view(instance->view_dispatcher, ShapShupViewPopup);
  100. popup_free(instance->popup);
  101. // Loading
  102. view_dispatcher_remove_view(instance->view_dispatcher, ShapShupViewLoading);
  103. loading_free(instance->loading);
  104. // ViewStack
  105. view_dispatcher_remove_view(instance->view_dispatcher, ShapShupViewStack);
  106. view_stack_free(instance->view_stack);
  107. //Dialog
  108. furi_record_close(RECORD_DIALOGS);
  109. instance->dialogs = NULL;
  110. // Scene manager
  111. scene_manager_free(instance->scene_manager);
  112. // View Dispatcher
  113. view_dispatcher_free(instance->view_dispatcher);
  114. // GUI
  115. furi_record_close(RECORD_GUI);
  116. instance->gui = NULL;
  117. // Storage
  118. furi_record_close(RECORD_STORAGE);
  119. instance->storage = NULL;
  120. furi_string_free(instance->file_path);
  121. // The rest
  122. free(instance);
  123. }
  124. void shapshup_show_loading_popup(void* context, bool show) {
  125. furi_assert(context);
  126. ShapShupState* instance = context;
  127. if(show) {
  128. // Raise timer priority so that animations can play
  129. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  130. view_dispatcher_switch_to_view(instance->view_dispatcher, ShapShupViewLoading);
  131. } else {
  132. // Restore default timer priority
  133. furi_timer_set_thread_priority(FuriTimerThreadPriorityNormal);
  134. }
  135. }
  136. void shapshup_text_input_callback(void* context) {
  137. furi_assert(context);
  138. ShapShupState* instance = context;
  139. view_dispatcher_send_custom_event(
  140. instance->view_dispatcher, ShapShupCustomEventTypeTextEditDone);
  141. }
  142. void shapshup_popup_closed_callback(void* context) {
  143. furi_assert(context);
  144. ShapShupState* instance = context;
  145. view_dispatcher_send_custom_event(
  146. instance->view_dispatcher, ShapShupCustomEventTypePopupClosed);
  147. }
  148. /**
  149. * @brief Entrypoint
  150. *
  151. * @param p
  152. * @return int32_t
  153. */
  154. int32_t shapshup_app(void* p) {
  155. UNUSED(p);
  156. ShapShupState* instance = shapshup_alloc();
  157. view_dispatcher_attach_to_gui(
  158. instance->view_dispatcher, instance->gui, ViewDispatcherTypeFullscreen);
  159. scene_manager_next_scene(instance->scene_manager, ShapshupSceneStart);
  160. notification_message(instance->notifications, &sequence_display_backlight_on);
  161. view_dispatcher_run(instance->view_dispatcher);
  162. shapshup_free(instance);
  163. return 0;
  164. }