diff --git a/src/app/app_settings.zi b/src/app/app_settings.zi index 9dfa33f4e..78017275e 100644 --- a/src/app/app_settings.zi +++ b/src/app/app_settings.zi @@ -19,6 +19,7 @@ app_force_dark_mode :: () -> bool #foreign c; #import "c_string" #import, file "manual_seen.zi"; #import, file "settings_cache.zi"; +#import, file "settings_upgrade.zi"; #import, file "settings_key.zi"; #import, file "settings_store.zi"; #import, file "bottom_nav_policy.zi"; @@ -524,8 +525,12 @@ app_load_settings :: (app: *InnerBreeze) -> s32 { load_clamped_setting(*app.style_index, "style_pack", 1, 1, 3) load_clamped_setting(*app.theme_id, "theme", 0, 0, ThemeCount - 1) + theme_source_default := default_theme_source() + if SettingsThemePickedBeforeSources() { + theme_source_default = APP_THEME_SOURCE_APP + } load_clamped_setting(*app.theme_source, "theme_source", - default_theme_source(), APP_THEME_SOURCE_APP, + theme_source_default, APP_THEME_SOURCE_APP, APP_THEME_SOURCE_SYSTEM) load_clamped_setting(*app.theme_mode, "theme_mode", APP_THEME_SYSTEM, APP_THEME_SYSTEM, APP_THEME_DARK) diff --git a/src/app/settings_upgrade.zi b/src/app/settings_upgrade.zi new file mode 100644 index 000000000..7d0030311 --- /dev/null +++ b/src/app/settings_upgrade.zi @@ -0,0 +1,12 @@ +#import, file "settings_cache.zi"; + +// Before 1.8.5 the app theme was the only theme source, so settings last +// saved by those versions hold a theme but no source. A theme picked there, +// anything but the first one, stays the app theme after the upgrade. +#program_export +SettingsThemePickedBeforeSources :: () -> bool { + if SettingsCacheContains("theme_source") { + return false + } + return SettingsCacheInt("theme", 0) != 0 +} diff --git a/src/storage/storage_core.zi b/src/storage/storage_core.zi index 5abac9a00..4ee17f43d 100644 --- a/src/storage/storage_core.zi +++ b/src/storage/storage_core.zi @@ -3,6 +3,7 @@ #import "file_linux" #import "byte_text_linux" #import "string_range" +#import "date_parse" #import, file "time_parts.zi"; #import, file "setting_visitor.zi"; #import, file "state.zi"; @@ -19,8 +20,6 @@ #import, file "sync_crypto_chacha.zi"; c :: #system_library "c"; StorageCacheFlush :: (database: *void) -> s32 #foreign c "sqlite3_db_cacheflush"; -difftime :: (end: s64, start: s64) -> float64 #foreign c; -mktime :: (tm: *Tm) -> s64 #foreign c; StorageSyncStatus :: struct { has_account: s32 enabled: s32 @@ -2217,30 +2216,32 @@ storage_purge_synced_deleted_data :: () { storage_schedule_persist() } +// Civil day arithmetic: C time_t is 32-bit on 32-bit Android, so mktime and +// difftime cannot be declared with one integer type for every target. +storage_profile_day_number :: (local_date: s32) -> s64 { + year := local_date / 10000 + month := (local_date / 100) % 100 + day := local_date % 100 + if !DateValid(year, month, day) { + return -1 + } + return DaysFromCivil(year, month, day) +} + storage_profile_day_offset :: (local_date: s32, today_date: s32) -> s32 { - day_tm: Tm = Tm.{} - today_tm: Tm = Tm.{} - day_time: s64 - today_time: s64 - diff_days: float64 if local_date <= 0 || today_date <= 0 { return -1 } - day_tm.tm_year = local_date / 10000 - 1900 - day_tm.tm_mon = (local_date / 100) % 100 - 1 - day_tm.tm_mday = local_date % 100 - day_tm.tm_hour = 12 - today_tm.tm_year = today_date / 10000 - 1900 - today_tm.tm_mon = (today_date / 100) % 100 - 1 - today_tm.tm_mday = today_date % 100 - today_tm.tm_hour = 12 - day_time = mktime(*day_tm) - today_time = mktime(*today_tm) - diff_days = difftime(today_time, day_time) / 86400.0 - if diff_days < -0.5 || diff_days > 370.5 { + day := storage_profile_day_number(local_date) + today := storage_profile_day_number(today_date) + if day < 0 || today < 0 { return -1 } - return cast(s32)(diff_days + 0.5) + offset := today - day + if offset < 0 || offset > 370 { + return -1 + } + return cast(s32)offset } storage_profile_date_from_epoch :: (epoch: s64) -> s32 { diff --git a/tests/settings_cache_behavior.zi b/tests/settings_cache_behavior.zi index 04b7cc990..6cbe97bf8 100644 --- a/tests/settings_cache_behavior.zi +++ b/tests/settings_cache_behavior.zi @@ -1,5 +1,6 @@ #import, file "../src/app/settings_cache.zi"; #import, file "../src/app/settings_key.zi"; +#import, file "../src/app/settings_upgrade.zi"; #program_export Answer :: () -> s32 { @@ -60,5 +61,18 @@ Answer :: () -> s32 { } SettingsCacheFree() if SettingsCacheContains("speed") { return 8 } + // Settings saved by 1.8.0 hold the picked theme without a source. + if SettingsThemePickedBeforeSources() || + !SettingsCacheAdd("theme", "3") || + !SettingsThemePickedBeforeSources() || + !SettingsCacheAdd("theme_source", "1") || + SettingsThemePickedBeforeSources() { + return 10 + } + SettingsCacheFree() + if !SettingsCacheAdd("theme", "0") || SettingsThemePickedBeforeSources() { + return 11 + } + SettingsCacheFree() return 42 } diff --git a/tests/storage_habits_behavior.zi b/tests/storage_habits_behavior.zi index e2f15e42b..fd8f4ad8f 100644 --- a/tests/storage_habits_behavior.zi +++ b/tests/storage_habits_behavior.zi @@ -427,6 +427,13 @@ test_profile_week_stats :: () { check_true("week stats call", storage_profile_week_stats(20260715, *active_days, *practice_sessions) != 0) check_int("week active days", active_days, 3) check_int("week practice sessions", practice_sessions, 2) + // The week crosses a month boundary; 20260627 is seven days back. + check_true("session start of month", storage_save_session_at_for_activity(20260701, 9, 0, 0, *rounds[0], 1, 0, 0, null, 0) != 0) + check_true("session end of month", storage_save_session_at_for_activity(20260628, 9, 0, 0, *rounds[0], 1, 0, 0, null, 0) != 0) + check_true("session before week", storage_save_session_at_for_activity(20260627, 9, 0, 0, *rounds[0], 1, 0, 0, null, 0) != 0) + check_true("month week stats call", storage_profile_week_stats(20260704, *active_days, *practice_sessions) != 0) + check_int("month week active days", active_days, 2) + check_int("month week practice sessions", practice_sessions, 2) habits_free(*habits_a) storage_close() }