infrared_progress_view.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "furi/check.h"
  2. #include "furi_hal_resources.h"
  3. #include "assets_icons.h"
  4. #include "gui/canvas.h"
  5. #include "gui/view.h"
  6. #include "input/input.h"
  7. #include "m-string.h"
  8. #include <gui/elements.h>
  9. #include <furi.h>
  10. #include "infrared_progress_view.h"
  11. #include "gui/modules/button_panel.h"
  12. #include <stdint.h>
  13. struct InfraredProgressView {
  14. View* view;
  15. InfraredProgressViewBackCallback back_callback;
  16. void* context;
  17. };
  18. typedef struct {
  19. size_t progress;
  20. size_t progress_total;
  21. } InfraredProgressViewModel;
  22. bool infrared_progress_view_increase_progress(InfraredProgressView* progress) {
  23. furi_assert(progress);
  24. bool result = false;
  25. InfraredProgressViewModel* model = view_get_model(progress->view);
  26. if(model->progress < model->progress_total) {
  27. ++model->progress;
  28. result = model->progress < model->progress_total;
  29. }
  30. view_commit_model(progress->view, true);
  31. return result;
  32. }
  33. static void infrared_progress_view_draw_callback(Canvas* canvas, void* _model) {
  34. InfraredProgressViewModel* model = (InfraredProgressViewModel*)_model;
  35. uint8_t x = 0;
  36. uint8_t y = 36;
  37. uint8_t width = 63;
  38. uint8_t height = 59;
  39. elements_bold_rounded_frame(canvas, x, y, width, height);
  40. canvas_set_font(canvas, FontSecondary);
  41. elements_multiline_text_aligned(
  42. canvas, x + 34, y + 9, AlignCenter, AlignCenter, "Sending ...");
  43. float progress_value = (float)model->progress / model->progress_total;
  44. elements_progress_bar(canvas, x + 4, y + 19, width - 7, progress_value);
  45. uint8_t percent_value = 100 * model->progress / model->progress_total;
  46. char percents_string[10] = {0};
  47. snprintf(percents_string, sizeof(percents_string), "%d%%", percent_value);
  48. elements_multiline_text_aligned(
  49. canvas, x + 33, y + 37, AlignCenter, AlignCenter, percents_string);
  50. canvas_draw_icon(canvas, x + 14, y + height - 14, &I_Pin_back_arrow_10x8);
  51. canvas_draw_str(canvas, x + 30, y + height - 6, "= stop");
  52. }
  53. void infrared_progress_view_set_progress_total(
  54. InfraredProgressView* progress,
  55. uint16_t progress_total) {
  56. furi_assert(progress);
  57. InfraredProgressViewModel* model = view_get_model(progress->view);
  58. model->progress = 0;
  59. model->progress_total = progress_total;
  60. view_commit_model(progress->view, false);
  61. }
  62. bool infrared_progress_view_input_callback(InputEvent* event, void* context) {
  63. InfraredProgressView* instance = context;
  64. if((event->type == InputTypeShort) && (event->key == InputKeyBack)) {
  65. if(instance->back_callback) {
  66. instance->back_callback(instance->context);
  67. }
  68. }
  69. return true;
  70. }
  71. InfraredProgressView* infrared_progress_view_alloc(void) {
  72. InfraredProgressView* instance = malloc(sizeof(InfraredProgressView));
  73. instance->view = view_alloc();
  74. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(InfraredProgressViewModel));
  75. InfraredProgressViewModel* model = view_get_model(instance->view);
  76. model->progress = 0;
  77. model->progress_total = 0;
  78. view_commit_model(instance->view, false);
  79. view_set_draw_callback(instance->view, infrared_progress_view_draw_callback);
  80. view_set_input_callback(instance->view, infrared_progress_view_input_callback);
  81. view_set_context(instance->view, instance);
  82. return instance;
  83. }
  84. void infrared_progress_view_free(InfraredProgressView* progress) {
  85. view_free(progress->view);
  86. free(progress);
  87. }
  88. void infrared_progress_view_set_back_callback(
  89. InfraredProgressView* instance,
  90. InfraredProgressViewBackCallback callback,
  91. void* context) {
  92. furi_assert(instance);
  93. instance->back_callback = callback;
  94. instance->context = context;
  95. }
  96. View* infrared_progress_view_get_view(InfraredProgressView* instance) {
  97. furi_assert(instance);
  98. furi_assert(instance->view);
  99. return instance->view;
  100. }