package com.vgmlr.varnish
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
import androidx.activity.compose.BackHandler
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.ime
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.BrokenImage
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveableStateHolder
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import coil.Coil
import coil.ImageLoader
class MainActivity : ComponentActivity() {
private val vm: VarnishViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge(
statusBarStyle = SystemBarStyle.dark(android.graphics.Color.TRANSPARENT),
navigationBarStyle = SystemBarStyle.dark(android.graphics.Color.TRANSPARENT)
)
super.onCreate(savedInstanceState)
if (!loaderReady) {
Coil.setImageLoader(
ImageLoader.Builder(applicationContext)
.components {
add(VideoThumbFetcher.Factory())
add(VideoThumbKeyer())
}
.crossfade(false)
.build()
)
loaderReady = true
}
setContent { VarnishTheme { VarnishApp(vm) { finish() } } }
if (savedInstanceState == null) handle(intent)
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
handle(intent)
}
override fun onResume() {
super.onResume()
vm.refresh()
}
private fun handle(i: Intent?) {
if (i?.action == Intent.ACTION_VIEW) i.data?.let(vm::openExternal)
}
private companion object {
var loaderReady = false
}
}
@Composable
private fun VarnishApp(vm: VarnishViewModel, onExit: () -> Unit) {
val access by vm.access.collectAsStateWithLifecycle()
val stack by vm.stack.collectAsStateWithLifecycle()
val holder = rememberSaveableStateHolder()
val seen = remember { mutableSetOf<String>() }
val focus = LocalFocusManager.current
val imeOpen = WindowInsets.ime.getBottom(LocalDensity.current) > 0
LaunchedEffect(imeOpen) { if (!imeOpen) focus.clearFocus() }
LaunchedEffect(stack) {
val live = stack.mapTo(HashSet()) { it.id }
(seen - live).forEach(holder::removeState)
seen.retainAll(live)
seen.addAll(live)
}
Box(Modifier.fillMaxSize().background(PageBg)) {
if (!access) {
AccessGate(vm)
} else {
BackHandler { if (!vm.back()) onExit() }
val top = stack.last()
holder.SaveableStateProvider(top.id) {
when (top) {
Screen.Home -> HomeScreen(vm)
Screen.Settings -> SettingsScreen(vm)
is Screen.Folder -> FolderScreen(vm, top)
is Screen.Image -> ImageScreen(vm, top)
is Screen.Crop -> CropScreen(vm, top)
}
}
}
}
}
@Composable
private fun AccessGate(vm: VarnishViewModel) {
val context = LocalContext.current
Column(
Modifier.fillMaxSize().padding(32.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Icon(
Icons.Outlined.BrokenImage,
contentDescription = null,
tint = PageText.copy(alpha = 0.4f),
modifier = Modifier.size(48.dp)
)
Spacer(Modifier.height(10.dp))
Text(
"Storage Access Required",
color = PageText,
fontSize = 16.sp
)
Spacer(Modifier.height(19.dp))
Button(
onClick = {
context.startOr(vm.repo.accessIntent()) {
runCatching {
context.startActivity(
Intent(android.provider.Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION)
)
}
}
},
modifier = Modifier.height(44.dp),
colors = ButtonDefaults.buttonColors(containerColor = BtnBg, contentColor = BtnInk),
shape = RoundedCornerShape(BTN_RADIUS)
) { Text("Grant", fontSize = 16.sp, letterSpacing = 1.5.sp) }
}
}