feat: persist values daily progress with sqlite
This commit is contained in:
@@ -3,3 +3,4 @@ build/
|
|||||||
*.o
|
*.o
|
||||||
*#*
|
*#*
|
||||||
adhd
|
adhd
|
||||||
|
*.sqlite3
|
||||||
@@ -2,7 +2,7 @@ CC = cc
|
|||||||
CFLAGS = -g -Wall -Iinclude
|
CFLAGS = -g -Wall -Iinclude
|
||||||
SOURCES = src/main.c src/window_state.c
|
SOURCES = src/main.c src/window_state.c
|
||||||
OBJECTS = src/main.o src/window_state.o
|
OBJECTS = src/main.o src/window_state.o
|
||||||
LDFLAGS = -lraylib -lm
|
LDFLAGS = -lraylib -lm -lsqlite3
|
||||||
HEADERS = \
|
HEADERS = \
|
||||||
include/raygui.h include/common.h include/window_state.h
|
include/raygui.h include/common.h include/window_state.h
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
|
|
||||||
- [x] model window state with intention and simple overlay, window modes
|
- [x] model window state with intention and simple overlay, window modes
|
||||||
|
|
||||||
- [ ] create 2d grid
|
- [x] create 2d grid
|
||||||
- [ ] toggle win / loss and persist values
|
- [x] usability: show week of the year / day of week, tooltips
|
||||||
|
- [x] toggle win / loss and persist values
|
||||||
|
|
||||||
|
- [x] persist results in sqlite3
|
||||||
|
|
||||||
- [ ] reminder animation
|
- [ ] reminder animation
|
||||||
|
|
||||||
- [ ] install via homebrew
|
- [ ] install via homebrew
|
||||||
- [ ] create windows download
|
- [ ] launch on hackernews "for developers with adhd"
|
||||||
- [ ] launch on hackernews "for developers with adhd"...create an html page as well on flowylabs site
|
|
||||||
|
|||||||
@@ -93,7 +93,6 @@ void draw_graph(int x, int y) {
|
|||||||
GuiSetStyle(BUTTON, BASE_COLOR_NORMAL, ColorToInt(BLANK));
|
GuiSetStyle(BUTTON, BASE_COLOR_NORMAL, ColorToInt(BLANK));
|
||||||
}
|
}
|
||||||
if (GuiButton((Rectangle) {cell_x + x, cell_y + y, CELL_SIZE, CELL_SIZE}, " ")) {
|
if (GuiButton((Rectangle) {cell_x + x, cell_y + y, CELL_SIZE, CELL_SIZE}, " ")) {
|
||||||
printf("clicked row %d | column %d\n", r, c);
|
|
||||||
set_result(time, abs(result - 1));
|
set_result(time, abs(result - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+89
-5
@@ -3,6 +3,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <sqlite3.h>
|
||||||
|
|
||||||
struct state *s = NULL;
|
struct state *s = NULL;
|
||||||
|
|
||||||
@@ -21,6 +22,84 @@ struct state {
|
|||||||
size_t capacity_records;
|
size_t capacity_records;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static sqlite3 *db = NULL;
|
||||||
|
|
||||||
|
static void db_open() {
|
||||||
|
int r = sqlite3_open("local.sqlite3", &db);
|
||||||
|
if (r == SQLITE_OK) {
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Unable to prepare sqlite db\n");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *sql =
|
||||||
|
"CREATE TABLE IF NOT EXISTS records ("
|
||||||
|
"key TEXT PRIMARY KEY,"
|
||||||
|
"value INT"
|
||||||
|
");"
|
||||||
|
;
|
||||||
|
|
||||||
|
char *err;
|
||||||
|
r = sqlite3_exec(db, sql, NULL, NULL, &err);
|
||||||
|
if (r != SQLITE_OK) {
|
||||||
|
fprintf(stderr, "SQL error during setup: %s\n", err);
|
||||||
|
sqlite3_free(err);
|
||||||
|
sqlite3_close(db);
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void db_close() {
|
||||||
|
if (db) {
|
||||||
|
sqlite3_close(db);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int db_get_record_callback(void *ctx, int argc, char *argv[], char **argv_col_name) {
|
||||||
|
Record *rec = calloc(1, sizeof(Record));
|
||||||
|
|
||||||
|
for (int i = 0; i < argc; i++) {
|
||||||
|
if (strcmp(argv_col_name[i], "key") == 0) {
|
||||||
|
rec->key = strdup(argv[i]);
|
||||||
|
} else if (strcmp(argv_col_name[i], "value") == 0) {
|
||||||
|
rec->value = (U32) atoi(argv[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s->size_records >= s->capacity_records) {
|
||||||
|
printf("MAX capacity");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
*(s->records + s->size_records) = rec;
|
||||||
|
s->size_records++;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void db_upsert_record(char *key, U32 value) {
|
||||||
|
sqlite3_stmt *stmt;
|
||||||
|
const char *sql = "INSERT INTO records (key, value) VALUES (?, ?) ON CONFLICT (key) DO UPDATE SET value = ?;";
|
||||||
|
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
|
||||||
|
assert(rc == SQLITE_OK);
|
||||||
|
sqlite3_bind_text(stmt, 1, key, -1, SQLITE_STATIC);
|
||||||
|
sqlite3_bind_int(stmt, 2, value);
|
||||||
|
sqlite3_bind_int(stmt, 3, value);
|
||||||
|
|
||||||
|
rc = sqlite3_step(stmt);
|
||||||
|
if (rc != SQLITE_DONE) {
|
||||||
|
printf("Failure in insert: %s\n", sqlite3_errmsg(db));
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlite3_finalize(stmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void db_get_all_records() {
|
||||||
|
const char *sql = "SELECT key, value FROM records;";
|
||||||
|
int rc = sqlite3_exec(db, sql, db_get_record_callback, NULL, NULL);
|
||||||
|
assert(rc == SQLITE_OK);
|
||||||
|
}
|
||||||
|
|
||||||
void init_window_state() {
|
void init_window_state() {
|
||||||
s = malloc(sizeof(struct state));
|
s = malloc(sizeof(struct state));
|
||||||
s->mode = WINDOW;
|
s->mode = WINDOW;
|
||||||
@@ -30,12 +109,15 @@ void init_window_state() {
|
|||||||
s->size_records = 0;
|
s->size_records = 0;
|
||||||
s->capacity_records = 1000; //TODO: make dynamic
|
s->capacity_records = 1000; //TODO: make dynamic
|
||||||
s->records = malloc(sizeof(Record *) * s->capacity_records);
|
s->records = malloc(sizeof(Record *) * s->capacity_records);
|
||||||
|
|
||||||
|
db_open();
|
||||||
|
db_get_all_records();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_record(char *key, U32 value) {
|
static Record *append_record(char *key, U32 value) {
|
||||||
if (s->size_records >= s->capacity_records) {
|
if (s->size_records >= s->capacity_records) {
|
||||||
printf("MAX capacity");
|
printf("MAX capacity");
|
||||||
exit(-1);
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *cpy = strdup(key);
|
char *cpy = strdup(key);
|
||||||
@@ -46,6 +128,7 @@ static void add_record(char *key, U32 value) {
|
|||||||
|
|
||||||
*(s->records + s->size_records) = new_record;
|
*(s->records + s->size_records) = new_record;
|
||||||
s->size_records++;
|
s->size_records++;
|
||||||
|
return new_record;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void upsert_record(char *key, U32 value) {
|
static void upsert_record(char *key, U32 value) {
|
||||||
@@ -60,7 +143,7 @@ static void upsert_record(char *key, U32 value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
add_record(key, value);
|
append_record(key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,6 +182,8 @@ void cleanup_window_state() {
|
|||||||
|
|
||||||
free(s);
|
free(s);
|
||||||
s = NULL;
|
s = NULL;
|
||||||
|
|
||||||
|
db_close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_intention(char *intention) {
|
void set_intention(char *intention) {
|
||||||
@@ -142,6 +227,5 @@ void set_result(struct tm *time, U32 result) {
|
|||||||
char key[10];
|
char key[10];
|
||||||
generate_key(time, key, sizeof(key));
|
generate_key(time, key, sizeof(key));
|
||||||
upsert_record(key, result);
|
upsert_record(key, result);
|
||||||
|
db_upsert_record(key, result);
|
||||||
print_records();
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user