Просмотр исходного кода

Merge pull request #2 from kala13x/settings

Finished settings implementation
Sandro Kalatozishvili 2 лет назад
Родитель
Сommit
7af67670d9
8 измененных файлов с 134 добавлено и 61 удалено
  1. 3 2
      README.md
  2. 0 24
      views/xremote_settings_view.c
  3. 0 13
      views/xremote_settings_view.h
  4. 0 1
      xremote.c
  5. 1 1
      xremote.h
  6. 9 17
      xremote_app.c
  7. 1 0
      xremote_app.h
  8. 120 3
      xremote_settings.c

+ 3 - 2
README.md

@@ -53,12 +53,13 @@ Button name | Description
     - [ ] Full button list
     - [ ] Full button list
     - [ ] Edit remote file
     - [ ] Edit remote file
     - [ ] Delete remote file
     - [ ] Delete remote file
-- [ ] Application settings
+- [x] Application settings
     - [x] Load settings from the file
     - [x] Load settings from the file
     - [x] Store settings to the file
     - [x] Store settings to the file
     - [x] Vertical/Horizontal menu
     - [x] Vertical/Horizontal menu
     - [x] IR command repeat count
     - [x] IR command repeat count
-    - [ ] GUI to change settings
+    - [x] GUI to change settings
+    - [ ] Return button configuration
 
 
 ## Screens
 ## Screens
 
 

+ 0 - 24
views/xremote_settings_view.c

@@ -1,24 +0,0 @@
-/*!
- *  @file flipper-xremote/views/xremote_settings_view.c
-    @license This project is released under the GNU GPLv3 License
- *  @copyright (c) 2023 Sandro Kalatozishvili (s.kalatoz@gmail.com)
- *
- * @brief UI and view functionality for the project settings page.
- */
-
-#include "xremote_settings_view.h"
-
-static void xremote_settings_view_draw_callback(Canvas* canvas, void* context)
-{
-    (void)context;
-    xremote_canvas_draw_header(canvas, "Settings");
-    canvas_set_font(canvas, FontSecondary);
-    canvas_draw_str(canvas, 0, 70, "Coming Soon");
-    xremote_canvas_draw_exit_footer(canvas, "Press to exit");
-}
-
-XRemoteView* xremote_settings_view_alloc(NotificationApp* notifications)
-{
-    return xremote_view_alloc(notifications,
-        NULL, xremote_settings_view_draw_callback);
-}

+ 0 - 13
views/xremote_settings_view.h

@@ -1,13 +0,0 @@
-/*!
- *  @file flipper-xremote/views/xremote_settings_view.h
-    @license This project is released under the GNU GPLv3 License
- *  @copyright (c) 2023 Sandro Kalatozishvili (s.kalatoz@gmail.com)
- *
- * @brief UI and view functionality for the project settings page.
- */
-
-#pragma once
-
-#include "xremote_common_view.h"
-
-XRemoteView* xremote_settings_view_alloc(NotificationApp* notifications);

+ 0 - 1
xremote.c

@@ -13,7 +13,6 @@
 
 
 #include "views/xremote_about_view.h"
 #include "views/xremote_about_view.h"
 #include "views/xremote_learn_view.h"
 #include "views/xremote_learn_view.h"
-#include "views/xremote_settings_view.h"
 
 
 #define TAG "XRemote"
 #define TAG "XRemote"
 
 

+ 1 - 1
xremote.h

@@ -10,6 +10,6 @@
 
 
 #define XREMOTE_VERSION_MAJOR  0
 #define XREMOTE_VERSION_MAJOR  0
 #define XREMOTE_VERSION_MINOR  9
 #define XREMOTE_VERSION_MINOR  9
-#define XREMOTE_BUILD_NUMBER   18
+#define XREMOTE_BUILD_NUMBER   19
 
 
 void xremote_get_version(char *version, size_t length);
 void xremote_get_version(char *version, size_t length);

+ 9 - 17
xremote_app.c

