package com.anas4tech.noteable import android.content.Context import android.content.SharedPreferences import android.content.res.Configuration /** Shared palette for Android home-screen widgets and the floating edge widget. */ internal object WidgetTheme { const val PREFERENCES = "HomeWidgetPreferences" const val THEME_MODE_KEY = "noteable_theme_mode" data class Palette( val mode: String, val isDark: Boolean, val background: Int, val primaryText: Int, val secondaryText: Int, val border: Int, val divider: Int, val accent: Int, val accentSoft: Int, val accentText: Int, val neutralBackground: Int, val neutralText: Int, val noteIconBackground: Int, val noteIconText: Int, val checkboxTint: Int, val edgeHandle: Int, val backgroundResource: Int ) { val followsSystem: Boolean get() = mode == "system" } fun palette( context: Context, preferences: SharedPreferences = context.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE) ): Palette { val mode = preferences.getString(THEME_MODE_KEY, "system") ?.lowercase() ?.takeIf { it == "light" || it == "dark" || it == "system" } ?: "system" val systemDark = (context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES val dark = when (mode) { "dark" -> true "light" -> false else -> systemDark } return if (dark) { Palette( mode = mode, isDark = true, background = 0xFF202124.toInt(), primaryText = 0xFFE8EAED.toInt(), secondaryText = 0xFFBDC1C6.toInt(), border = 0xFF44474E.toInt(), divider = 0xFF3C4043.toInt(), accent = 0xFF8AB4F8.toInt(), accentSoft = 0xFF26374B.toInt(), accentText = 0xFF8AB4F8.toInt(), neutralBackground = 0xFF303134.toInt(), neutralText = 0xFFDADCE0.toInt(), noteIconBackground = 0xFF26374B.toInt(), noteIconText = 0xFFB9D4FC.toInt(), checkboxTint = 0xFF9BBFE9.toInt(), edgeHandle = 0xFF405563.toInt(), backgroundResource = if (mode == "system") R.drawable.note_widget_background else R.drawable.note_widget_background_dark ) } else { Palette( mode = mode, isDark = false, background = 0xFFFFFFFF.toInt(), primaryText = 0xFF222222.toInt(), secondaryText = 0xFF555555.toInt(), border = 0xFFDADCE0.toInt(), divider = 0xFFE6E6E6.toInt(), accent = 0xFF2878E8.toInt(), accentSoft = 0xFFDCEAFE.toInt(), accentText = 0xFF075BC7.toInt(), neutralBackground = 0xFFF0F3F7.toInt(), neutralText = 0xFF4B5563.toInt(), noteIconBackground = 0xFFE5EFFD.toInt(), noteIconText = 0xFF315579.toInt(), checkboxTint = 0xFF607D8B.toInt(), edgeHandle = 0xFF607D8B.toInt(), backgroundResource = if (mode == "system") R.drawable.note_widget_background else R.drawable.note_widget_background_light ) } } }