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);
|
||||
}
|
||||
|
||||
// Constructors
|
||||
FStorage* store_load(char* type) {
|
||||
// Functions
|
||||
char* store_load(char* type) {
|
||||
// Init Folders
|
||||
store_folder_init();
|
||||
|
||||
// Allocating memory for storage struct
|
||||
FStorage* result = malloc(sizeof(FStorage));
|
||||
result->valid = false;
|
||||
char* result = malloc(sizeof(char));
|
||||
|
||||
// Reading storage
|
||||
Storage* _storage = furi_record_open(RECORD_STORAGE);
|
||||
|
@ -38,33 +37,31 @@ FStorage* store_load(char* type) {
|
|||
|
||||
// Memory issue?
|
||||
if (!_format || !_data) {
|
||||
result->data = "Storage Memory Allication Issue";
|
||||
FURI_LOG_E(TAG, result->data);
|
||||
return result;
|
||||
FURI_LOG_E(TAG, "Storage Memory Allication Issue");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Opening file
|
||||
if (!flipper_format_file_open_existing(_format, PATH)) {
|
||||
result->data = "Sorry, please register.";
|
||||
FURI_LOG_E(TAG, result->data);
|
||||
FURI_LOG_E(TAG, "Sorry, please register.");
|
||||
|
||||
// Cleaning up
|
||||
furi_string_free(_data);
|
||||
flipper_format_free(_format);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
return result;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Reading data from file
|
||||
if (!flipper_format_read_string(_format, type, _data)) {
|
||||
result->data = "Couldn't read data";
|
||||
FURI_LOG_E(TAG, result->data);
|
||||
FURI_LOG_E(TAG, "Couldn't read data");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Copy data into result
|
||||
const char* _cstr_data = furi_string_get_cstr(_data);
|
||||
result->data = malloc(strlen(_cstr_data) + 1);
|
||||
strcpy(result->data, _cstr_data);
|
||||
result = malloc(strlen(_cstr_data) + 1);
|
||||
strcpy(result, _cstr_data);
|
||||
|
||||
// Free the original string
|
||||
furi_string_free(_data);
|
||||
|
@ -73,13 +70,9 @@ FStorage* store_load(char* type) {
|
|||
flipper_format_free(_format);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
|
||||
// We're valid!
|
||||
result->valid = true;
|
||||
|
||||
// Returning result
|
||||
return result;
|
||||
}
|
||||
|
||||
void store_save(char* type, char* data) {
|
||||
// Creating new file
|
||||
Storage* _storage = furi_record_open(RECORD_STORAGE);
|
||||
|
@ -120,10 +113,3 @@ void store_save(char* type, char* data) {
|
|||
flipper_format_free(_format);
|
||||
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 "app.h"
|
||||
|
||||
// Structures
|
||||
typedef struct {
|
||||
char* data;
|
||||
bool valid;
|
||||
} FStorage;
|
||||
|
||||
// Constructors
|
||||
FStorage* store_load(char* type);
|
||||
// Functions
|
||||
char* store_load(char* type);
|
||||
void store_save(char* type, char* data);
|
||||
|
||||
// Functions
|
||||
void store_unload(FStorage* store);
|
||||
|
||||
#endif
|
|
@ -6,27 +6,13 @@ UIManager* ui_create() {
|
|||
// Creating the UI Manager
|
||||
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
|
||||
result->running = true;
|
||||
result->selection = 0;
|
||||
result->page = 0;
|
||||
|
||||
result->press_avail = true;
|
||||
result->press_used = false;
|
||||
result->is_pressing = false;
|
||||
|
||||
// Creating a GUI
|
||||
result->gui = furi_record_open(RECORD_GUI);
|
||||
|
@ -49,7 +35,7 @@ void ui_input(InputEvent* event, void* ctx) {
|
|||
if (event->type == InputTypePress
|
||||
&& manager->press_avail) {
|
||||
manager->press_avail = false;
|
||||
manager->press_used = true;
|
||||
manager->is_pressing = true;
|
||||
|
||||
switch(event->key) {
|
||||
case InputKeyUp:
|
||||
|
@ -78,12 +64,12 @@ void ui_input(InputEvent* event, void* ctx) {
|
|||
}
|
||||
}
|
||||
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) {
|
||||
manager->running = false;
|
||||
} else {
|
||||
manager->press_used = false;
|
||||
manager->is_pressing = false;
|
||||
manager->page = 0;
|
||||
}
|
||||
}
|
||||
|
@ -123,9 +109,9 @@ void ui_p_mainmenu(Canvas* canvas, UIManager* manager) {
|
|||
canvas_draw_str(canvas, 22, 55, "About");
|
||||
|
||||
// Cursor
|
||||
if(manager->press_used) {
|
||||
if(manager->is_pressing) {
|
||||
// We used a press
|
||||
manager->press_used = false;
|
||||
manager->is_pressing = false;
|
||||
|
||||
// Reacting
|
||||
switch (manager->input){
|
||||
|
@ -190,11 +176,13 @@ void ui_p_view(Canvas* canvas, UIManager* manager){
|
|||
canvas_set_font(canvas, FontSecondary);
|
||||
canvas_draw_str(canvas, 15, 53, "Facebook"); */
|
||||
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
canvas_draw_str(canvas, 15, 23, manager->store->data);
|
||||
// Loading data storage
|
||||
|
||||
if (manager->press_used) {
|
||||
manager->press_used = false;
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
canvas_draw_str(canvas, 15, 23, "TODO: Create Manager");
|
||||
|
||||
if (manager->is_pressing) {
|
||||
manager->is_pressing = false;
|
||||
|
||||
switch (manager->input) {
|
||||
case Up:
|
||||
|
@ -213,7 +201,6 @@ void ui_p_view(Canvas* canvas, UIManager* manager){
|
|||
}
|
||||
|
||||
void ui_delete(UIManager* manager) {
|
||||
store_unload(manager->store);
|
||||
view_port_enabled_set(manager->canvas, false);
|
||||
gui_remove_view_port(manager->gui, manager->canvas);
|
||||
view_port_free(manager->canvas);
|
||||
|
|
|
@ -13,8 +13,6 @@
|
|||
|
||||
// Structures
|
||||
typedef struct {
|
||||
FStorage* store;
|
||||
|
||||
ViewPort* canvas;
|
||||
Gui* gui;
|
||||
enum ui_input {
|
||||
|
@ -31,7 +29,7 @@ typedef struct {
|
|||
int page;
|
||||
|
||||
bool running;
|
||||
bool press_used;
|
||||
bool is_pressing;
|
||||
bool press_avail;
|
||||
} UIManager;
|
||||
|
||||
|
|
Loading…
Reference in a new issue