@@ -15,7 +15,7 @@
 XRemoteAppSettings* xremote_app_settings_alloc()
 XRemoteAppSettings* xremote_app_settings_alloc()
 {
 {
     XRemoteAppSettings* settings = malloc(sizeof(XRemoteAppSettings));
     XRemoteAppSettings* settings = malloc(sizeof(XRemoteAppSettings));
-    settings->orientation = ViewOrientationVertical;
+    settings->orientation = ViewOrientationHorizontal;
     settings->repeat_count = 1;
     settings->repeat_count = 1;
     return settings;
     return settings;
 }
 }
@@ -32,7 +32,6 @@ bool xremote_app_settings_store(XRemoteAppSettings* settings)
     FlipperFormat* ff = flipper_format_file_alloc(storage);
     FlipperFormat* ff = flipper_format_file_alloc(storage);
 
 
     FURI_LOG_I(TAG, "store config file: \'%s\'", XREMOTE_APP_SETTINGS);
     FURI_LOG_I(TAG, "store config file: \'%s\'", XREMOTE_APP_SETTINGS);
-    bool vertical = settings->orientation == ViewOrientationVertical;
     bool success = false;
     bool success = false;
 
 
     do {
     do {
@@ -42,9 +41,9 @@ bool xremote_app_settings_store(XRemoteAppSettings* settings)
         if (!flipper_format_write_comment_cstr(ff, "")) break;
         if (!flipper_format_write_comment_cstr(ff, "")) break;
 
 
         /* Write actual configuration to the settings file */
         /* Write actual configuration to the settings file */
-        const char *orientation = vertical ? "vertical" : "horizontal";
-        if (!flipper_format_write_string_cstr(ff, "orientation", orientation)) break;
-        if (!flipper_format_write_uint32(ff, "cmd_repeat", &settings->repeat_count, 1)) break;
+        uint32_t orientation = settings->orientation;
+        if (!flipper_format_write_uint32(ff, "orientation", &orientation, 1)) break;
+        if (!flipper_format_write_uint32(ff, "repeat", &settings->repeat_count, 1)) break;
 
 
         success = true;
         success = true;
     } while(false);
     } while(false);
@@ -60,11 +59,9 @@ bool xremote_app_settings_load(XRemoteAppSettings* settings)
     Storage* storage = furi_record_open(RECORD_STORAGE);
     Storage* storage = furi_record_open(RECORD_STORAGE);
     FlipperFormat* ff = flipper_format_buffered_file_alloc(storage);
     FlipperFormat* ff = flipper_format_buffered_file_alloc(storage);
     FuriString* header = furi_string_alloc();
     FuriString* header = furi_string_alloc();
-    FuriString* orient = furi_string_alloc();
 
 
     FURI_LOG_I(TAG, "load config file: \'%s\'", XREMOTE_APP_SETTINGS);
     FURI_LOG_I(TAG, "load config file: \'%s\'", XREMOTE_APP_SETTINGS);
-    uint32_t version = 0;
-    uint32_t repeat = 0;
+    uint32_t version, orientation, repeat = 0;
     bool success = false;
     bool success = false;
 
 
     do {
     do {
@@ -73,22 +70,17 @@ bool xremote_app_settings_load(XRemoteAppSettings* settings)
         if (!flipper_format_read_header(ff, header, &version)) break;
         if (!flipper_format_read_header(ff, header, &version)) break;
         if (!furi_string_equal(header, "XRemote settings file") || (version != 1)) break;
         if (!furi_string_equal(header, "XRemote settings file") || (version != 1)) break;
 
 
-        /* Read config data from the file */
-        if (!flipper_format_read_string(ff, "orientation", orient)) break;
-        if (!flipper_format_read_uint32(ff, "cmd_repeat", &repeat, 1)) break;
-
         /* Parse config data from the buffer */
         /* Parse config data from the buffer */
-        if (furi_string_equal(orient, "vertical"))
-            settings->orientation = ViewOrientationVertical;
-        else if (furi_string_equal(orient, "horizontal"))
-            settings->orientation = ViewOrientationHorizontal;
+        if (!flipper_format_read_uint32(ff, "orientation", &orientation, 1)) break;
+        if (!flipper_format_read_uint32(ff, "repeat", &repeat, 1)) break;
 
 
+        settings->orientation = orientation;
         settings->repeat_count = repeat;
         settings->repeat_count = repeat;
+
         success = true;
         success = true;
     } while(false);
     } while(false);
 
 
     furi_record_close(RECORD_STORAGE);
     furi_record_close(RECORD_STORAGE);
