MainActivity.kt

3.5 KB · raw

1package com.peachpasswords23import android.content.res.Configuration4import android.graphics.Color as AndroidColor5import android.graphics.drawable.ColorDrawable6import android.os.Bundle7import android.view.WindowManager8import androidx.activity.compose.setContent9import androidx.activity.enableEdgeToEdge10import androidx.compose.foundation.isSystemInDarkTheme11import androidx.compose.runtime.collectAsState12import androidx.compose.runtime.getValue13import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen14import androidx.fragment.app.FragmentActivity15import dagger.hilt.android.AndroidEntryPoint16import javax.inject.Inject17import com.peachpasswords.ui.PeachAppNavHost18import com.peachpasswords.ui.onboarding.ONBOARD_KEY19import com.peachpasswords.ui.onboarding.ONBOARD_PREFS20import com.peachpasswords.ui.onboarding.onboardingStartDestination21import com.peachpasswords.ui.theme.ACCENT_COLORS22import com.peachpasswords.ui.theme.DEFAULT_ACCENT23import com.peachpasswords.ui.theme.PeachTheme24import com.peachpasswords.vault.VaultLockTimer25import com.peachpasswords.vault.VaultRepository2627// FragmentActivity (not ComponentActivity) so androidx BiometricPrompt can attach.28@AndroidEntryPoint29class MainActivity : FragmentActivity() {30    @Inject lateinit var vault: VaultRepository31    @Inject lateinit var lockTimer: VaultLockTimer3233    // Every touch/key event resets the idle auto-lock countdown (CF-1).34    override fun onUserInteraction() {35        super.onUserInteraction()36        lockTimer.poke()37    }3839    override fun onCreate(savedInstanceState: Bundle?) {40        installSplashScreen()41        super.onCreate(savedInstanceState)4243        // Match the splash/window fill to the resolved theme's --bg so launch44        // never flashes the wrong colour before Compose draws. Resolved from the45        // persisted setting synchronously (system mode reads the device night mode).46        val initialTheme = vault.settings.value.theme47        val initialDark = when (initialTheme) {48            "light" -> false49            "system" -> (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) ==50                Configuration.UI_MODE_NIGHT_YES51            else -> true52        }53        window.setBackgroundDrawable(54            ColorDrawable(AndroidColor.parseColor(if (initialDark) "#0C0A06" else "#EFEEE9")),55        )5657        enableEdgeToEdge()5859        // Block screenshots / recents thumbnails of the vault in release builds.60        if (!BuildConfig.DEBUG) {61            window.setFlags(62                WindowManager.LayoutParams.FLAG_SECURE,63                WindowManager.LayoutParams.FLAG_SECURE,64            )65        }6667        // Onboarding runs once, only on a fresh install with no vault yet.68        val onboarded = getSharedPreferences(ONBOARD_PREFS, MODE_PRIVATE)69            .getBoolean(ONBOARD_KEY, false)70        val start = onboardingStartDestination(hasVault = vault.hasVault(), onboarded = onboarded)7172        setContent {73            val settings by vault.settings.collectAsState()74            val darkTheme = when (settings.theme) {75                "light" -> false76                "system" -> isSystemInDarkTheme()77                else -> true78            }79            val accent = ACCENT_COLORS[settings.accentColor] ?: DEFAULT_ACCENT80            PeachTheme(81                darkTheme = darkTheme,82                accent = accent,83                reduceMotion = settings.reduceMotion,84            ) {85                PeachAppNavHost(startDestination = start, isUnlocked = vault.isUnlocked)86            }87        }88    }89}