Stuck on creating new user

This commit is contained in:
Maddox Werts 2024-08-28 00:43:31 -04:00
parent 6aff5bfa24
commit b0e0862c0e
6 changed files with 84 additions and 17 deletions

View file

@ -4,6 +4,6 @@
// Constants // Constants
#define TAG "FlippyPass" #define TAG "FlippyPass"
#define PATH "/ext/apps_data/flippypass/user.dat" #define PATH EXT_PATH("apps_data/flippypass")
#endif #endif

View file

@ -1,12 +1,12 @@
// Header // Header
#include "store.h" #include "store.h"
#include <furi.h>
#include <flipper_format.h> #include <flipper_format.h>
// Constructors // Constructors
FStorage* store_load() { FStorage* store_load() {
// Allocating memory for storage struct // Allocating memory for storage struct
FStorage* result = malloc(sizeof(FStorage)); FStorage* result = malloc(sizeof(FStorage));
result->valid = false;
// Reading storage // Reading storage
Storage* _storage = furi_record_open(RECORD_STORAGE); Storage* _storage = furi_record_open(RECORD_STORAGE);
@ -14,13 +14,29 @@ FStorage* store_load() {
FuriString* _data = furi_string_alloc(); FuriString* _data = furi_string_alloc();
// Memory issue?
if (!_format || !_data) {
result->data = "Storage Memory Allication Issue";
FURI_LOG_E(TAG, result->data);
return result;
}
// Opening file // Opening file
if (!flipper_format_file_open_existing(_format, PATH)) { if (!flipper_format_file_open_existing(_format, PATH)) {
FURI_LOG_E(TAG, "Couldn't open %s", PATH); result->data = "Sorry, please register.";
return NULL; FURI_LOG_E(TAG, result->data);
} else if (!flipper_format_read_string(_format, "Data", _data)) {
FURI_LOG_E(TAG, "Couldn't read %s", PATH); // Cleaning up
return NULL; furi_string_free(_data);
flipper_format_free(_format);
furi_record_close(RECORD_STORAGE);
return result;
}
// Reading data from file
if (!flipper_format_read_string(_format, "Data", _data)) {
result->data = "Couldn't read data";
FURI_LOG_E(TAG, result->data);
} }
// Copy data into result // Copy data into result
@ -35,10 +51,47 @@ FStorage* store_load() {
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* data) {
// Creating new file
Storage* _storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* _format = flipper_format_file_alloc(_storage);
FuriString* _data = furi_string_alloc();
// Setting furi string data
furi_string_set_str(_data, data);
// Opening new file
if (!flipper_format_file_open_existing(_format, PATH)) {
// New user?
FURI_LOG_I(TAG, "Creating new user.");
// Trying to create a new file
if (!flipper_format_file_open_new(_format, PATH)) {
FURI_LOG_E(TAG, "Failed to create new file at %s", PATH);
return;
}
}
// Writing string
if (!flipper_format_write_string(_format, "Data", _data)) {
FURI_LOG_E(TAG, "Failed to write to file");
return;
}
// Closing file
furi_string_free(_data);
flipper_format_free(_format);
furi_record_close(RECORD_STORAGE);
}
// Functions // Functions
void store_unload(FStorage* store) { void store_unload(FStorage* store) {
// Cleaning up memory // Cleaning up memory

View file

@ -3,15 +3,18 @@
#define H_STORE #define H_STORE
// Libraries // Libraries
#include <furi.h>
#include "app.h" #include "app.h"
// Structures // Structures
typedef struct { typedef struct {
char* data; char* data;
bool valid;
} FStorage; } FStorage;
// Constructors // Constructors
FStorage* store_load(); FStorage* store_load();
void store_save(char* data);
// Functions // Functions
void store_unload(FStorage* store); void store_unload(FStorage* store);

View file

@ -7,7 +7,14 @@ UIManager* ui_create() {
UIManager* result = malloc(sizeof(UIManager)); UIManager* result = malloc(sizeof(UIManager));
// Creating Storage // Creating Storage
result->store = store_load(); result->store = store_load();
// Wait, do we need to register?
if (!result->store->valid) {
FURI_LOG_E(TAG, "You are not registered!");
store_save("Hello, World");
FURI_LOG_I(TAG, "Created new user.");
}
// Defining basic variables // Defining basic variables
result->running = true; result->running = true;
@ -66,6 +73,16 @@ void ui_input(InputEvent* event, void* ctx) {
manager->press_avail = true; manager->press_avail = true;
} }
} }
void ui_quit(UIManager* manager) {
if (!(manager->press_used && manager->input == Back)) {return;}
if(manager->page == 0) {
manager->running = false;
} else {
manager->press_used = false;
manager->page = 0;
}
}
void ui_draw(Canvas* canvas, void* ctx) { void ui_draw(Canvas* canvas, void* ctx) {
// Context into Result // Context into Result
@ -192,6 +209,7 @@ 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);

View file

@ -40,6 +40,7 @@ UIManager* ui_create();
// Functions // Functions
void ui_input(InputEvent* event, void* ctx); void ui_input(InputEvent* event, void* ctx);
void ui_quit(UIManager* manager);
void ui_draw(Canvas* canvas, void* ctx); void ui_draw(Canvas* canvas, void* ctx);
void ui_p_mainmenu(Canvas* canvas, UIManager* manager); void ui_p_mainmenu(Canvas* canvas, UIManager* manager);

View file

@ -14,15 +14,7 @@ int32_t flippypass_app(void* p) {
// Drawwing the UI // Drawwing the UI
while(ui->running) { while(ui->running) {
// Do we want to quit? // Do we want to quit?
if (ui->press_used ui_quit(ui);
&& ui->input == Back) {
if(ui->page == 0) {
ui->running = false;
} else {
ui->press_used = false;
ui->page = 0;
}
}
// Updating canvas // Updating canvas
view_port_update(ui->canvas); view_port_update(ui->canvas);