flipp_pomodoro_app.c 4.3 KB

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