Use Kryon Android viewport defaults
This commit is contained in:
1 parent
f6ef1ea93d
commit
c02bda07c7
22 files changed
+362
-420
No files matched your search
@@ -326,7 +326,7 @@ SETTINGS_KEYS_TEST := $(TEST_BIN_DIR)/settings_keys_test
|
||||
TESTS := $(STORAGE_IMPORT_TEST) $(LOCALE_KEYS_TEST) $(SYNC_URL_TEST) $(SYNC_ACCOUNT_TEST) $(SYNC_REVIEW_TEST) $(FONT_LOCALE_TEST) $(FONT_GLYPH_COVERAGE_TEST) $(APP_BOTTOM_NAV_TEST) $(HABIT_MODEL_TEST) $(HABIT_SESSIONS_TEST) $(BREATH_TIMING_TEST) $(BREAK_ENGINE_TEST) $(ACTIVITY_MONITOR_TEST) $(SETTINGS_KEYS_TEST)
|
||||
RUNTIME_ASSET_CFLAGS := -DHAS_LIBCURL=1 $(KRYON_CURL_CFLAGS)
|
||||
RUNTIME_ASSET_LDLIBS := $(KRYON_CURL_LDLIBS)
|
||||
STORAGE_CORE_SRCS := src/storage/storage.c src/storage/storage_habits.c src/storage/storage_habit_materialize.c src/storage/storage_habit_sync.c
|
||||
STORAGE_CORE_SRCS := src/storage/storage.c src/storage/storage_json_builder.c src/storage/storage_habits.c src/storage/storage_habit_materialize.c src/storage/storage_habit_sync.c
|
||||
|
||||
APP_SRCS := \
|
||||
src/main.c \
|
||||
@@ -1600,7 +1600,17 @@ android-copy-bundle: | $(ANDROID_BUILD_DIR)
|
||||
cp "$(ANDROID_BUILD_DIR)/$(APP_NAME)-$(APP_VERSION)-gplay.apk" "$(ANDROID_BUILD_DIR)/$(APP_NAME)-latest.apk"; \
|
||||
fi
|
||||
|
||||
android-install: android-debug
|
||||
android-install: android-copy-assets android-local-properties
|
||||
@set -eu; \
|
||||
adb_cmd='$(ADB)'; \
|
||||
abi=$$($$adb_cmd shell getprop ro.product.cpu.abi 2>/dev/null | tr -d '\r' | head -n 1); \
|
||||
if [ -z "$$abi" ]; then \
|
||||
echo "Could not read Android device ABI. Connect one device, or run with ADB='adb -s SERIAL'."; \
|
||||
exit 1; \
|
||||
fi; \
|
||||
echo "Android target ABI: $$abi"; \
|
||||
$(ANDROID_GRADLE_ENV) $(GRADLE) -p droid assembleDebug -Pinbe.onlyAbi="$$abi" $(ANDROID_GRADLE_ARGS)
|
||||
$(MAKE) android-copy-debug-apks
|
||||
ADB='$(ADB)' sh scripts/android-install-apk.sh \
|
||||
"$(ANDROID_APP_ID)" "$(ANDROID_ACTIVITY)" \
|
||||
droid/app/build/outputs/apk debug
|
||||
|
||||
@@ -12,6 +12,7 @@ if (hasGplayFirebaseConfig) {
|
||||
}
|
||||
}
|
||||
}
|
||||
def inbeOnlyAbi = (project.findProperty('inbe.onlyAbi') ?: '').toString()
|
||||
|
||||
android {
|
||||
namespace 'xyz.waozi.inbe'
|
||||
@@ -96,8 +97,13 @@ android {
|
||||
abi {
|
||||
enable true
|
||||
reset()
|
||||
include 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
|
||||
universalApk true
|
||||
if (inbeOnlyAbi) {
|
||||
include inbeOnlyAbi
|
||||
universalApk false
|
||||
} else {
|
||||
include 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
|
||||
universalApk true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -221,6 +221,7 @@ add_library(main SHARED
|
||||
"${SRC_DIR}/main.c"
|
||||
${APP_C_SOURCES}
|
||||
"${SRC_DIR}/storage/storage.c"
|
||||
"${SRC_DIR}/storage/storage_json_builder.c"
|
||||
"${SRC_DIR}/storage/storage_habits.c"
|
||||
"${SRC_DIR}/storage/storage_habit_materialize.c"
|
||||
"${SRC_DIR}/storage/storage_habit_sync.c"
|
||||
|
||||
@@ -45,6 +45,10 @@ ABI=$(adb_run shell getprop ro.product.cpu.abi 2>&1 | tr -d '\r' | head -n 1) ||
|
||||
|
||||
APK=""
|
||||
for candidate in \
|
||||
"$APK_DIR"/"$VARIANT"/app-*-"$ABI"-"$VARIANT".apk \
|
||||
"$APK_DIR"/"$VARIANT"/app-"$ABI"-"$VARIANT".apk \
|
||||
"$APK_DIR"/"$VARIANT"/app-*-"$VARIANT".apk \
|
||||
"$APK_DIR"/"$VARIANT"/app-"$VARIANT".apk \
|
||||
"$APK_DIR"/*/"$VARIANT"/app-*-"$ABI"-"$VARIANT".apk \
|
||||
"$APK_DIR"/app-*-"$ABI"-"$VARIANT".apk \
|
||||
"$APK_DIR"/*/"$VARIANT"/app-"$ABI"-"$VARIANT".apk \
|
||||
@@ -57,7 +61,9 @@ for candidate in \
|
||||
fi
|
||||
done
|
||||
if [ ! -f "$APK" ]; then
|
||||
echo "Expected ABI APK below: $APK_DIR/$VARIANT/app-*-$ABI-$VARIANT.apk" >&2
|
||||
echo "Expected ABI APK below: $APK_DIR/*/$VARIANT/app-*-$ABI-$VARIANT.apk" >&2
|
||||
echo "Expected fallback below: $APK_DIR/$VARIANT/app-*-$VARIANT.apk" >&2
|
||||
echo "Expected fallback below: $APK_DIR/*/$VARIANT/app-*-$VARIANT.apk" >&2
|
||||
echo "Available APKs:" >&2
|
||||
find "$APK_DIR" -type f -name '*.apk' -print >&2 2>/dev/null || true
|
||||
|
||||
+2
-9
@@ -778,20 +778,13 @@ app_toolbar_height(void)
|
||||
int
|
||||
app_content_top_reserved(const InbeApp *app)
|
||||
{
|
||||
int system_top = 0;
|
||||
TabBarProps tabs;
|
||||
|
||||
#if ANDROID_BUILD
|
||||
// Include system status bar and camera cutout on Android
|
||||
extern int android_get_system_top_reserved(void);
|
||||
system_top = android_get_system_top_reserved();
|
||||
#endif
|
||||
|
||||
if(app != NULL && app->inbe.screen == InbeScreenStart) {
|
||||
memset(&tabs, 0, sizeof(tabs));
|
||||
return system_top + UIGetNodeHeight(UINodeTabBar(tabs));
|
||||
return UIGetNodeHeight(UINodeTabBar(tabs));
|
||||
}
|
||||
return system_top + app_toolbar_height();
|
||||
return app_toolbar_height();
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -10,8 +10,6 @@ int app_current_nav_route(const InbeApp *app);
|
||||
int app_page_height(const InbeApp *app, int full_height);
|
||||
int app_content_bottom_reserved(const InbeApp *app);
|
||||
int app_fullscreen_bottom_reserved(const InbeApp *app);
|
||||
void app_set_android_bottom_nav_height(int height);
|
||||
int app_android_bottom_nav_height(void);
|
||||
void app_draw_bottom_nav(InbeApp *app);
|
||||
void app_draw_nav_sidebar(InbeApp *app);
|
||||
int app_draw_customize_nav_page(InbeApp *app);
|
||||
|
||||
+2
-14
@@ -31,8 +31,6 @@ AppSidebarSettingsItem :: struct {
|
||||
tab: int
|
||||
}
|
||||
|
||||
static android_bottom_nav_height: int = 0
|
||||
|
||||
app_nav_sidebar_separator :: () -> Color #private {
|
||||
return DarkenUIColor(GetThemeSurface(), 16)
|
||||
}
|
||||
@@ -212,14 +210,6 @@ app_reconcile_bottom_nav_selection :: (app: InbeApp*) #private {
|
||||
}
|
||||
}
|
||||
|
||||
app_set_android_bottom_nav_height :: (height: int) {
|
||||
android_bottom_nav_height = height > 0 ? height : 0
|
||||
}
|
||||
|
||||
app_android_bottom_nav_height :: () -> int {
|
||||
return android_bottom_nav_height
|
||||
}
|
||||
|
||||
app_open_main_tab :: (app: InbeApp*, main_tab: int, persist: int) #private {
|
||||
if app == NULL {
|
||||
return
|
||||
@@ -414,10 +404,8 @@ app_current_nav_route :: (app: const InbeApp*) -> int {
|
||||
}
|
||||
|
||||
app_fullscreen_bottom_reserved :: (app: const InbeApp*) -> int {
|
||||
if app == NULL {
|
||||
return 0
|
||||
}
|
||||
return app_android_bottom_nav_height()
|
||||
unused app
|
||||
return 0
|
||||
}
|
||||
|
||||
app_sidebar_button :: (app: InbeApp*, x: int, y: int*, w: int,
|
||||
|
||||
+22
-71
@@ -350,70 +350,31 @@ windows_close_logger(void)
|
||||
#endif
|
||||
|
||||
#if ANDROID_BUILD
|
||||
static AndroidInsets insets;
|
||||
|
||||
typedef struct AndroidViewport {
|
||||
int x;
|
||||
int y;
|
||||
int width;
|
||||
int height;
|
||||
int top;
|
||||
int bottom;
|
||||
int left;
|
||||
int right;
|
||||
} AndroidViewport;
|
||||
|
||||
static int
|
||||
android_clamp_content_size(int size, int leading_inset, int trailing_inset)
|
||||
android_viewport_equal(AndroidViewport a, AndroidViewport b)
|
||||
{
|
||||
int content_size = size - leading_inset - trailing_inset;
|
||||
|
||||
if(content_size <= 0)
|
||||
return size;
|
||||
|
||||
return content_size;
|
||||
return a.x == b.x && a.y == b.y &&
|
||||
a.width == b.width && a.height == b.height &&
|
||||
a.insets.left == b.insets.left &&
|
||||
a.insets.top == b.insets.top &&
|
||||
a.insets.right == b.insets.right &&
|
||||
a.insets.bottom == b.insets.bottom &&
|
||||
a.ready == b.ready;
|
||||
}
|
||||
|
||||
static AndroidViewport
|
||||
android_resolve_viewport(int width, int height, AndroidInsets value)
|
||||
static void
|
||||
android_log_viewport_if_changed(int width, int height, AndroidViewport viewport)
|
||||
{
|
||||
static AndroidViewport last_logged = {-1, -1, -1, -1, -1, -1, -1, -1};
|
||||
AndroidViewport viewport;
|
||||
KrySafeArea safe_area = GetAndroidSafeArea();
|
||||
static AndroidViewport last_logged = {-1, -1, -1, -1, {-1, -1, -1, -1}, -1};
|
||||
|
||||
(void)value;
|
||||
viewport.left = safe_area.left;
|
||||
viewport.top = safe_area.top;
|
||||
viewport.right = safe_area.right;
|
||||
viewport.bottom = safe_area.bottom;
|
||||
|
||||
viewport.x = viewport.left;
|
||||
viewport.y = viewport.top;
|
||||
viewport.width = android_clamp_content_size(width, viewport.left, viewport.right);
|
||||
viewport.height = android_clamp_content_size(height, viewport.top, viewport.bottom);
|
||||
|
||||
if(viewport.width <= 0) {
|
||||
viewport.x = 0;
|
||||
viewport.width = width;
|
||||
}
|
||||
if(viewport.height <= 0) {
|
||||
viewport.y = 0;
|
||||
viewport.height = height;
|
||||
}
|
||||
|
||||
if(viewport.x != last_logged.x || viewport.y != last_logged.y ||
|
||||
viewport.width != last_logged.width || viewport.height != last_logged.height ||
|
||||
viewport.top != last_logged.top || viewport.bottom != last_logged.bottom ||
|
||||
viewport.left != last_logged.left || viewport.right != last_logged.right) {
|
||||
TraceLog(LOG_INFO,
|
||||
"ANDROID_VIEWPORT: surface=%dx%d viewport=%d,%d %dx%d insets l=%d t=%d r=%d b=%d",
|
||||
width, height,
|
||||
viewport.x, viewport.y, viewport.width, viewport.height,
|
||||
viewport.left, viewport.top, viewport.right, viewport.bottom);
|
||||
last_logged = viewport;
|
||||
}
|
||||
|
||||
return viewport;
|
||||
if(android_viewport_equal(viewport, last_logged))
|
||||
return;
|
||||
TraceLog(LOG_INFO,
|
||||
"ANDROID_VIEWPORT: surface=%dx%d viewport=%d,%d %dx%d insets l=%d t=%d r=%d b=%d",
|
||||
width, height, viewport.x, viewport.y, viewport.width,
|
||||
viewport.height, viewport.insets.left, viewport.insets.top,
|
||||
viewport.insets.right, viewport.insets.bottom);
|
||||
last_logged = viewport;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -461,22 +422,12 @@ frame(void)
|
||||
|
||||
#if ANDROID_BUILD
|
||||
AndroidViewport viewport;
|
||||
static AndroidViewport previous_viewport = {-1, -1, -1, -1, -1, -1, -1, -1};
|
||||
|
||||
android_insets_get(&insets);
|
||||
if (!android_insets_is_initialized()) {
|
||||
if(!SyncAndroidViewport(&viewport)) {
|
||||
return;
|
||||
}
|
||||
|
||||
viewport = android_resolve_viewport(width, height, insets);
|
||||
app_set_android_bottom_nav_height(viewport.bottom);
|
||||
if(viewport.x != previous_viewport.x || viewport.y != previous_viewport.y ||
|
||||
viewport.width != previous_viewport.width || viewport.height != previous_viewport.height ||
|
||||
viewport.top != previous_viewport.top || viewport.bottom != previous_viewport.bottom ||
|
||||
viewport.left != previous_viewport.left || viewport.right != previous_viewport.right) {
|
||||
InvalidateUIDPI();
|
||||
previous_viewport = viewport;
|
||||
}
|
||||
GetAndroidSurfaceSize(&width, &height);
|
||||
android_log_viewport_if_changed(width, height, viewport);
|
||||
|
||||
BeginDrawing();
|
||||
ClearBackground(BLACK);
|
||||
|
||||
@@ -11,7 +11,7 @@ ANDROID :: ANDROID_BUILD
|
||||
#import "kryon.h" #private
|
||||
#import <android/log.h>
|
||||
|
||||
LOG_TAG :: #define "INBE_HEALTH"
|
||||
static health_log_tag: const char* = "INBE_HEALTH"
|
||||
|
||||
# Writes the session list (start,end,activity per line) and asks the
|
||||
# activity to run the Health Connect permission flow and insert the
|
||||
@@ -22,7 +22,7 @@ ANDROID :: ANDROID_BUILD
|
||||
snprintf(path, sizeof(path), "%s/health-connect-export.csv",
|
||||
data_root())
|
||||
if !storage_export_health_connect_csv(path) {
|
||||
__android_log_write(ANDROID_LOG_WARN, LOG_TAG,
|
||||
__android_log_write(ANDROID_LOG_WARN, health_log_tag,
|
||||
"nothing to export")
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -302,15 +302,3 @@ void android_insets_init(void) {
|
||||
pending_donation_reminder = 0;
|
||||
pthread_mutex_unlock(&insets_mutex);
|
||||
}
|
||||
|
||||
void android_insets_get(AndroidInsets *out) {
|
||||
GetAndroidWindowInsets(out);
|
||||
}
|
||||
|
||||
int android_insets_is_initialized(void) {
|
||||
return GetAndroidWindowInsets(NULL);
|
||||
}
|
||||
|
||||
int android_get_system_top_reserved(void) {
|
||||
return GetAndroidSafeArea().top;
|
||||
}
|
||||
@@ -8,22 +8,12 @@
|
||||
#if ANDROID_BUILD
|
||||
#include <jni.h>
|
||||
|
||||
typedef AndroidWindowInsets AndroidInsets;
|
||||
|
||||
void android_insets_init(void);
|
||||
void android_insets_get(AndroidInsets *out);
|
||||
int android_insets_is_initialized(void);
|
||||
int android_get_system_top_reserved(void);
|
||||
int android_take_pending_practice_start(void);
|
||||
int android_take_pending_donation_reminder(void);
|
||||
void android_wakelock_set_activity(JNIEnv *env, jobject activity);
|
||||
#else
|
||||
typedef AndroidWindowInsets AndroidInsets;
|
||||
|
||||
static inline int android_get_system_top_reserved(void) { return 0; }
|
||||
static inline void android_insets_init(void) {}
|
||||
static inline void android_insets_get(AndroidInsets *out) { if(out) memset(out, 0, sizeof(AndroidInsets)); }
|
||||
static inline int android_insets_is_initialized(void) { return 0; }
|
||||
static inline int android_take_pending_practice_start(void) { return -1; }
|
||||
static inline int android_take_pending_donation_reminder(void) { return 0; }
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ ANDROID :: ANDROID_BUILD
|
||||
session.jvm = app->activity->vm
|
||||
session.activity = app->activity->clazz
|
||||
|
||||
if (*session.jvm)->GetEnv(session.jvm, &session.env, JNI_VERSION_1_6) != JNI_OK {
|
||||
if (*session.jvm)->GetEnv(session.jvm, (void**)&session.env, JNI_VERSION_1_6) != JNI_OK {
|
||||
if (*session.jvm)->AttachCurrentThread(session.jvm, &session.env, nil) != JNI_OK ||
|
||||
session.env == nil {
|
||||
session.jvm = nil
|
||||
|
||||
@@ -8,7 +8,7 @@ ANDROID :: ANDROID_BUILD
|
||||
#import "kryon.h" #private
|
||||
#import <android/log.h>
|
||||
|
||||
LOG_TAG :: #define "INBE_PUSH"
|
||||
static push_log_tag: const char* = "INBE_PUSH"
|
||||
|
||||
static push_registered_cache: int = -1
|
||||
static push_registered_frames: int = 0
|
||||
@@ -36,7 +36,7 @@ ANDROID :: ANDROID_BUILD
|
||||
array = (jobjectArray)(*session.env)->CallObjectMethod(
|
||||
session.env, session.activity, method)
|
||||
} else {
|
||||
__android_log_write(ANDROID_LOG_ERROR, LOG_TAG,
|
||||
__android_log_write(ANDROID_LOG_ERROR, push_log_tag,
|
||||
"getUnifiedPushDistributors not found")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ ANDROID :: ANDROID_BUILD
|
||||
#import <android/log.h>
|
||||
#import <unistd.h>
|
||||
|
||||
LOG_TAG :: #define "INBE_TIMER"
|
||||
static timer_log_tag: const char* = "INBE_TIMER"
|
||||
TARGET_FPS :: #define 60
|
||||
FRAME_TIME_US :: #define (1000000 / TARGET_FPS)
|
||||
|
||||
@@ -53,7 +53,7 @@ ANDROID :: ANDROID_BUILD
|
||||
|
||||
if !timer_running {
|
||||
pthread_mutex_unlock(&timer_mutex)
|
||||
__android_log_write(ANDROID_LOG_INFO, LOG_TAG,
|
||||
__android_log_write(ANDROID_LOG_INFO, timer_log_tag,
|
||||
"Timer thread: stopping")
|
||||
break
|
||||
}
|
||||
@@ -73,7 +73,7 @@ ANDROID :: ANDROID_BUILD
|
||||
usleep(FRAME_TIME_US)
|
||||
} else {
|
||||
if frame_count > 0 {
|
||||
__android_log_write(ANDROID_LOG_INFO, LOG_TAG,
|
||||
__android_log_write(ANDROID_LOG_INFO, timer_log_tag,
|
||||
"Timer: Paused (inactive)")
|
||||
frame_count = 0
|
||||
}
|
||||
@@ -82,7 +82,7 @@ ANDROID :: ANDROID_BUILD
|
||||
}
|
||||
}
|
||||
|
||||
__android_log_write(ANDROID_LOG_INFO, LOG_TAG, "Timer thread stopped")
|
||||
__android_log_write(ANDROID_LOG_INFO, timer_log_tag, "Timer thread stopped")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ ANDROID :: ANDROID_BUILD
|
||||
|
||||
result := pthread_create(&timer_thread, nil, timer_thread_func, nil)
|
||||
if result != 0 {
|
||||
__android_log_write(ANDROID_LOG_ERROR, LOG_TAG,
|
||||
__android_log_write(ANDROID_LOG_ERROR, timer_log_tag,
|
||||
"Failed to create timer thread")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ ANDROID :: ANDROID_BUILD
|
||||
#import <android/log.h>
|
||||
#import <jni.h>
|
||||
|
||||
LOG_TAG :: #define "INBE_WAKE"
|
||||
static wakelock_log_tag: const char* = "INBE_WAKE"
|
||||
|
||||
g_jvm :: JavaVM* = nil #global
|
||||
g_activity :: jobject = nil #global
|
||||
@@ -73,7 +73,7 @@ ANDROID :: ANDROID_BUILD
|
||||
|
||||
pthread_mutex_lock(&wakelock_mutex)
|
||||
if !wakelock_get_env(&env) {
|
||||
__android_log_write(ANDROID_LOG_ERROR, LOG_TAG,
|
||||
__android_log_write(ANDROID_LOG_ERROR, wakelock_log_tag,
|
||||
"ACQUIRE: JNI not initialized - skipping wake lock")
|
||||
pthread_mutex_unlock(&wakelock_mutex)
|
||||
return
|
||||
@@ -114,7 +114,7 @@ ANDROID :: ANDROID_BUILD
|
||||
|
||||
pthread_mutex_lock(&wakelock_mutex)
|
||||
if !wakelock_get_env(&env) {
|
||||
__android_log_write(ANDROID_LOG_ERROR, LOG_TAG,
|
||||
__android_log_write(ANDROID_LOG_ERROR, wakelock_log_tag,
|
||||
"RELEASE: JNI not initialized - skipping wake lock")
|
||||
pthread_mutex_unlock(&wakelock_mutex)
|
||||
return
|
||||
@@ -130,7 +130,7 @@ ANDROID :: ANDROID_BUILD
|
||||
|
||||
pthread_mutex_lock(&wakelock_mutex)
|
||||
if !wakelock_get_env(&env) {
|
||||
__android_log_write(ANDROID_LOG_ERROR, LOG_TAG,
|
||||
__android_log_write(ANDROID_LOG_ERROR, wakelock_log_tag,
|
||||
"KEEP_SCREEN_ON: JNI not initialized - skipping")
|
||||
pthread_mutex_unlock(&wakelock_mutex)
|
||||
return
|
||||
@@ -146,7 +146,7 @@ ANDROID :: ANDROID_BUILD
|
||||
|
||||
pthread_mutex_lock(&wakelock_mutex)
|
||||
if !wakelock_get_env(&env) {
|
||||
__android_log_write(ANDROID_LOG_ERROR, LOG_TAG,
|
||||
__android_log_write(ANDROID_LOG_ERROR, wakelock_log_tag,
|
||||
"ALLOW_SCREEN_OFF: JNI not initialized - skipping")
|
||||
pthread_mutex_unlock(&wakelock_mutex)
|
||||
return
|
||||
|
||||
@@ -208,6 +208,7 @@ update_retention_marker :: (app: InbeApp*, phase_changed: int) #private {
|
||||
session_update_notification :: (app: InbeApp*) #private {
|
||||
text: [96] char = ""
|
||||
count: int
|
||||
notification_pause: int
|
||||
|
||||
if app == nil {
|
||||
return
|
||||
@@ -228,7 +229,7 @@ session_update_notification :: (app: InbeApp*) #private {
|
||||
FormatLocaleText(text, sizeof(text), "notification_breath_in_seconds", count)
|
||||
break
|
||||
case InbePhaseStarting:
|
||||
notification_pause: int = inbe_effective_pause_seconds(&app->inbe)
|
||||
notification_pause = inbe_effective_pause_seconds(&app->inbe)
|
||||
FormatLocaleText(text, sizeof(text), "notification_starting_seconds",
|
||||
notification_pause > 0
|
||||
? notification_pause - app->inbe.sectick / 60
|
||||
|
||||
+120
-262
@@ -1,12 +1,12 @@
|
||||
#include "storage.h"
|
||||
|
||||
#include "db.h"
|
||||
#include "storage_json_builder.h"
|
||||
|
||||
#include "ksync_account.h"
|
||||
#include "ksync_crypto.h"
|
||||
#include "kryon.h"
|
||||
#include <sqlite3.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -128,148 +128,6 @@ storage_make_uuid(char out[37])
|
||||
make_client_uuid(out);
|
||||
}
|
||||
|
||||
typedef struct JsonBuilder {
|
||||
char *data;
|
||||
size_t len;
|
||||
size_t cap;
|
||||
int ok;
|
||||
} JsonBuilder;
|
||||
|
||||
static int
|
||||
json_reserve(JsonBuilder *json, size_t extra)
|
||||
{
|
||||
char *next;
|
||||
size_t next_cap;
|
||||
|
||||
if(json == NULL || !json->ok)
|
||||
return 0;
|
||||
if(extra <= json->cap - json->len)
|
||||
return 1;
|
||||
next_cap = json->cap > 0 ? json->cap : 1024;
|
||||
while(extra > next_cap - json->len)
|
||||
next_cap *= 2;
|
||||
next = (char *)realloc(json->data, next_cap);
|
||||
if(next == NULL) {
|
||||
json->ok = 0;
|
||||
return 0;
|
||||
}
|
||||
json->data = next;
|
||||
json->cap = next_cap;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
json_append_len(JsonBuilder *json, const char *text, size_t len)
|
||||
{
|
||||
if(text == NULL || len == 0)
|
||||
return;
|
||||
if(!json_reserve(json, len + 1))
|
||||
return;
|
||||
memcpy(json->data + json->len, text, len);
|
||||
json->len += len;
|
||||
json->data[json->len] = '\0';
|
||||
}
|
||||
|
||||
static void
|
||||
json_append(JsonBuilder *json, const char *text)
|
||||
{
|
||||
if(text != NULL)
|
||||
json_append_len(json, text, strlen(text));
|
||||
}
|
||||
|
||||
static void
|
||||
json_appendf(JsonBuilder *json, const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_list copy;
|
||||
int needed;
|
||||
|
||||
if(json == NULL || fmt == NULL || !json->ok)
|
||||
return;
|
||||
va_start(args, fmt);
|
||||
va_copy(copy, args);
|
||||
needed = vsnprintf(NULL, 0, fmt, copy);
|
||||
va_end(copy);
|
||||
if(needed < 0) {
|
||||
json->ok = 0;
|
||||
va_end(args);
|
||||
return;
|
||||
}
|
||||
if(json_reserve(json, (size_t)needed + 1)) {
|
||||
vsnprintf(json->data + json->len, json->cap - json->len, fmt, args);
|
||||
json->len += (size_t)needed;
|
||||
}
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
static void
|
||||
json_append_string(JsonBuilder *json, const char *text)
|
||||
{
|
||||
const unsigned char *p = (const unsigned char *)(text != NULL ? text : "");
|
||||
|
||||
json_append(json, "\"");
|
||||
while(*p != '\0') {
|
||||
char escaped[8];
|
||||
if(*p == '"' || *p == '\\') {
|
||||
escaped[0] = '\\';
|
||||
escaped[1] = (char)*p;
|
||||
json_append_len(json, escaped, 2);
|
||||
} else if(*p == '\b') {
|
||||
json_append(json, "\\b");
|
||||
} else if(*p == '\f') {
|
||||
json_append(json, "\\f");
|
||||
} else if(*p == '\n') {
|
||||
json_append(json, "\\n");
|
||||
} else if(*p == '\r') {
|
||||
json_append(json, "\\r");
|
||||
} else if(*p == '\t') {
|
||||
json_append(json, "\\t");
|
||||
} else if(*p < 0x20) {
|
||||
snprintf(escaped, sizeof(escaped), "\\u%04x", *p);
|
||||
json_append(json, escaped);
|
||||
} else {
|
||||
json_append_len(json, (const char *)p, 1);
|
||||
}
|
||||
p++;
|
||||
}
|
||||
json_append(json, "\"");
|
||||
}
|
||||
|
||||
static void
|
||||
json_append_key_string(JsonBuilder *json, const char *key, const char *value)
|
||||
{
|
||||
json_append_string(json, key);
|
||||
json_append(json, ":");
|
||||
json_append_string(json, value);
|
||||
}
|
||||
|
||||
static void
|
||||
json_free(JsonBuilder *json)
|
||||
{
|
||||
if(json == NULL)
|
||||
return;
|
||||
free(json->data);
|
||||
memset(json, 0, sizeof(*json));
|
||||
}
|
||||
|
||||
static void
|
||||
json_append_epoch(JsonBuilder *json, long long seconds)
|
||||
{
|
||||
char formatted[32];
|
||||
struct tm tm_value;
|
||||
time_t value = (time_t)seconds;
|
||||
|
||||
if(seconds <= 0)
|
||||
value = time(NULL);
|
||||
#if defined(_WIN32)
|
||||
gmtime_s(&tm_value, &value);
|
||||
#else
|
||||
gmtime_r(&value, &tm_value);
|
||||
#endif
|
||||
strftime(formatted, sizeof(formatted), "%Y-%m-%dT%H:%M:%SZ", &tm_value);
|
||||
json_append_string(json, formatted);
|
||||
}
|
||||
|
||||
void
|
||||
storage_schedule_persist(void)
|
||||
{
|
||||
@@ -736,48 +594,48 @@ storage_set_sync_server_connected(int connected)
|
||||
}
|
||||
|
||||
static void
|
||||
storage_append_habit_row_json(JsonBuilder *json, sqlite3_stmt *stmt)
|
||||
storage_append_habit_row_json(StorageJsonBuilder *json, sqlite3_stmt *stmt)
|
||||
{
|
||||
json_append(json, "{");
|
||||
json_append_key_string(json, "id", (const char *)sqlite3_column_text(stmt, 0));
|
||||
json_append(json, ",");
|
||||
json_append_key_string(json, "name", (const char *)sqlite3_column_text(stmt, 1));
|
||||
json_appendf(json, ",\"color_r\":%d,\"color_g\":%d,\"color_b\":%d",
|
||||
storage_json_builder_append(json, "{");
|
||||
storage_json_builder_append_key_string(json, "id", (const char *)sqlite3_column_text(stmt, 0));
|
||||
storage_json_builder_append(json, ",");
|
||||
storage_json_builder_append_key_string(json, "name", (const char *)sqlite3_column_text(stmt, 1));
|
||||
storage_json_builder_appendf(json, ",\"color_r\":%d,\"color_g\":%d,\"color_b\":%d",
|
||||
sqlite3_column_int(stmt, 2), sqlite3_column_int(stmt, 3),
|
||||
sqlite3_column_int(stmt, 4));
|
||||
json_appendf(json,
|
||||
storage_json_builder_appendf(json,
|
||||
",\"sync_mode\":%d,\"sync_activity\":%d,\"counter_enabled\":"
|
||||
"%d,\"sort_order\":%d,\"deleted_at\":%lld",
|
||||
sqlite3_column_int(stmt, 5), sqlite3_column_int(stmt, 6),
|
||||
sqlite3_column_int(stmt, 7) != 0 ? 1 : 0, sqlite3_column_int(stmt, 8),
|
||||
sqlite3_column_int64(stmt, 9));
|
||||
json_append(json, ",\"updated_at\":");
|
||||
json_append_epoch(json, sqlite3_column_int64(stmt, 10));
|
||||
json_appendf(json, ",\"weekdays\":%d,\"reminder_hour\":%d",
|
||||
storage_json_builder_append(json, ",\"updated_at\":");
|
||||
storage_json_builder_append_epoch(json, sqlite3_column_int64(stmt, 10));
|
||||
storage_json_builder_appendf(json, ",\"weekdays\":%d,\"reminder_hour\":%d",
|
||||
sqlite3_column_int(stmt, 11), sqlite3_column_int(stmt, 12));
|
||||
json_append(json, "}");
|
||||
storage_json_builder_append(json, "}");
|
||||
}
|
||||
|
||||
static void
|
||||
storage_append_habit_day_row_json(JsonBuilder *json, sqlite3_stmt *stmt)
|
||||
storage_append_habit_day_row_json(StorageJsonBuilder *json, sqlite3_stmt *stmt)
|
||||
{
|
||||
json_append(json, "{");
|
||||
json_append_key_string(json, "habit_id", (const char *)sqlite3_column_text(stmt, 0));
|
||||
json_appendf(json, ",\"local_date\":%d,\"completed\":%s,\"count\":%d,\"updated_at\":",
|
||||
storage_json_builder_append(json, "{");
|
||||
storage_json_builder_append_key_string(json, "habit_id", (const char *)sqlite3_column_text(stmt, 0));
|
||||
storage_json_builder_appendf(json, ",\"local_date\":%d,\"completed\":%s,\"count\":%d,\"updated_at\":",
|
||||
sqlite3_column_int(stmt, 1),
|
||||
sqlite3_column_int(stmt, 2) != 0 ? "true" : "false",
|
||||
sqlite3_column_int(stmt, 3));
|
||||
json_append_epoch(json, sqlite3_column_int64(stmt, 4));
|
||||
json_append(json, "}");
|
||||
storage_json_builder_append_epoch(json, sqlite3_column_int64(stmt, 4));
|
||||
storage_json_builder_append(json, "}");
|
||||
}
|
||||
|
||||
static void
|
||||
storage_append_session_rounds_json(JsonBuilder *json, const char *session_id)
|
||||
storage_append_session_rounds_json(StorageJsonBuilder *json, const char *session_id)
|
||||
{
|
||||
sqlite3_stmt *stmt = NULL;
|
||||
int first = 1;
|
||||
|
||||
json_append(json, "\"rounds\":[");
|
||||
storage_json_builder_append(json, "\"rounds\":[");
|
||||
if(g_storage.db != NULL && session_id != NULL &&
|
||||
sqlite3_prepare_v2(g_storage.db,
|
||||
"SELECT round_index,seconds FROM session_rounds "
|
||||
@@ -787,53 +645,53 @@ storage_append_session_rounds_json(JsonBuilder *json, const char *session_id)
|
||||
while(sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
int seconds = sqlite3_column_int(stmt, 1);
|
||||
if(!first)
|
||||
json_append(json, ",");
|
||||
storage_json_builder_append(json, ",");
|
||||
first = 0;
|
||||
json_appendf(json, "{\"round_index\":%d,\"breaths\":0,\"hold_seconds\":%d}",
|
||||
storage_json_builder_appendf(json, "{\"round_index\":%d,\"breaths\":0,\"hold_seconds\":%d}",
|
||||
sqlite3_column_int(stmt, 0), seconds);
|
||||
}
|
||||
}
|
||||
if(stmt != NULL)
|
||||
sqlite3_finalize(stmt);
|
||||
json_append(json, "]");
|
||||
storage_json_builder_append(json, "]");
|
||||
}
|
||||
|
||||
static void
|
||||
storage_append_session_row_json(JsonBuilder *json, sqlite3_stmt *stmt)
|
||||
storage_append_session_row_json(StorageJsonBuilder *json, sqlite3_stmt *stmt)
|
||||
{
|
||||
const char *id = (const char *)sqlite3_column_text(stmt, 0);
|
||||
long long started_at = sqlite3_column_int64(stmt, 1);
|
||||
long long updated_at = sqlite3_column_int64(stmt, 14);
|
||||
|
||||
json_append(json, "{");
|
||||
json_append_key_string(json, "id", id);
|
||||
json_append(json, ",\"started_at\":");
|
||||
json_append_epoch(json, started_at);
|
||||
json_appendf(json, ",\"local_date\":%d,\"topic\":\"%d\",\"activity\":%d,",
|
||||
storage_json_builder_append(json, "{");
|
||||
storage_json_builder_append_key_string(json, "id", id);
|
||||
storage_json_builder_append(json, ",\"started_at\":");
|
||||
storage_json_builder_append_epoch(json, started_at);
|
||||
storage_json_builder_appendf(json, ",\"local_date\":%d,\"topic\":\"%d\",\"activity\":%d,",
|
||||
sqlite3_column_int(stmt, 2), sqlite3_column_int(stmt, 3),
|
||||
sqlite3_column_int(stmt, 4));
|
||||
json_append_key_string(json, "source", (const char *)sqlite3_column_text(stmt, 5));
|
||||
json_append(json, ",");
|
||||
json_append_key_string(json, "rounds_hash", (const char *)sqlite3_column_text(stmt, 6));
|
||||
json_appendf(json,
|
||||
storage_json_builder_append_key_string(json, "source", (const char *)sqlite3_column_text(stmt, 5));
|
||||
storage_json_builder_append(json, ",");
|
||||
storage_json_builder_append_key_string(json, "rounds_hash", (const char *)sqlite3_column_text(stmt, 6));
|
||||
storage_json_builder_appendf(json,
|
||||
",\"mood_before\":%d,\"mood_after\":%d,\"energy\":%d,"
|
||||
"\"stress\":%d,",
|
||||
sqlite3_column_int(stmt, 7), sqlite3_column_int(stmt, 8),
|
||||
sqlite3_column_int(stmt, 9), sqlite3_column_int(stmt, 10));
|
||||
json_append_key_string(json, "note", (const char *)sqlite3_column_text(stmt, 11));
|
||||
json_append(json, ",");
|
||||
json_append_key_string(json, "tags", (const char *)sqlite3_column_text(stmt, 12));
|
||||
json_appendf(json, ",\"deleted_at\":%lld,\"updated_at\":", sqlite3_column_int64(stmt, 13));
|
||||
json_append_epoch(json, updated_at > 0 ? updated_at : started_at);
|
||||
json_append(json, ",");
|
||||
storage_json_builder_append_key_string(json, "note", (const char *)sqlite3_column_text(stmt, 11));
|
||||
storage_json_builder_append(json, ",");
|
||||
storage_json_builder_append_key_string(json, "tags", (const char *)sqlite3_column_text(stmt, 12));
|
||||
storage_json_builder_appendf(json, ",\"deleted_at\":%lld,\"updated_at\":", sqlite3_column_int64(stmt, 13));
|
||||
storage_json_builder_append_epoch(json, updated_at > 0 ? updated_at : started_at);
|
||||
storage_json_builder_append(json, ",");
|
||||
storage_append_session_rounds_json(json, id);
|
||||
json_append(json, "}");
|
||||
storage_json_builder_append(json, "}");
|
||||
}
|
||||
|
||||
typedef void (*StorageJsonRowFn)(JsonBuilder *json, sqlite3_stmt *stmt);
|
||||
typedef void (*StorageJsonRowFn)(StorageJsonBuilder *json, sqlite3_stmt *stmt);
|
||||
|
||||
static int
|
||||
storage_append_id_payload_json(JsonBuilder *json, const char *sql,
|
||||
storage_append_id_payload_json(StorageJsonBuilder *json, const char *sql,
|
||||
const char *id, StorageJsonRowFn append_row)
|
||||
{
|
||||
sqlite3_stmt *stmt = NULL;
|
||||
@@ -854,7 +712,7 @@ storage_append_id_payload_json(JsonBuilder *json, const char *sql,
|
||||
}
|
||||
|
||||
static int
|
||||
storage_append_dated_payload_json(JsonBuilder *json, const char *sql,
|
||||
storage_append_dated_payload_json(StorageJsonBuilder *json, const char *sql,
|
||||
const char *id, int local_date,
|
||||
StorageJsonRowFn append_row)
|
||||
{
|
||||
@@ -877,7 +735,7 @@ storage_append_dated_payload_json(JsonBuilder *json, const char *sql,
|
||||
}
|
||||
|
||||
static int
|
||||
storage_append_habit_payload_json(JsonBuilder *json, const char *habit_id)
|
||||
storage_append_habit_payload_json(StorageJsonBuilder *json, const char *habit_id)
|
||||
{
|
||||
return storage_append_id_payload_json(
|
||||
json,
|
||||
@@ -889,7 +747,7 @@ storage_append_habit_payload_json(JsonBuilder *json, const char *habit_id)
|
||||
}
|
||||
|
||||
static int
|
||||
storage_append_habit_day_payload_json(JsonBuilder *json, const char *habit_id, int local_date)
|
||||
storage_append_habit_day_payload_json(StorageJsonBuilder *json, const char *habit_id, int local_date)
|
||||
{
|
||||
return storage_append_dated_payload_json(
|
||||
json,
|
||||
@@ -901,7 +759,7 @@ storage_append_habit_day_payload_json(JsonBuilder *json, const char *habit_id, i
|
||||
}
|
||||
|
||||
static int
|
||||
storage_append_session_payload_json(JsonBuilder *json, const char *session_id)
|
||||
storage_append_session_payload_json(StorageJsonBuilder *json, const char *session_id)
|
||||
{
|
||||
return storage_append_id_payload_json(
|
||||
json,
|
||||
@@ -959,7 +817,7 @@ storage_sync_op_is_delete(const char *entity_type, const char *entity_id, int lo
|
||||
}
|
||||
|
||||
static void
|
||||
storage_append_sync_op_payload(JsonBuilder *json, const char *entity_type, const char *entity_id,
|
||||
storage_append_sync_op_payload(StorageJsonBuilder *json, const char *entity_type, const char *entity_id,
|
||||
int local_date)
|
||||
{
|
||||
int found = 0;
|
||||
@@ -971,17 +829,17 @@ storage_append_sync_op_payload(JsonBuilder *json, const char *entity_type, const
|
||||
else if(strcmp(entity_type, "session") == 0)
|
||||
found = storage_append_session_payload_json(json, entity_id);
|
||||
if(!found)
|
||||
json_append(json, "{}");
|
||||
storage_json_builder_append(json, "{}");
|
||||
}
|
||||
|
||||
static void
|
||||
storage_append_sync_ops_json(JsonBuilder *json, long long through_seq)
|
||||
storage_append_sync_ops_json(StorageJsonBuilder *json, long long through_seq)
|
||||
{
|
||||
sqlite3_stmt *stmt = NULL;
|
||||
const char *client_id = storage_sync_client_id();
|
||||
int first = 1;
|
||||
|
||||
json_append(json, "\"ops\":[");
|
||||
storage_json_builder_append(json, "\"ops\":[");
|
||||
if(g_storage.db != NULL &&
|
||||
sqlite3_prepare_v2(g_storage.db,
|
||||
"SELECT seq,entity_type,entity_id,local_date,queued_at "
|
||||
@@ -1001,32 +859,32 @@ storage_append_sync_ops_json(JsonBuilder *json, long long through_seq)
|
||||
continue;
|
||||
snprintf(op_id, sizeof(op_id), "%s:%lld", client_id, seq);
|
||||
if(!first)
|
||||
json_append(json, ",");
|
||||
storage_json_builder_append(json, ",");
|
||||
first = 0;
|
||||
json_append(json, "{");
|
||||
json_append(json, "\"op_id\":");
|
||||
json_append_string(json, op_id);
|
||||
json_append(json, ",\"client_id\":");
|
||||
json_append_string(json, client_id);
|
||||
json_appendf(json, ",\"seq\":%lld", seq);
|
||||
json_append(json, ",\"entity_type\":");
|
||||
json_append_string(json, entity_type);
|
||||
json_append(json, ",\"entity_id\":");
|
||||
json_append_string(json, entity_id);
|
||||
storage_json_builder_append(json, "{");
|
||||
storage_json_builder_append(json, "\"op_id\":");
|
||||
storage_json_builder_append_string(json, op_id);
|
||||
storage_json_builder_append(json, ",\"client_id\":");
|
||||
storage_json_builder_append_string(json, client_id);
|
||||
storage_json_builder_appendf(json, ",\"seq\":%lld", seq);
|
||||
storage_json_builder_append(json, ",\"entity_type\":");
|
||||
storage_json_builder_append_string(json, entity_type);
|
||||
storage_json_builder_append(json, ",\"entity_id\":");
|
||||
storage_json_builder_append_string(json, entity_id);
|
||||
if(local_date > 0)
|
||||
json_appendf(json, ",\"local_date\":%d", local_date);
|
||||
json_append(json, ",\"op_type\":");
|
||||
json_append_string(json, is_delete ? "delete" : "upsert");
|
||||
json_append(json, ",\"payload\":");
|
||||
storage_json_builder_appendf(json, ",\"local_date\":%d", local_date);
|
||||
storage_json_builder_append(json, ",\"op_type\":");
|
||||
storage_json_builder_append_string(json, is_delete ? "delete" : "upsert");
|
||||
storage_json_builder_append(json, ",\"payload\":");
|
||||
storage_append_sync_op_payload(json, entity_type, entity_id, local_date);
|
||||
json_append(json, ",\"created_at\":");
|
||||
json_append_epoch(json, queued_at);
|
||||
json_append(json, "}");
|
||||
storage_json_builder_append(json, ",\"created_at\":");
|
||||
storage_json_builder_append_epoch(json, queued_at);
|
||||
storage_json_builder_append(json, "}");
|
||||
}
|
||||
}
|
||||
if(stmt != NULL)
|
||||
sqlite3_finalize(stmt);
|
||||
json_append(json, "]");
|
||||
storage_json_builder_append(json, "]");
|
||||
}
|
||||
|
||||
static const char *
|
||||
@@ -1080,13 +938,13 @@ storage_private_record_key(uint8_t out[32])
|
||||
}
|
||||
|
||||
static int
|
||||
storage_append_encrypted_record_json(JsonBuilder *json, const uint8_t key[32],
|
||||
storage_append_encrypted_record_json(StorageJsonBuilder *json, const uint8_t key[32],
|
||||
const char *collection, const char *record_id,
|
||||
const char *entity_type, const char *entity_id,
|
||||
int local_date, long long queued_at, int deleted)
|
||||
{
|
||||
JsonBuilder plain = {0};
|
||||
JsonBuilder aad = {0};
|
||||
StorageJsonBuilder plain = {0};
|
||||
StorageJsonBuilder aad = {0};
|
||||
uint8_t nonce[12];
|
||||
uint8_t *sealed = NULL;
|
||||
char *sealed_hex = NULL;
|
||||
@@ -1108,11 +966,11 @@ storage_append_encrypted_record_json(JsonBuilder *json, const uint8_t key[32],
|
||||
goto done;
|
||||
|
||||
aad.ok = 1;
|
||||
json_append(&aad, collection);
|
||||
json_append(&aad, "\n");
|
||||
json_append(&aad, record_id);
|
||||
json_append(&aad, "\n");
|
||||
json_append(&aad, STORAGE_SYNC_RECORD_KEY_ID);
|
||||
storage_json_builder_append(&aad, collection);
|
||||
storage_json_builder_append(&aad, "\n");
|
||||
storage_json_builder_append(&aad, record_id);
|
||||
storage_json_builder_append(&aad, "\n");
|
||||
storage_json_builder_append(&aad, STORAGE_SYNC_RECORD_KEY_ID);
|
||||
if(!aad.ok || aad.data == NULL)
|
||||
goto done;
|
||||
|
||||
@@ -1125,40 +983,40 @@ storage_append_encrypted_record_json(JsonBuilder *json, const uint8_t key[32],
|
||||
!KsyncCryptoBytesToHex(sealed, sealed_len, sealed_hex, sealed_len * 2 + 1))
|
||||
goto done;
|
||||
|
||||
json_append(json, "{");
|
||||
json_append_key_string(json, "collection", collection);
|
||||
json_append(json, ",");
|
||||
json_append_key_string(json, "id", record_id);
|
||||
json_append(json, ",");
|
||||
json_append_key_string(json, "key_id", STORAGE_SYNC_RECORD_KEY_ID);
|
||||
json_append(json, ",");
|
||||
json_append_key_string(json, "nonce", nonce_hex);
|
||||
json_append(json, ",");
|
||||
json_append_key_string(json, "ciphertext", sealed_hex);
|
||||
json_append(json, ",\"updated_at\":");
|
||||
json_append_epoch(json, queued_at);
|
||||
storage_json_builder_append(json, "{");
|
||||
storage_json_builder_append_key_string(json, "collection", collection);
|
||||
storage_json_builder_append(json, ",");
|
||||
storage_json_builder_append_key_string(json, "id", record_id);
|
||||
storage_json_builder_append(json, ",");
|
||||
storage_json_builder_append_key_string(json, "key_id", STORAGE_SYNC_RECORD_KEY_ID);
|
||||
storage_json_builder_append(json, ",");
|
||||
storage_json_builder_append_key_string(json, "nonce", nonce_hex);
|
||||
storage_json_builder_append(json, ",");
|
||||
storage_json_builder_append_key_string(json, "ciphertext", sealed_hex);
|
||||
storage_json_builder_append(json, ",\"updated_at\":");
|
||||
storage_json_builder_append_epoch(json, queued_at);
|
||||
if(deleted)
|
||||
json_appendf(json, ",\"deleted_at\":%lld", queued_at > 0 ? queued_at : now_seconds());
|
||||
json_append(json, "}");
|
||||
storage_json_builder_appendf(json, ",\"deleted_at\":%lld", queued_at > 0 ? queued_at : now_seconds());
|
||||
storage_json_builder_append(json, "}");
|
||||
ok = json->ok;
|
||||
|
||||
done:
|
||||
json_free(&plain);
|
||||
json_free(&aad);
|
||||
storage_json_builder_free(&plain);
|
||||
storage_json_builder_free(&aad);
|
||||
free(sealed);
|
||||
free(sealed_hex);
|
||||
return ok;
|
||||
}
|
||||
|
||||
static void
|
||||
storage_append_encrypted_records_json(JsonBuilder *json, long long through_seq)
|
||||
storage_append_encrypted_records_json(StorageJsonBuilder *json, long long through_seq)
|
||||
{
|
||||
sqlite3_stmt *stmt = NULL;
|
||||
uint8_t key[32];
|
||||
int has_key;
|
||||
int first = 1;
|
||||
|
||||
json_append(json, "\"encrypted_records\":[");
|
||||
storage_json_builder_append(json, "\"encrypted_records\":[");
|
||||
has_key = storage_private_record_key(key);
|
||||
if(has_key && g_storage.db != NULL &&
|
||||
sqlite3_prepare_v2(g_storage.db,
|
||||
@@ -1180,7 +1038,7 @@ storage_append_encrypted_records_json(JsonBuilder *json, long long through_seq)
|
||||
record_id, sizeof(record_id)))
|
||||
continue;
|
||||
if(!first)
|
||||
json_append(json, ",");
|
||||
storage_json_builder_append(json, ",");
|
||||
first = 0;
|
||||
if(!storage_append_encrypted_record_json(json, key, collection, record_id,
|
||||
entity_type, entity_id, local_date,
|
||||
@@ -1193,13 +1051,13 @@ storage_append_encrypted_records_json(JsonBuilder *json, long long through_seq)
|
||||
if(stmt != NULL)
|
||||
sqlite3_finalize(stmt);
|
||||
memset(key, 0, sizeof(key));
|
||||
json_append(json, "]");
|
||||
storage_json_builder_append(json, "]");
|
||||
}
|
||||
|
||||
char *
|
||||
storage_build_sync_payload_json(const char *user_id_hash, const char *public_key_hex)
|
||||
{
|
||||
JsonBuilder json = {0};
|
||||
StorageJsonBuilder json = {0};
|
||||
long long since_server_version;
|
||||
long long through_seq;
|
||||
int full_upload_done;
|
||||
@@ -1230,42 +1088,42 @@ storage_build_sync_payload_json(const char *user_id_hash, const char *public_key
|
||||
through_seq = storage_sync_outbox_batch_seq(STORAGE_SYNC_OP_BATCH_LIMIT);
|
||||
g_storage.pending_sync_outbox_seq = through_seq;
|
||||
json.ok = 1;
|
||||
json_append(&json, "{");
|
||||
json_appendf(&json, "\"protocol_version\":%d,", INBE_SYNC_PROTOCOL_VERSION);
|
||||
json_append(&json, "\"app_id\":\"inbe\",");
|
||||
json_append(&json, "\"client_capabilities\":[\"v4-encrypted-records\","
|
||||
storage_json_builder_append(&json, "{");
|
||||
storage_json_builder_appendf(&json, "\"protocol_version\":%d,", INBE_SYNC_PROTOCOL_VERSION);
|
||||
storage_json_builder_append(&json, "\"app_id\":\"inbe\",");
|
||||
storage_json_builder_append(&json, "\"client_capabilities\":[\"v4-encrypted-records\","
|
||||
"\"v4-dual-write-transition\","
|
||||
"\"v5-dual-read\","
|
||||
"\"v5-legacy-encrypted-collections\"],");
|
||||
json_append(&json, "\"include_legacy_data\":true,");
|
||||
json_append_key_string(&json, "user_id_hash", user_id_hash);
|
||||
json_append(&json, ",");
|
||||
json_append_key_string(&json, "client_id", storage_sync_client_id());
|
||||
json_appendf(&json, ",\"since_server_version\":%lld", since_server_version);
|
||||
json_appendf(&json, ",\"client_clock\":%lld", get_meta_int64(STORAGE_SYNC_SERVER_CLOCK_KEY, 0));
|
||||
storage_json_builder_append(&json, "\"include_legacy_data\":true,");
|
||||
storage_json_builder_append_key_string(&json, "user_id_hash", user_id_hash);
|
||||
storage_json_builder_append(&json, ",");
|
||||
storage_json_builder_append_key_string(&json, "client_id", storage_sync_client_id());
|
||||
storage_json_builder_appendf(&json, ",\"since_server_version\":%lld", since_server_version);
|
||||
storage_json_builder_appendf(&json, ",\"client_clock\":%lld", get_meta_int64(STORAGE_SYNC_SERVER_CLOCK_KEY, 0));
|
||||
if(get_meta_int64(STORAGE_SYNC_FULL_REPLACE_KEY, 0) != 0)
|
||||
json_append(&json, ",\"full_sync_requested\":true");
|
||||
storage_json_builder_append(&json, ",\"full_sync_requested\":true");
|
||||
{
|
||||
const char *last_server_hash = get_meta_text(STORAGE_SYNC_LAST_SERVER_HASH_KEY);
|
||||
if(!force_zero_day_repair && last_server_hash != NULL && last_server_hash[0] != '\0') {
|
||||
json_append(&json, ",");
|
||||
json_append_key_string(&json, "last_server_state_hash", last_server_hash);
|
||||
storage_json_builder_append(&json, ",");
|
||||
storage_json_builder_append_key_string(&json, "last_server_state_hash", last_server_hash);
|
||||
}
|
||||
}
|
||||
if(since_server_version <= 0 || !full_upload_done)
|
||||
json_append(&json, ",\"bootstrap\":true");
|
||||
storage_json_builder_append(&json, ",\"bootstrap\":true");
|
||||
if(public_key_hex != NULL && public_key_hex[0] != '\0') {
|
||||
json_append(&json, ",");
|
||||
json_append_key_string(&json, "public_key", public_key_hex);
|
||||
storage_json_builder_append(&json, ",");
|
||||
storage_json_builder_append_key_string(&json, "public_key", public_key_hex);
|
||||
}
|
||||
json_append(&json, ",\"habits\":[]");
|
||||
json_append(&json, ",\"habit_days\":[]");
|
||||
json_append(&json, ",\"sessions\":[]");
|
||||
json_append(&json, ",");
|
||||
storage_json_builder_append(&json, ",\"habits\":[]");
|
||||
storage_json_builder_append(&json, ",\"habit_days\":[]");
|
||||
storage_json_builder_append(&json, ",\"sessions\":[]");
|
||||
storage_json_builder_append(&json, ",");
|
||||
storage_append_sync_ops_json(&json, through_seq);
|
||||
json_append(&json, ",");
|
||||
storage_json_builder_append(&json, ",");
|
||||
storage_append_encrypted_records_json(&json, through_seq);
|
||||
json_append(&json, "}");
|
||||
storage_json_builder_append(&json, "}");
|
||||
|
||||
if(!json.ok || json.data == NULL) {
|
||||
free(json.data);
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
#include "storage_json_builder.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
int
|
||||
storage_json_builder_reserve(StorageJsonBuilder *json, size_t extra)
|
||||
{
|
||||
char *next;
|
||||
size_t next_cap;
|
||||
|
||||
if(json == NULL || !json->ok)
|
||||
return 0;
|
||||
if(extra <= json->cap - json->len)
|
||||
return 1;
|
||||
next_cap = json->cap > 0 ? json->cap : 1024;
|
||||
while(extra > next_cap - json->len)
|
||||
next_cap *= 2;
|
||||
next = (char *)realloc(json->data, next_cap);
|
||||
if(next == NULL) {
|
||||
json->ok = 0;
|
||||
return 0;
|
||||
}
|
||||
json->data = next;
|
||||
json->cap = next_cap;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void
|
||||
storage_json_builder_append_len(StorageJsonBuilder *json, const char *text,
|
||||
size_t len)
|
||||
{
|
||||
if(text == NULL || len == 0)
|
||||
return;
|
||||
if(!storage_json_builder_reserve(json, len + 1))
|
||||
return;
|
||||
memcpy(json->data + json->len, text, len);
|
||||
json->len += len;
|
||||
json->data[json->len] = '\0';
|
||||
}
|
||||
|
||||
void
|
||||
storage_json_builder_append(StorageJsonBuilder *json, const char *text)
|
||||
{
|
||||
if(text != NULL)
|
||||
storage_json_builder_append_len(json, text, strlen(text));
|
||||
}
|
||||
|
||||
void
|
||||
storage_json_builder_appendf(StorageJsonBuilder *json, const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_list copy;
|
||||
int needed;
|
||||
|
||||
if(json == NULL || fmt == NULL || !json->ok)
|
||||
return;
|
||||
va_start(args, fmt);
|
||||
va_copy(copy, args);
|
||||
needed = vsnprintf(NULL, 0, fmt, copy);
|
||||
va_end(copy);
|
||||
if(needed < 0) {
|
||||
json->ok = 0;
|
||||
va_end(args);
|
||||
return;
|
||||
}
|
||||
if(storage_json_builder_reserve(json, (size_t)needed + 1)) {
|
||||
vsnprintf(json->data + json->len, json->cap - json->len, fmt, args);
|
||||
json->len += (size_t)needed;
|
||||
}
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void
|
||||
storage_json_builder_append_string(StorageJsonBuilder *json, const char *text)
|
||||
{
|
||||
const unsigned char *p = (const unsigned char *)(text != NULL ? text : "");
|
||||
|
||||
storage_json_builder_append(json, "\"");
|
||||
while(*p != '\0') {
|
||||
char escaped[8];
|
||||
if(*p == '"' || *p == '\\') {
|
||||
escaped[0] = '\\';
|
||||
escaped[1] = (char)*p;
|
||||
storage_json_builder_append_len(json, escaped, 2);
|
||||
} else if(*p == '\b') {
|
||||
storage_json_builder_append(json, "\\b");
|
||||
} else if(*p == '\f') {
|
||||
storage_json_builder_append(json, "\\f");
|
||||
} else if(*p == '\n') {
|
||||
storage_json_builder_append(json, "\\n");
|
||||
} else if(*p == '\r') {
|
||||
storage_json_builder_append(json, "\\r");
|
||||
} else if(*p == '\t') {
|
||||
storage_json_builder_append(json, "\\t");
|
||||
} else if(*p < 0x20) {
|
||||
snprintf(escaped, sizeof(escaped), "\\u%04x", *p);
|
||||
storage_json_builder_append(json, escaped);
|
||||
} else {
|
||||
storage_json_builder_append_len(json, (const char *)p, 1);
|
||||
}
|
||||
p++;
|
||||
}
|
||||
storage_json_builder_append(json, "\"");
|
||||
}
|
||||
|
||||
void
|
||||
storage_json_builder_append_key_string(StorageJsonBuilder *json, const char *key,
|
||||
const char *value)
|
||||
{
|
||||
storage_json_builder_append_string(json, key);
|
||||
storage_json_builder_append(json, ":");
|
||||
storage_json_builder_append_string(json, value);
|
||||
}
|
||||
|
||||
void
|
||||
storage_json_builder_free(StorageJsonBuilder *json)
|
||||
{
|
||||
if(json == NULL)
|
||||
return;
|
||||
free(json->data);
|
||||
memset(json, 0, sizeof(*json));
|
||||
}
|
||||
|
||||
void
|
||||
storage_json_builder_append_epoch(StorageJsonBuilder *json, long long seconds)
|
||||
{
|
||||
char formatted[32];
|
||||
struct tm tm_value;
|
||||
time_t value = (time_t)seconds;
|
||||
|
||||
if(seconds <= 0)
|
||||
value = time(NULL);
|
||||
#if defined(_WIN32)
|
||||
gmtime_s(&tm_value, &value);
|
||||
#else
|
||||
gmtime_r(&value, &tm_value);
|
||||
#endif
|
||||
strftime(formatted, sizeof(formatted), "%Y-%m-%dT%H:%M:%SZ", &tm_value);
|
||||
storage_json_builder_append_string(json, formatted);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef INBE_STORAGE_JSON_BUILDER_H
|
||||
#define INBE_STORAGE_JSON_BUILDER_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct StorageJsonBuilder {
|
||||
char *data;
|
||||
size_t len;
|
||||
size_t cap;
|
||||
int ok;
|
||||
} StorageJsonBuilder;
|
||||
|
||||
int storage_json_builder_reserve(StorageJsonBuilder *json, size_t extra);
|
||||
void storage_json_builder_append_len(StorageJsonBuilder *json, const char *text,
|
||||
size_t len);
|
||||
void storage_json_builder_append(StorageJsonBuilder *json, const char *text);
|
||||
void storage_json_builder_appendf(StorageJsonBuilder *json, const char *fmt, ...);
|
||||
void storage_json_builder_append_string(StorageJsonBuilder *json, const char *text);
|
||||
void storage_json_builder_append_key_string(StorageJsonBuilder *json, const char *key,
|
||||
const char *value);
|
||||
void storage_json_builder_free(StorageJsonBuilder *json);
|
||||
void storage_json_builder_append_epoch(StorageJsonBuilder *json, long long seconds);
|
||||
|
||||
#endif
|
||||
@@ -124,11 +124,6 @@ app_draw_bottom_nav :: (app: InbeApp*) {
|
||||
items := (BottomNavItem[APP_BOTTOM_NAV_CONTENT_MAX + 1]){0}
|
||||
result := (BottomNavResult){0}
|
||||
|
||||
if app_android_bottom_nav_height() > 0 {
|
||||
DrawRectangle(0, view_height - app_android_bottom_nav_height(), view_width,
|
||||
app_android_bottom_nav_height(), GetThemeBackground())
|
||||
}
|
||||
|
||||
if app != NULL && (app->nav_sidebar_open || bottom_nav_sidebar_screen_active(app)) {
|
||||
app_draw_nav_sidebar(app)
|
||||
return
|
||||
@@ -147,7 +142,7 @@ app_draw_bottom_nav :: (app: InbeApp*) {
|
||||
route := routes[i]
|
||||
items[i] = (BottomNavItem){route, bottom_nav_route_label(route), bottom_nav_route_icon(app, route), bottom_nav_route_active(app, route), app->modal.active}
|
||||
}
|
||||
result = BottomNav((BottomNavProps){.view_width = view_width, .view_height = view_height, .bottom_margin = app_android_bottom_nav_height(), .count = route_count, .items = items})
|
||||
result = BottomNav((BottomNavProps){.view_width = view_width, .view_height = view_height, .bottom_margin = 0, .count = route_count, .items = items})
|
||||
if result.clicked_route != APP_NAV_ROUTE_NONE {
|
||||
app_apply_nav_route(app, result.clicked_route)
|
||||
}
|
||||
|
||||
@@ -55,7 +55,6 @@ reset_state(void)
|
||||
mouse_position = (Vector2){0};
|
||||
view_width = 320;
|
||||
view_height = 560;
|
||||
app_set_android_bottom_nav_height(0);
|
||||
}
|
||||
|
||||
bool
|
||||
@@ -852,18 +851,8 @@ test_edge_bottom_nav_routes_are_applied(void)
|
||||
expect(app.inbe.screen == InbeScreenHabits,
|
||||
"habits edge route should switch to habits");
|
||||
|
||||
app = test_app();
|
||||
reset_state();
|
||||
bottom_nav_clicked_route = APP_NAV_ROUTE_HABITS;
|
||||
app_set_android_bottom_nav_height(24);
|
||||
app_draw_bottom_nav(&app);
|
||||
|
||||
expect(bottom_nav_draw_count == 1,
|
||||
"habits edge route should draw bottom nav with system margin");
|
||||
expect(app.inbe.screen == InbeScreenHabits,
|
||||
"habits edge route should switch to habits with system margin");
|
||||
expect(bottom_nav_last.bottom_margin == 24,
|
||||
"bottom nav should preserve Android system nav margin");
|
||||
expect(bottom_nav_last.bottom_margin == 0,
|
||||
"bottom nav should not apply Android system nav margin inside the safe viewport");
|
||||
}
|
||||
|
||||
static void
|
||||
|
||||
Vendored
+1
-1
Submodule vendor/kryon updated: 2c5c23e053...3ee913f7a6.
Reference in new issue
Block a user