flipp_pomodoro_app.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "flipp_pomodoro_app_i.h"
  2. enum {
  3. CustomEventConsumed = true,
  4. CustomEventNotConsumed = false,
  5. };
  6. static bool flipp_pomodoro_app_back_event_callback(void* ctx) {
  7. furi_assert(ctx);
  8. FlippPomodoroApp* app = ctx;
  9. return scene_manager_handle_back_event(app->scene_manager);
  10. };
  11. static void flipp_pomodoro_app_tick_event_callback(void* ctx) {
  12. furi_assert(ctx);
  13. FlippPomodoroApp* app = ctx;
  14. scene_manager_handle_custom_event(app->scene_manager, FlippPomodoroAppCustomEventTimerTick);
  15. };
  16. static bool flipp_pomodoro_app_custom_event_callback(void* ctx, uint32_t event) {
  17. furi_assert(ctx);
  18. FlippPomodoroApp* app = ctx;
  19. switch(event) {
  20. case FlippPomodoroAppCustomEventStageSkip:
  21. flipp_pomodoro__toggle_stage(app->state);
  22. view_dispatcher_send_custom_event(
  23. app->view_dispatcher, FlippPomodoroAppCustomEventStateUpdated);
  24. return CustomEventConsumed;
  25. case FlippPomodoroAppCustomEventStageComplete:
  26. if(flipp_pomodoro__get_stage(app->state) == FlippPomodoroStageFocus) {
  27. // REGISTER a deed on work stage complete to get an acheivement
  28. dolphin_deed(DolphinDeedPluginGameWin);
  29. };
  30. flipp_pomodoro__toggle_stage(app->state);
  31. notification_message(
  32. app->notification_app,
  33. stage_start_notification_sequence_map[flipp_pomodoro__get_stage(app->state)]);
  34. view_dispatcher_send_custom_event(
  35. app->view_dispatcher, FlippPomodoroAppCustomEventStateUpdated);
  36. return CustomEventConsumed;
  37. default:
  38. break;
  39. }
  40. return scene_manager_handle_custom_event(app->scene_manager, event);
  41. };
  42. FlippPomodoroApp* flipp_pomodoro_app_alloc() {
  43. FlippPomodoroApp* app = malloc(sizeof(FlippPomodoroApp));
  44. app->state = flipp_pomodoro__new();
  45. app->scene_manager = scene_manager_alloc(&flipp_pomodoro_scene_handlers, app);
  46. app->gui = furi_record_open(RECORD_GUI);
  47. app->notification_app = furi_record_open(RECORD_NOTIFICATION);
  48. app->view_dispatcher = view_dispatcher_alloc();
  49. view_dispatcher_enable_queue(app->view_dispatcher);
  50. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  51. view_dispatcher_set_custom_event_callback(
  52. app->view_dispatcher, flipp_pomodoro_app_custom_event_callback);
  53. view_dispatcher_set_tick_event_callback(
  54. app->view_dispatcher, flipp_pomodoro_app_tick_event_callback, 1000);
  55. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  56. view_dispatcher_set_navigation_event_callback(
  57. app->view_dispatcher, flipp_pomodoro_app_back_event_callback);
  58. app->timer_view = flipp_pomodoro_view_timer_alloc();
  59. view_dispatcher_add_view(
  60. app->view_dispatcher,
  61. FlippPomodoroAppViewTimer,
  62. flipp_pomodoro_view_timer_get_view(app->timer_view));
  63. scene_manager_next_scene(app->scene_manager, FlippPomodoroSceneTimer);
  64. return app;
  65. };
  66. void flipp_pomodoro_app_free(FlippPomodoroApp* app) {
  67. view_dispatcher_remove_view(app->view_dispatcher, FlippPomodoroAppViewTimer);
  68. view_dispatcher_free(app->view_dispatcher);
  69. scene_manager_free(app->scene_manager);
  70. flipp_pomodoro_view_timer_free(app->timer_view);
  71. free(app);
  72. furi_record_close(RECORD_GUI);
  73. furi_record_close(RECORD_NOTIFICATION);
  74. };
  75. int32_t flipp_pomodoro_app(void* p) {
  76. UNUSED(p);
  77. FlippPomodoroApp* app = flipp_pomodoro_app_alloc();
  78. view_dispatcher_run(app->view_dispatcher);
  79. flipp_pomodoro_app_free(app);
  80. return 0;
  81. };