shapshup.c 6.2 KB

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