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
#define TAG "FlippyPass"
#define PATH "/ext/apps_data/flippypass/user.dat"
#define PATH EXT_PATH("apps_data/flippypass")
#endif

View file

@ -1,12 +1,12 @@
// Header
#include "store.h"
#include <furi.h>
#include <flipper_format.h>
// Constructors
FStorage* store_load() {
// Allocating memory for storage struct
FStorage* result = malloc(sizeof(FStorage));
result->valid = false;
// Reading storage
Storage* _storage = furi_record_open(RECORD_STORAGE);
@ -14,13 +14,29 @@ FStorage* store_load() {
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
if (!flipper_format_file_open_existing(_format, PATH)) {
FURI_LOG_E(TAG, "Couldn't open %s", PATH);
return NULL;
} else if (!flipper_format_read_string(_format, "Data", _data)) {
FURI_LOG_E(TAG, "Couldn't read %s", PATH);
return NULL;
result->data = "Sorry, please register.";
FURI_LOG_E(TAG, result->data);
// Cleaning up
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
@ -35,10 +51,47 @@ FStorage* store_load() {
flipper_format_free(_format);
furi_record_close(RECORD_STORAGE);
// We're valid!
result->valid = true;
// Returning 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
void store_unload(FStorage* store) {
// Cleaning up memory

View file

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

View file

@ -9,6 +9,13 @@ UIManager* ui_create() {
// Creating Storage
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
result->running = true;
result->selection = 0;
@ -66,6 +73,16 @@ void ui_input(InputEvent* event, void* ctx) {
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) {
// Context into Result
@ -192,6 +209,7 @@ 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);

View file

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

View file

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