195 lines
7.2 KiB
Kotlin
195 lines
7.2 KiB
Kotlin
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 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 com.example.smarthome.ui.LoginScreen
|
||
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
|
||
import androidx.compose.runtime.collectAsState
|
||
import com.example.smarthome.data.BackgroundManager
|
||
import com.example.smarthome.data.LanguageManager
|
||
import com.example.smarthome.data.UserManager
|
||
import com.example.smarthome.ui.SmartHomeScreen
|
||
|
||
class MainActivity : ComponentActivity() {
|
||
override fun onCreate(savedInstanceState: Bundle?) {
|
||
super.onCreate(savedInstanceState)
|
||
|
||
// 初始化语言管理器
|
||
LanguageManager.init(this)
|
||
// 初始化用户管理器
|
||
UserManager.init(this)
|
||
|
||
// 隐藏状态栏和导航栏
|
||
hideStatusBar()
|
||
|
||
setContentView(R.layout.activity_main)
|
||
val composeView = findViewById<ComposeView>(R.id.composeView)
|
||
composeView.setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
|
||
composeView.setContent {
|
||
SmartHomeTheme {
|
||
AppRoot()
|
||
}
|
||
}
|
||
|
||
}
|
||
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 isLoggedIn by UserManager.isLoggedIn.collectAsState()
|
||
|
||
if (!isLoggedIn) {
|
||
// 未登录,显示登录页面
|
||
LoginScreen(
|
||
onLoginSuccess = {
|
||
// 登录成功后会自动更新isLoggedIn状态
|
||
}
|
||
)
|
||
} else {
|
||
// 已登录,显示主界面
|
||
MainContent()
|
||
}
|
||
}
|
||
|
||
@Composable
|
||
fun MainContent() {
|
||
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) }
|
||
var rooms by remember { mutableStateOf(savedRooms) }
|
||
|
||
// 保存房间列表到SharedPreferences
|
||
fun saveRooms(roomList: List<String>) {
|
||
sharedPreferences.edit().putString("rooms", roomList.joinToString(",")).apply()
|
||
rooms = roomList
|
||
}
|
||
|
||
// 初始化背景管理器并获取当前选中的背景
|
||
androidx.compose.runtime.LaunchedEffect(Unit) {
|
||
BackgroundManager.init(context)
|
||
}
|
||
val selectedBgId by BackgroundManager.selectedBackground.collectAsState()
|
||
val currentBg = BackgroundManager.backgrounds.getOrNull(selectedBgId)
|
||
val backgroundRes = currentBg?.resourceId ?: R.drawable.background1
|
||
// 根据背景类型调整遮罩透明度:暗色背景减少遮罩,亮色背景增加遮罩
|
||
val overlayAlpha = if (currentBg?.isDark == true) 0x11 else 0x88
|
||
|
||
Box(modifier = Modifier.fillMaxSize()) {
|
||
// 背景图片 - 填充整个屏幕包括系统栏,使用FillBounds确保完全填充
|
||
Image(
|
||
painter = painterResource(id = backgroundRes),
|
||
contentDescription = null,
|
||
modifier = Modifier.fillMaxSize(),
|
||
contentScale = ContentScale.FillBounds,
|
||
alpha = 0.8f
|
||
)
|
||
|
||
// 半透明遮罩层 - 填充整个屏幕包括系统栏
|
||
Box(
|
||
modifier = Modifier
|
||
.fillMaxSize()
|
||
.background(Color(overlayAlpha shl 24 or 0x121212))
|
||
)
|
||
|
||
// 主内容 - 全屏显示
|
||
Box(modifier = Modifier.fillMaxSize()) {
|
||
MainScaffold(
|
||
selectedRoom = selectedRoom,
|
||
onRoomSelect = { roomIndex ->
|
||
selectedRoom = roomIndex
|
||
selectedNavItem = 0 // 切换到控制台页面
|
||
},
|
||
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)
|
||
}
|
||
}
|
||
},
|
||
onRenameRoom = { index, newName ->
|
||
saveRooms(rooms.mapIndexed { i, name -> if (i == index) newName else name })
|
||
}
|
||
)
|
||
}
|
||
}
|
||
}
|
||
|
||
|