-    furi_string_free(orient);
     furi_string_free(header);
     furi_string_free(header);
     flipper_format_free(ff);
     flipper_format_free(ff);
 
 

+ 1 - 0
xremote_app.h

@@ -14,6 +14,7 @@
 #include <gui/view_dispatcher.h>
 #include <gui/view_dispatcher.h>
 #include <gui/modules/submenu.h>
 #include <gui/modules/submenu.h>
 #include <gui/modules/dialog_ex.h>
 #include <gui/modules/dialog_ex.h>
+#include <gui/modules/variable_item_list.h>
 
 
 #include <notification/notification.h>
 #include <notification/notification.h>
 #include <notification/notification_messages.h>
 #include <notification/notification_messages.h>

+ 120 - 3
xremote_settings.c

@@ -7,7 +7,23 @@
  */
  */
 
 
 #include "xremote_settings.h"
 #include "xremote_settings.h"
-#include "views/xremote_settings_view.h"
+
+typedef struct {
+    VariableItemList *item_list;
+    XRemoteAppContext* app_ctx;
+} XRemoteSettingsContext;
+
+#define XREMOTE_ORIENTATION_TEXT_HORIZONTAL     "Horizontal"
+#define XREMOTE_ORIENTATION_INDEX_HORIZONTAL    0
+
+#define XREMOTE_ORIENTATION_TEXT_VERTICAL       "Vertical"
+#define XREMOTE_ORIENTATION_INDEX_VERTICAL      1
+
+#define XREMOTE_ORIENTATION_TEXT                "Orientation"
+#define XREMOTE_ORIENTATION_MAX                 2
+
+#define XREMOTE_REPEAT_TEXT                     "IR Msg Repeat"
+#define XREMOTE_REPEAT_MAX                      128
 
 
 static uint32_t xremote_settings_view_exit_callback(void* context)
 static uint32_t xremote_settings_view_exit_callback(void* context)
 {
 {
@@ -15,10 +31,111 @@ static uint32_t xremote_settings_view_exit_callback(void* context)
     return XRemoteViewSubmenu;
     return XRemoteViewSubmenu;
 }
 }
 
 
