Major code cleanup
This commit is contained in:
parent
029da1eb25
commit
f2948ccc2d
4 changed files with 26 additions and 64 deletions
|
@ -21,14 +21,13 @@ void store_folder_init() {
|
||||||
furi_record_close(RECORD_STORAGE);
|
furi_record_close(RECORD_STORAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Constructors
|
// Functions
|
||||||
FStorage* store_load(char* type) {
|
char* store_load(char* type) {
|
||||||
// Init Folders
|
// Init Folders
|
||||||
store_folder_init();
|
store_folder_init();
|
||||||
|
|
||||||
// Allocating memory for storage struct
|
// Allocating memory for storage struct
|
||||||
FStorage* result = malloc(sizeof(FStorage));
|
char* result = malloc(sizeof(char));
|
||||||
result->valid = false;
|
|
||||||
|
|
||||||
// Reading storage
|
// Reading storage
|
||||||
Storage* _storage = furi_record_open(RECORD_STORAGE);
|
Storage* _storage = furi_record_open(RECORD_STORAGE);
|
||||||
|
@ -38,33 +37,31 @@ FStorage* store_load(char* type) {
|
||||||
|
|
||||||
// Memory issue?
|
// Memory issue?
|
||||||
if (!_format || !_data) {
|
if (!_format || !_data) {
|
||||||
result->data = "Storage Memory Allication Issue";
|
FURI_LOG_E(TAG, "Storage Memory Allication Issue");
|
||||||
FURI_LOG_E(TAG, result->data);
|
return NULL;
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Opening file
|
// Opening file
|
||||||
if (!flipper_format_file_open_existing(_format, PATH)) {
|
if (!flipper_format_file_open_existing(_format, PATH)) {
|
||||||
result->data = "Sorry, please register.";
|
FURI_LOG_E(TAG, "Sorry, please register.");
|
||||||
FURI_LOG_E(TAG, result->data);
|
|
||||||
|
|
||||||
// Cleaning up
|
// Cleaning up
|
||||||
furi_string_free(_data);
|
furi_string_free(_data);
|
||||||
flipper_format_free(_format);
|
flipper_format_free(_format);
|
||||||
furi_record_close(RECORD_STORAGE);
|
furi_record_close(RECORD_STORAGE);
|
||||||
return result;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reading data from file
|
// Reading data from file
|
||||||
if (!flipper_format_read_string(_format, type, _data)) {
|
if (!flipper_format_read_string(_format, type, _data)) {
|
||||||
result->data = "Couldn't read data";
|
FURI_LOG_E(TAG, "Couldn't read data");
|
||||||
FURI_LOG_E(TAG, result->data);
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy data into result
|
// Copy data into result
|
||||||
const char* _cstr_data = furi_string_get_cstr(_data);
|
const char* _cstr_data = furi_string_get_cstr(_data);
|
||||||
result->data = malloc(strlen(_cstr_data) + 1);
|
result = malloc(strlen(_cstr_data) + 1);
|
||||||
strcpy(result->data, _cstr_data);
|
strcpy(result, _cstr_data);
|
||||||
|
|
||||||
// Free the original string
|
// Free the original string
|
||||||
furi_string_free(_data);
|
furi_string_free(_data);
|
||||||
|
@ -73,13 +70,9 @@ FStorage* store_load(char* type) {
|
||||||
flipper_format_free(_format);
|
flipper_format_free(_format);
|
||||||
furi_record_close(RECORD_STORAGE);
|
furi_record_close(RECORD_STORAGE);
|
||||||
|
|
||||||
// We're valid!
|
|
||||||
result->valid = true;
|
|
||||||
|
|
||||||
// Returning result
|
// Returning result
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void store_save(char* type, char* data) {
|
void store_save(char* type, char* data) {
|
||||||
// Creating new file
|
// Creating new file
|
||||||
Storage* _storage = furi_record_open(RECORD_STORAGE);
|
Storage* _storage = furi_record_open(RECORD_STORAGE);
|
||||||
|
@ -119,11 +112,4 @@ void store_save(char* type, char* data) {
|
||||||
furi_string_free(_data);
|
furi_string_free(_data);
|
||||||
flipper_format_free(_format);
|
flipper_format_free(_format);
|
||||||
furi_record_close(RECORD_STORAGE);
|
furi_record_close(RECORD_STORAGE);
|
||||||
}
|
|
||||||
|
|
||||||
// Functions
|
|
||||||
void store_unload(FStorage* store) {
|
|
||||||
// Cleaning up memory
|
|
||||||
free(store->data);
|
|
||||||
free(store);
|
|
||||||
}
|
}
|
|
@ -6,17 +6,8 @@
|
||||||
#include <furi.h>
|
#include <furi.h>
|
||||||
#include "app.h"
|
#include "app.h"
|
||||||
|
|
||||||
// Structures
|
// Functions
|
||||||
typedef struct {
|
char* store_load(char* type);
|
||||||
char* data;
|
|
||||||
bool valid;
|
|
||||||
} FStorage;
|
|
||||||
|
|
||||||
// Constructors
|
|
||||||
FStorage* store_load(char* type);
|
|
||||||
void store_save(char* type, char* data);
|
void store_save(char* type, char* data);
|
||||||
|
|
||||||
// Functions
|
|
||||||
void store_unload(FStorage* store);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -6,27 +6,13 @@ UIManager* ui_create() {
|
||||||
// Creating the UI Manager
|
// Creating the UI Manager
|
||||||
UIManager* result = malloc(sizeof(UIManager));
|
UIManager* result = malloc(sizeof(UIManager));
|
||||||
|
|
||||||
// Creating Storage
|
|
||||||
result->store = store_load("Data");
|
|
||||||
|
|
||||||
// Wait, do we need to register?
|
|
||||||
if (!result->store->valid) {
|
|
||||||
FURI_LOG_E(TAG, "You are not registered!");
|
|
||||||
|
|
||||||
store_save("Data", "Hello, World");
|
|
||||||
|
|
||||||
FURI_LOG_I(TAG, "Created new user.");
|
|
||||||
} else {
|
|
||||||
FURI_LOG_I(TAG, "File Contains: %s", result->store->data);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Defining basic variables
|
// Defining basic variables
|
||||||
result->running = true;
|
result->running = true;
|
||||||
result->selection = 0;
|
result->selection = 0;
|
||||||
result->page = 0;
|
result->page = 0;
|
||||||
|
|
||||||
result->press_avail = true;
|
result->press_avail = true;
|
||||||
result->press_used = false;
|
result->is_pressing = false;
|
||||||
|
|
||||||
// Creating a GUI
|
// Creating a GUI
|
||||||
result->gui = furi_record_open(RECORD_GUI);
|
result->gui = furi_record_open(RECORD_GUI);
|
||||||
|
@ -49,7 +35,7 @@ void ui_input(InputEvent* event, void* ctx) {
|
||||||
if (event->type == InputTypePress
|
if (event->type == InputTypePress
|
||||||
&& manager->press_avail) {
|
&& manager->press_avail) {
|
||||||
manager->press_avail = false;
|
manager->press_avail = false;
|
||||||
manager->press_used = true;
|
manager->is_pressing = true;
|
||||||
|
|
||||||
switch(event->key) {
|
switch(event->key) {
|
||||||
case InputKeyUp:
|
case InputKeyUp:
|
||||||
|
@ -78,12 +64,12 @@ void ui_input(InputEvent* event, void* ctx) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void ui_quit(UIManager* manager) {
|
void ui_quit(UIManager* manager) {
|
||||||
if (!(manager->press_used && manager->input == Back)) {return;}
|
if (!(manager->is_pressing && manager->input == Back)) {return;}
|
||||||
|
|
||||||
if(manager->page == 0) {
|
if(manager->page == 0) {
|
||||||
manager->running = false;
|
manager->running = false;
|
||||||
} else {
|
} else {
|
||||||
manager->press_used = false;
|
manager->is_pressing = false;
|
||||||
manager->page = 0;
|
manager->page = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -123,9 +109,9 @@ void ui_p_mainmenu(Canvas* canvas, UIManager* manager) {
|
||||||
canvas_draw_str(canvas, 22, 55, "About");
|
canvas_draw_str(canvas, 22, 55, "About");
|
||||||
|
|
||||||
// Cursor
|
// Cursor
|
||||||
if(manager->press_used) {
|
if(manager->is_pressing) {
|
||||||
// We used a press
|
// We used a press
|
||||||
manager->press_used = false;
|
manager->is_pressing = false;
|
||||||
|
|
||||||
// Reacting
|
// Reacting
|
||||||
switch (manager->input){
|
switch (manager->input){
|
||||||
|
@ -190,11 +176,13 @@ void ui_p_view(Canvas* canvas, UIManager* manager){
|
||||||
canvas_set_font(canvas, FontSecondary);
|
canvas_set_font(canvas, FontSecondary);
|
||||||
canvas_draw_str(canvas, 15, 53, "Facebook"); */
|
canvas_draw_str(canvas, 15, 53, "Facebook"); */
|
||||||
|
|
||||||
canvas_set_font(canvas, FontSecondary);
|
// Loading data storage
|
||||||
canvas_draw_str(canvas, 15, 23, manager->store->data);
|
|
||||||
|
|
||||||
if (manager->press_used) {
|
canvas_set_font(canvas, FontSecondary);
|
||||||
manager->press_used = false;
|
canvas_draw_str(canvas, 15, 23, "TODO: Create Manager");
|
||||||
|
|
||||||
|
if (manager->is_pressing) {
|
||||||
|
manager->is_pressing = false;
|
||||||
|
|
||||||
switch (manager->input) {
|
switch (manager->input) {
|
||||||
case Up:
|
case Up:
|
||||||
|
@ -213,7 +201,6 @@ void ui_p_view(Canvas* canvas, UIManager* manager){
|
||||||
}
|
}
|
||||||
|
|
||||||
void ui_delete(UIManager* manager) {
|
void ui_delete(UIManager* manager) {
|
||||||
store_unload(manager->store);
|
|
||||||
view_port_enabled_set(manager->canvas, false);
|
view_port_enabled_set(manager->canvas, false);
|
||||||
gui_remove_view_port(manager->gui, manager->canvas);
|
gui_remove_view_port(manager->gui, manager->canvas);
|
||||||
view_port_free(manager->canvas);
|
view_port_free(manager->canvas);
|
||||||
|
|
|
@ -13,8 +13,6 @@
|
||||||
|
|
||||||
// Structures
|
// Structures
|
||||||
typedef struct {
|
typedef struct {
|
||||||
FStorage* store;
|
|
||||||
|
|
||||||
ViewPort* canvas;
|
ViewPort* canvas;
|
||||||
Gui* gui;
|
Gui* gui;
|
||||||
enum ui_input {
|
enum ui_input {
|
||||||
|
@ -31,7 +29,7 @@ typedef struct {
|
||||||
int page;
|
int page;
|
||||||
|
|
||||||
bool running;
|
bool running;
|
||||||
bool press_used;
|
bool is_pressing;
|
||||||
bool press_avail;
|
bool press_avail;
|
||||||
} UIManager;
|
} UIManager;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue