MainActivity.kt

3.2 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.VaultRepository2526// FragmentActivity (not ComponentActivity) so androidx BiometricPrompt can attach.27@AndroidEntryPoint28class MainActivity : FragmentActivity() {29    @Inject lateinit var vault: VaultRepository3031    override fun onCreate(savedInstanceState: Bundle?) {32        installSplashScreen()33        super.onCreate(savedInstanceState)3435        // Match the splash/window fill to the resolved theme's --bg so launch36        // never flashes the wrong colour before Compose draws. Resolved from the37        // persisted setting synchronously (system mode reads the device night mode).38        val initialTheme = vault.settings.value.theme39        val initialDark = when (initialTheme) {40            "light" -> false41            "system" -> (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) ==42                Configuration.UI_MODE_NIGHT_YES43            else -> true44        }45        window.setBackgroundDrawable(46            ColorDrawable(AndroidColor.parseColor(if (initialDark) "#0C0A06" else "#EFEEE9")),47        )4849        enableEdgeToEdge()5051        // Block screenshots / recents thumbnails of the vault in release builds.52        if (!BuildConfig.DEBUG) {53            window.setFlags(54                WindowManager.LayoutParams.FLAG_SECURE,55                WindowManager.LayoutParams.FLAG_SECURE,56            )57        }5859        // Onboarding runs once, only on a fresh install with no vault yet.60        val onboarded = getSharedPreferences(ONBOARD_PREFS, MODE_PRIVATE)61            .getBoolean(ONBOARD_KEY, false)62        val start =63            if (!vault.hasVault() && !onboarded) PeachNav.Onboarding else PeachNav.Lock6465        setContent {66            val settings by vault.settings.collectAsState()67            val darkTheme = when (settings.theme) {68                "light" -> false69                "system" -> isSystemInDarkTheme()70                else -> true71            }72            val accent = ACCENT_COLORS[settings.accentColor] ?: DEFAULT_ACCENT73            PeachTheme(74                darkTheme = darkTheme,75                accent = accent,76                reduceMotion = settings.reduceMotion,77            ) {78                PeachAppNavHost(startDestination = start)79            }80        }81    }82}