flipp_pomodoro_app.c 4.2 KB

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