MPVN_Android/app/src/main/java/com/example/smarthome/MainActivity.kt

158 lines
5.9 KiB
Kotlin
Raw Normal View History

package com.example.smarthome
import android.graphics.drawable.Drawable
import android.os.Bundle
import androidx.activity.ComponentActivity
import com.example.smarthome.ui.theme.SmartHomeTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ViewCompositionStrategy
import eightbitlab.com.blurview.BlurTarget
import eightbitlab.com.blurview.BlurView
import androidx.compose.material3.Surface
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Modifier
import androidx.core.view.WindowCompat
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.foundation.layout.Box
import androidx.compose.ui.unit.dp
import com.example.smarthome.ui.MainScaffold
import androidx.compose.ui.platform.ComposeView
import androidx.compose.foundation.Image
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.layout.ContentScale
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.systemBarsPadding
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 隐藏状态栏和导航栏
hideStatusBar()
setContentView(R.layout.activity_main)
val composeView = findViewById<ComposeView>(R.id.composeView)
composeView.setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
composeView.setContent {
SmartHomeTheme {
AppRoot()
}
}
val decorView = window.decorView
val blurTarget = findViewById<BlurTarget>(R.id.blurTarget)
val blurView = findViewById<BlurView>(R.id.blurView)
val windowBackground: Drawable? = decorView.background
blurView.setupWith(blurTarget)
.setFrameClearDrawable(windowBackground)
.setBlurRadius(20f)
}
private fun hideStatusBar() {
// 设置全屏模式,让内容延伸到系统栏区域
WindowCompat.setDecorFitsSystemWindows(window, false)
// 设置全屏标志
window.setFlags(
android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN,
android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN
)
// 设置状态栏和导航栏为透明
window.statusBarColor = android.graphics.Color.TRANSPARENT
window.navigationBarColor = android.graphics.Color.TRANSPARENT
// 隐藏状态栏和导航栏
window.decorView.systemUiVisibility = (
android.view.View.SYSTEM_UI_FLAG_FULLSCREEN
or android.view.View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or android.view.View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or android.view.View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or android.view.View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or android.view.View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
)
// 支持刘海屏设备
val lp = window.attributes
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
lp.layoutInDisplayCutoutMode = android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
}
window.attributes = lp
}
}
@Composable
fun AppRoot() {
val context = androidx.compose.ui.platform.LocalContext.current
val sharedPreferences = remember {
context.getSharedPreferences("smart_home_prefs", android.content.Context.MODE_PRIVATE)
}
// 从SharedPreferences加载房间列表
val defaultRooms = listOf("总览", "客厅", "厨房", "卧室", "影音室", "游戏房")
val savedRooms = remember {
val saved = sharedPreferences.getString("rooms", null)
if (saved != null) {
saved.split(",").filter { it.isNotBlank() }
} else {
defaultRooms
}
}
var selectedRoom by remember { mutableStateOf(0) }
var selectedNavItem by remember { mutableStateOf(0) } // 0=控制台, 5=设置
var rooms by remember { mutableStateOf(savedRooms) }
// 保存房间列表到SharedPreferences
fun saveRooms(roomList: List<String>) {
sharedPreferences.edit().putString("rooms", roomList.joinToString(",")).apply()
rooms = roomList
}
Box(modifier = Modifier.fillMaxSize()) {
// 背景图片 - 填充整个屏幕包括系统栏使用FillBounds确保完全填充
Image(
painter = painterResource(id = R.drawable.background),
contentDescription = null,
modifier = Modifier.fillMaxSize(),
contentScale = ContentScale.FillBounds,
alpha = 0.8f
)
// 半透明遮罩层 - 填充整个屏幕包括系统栏
Box(
modifier = Modifier
.fillMaxSize()
.background(Color(0x88121212))
)
// 主内容 - 添加系统栏内边距
Box(modifier = Modifier.fillMaxSize().systemBarsPadding()) {
MainScaffold(
selectedRoom = selectedRoom,
onRoomSelect = { selectedRoom = it },
selectedNavItem = selectedNavItem,
onNavItemSelect = { selectedNavItem = it },
rooms = rooms,
onAddRoom = { newRoomName ->
saveRooms(rooms + newRoomName)
},
onDeleteRoom = { index ->
if (rooms.size > 1) { // 至少保留一个房间
saveRooms(rooms.filterIndexed { i, _ -> i != index })
if (selectedRoom >= rooms.size - 1) {
selectedRoom = (rooms.size - 2).coerceAtLeast(0)
}
}
}
)
}
}
}