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.PeachNav19import com.peachpasswords.ui.onboarding.ONBOARD_KEY20import com.peachpasswords.ui.onboarding.ONBOARD_PREFS21import 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 =71            if (!vault.hasVault() && !onboarded) PeachNav.Onboarding else PeachNav.Lock7273        setContent {74            val settings by vault.settings.collectAsState()75            val darkTheme = when (settings.theme) {76                "light" -> false77                "system" -> isSystemInDarkTheme()78                else -> true79            }80            val accent = ACCENT_COLORS[settings.accentColor] ?: DEFAULT_ACCENT81            PeachTheme(82                darkTheme = darkTheme,83                accent = accent,84                reduceMotion = settings.reduceMotion,85            ) {86                PeachAppNavHost(startDestination = start, isUnlocked = vault.isUnlocked)87            }88        }89    }90}