flipp_pomodoro_app.c 3.5 KB

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