flipp_pomodoro_info_view.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <gui/elements.h>
  4. #include <gui/view.h>
  5. #include "flipp_pomodoro_info_view.h"
  6. // Auto-compiled icons
  7. #include "flipp_pomodoro_icons.h"
  8. enum
  9. {
  10. ViewInputConsumed = true,
  11. ViewInputNotConusmed = false,
  12. };
  13. struct FlippPomodoroInfoView
  14. {
  15. View *view;
  16. FlippPomodoroInfoViewUserActionCb resume_timer_cb;
  17. void *user_action_cb_ctx;
  18. };
  19. typedef struct
  20. {
  21. uint8_t pomodoros_completed;
  22. FlippPomodoroInfoViewMode mode;
  23. } FlippPomodoroInfoViewModel;
  24. static void flipp_pomodoro_info_view_draw_statistics(Canvas *canvas, FlippPomodoroInfoViewModel *model)
  25. {
  26. FuriString *stats_string = furi_string_alloc();
  27. furi_string_printf(stats_string, "So Long,\nand Thanks for All the Focus...\nand for completing\n\e#%i\e# pomodoro(s)", model->pomodoros_completed);
  28. const char *stats_string_formatted = furi_string_get_cstr(stats_string);
  29. elements_text_box(
  30. canvas,
  31. 0,
  32. 0,
  33. canvas_width(canvas),
  34. canvas_height(canvas) - 10,
  35. AlignCenter,
  36. AlignCenter,
  37. stats_string_formatted,
  38. true);
  39. furi_string_free(stats_string);
  40. elements_button_left(canvas, "Guide");
  41. }
  42. static void flipp_pomodoro_info_view_draw_about(Canvas *canvas, FlippPomodoroInfoViewModel *model)
  43. {
  44. UNUSED(model);
  45. canvas_draw_icon(canvas, 0, 0, &I_flipp_pomodoro_learn_50x128);
  46. elements_button_left(canvas, "Stats");
  47. }
  48. static void flipp_pomodoro_info_view_draw_callback(Canvas *canvas, void *_model)
  49. {
  50. if (!_model)
  51. {
  52. return;
  53. };
  54. FlippPomodoroInfoViewModel *model = _model;
  55. canvas_clear(canvas);
  56. if (model->mode == FlippPomodoroInfoViewModeStats)
  57. {
  58. flipp_pomodoro_info_view_draw_statistics(canvas, model);
  59. }
  60. else
  61. {
  62. flipp_pomodoro_info_view_draw_about(canvas, model);
  63. }
  64. elements_button_right(canvas, "Resume");
  65. }
  66. void flipp_pomodoro_info_view_set_mode(View *view, FlippPomodoroInfoViewMode desired_mode)
  67. {
  68. with_view_model(
  69. view,
  70. FlippPomodoroInfoViewModel * model,
  71. {
  72. model->mode = desired_mode;
  73. },
  74. false);
  75. }
  76. void flipp_pomodoro_info_view_toggle_mode(FlippPomodoroInfoView *info_view)
  77. {
  78. with_view_model(
  79. flipp_pomodoro_info_view_get_view(info_view),
  80. FlippPomodoroInfoViewModel * model,
  81. {
  82. flipp_pomodoro_info_view_set_mode(
  83. flipp_pomodoro_info_view_get_view(info_view),
  84. (model->mode == FlippPomodoroInfoViewModeStats) ? FlippPomodoroInfoViewModeAbout : FlippPomodoroInfoViewModeStats);
  85. },
  86. true);
  87. }
  88. bool flipp_pomodoro_info_view_input_callback(InputEvent *event, void *ctx)
  89. {
  90. FlippPomodoroInfoView *info_view = ctx;
  91. if (event->type == InputTypePress)
  92. {
  93. if (event->key == InputKeyRight && info_view->resume_timer_cb != NULL)
  94. {
  95. info_view->resume_timer_cb(info_view->user_action_cb_ctx);
  96. return ViewInputConsumed;
  97. }
  98. else if (event->key == InputKeyLeft)
  99. {
  100. flipp_pomodoro_info_view_toggle_mode(info_view);
  101. return ViewInputConsumed;
  102. }
  103. }
  104. return ViewInputNotConusmed;
  105. }
  106. FlippPomodoroInfoView *flipp_pomodoro_info_view_alloc()
  107. {
  108. FlippPomodoroInfoView *info_view = malloc(sizeof(FlippPomodoroInfoView));
  109. info_view->view = view_alloc();
  110. view_allocate_model(flipp_pomodoro_info_view_get_view(info_view), ViewModelTypeLockFree, sizeof(FlippPomodoroInfoViewModel));
  111. view_set_context(flipp_pomodoro_info_view_get_view(info_view), info_view);
  112. view_set_draw_callback(flipp_pomodoro_info_view_get_view(info_view), flipp_pomodoro_info_view_draw_callback);
  113. view_set_input_callback(flipp_pomodoro_info_view_get_view(info_view), flipp_pomodoro_info_view_input_callback);
  114. return info_view;
  115. }
  116. View *flipp_pomodoro_info_view_get_view(FlippPomodoroInfoView *info_view)
  117. {
  118. return info_view->view;
  119. }
  120. void flipp_pomodoro_info_view_free(FlippPomodoroInfoView *info_view)
  121. {
  122. furi_assert(info_view);
  123. view_free(info_view->view);
  124. free(info_view);
  125. }
  126. void flipp_pomodoro_info_view_set_pomodoros_completed(View *view, uint8_t pomodoros_completed)
  127. {
  128. with_view_model(
  129. view,
  130. FlippPomodoroInfoViewModel * model,
  131. {
  132. model->pomodoros_completed = pomodoros_completed;
  133. },
  134. false);
  135. }
  136. void flipp_pomodoro_info_view_set_resume_timer_cb(FlippPomodoroInfoView *info_view, FlippPomodoroInfoViewUserActionCb user_action_cb, void *user_action_cb_ctx)
  137. {
  138. info_view->resume_timer_cb = user_action_cb;
  139. info_view->user_action_cb_ctx = user_action_cb_ctx;
  140. }