-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
384 lines (317 loc) · 10.8 KB
/
main.c
File metadata and controls
384 lines (317 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include "os_generic.h"
#include <GLES3/gl3.h>
#include <android_native_app_glue.h>
#include "CNFGAndroid.h"
#define CNFG_IMPLEMENTATION
#include "CNFG.h"
#define MAX_ENTRIES 1000
#define FILENAME "streak_data.txt"
// scaling helpers
float GetScale(int w, int h) {
int min_dim = (w < h) ? w : h;
// 700.0f reference to make UI larger on 1080p+ screens
float s = min_dim / 700.0f;
if (s < 0.5f) s = 0.5f;
return s;
}
int GetTextScale(int baseSize, float globalScale) {
int s = (int)(baseSize * globalScale + 0.5f);
return (s < 1) ? 1 : s;
}
typedef struct {
int year;
int month;
int day;
time_t timestamp;
} DateEntry;
DateEntry entries[MAX_ENTRIES];
int entryCount = 0;
// input & ui state
char inputBuffer[64] = {0};
int inputLen = 0;
int isTyping = 0;
int streakCount = 0;
// scrolling & touch state
int scrollY = 0;
int lastTouchY = 0;
int startTouchY = 0;
int isDragging = 0;
int isTap = 0;
// helper to center text
void DrawTextCentered(const char* text, int scale, int y, int screenW) {
int charWidth = 6 * scale;
int textLen = strlen(text);
int totalWidth = textLen * charWidth;
CNFGPenX = (screenW - totalWidth) / 2;
CNFGPenY = y;
CNFGDrawText(text, scale);
}
// sorts dates newest first
int CompareDates(const void* a, const void* b) {
return (int)(((DateEntry*)b)->timestamp - ((DateEntry*)a)->timestamp);
}
void CalculateStreak() {
if (entryCount == 0) { streakCount = 0; return; }
qsort(entries, entryCount, sizeof(DateEntry), CompareDates);
time_t now = time(0);
double secondsSinceLast = difftime(now, entries[0].timestamp);
int daysSinceLast = (int)(secondsSinceLast / (60 * 60 * 24));
if (daysSinceLast > 1) {
streakCount = 0;
return;
}
streakCount = 1;
for (int i = 0; i < entryCount - 1; i++) {
double diff = difftime(entries[i].timestamp, entries[i+1].timestamp);
int daysDiff = (int)(diff / (60 * 60 * 24));
if (daysDiff == 1) streakCount++;
else if (daysDiff == 0) continue;
else break;
}
}
void SaveData() {
const char* path = AndroidGetExternalFilesDir();
char fullPath[256];
snprintf(fullPath, sizeof(fullPath), "%s/%s", path, FILENAME);
FILE* f = fopen(fullPath, "w");
if (!f) return;
for (int i = 0; i < entryCount; i++) {
fprintf(f, "%d %d %d\n", entries[i].year, entries[i].month, entries[i].day);
}
fclose(f);
}
void LoadData() {
const char* path = AndroidGetExternalFilesDir();
char fullPath[256];
snprintf(fullPath, sizeof(fullPath), "%s/%s", path, FILENAME);
FILE* f = fopen(fullPath, "r");
if (!f) return;
entryCount = 0;
struct tm tm = {0};
while (fscanf(f, "%d %d %d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday) == 3 && entryCount < MAX_ENTRIES) {
entries[entryCount].year = tm.tm_year;
entries[entryCount].month = tm.tm_mon;
entries[entryCount].day = tm.tm_mday;
tm.tm_year -= 1900; tm.tm_mon -= 1; tm.tm_hour = 12;
entries[entryCount].timestamp = mktime(&tm);
entryCount++;
}
fclose(f);
CalculateStreak();
}
void AddEntry(int y, int m, int d) {
if (entryCount >= MAX_ENTRIES) return;
struct tm tm = {0};
tm.tm_year = y - 1900; tm.tm_mon = m - 1; tm.tm_mday = d; tm.tm_hour = 12;
entries[entryCount].year = y; entries[entryCount].month = m; entries[entryCount].day = d;
entries[entryCount].timestamp = mktime(&tm);
entryCount++;
SaveData();
CalculateStreak();
}
void LazyEntry() {
time_t t = time(0);
struct tm *now = localtime(&t);
// check if today is already added to prevent dupes and future dates
if (entryCount > 0) {
if (entries[0].year == (now->tm_year + 1900) &&
entries[0].month == (now->tm_mon + 1) &&
entries[0].day == now->tm_mday) {
return;
}
}
AddEntry(now->tm_year + 1900, now->tm_mon + 1, now->tm_mday);
}
void Undo() {
if (entryCount <= 0) return;
// remove the newest entry (index 0) and shift everything up
for (int i = 0; i < entryCount - 1; i++) {
entries[i] = entries[i+1];
}
entryCount--;
SaveData();
CalculateStreak();
}
volatile int suspended = 0;
void HandleKey( int keycode, int bDown ) {
if (keycode == 4) { exit(0); } // back button
if (!bDown || !isTyping) return;
// enter key
if (keycode == 66 || keycode == 10 || keycode == 13) {
int y, m, d;
if (sscanf(inputBuffer, "%d %d %d", &y, &m, &d) == 3) {
AddEntry(y, m, d);
inputBuffer[0] = 0; inputLen = 0; isTyping = 0;
AndroidDisplayKeyboard(0);
}
return;
}
// backspace
if (keycode == 67 || keycode == 127 || keycode == 8) {
if (inputLen > 0) inputBuffer[--inputLen] = 0;
return;
}
// typing
char c = 0;
if (keycode >= '0' && keycode <= '9') c = keycode;
else if (keycode >= 7 && keycode <= 16) c = '0' + (keycode - 7);
else if (keycode == 62 || keycode == 32) c = ' ';
if (c != 0 && inputLen < 63) {
inputBuffer[inputLen++] = c; inputBuffer[inputLen] = 0;
}
}
void HandleButton( int x, int y, int button, int bDown ) {
short w, h;
CNFGGetDimensions(&w, &h);
float scale = GetScale(w, h);
if (bDown) {
lastTouchY = y;
startTouchY = y;
isTap = 1;
isDragging = 1;
} else {
isDragging = 0;
if (isTap) {
char streakStr[32];
sprintf(streakStr, "Streak: %d", streakCount);
int titleScale = GetTextScale(10, scale);
int charWidth = 6 * titleScale;
int textWidth = strlen(streakStr) * charWidth;
int textEndX = (w + textWidth) / 2;
// bigger touch targets to match bigger buttons
int btnY = 90 * scale;
int btnH = 120 * scale;
int btnX = textEndX + (20 * scale);
int btnW = 120 * scale;
// check lazy (+)
if (x > btnX && x < btnX + btnW && y > btnY && y < btnY + btnH) {
LazyEntry();
return;
}
// check undo (<)
int undoY = btnY + (130 * scale);
if (x > btnX && x < btnX + btnW && y > undoY && y < undoY + btnH) {
Undo();
return;
}
// if keyboard is open, checking taps anywhere outside input box closes it
if (isTyping) {
isTyping = 0;
AndroidDisplayKeyboard(0);
}
// if keyboard is closed, tapping add date opens it
else if (y > h - (150 * scale)) {
isTyping = 1;
AndroidDisplayKeyboard(1);
}
}
}
}
void HandleMotion( int x, int y, int mask ) {
if (isDragging) {
short w, h;
CNFGGetDimensions(&w, &h);
float scale = GetScale(w, h);
int dy = y - lastTouchY;
if (abs(y - startTouchY) > 10) isTap = 0;
// only scroll if not typing
if (!isTyping) {
scrollY += dy;
if (scrollY > 0) scrollY = 0;
int rowHeight = 80 * scale;
int maxScroll = -(entryCount * rowHeight);
if (maxScroll > 0) maxScroll = 0;
if (scrollY < maxScroll - 500) scrollY = maxScroll - 500;
}
lastTouchY = y;
}
}
int HandleDestroy() { return 0; }
void HandleSuspend() { suspended = 1; }
void HandleResume() { suspended = 0; }
int main( int argc, char ** argv ) {
CNFGSetupFullscreen( "Streak Tracker", 0 );
LoadData();
while(1) {
CNFGHandleInput();
if( suspended ) { usleep(50000); continue; }
CNFGBGColor = 0x000000FF;
CNFGClearFrame();
short w, h;
CNFGGetDimensions( &w, &h );
if (w == 0 || h == 0) { CNFGSwapBuffers(); continue; }
float scale = GetScale(w, h);
// draw streak count
CNFGColor(0xFFFFFFFF);
char streakStr[32];
sprintf(streakStr, "Streak: %d", streakCount);
CNFGSetLineWidth(GetTextScale(5, scale));
DrawTextCentered(streakStr, GetTextScale(10, scale), 100 * scale, w);
// draw header buttons
int titleScale = GetTextScale(10, scale);
int charWidth = 6 * titleScale;
int textWidth = strlen(streakStr) * charWidth;
int textEndX = (w + textWidth) / 2;
int btnScale = GetTextScale(20, scale);
// lazy (+)
CNFGColor(0x00FF00FF);
CNFGPenX = textEndX + (20 * scale);
CNFGPenY = 100 * scale;
CNFGDrawText("+", btnScale);
// undo (<)
CNFGColor(0xFF0000FF);
CNFGPenX = textEndX + (20 * scale);
CNFGPenY = 230 * scale;
CNFGDrawText("<", btnScale);
// draw scrollable list
CNFGColor(0xFFFFFFFF);
CNFGSetLineWidth(GetTextScale(2, scale));
int listStartY = (300 * scale) + scrollY;
int rowHeight = 80 * scale;
char dateStr[64];
for(int i = 0; i < entryCount; i++) {
struct tm* t = localtime(&entries[i].timestamp);
strftime(dateStr, sizeof(dateStr), "%A, %b %d %Y", t);
int itemY = listStartY + (i * rowHeight);
if (itemY < -50 || itemY > h) continue;
DrawTextCentered(dateStr, GetTextScale(5, scale), itemY, w);
}
// ui layer
if (isTyping) {
CNFGColor(0x000000CC);
CNFGTackRectangle(0, 0, w, h);
// avoid keyboard obstruction
int boxY = h / 5;
int boxH = 250 * scale;
CNFGColor(0x333333FF);
CNFGTackRectangle(20, boxY, w-20, boxY + boxH);
// instructions
CNFGColor(0xAAAAAAFF);
DrawTextCentered("Enter Date:", GetTextScale(4, scale), boxY + (30 * scale), w);
DrawTextCentered("YYYY MM DD", GetTextScale(3, scale), boxY + (80 * scale), w);
// actual input
CNFGColor(0x00FF00FF);
char prompt[100];
sprintf(prompt, "%s_", inputBuffer);
DrawTextCentered(prompt, GetTextScale(6, scale), boxY + (150 * scale), w);
} else {
// draw black backing to hide scrolling text
int footerHeight = 150 * scale;
CNFGColor(0x000000FF);
CNFGTackRectangle(0, h - footerHeight, w, h);
CNFGColor(0x333333FF);
CNFGTackRectangle(0, h - footerHeight, w, h);
CNFGColor(0xFFFFFFFF);
DrawTextCentered("+ Add Date", GetTextScale(5, scale), h - (110 * scale), w);
}
CNFGSwapBuffers();
}
return 0;
}