infrared_progress_view.c 3.8 KB

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