+static uint32_t xremote_settings_get_orientation_index(XRemoteAppSettings* settings)
+{
+    return settings->orientation == ViewOrientationHorizontal ?
+        XREMOTE_ORIENTATION_INDEX_HORIZONTAL :
+        XREMOTE_ORIENTATION_INDEX_VERTICAL;
+}
+
+static const char* xremote_settings_get_orientation_str(XRemoteAppSettings* settings)
+{
+    return settings->orientation == ViewOrientationHorizontal ?
+        XREMOTE_ORIENTATION_TEXT_HORIZONTAL :
+        XREMOTE_ORIENTATION_TEXT_VERTICAL;
+}
+
+static ViewOrientation xremote_settings_get_orientation(uint8_t orientation)
+{
+    return orientation == XREMOTE_ORIENTATION_INDEX_HORIZONTAL ?
+        ViewOrientationHorizontal :
+        ViewOrientationVertical;
+}
+
+static void infrared_settings_orientation_changed(VariableItem* item)
+{
+    XRemoteSettingsContext* ctx = variable_item_get_context(item);
+    XRemoteAppSettings* settings = ctx->app_ctx->app_settings;
+
+    uint8_t orientation = variable_item_get_current_value_index(item);
+    settings->orientation = xremote_settings_get_orientation(orientation);
+    const char* orientation_str = xremote_settings_get_orientation_str(settings);
+
+    variable_item_set_current_value_text(item, orientation_str);
+    xremote_app_settings_store(settings);
+}
+
+static void infrared_settings_repeat_changed(VariableItem* item)
+{
+    XRemoteSettingsContext* ctx = variable_item_get_context(item);
+    XRemoteAppSettings* settings = ctx->app_ctx->app_settings;
+    char repeat_str[8];
+
+    settings->repeat_count = variable_item_get_current_value_index(item);
+    snprintf(repeat_str, sizeof(repeat_str), "%lu", settings->repeat_count);
+
+    variable_item_set_current_value_text(item, repeat_str);
+    xremote_app_settings_store(settings);
+}
+
+XRemoteSettingsContext* xremote_settings_context_alloc(XRemoteAppContext* app_ctx)
+{
+    XRemoteSettingsContext *context = malloc(sizeof(XRemoteSettingsContext));
+    XRemoteAppSettings* settings = app_ctx->app_settings;
+
+    context->item_list = variable_item_list_alloc();
+    context->app_ctx = app_ctx;
+    char repeat_str[8];
+
+    /* Configure variable item list view */
+    View *view = variable_item_list_get_view(context->item_list);
+    view_set_previous_callback(view, xremote_settings_view_exit_callback);
+    view_dispatcher_add_view(app_ctx->view_dispatcher, XRemoteViewSettings, view);
+
+    /* Add settings to variable item list */
+    VariableItem* item = variable_item_list_add(
+        context->item_list,
+        XREMOTE_ORIENTATION_TEXT,
+        XREMOTE_ORIENTATION_MAX,
+        infrared_settings_orientation_changed,
+        context);
+
+    /* Get application orientation settings */
+    const char* orient_str = xremote_settings_get_orientation_str(settings);
+    uint32_t orient_index = xremote_settings_get_orientation_index(settings);
+
+    /* Set current orientation item index and string */
+    variable_item_set_current_value_index(item, orient_index);
+    variable_item_set_current_value_text(item, orient_str);
+
+    /* Add IR message repeat counter to variable item list */
+    item = variable_item_list_add(
+        context->item_list,
+        XREMOTE_REPEAT_TEXT,
+        XREMOTE_REPEAT_MAX,
+        infrared_settings_repeat_changed,
+        context);
+
+    /* Set repeat count item index and string */
+    snprintf(repeat_str, sizeof(repeat_str), "%lu", settings->repeat_count);
+    variable_item_set_current_value_index(item, settings->repeat_count);
+    variable_item_set_current_value_text(item, repeat_str);
+
+    return context;
+}
+
+static void xremote_settings_context_clear_callback(void *context)
+{
+    XRemoteSettingsContext *ctx = (XRemoteSettingsContext*)context;
+    ViewDispatcher* view_disp = ctx->app_ctx->view_dispatcher;
+    view_dispatcher_remove_view(view_disp, XRemoteViewSettings);
+    variable_item_list_free(ctx->item_list);
+}
+
 XRemoteApp* xremote_settings_alloc(XRemoteAppContext* app_ctx)
 XRemoteApp* xremote_settings_alloc(XRemoteAppContext* app_ctx)
 {
 {
     XRemoteApp* app = xremote_app_alloc(app_ctx);
     XRemoteApp* app = xremote_app_alloc(app_ctx);
-    xremote_app_view_alloc(app, XRemoteViewSettings, xremote_settings_view_alloc);
-    xremote_app_view_set_previous_callback(app, xremote_settings_view_exit_callback);
+    XRemoteSettingsContext* context = xremote_settings_context_alloc(app_ctx);
+    xremote_app_set_user_context(app, context, xremote_settings_context_clear_callback);
     return app;
     return app;
 }
 }