irda_progress_view.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "irda_progress_view.h"
  11. #include "gui/modules/button_panel.h"
  12. #include <stdint.h>
  13. struct IrdaProgressView {
  14. View* view;
  15. IrdaProgressViewBackCallback back_callback;
  16. void* context;
  17. };
  18. typedef struct {
  19. size_t progress;
  20. size_t progress_total;
  21. } IrdaProgressViewModel;
  22. bool irda_progress_view_increase_progress(IrdaProgressView* progress) {
  23. furi_assert(progress);
  24. bool result = false;
  25. IrdaProgressViewModel* 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 irda_progress_view_draw_callback(Canvas* canvas, void* _model) {
  34. IrdaProgressViewModel* model = (IrdaProgressViewModel*)_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 + 11, y + height - 15, &I_Back_15x10);
  51. canvas_draw_str(canvas, x + 30, y + height - 6, "= stop");
  52. }
  53. void irda_progress_view_set_progress_total(IrdaProgressView* progress, uint16_t progress_total) {
  54. furi_assert(progress);
  55. IrdaProgressViewModel* model = view_get_model(progress->view);
  56. model->progress = 0;
  57. model->progress_total = progress_total;
  58. view_commit_model(progress->view, false);
  59. }
  60. bool irda_progress_view_input_callback(InputEvent* event, void* context) {
  61. IrdaProgressView* instance = context;
  62. if((event->type == InputTypeShort) && (event->key == InputKeyBack)) {
  63. if(instance->back_callback) {
  64. instance->back_callback(instance->context);
  65. }
  66. }
  67. return true;
  68. }
  69. IrdaProgressView* irda_progress_view_alloc(void) {
  70. IrdaProgressView* instance = malloc(sizeof(IrdaProgressView));
  71. instance->view = view_alloc();
  72. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(IrdaProgressViewModel));
  73. IrdaProgressViewModel* model = view_get_model(instance->view);
  74. model->progress = 0;
  75. model->progress_total = 0;
  76. view_commit_model(instance->view, false);
  77. view_set_draw_callback(instance->view, irda_progress_view_draw_callback);
  78. view_set_input_callback(instance->view, irda_progress_view_input_callback);
  79. view_set_context(instance->view, instance);
  80. return instance;
  81. }
  82. void irda_progress_view_free(IrdaProgressView* progress) {
  83. view_free(progress->view);
  84. free(progress);
  85. }
  86. void irda_progress_view_set_back_callback(
  87. IrdaProgressView* instance,
  88. IrdaProgressViewBackCallback callback,
  89. void* context) {
  90. furi_assert(instance);
  91. instance->back_callback = callback;
  92. instance->context = context;
  93. }
  94. View* irda_progress_view_get_view(IrdaProgressView* instance) {
  95. furi_assert(instance);
  96. furi_assert(instance->view);
  97. return instance->view;
  98. }