103 lines
3.7 KiB
Kotlin
103 lines
3.7 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 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
|
||
|
|
|
||
|
|
class MainActivity : ComponentActivity() {
|
||
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||
|
|
super.onCreate(savedInstanceState)
|
||
|
|
WindowCompat.setDecorFitsSystemWindows(window, true)
|
||
|
|
|
||
|
|
setContentView(R.layout.activity_main)
|
||
|
|
val composeView = findViewById<ComposeView>(R.id.composeView)
|
||
|
|
composeView.setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
|
||
|
|
composeView.setContent {
|
||
|
|
SmartHomeTheme {
|
||
|
|
Surface(modifier = Modifier.fillMaxSize(), color = Color(0xFF121212)) {
|
||
|
|
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)
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@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()) {
|
||
|
|
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)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|