ble: DeviceScanView 2섹션 — Saved Devices + New Devices
상단: KnownDeviceStore.getAll() 의 저장된 기기 카드 리스트 · 탭 = connectByAddress 로 PIN 없이 빠른 재연결 · 휴지통 아이콘 = 페어링 해제 확인 다이얼로그 후 unbond · 마지막 연결 시각 표시 · 파란 calm 톤 카드 (MlPrimary.copy alpha 0.06) 하단: 스캔된 기기 중 known 에 없는 것만 (New Devices) · 기존 DeviceRow 로직 그대로 비어있는 상태 / 권한 / Bluetooth off 경고는 기존대로.
This commit is contained in:
+135
-10
@@ -17,6 +17,9 @@ import androidx.compose.material.icons.automirrored.filled.ArrowBack
|
|||||||
import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight
|
import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight
|
||||||
import androidx.compose.material.icons.filled.Warning
|
import androidx.compose.material.icons.filled.Warning
|
||||||
import androidx.compose.material.icons.filled.Close
|
import androidx.compose.material.icons.filled.Close
|
||||||
|
import androidx.compose.material.icons.filled.Bluetooth
|
||||||
|
import androidx.compose.material.icons.filled.BluetoothConnected
|
||||||
|
import androidx.compose.material.icons.filled.Delete
|
||||||
import androidx.compose.material3.*
|
import androidx.compose.material3.*
|
||||||
import androidx.compose.runtime.*
|
import androidx.compose.runtime.*
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
@@ -34,8 +37,13 @@ import com.example.medilightv2android.AppState
|
|||||||
import com.example.medilightv2android.ble.BleDevice
|
import com.example.medilightv2android.ble.BleDevice
|
||||||
import com.example.medilightv2android.ble.BleManager
|
import com.example.medilightv2android.ble.BleManager
|
||||||
import com.example.medilightv2android.models.SensorMode
|
import com.example.medilightv2android.models.SensorMode
|
||||||
|
import com.example.medilightv2android.services.KnownDevice
|
||||||
|
import com.example.medilightv2android.services.KnownDeviceStore
|
||||||
import com.example.medilightv2android.ui.components.BladdyIdleRiveView
|
import com.example.medilightv2android.ui.components.BladdyIdleRiveView
|
||||||
import com.example.medilightv2android.ui.theme.*
|
import com.example.medilightv2android.ui.theme.*
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
|
import java.util.Date
|
||||||
|
import java.util.Locale
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun DeviceScanView(appState: AppState) {
|
fun DeviceScanView(appState: AppState) {
|
||||||
@@ -44,8 +52,12 @@ fun DeviceScanView(appState: AppState) {
|
|||||||
var connectingDeviceAddress by remember { mutableStateOf<String?>(null) }
|
var connectingDeviceAddress by remember { mutableStateOf<String?>(null) }
|
||||||
var permissionsGranted by remember { mutableStateOf(false) }
|
var permissionsGranted by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
val filteredDevices = bleManager.discoveredDevices
|
var knownDevices by remember { mutableStateOf(KnownDeviceStore.getAll(context)) }
|
||||||
|
var pendingDelete by remember { mutableStateOf<KnownDevice?>(null) }
|
||||||
|
val knownAddresses = knownDevices.map { it.address }.toSet()
|
||||||
|
val newDevices = bleManager.discoveredDevices
|
||||||
.filter { it.name.startsWith("2025") || it.name.startsWith("VBT") }
|
.filter { it.name.startsWith("2025") || it.name.startsWith("VBT") }
|
||||||
|
.filter { it.address !in knownAddresses }
|
||||||
.sortedBy { it.name }
|
.sortedBy { it.name }
|
||||||
|
|
||||||
val isConnected by bleManager.isConnected
|
val isConnected by bleManager.isConnected
|
||||||
@@ -159,10 +171,14 @@ fun DeviceScanView(appState: AppState) {
|
|||||||
) {
|
) {
|
||||||
Text("Connect Device", fontSize = 24.sp, fontWeight = FontWeight.Bold)
|
Text("Connect Device", fontSize = 24.sp, fontWeight = FontWeight.Bold)
|
||||||
Spacer(modifier = Modifier.height(8.dp))
|
Spacer(modifier = Modifier.height(8.dp))
|
||||||
Text("Searching for nearby devices...", fontSize = 14.sp, color = MlSecondaryText)
|
Text(
|
||||||
|
if (knownDevices.isEmpty()) "Searching for nearby devices..."
|
||||||
|
else "Tap a saved device or scan for new",
|
||||||
|
fontSize = 14.sp, color = MlSecondaryText
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
Spacer(modifier = Modifier.height(24.dp))
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
|
|
||||||
// Bluetooth off warning
|
// Bluetooth off warning
|
||||||
if (!bluetoothEnabled) {
|
if (!bluetoothEnabled) {
|
||||||
@@ -227,8 +243,9 @@ fun DeviceScanView(appState: AppState) {
|
|||||||
Spacer(modifier = Modifier.height(8.dp))
|
Spacer(modifier = Modifier.height(8.dp))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Device list
|
// 디바이스 목록 — Known + New 두 섹션
|
||||||
if (filteredDevices.isEmpty() && isScanning) {
|
val emptyAll = knownDevices.isEmpty() && newDevices.isEmpty()
|
||||||
|
if (emptyAll && isScanning) {
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier.weight(1f).fillMaxWidth(),
|
modifier = Modifier.weight(1f).fillMaxWidth(),
|
||||||
horizontalAlignment = Alignment.CenterHorizontally,
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
@@ -238,7 +255,7 @@ fun DeviceScanView(appState: AppState) {
|
|||||||
Spacer(modifier = Modifier.height(16.dp))
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
Text("Scanning...", color = MlSecondaryText)
|
Text("Scanning...", color = MlSecondaryText)
|
||||||
}
|
}
|
||||||
} else if (filteredDevices.isEmpty()) {
|
} else if (emptyAll) {
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.weight(1f)
|
.weight(1f)
|
||||||
@@ -259,7 +276,6 @@ fun DeviceScanView(appState: AppState) {
|
|||||||
TroubleshootItem("4.", "Try restarting the device")
|
TroubleshootItem("4.", "Try restarting the device")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// DEV: BLE 없이 측정 모드 진입 — 디자인 작업용
|
|
||||||
if (appState.isDevMode) {
|
if (appState.isDevMode) {
|
||||||
Spacer(modifier = Modifier.height(16.dp))
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
Button(
|
Button(
|
||||||
@@ -278,10 +294,32 @@ fun DeviceScanView(appState: AppState) {
|
|||||||
} else {
|
} else {
|
||||||
LazyColumn(
|
LazyColumn(
|
||||||
modifier = Modifier.weight(1f),
|
modifier = Modifier.weight(1f),
|
||||||
contentPadding = PaddingValues(horizontal = 24.dp),
|
contentPadding = PaddingValues(horizontal = 24.dp, vertical = 4.dp),
|
||||||
verticalArrangement = Arrangement.spacedBy(12.dp)
|
verticalArrangement = Arrangement.spacedBy(10.dp)
|
||||||
) {
|
) {
|
||||||
items(filteredDevices, key = { it.address }) { device ->
|
if (knownDevices.isNotEmpty()) {
|
||||||
|
item("known_header") {
|
||||||
|
SectionHeader("Saved Devices", knownDevices.size)
|
||||||
|
}
|
||||||
|
items(knownDevices, key = { "k_${it.address}" }) { known ->
|
||||||
|
KnownDeviceRow(
|
||||||
|
device = known,
|
||||||
|
isConnecting = connectingDeviceAddress == known.address,
|
||||||
|
enabled = connectingDeviceAddress == null,
|
||||||
|
onConnect = {
|
||||||
|
connectingDeviceAddress = known.address
|
||||||
|
bleManager.connectByAddress(known.address, known.name)
|
||||||
|
},
|
||||||
|
onDelete = { pendingDelete = known }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (newDevices.isNotEmpty()) {
|
||||||
|
item("new_header") {
|
||||||
|
Spacer(modifier = Modifier.height(8.dp))
|
||||||
|
SectionHeader("New Devices", newDevices.size)
|
||||||
|
}
|
||||||
|
items(newDevices, key = { "n_${it.address}" }) { device ->
|
||||||
DeviceRow(
|
DeviceRow(
|
||||||
device = device,
|
device = device,
|
||||||
isConnecting = connectingDeviceAddress == device.address,
|
isConnecting = connectingDeviceAddress == device.address,
|
||||||
@@ -294,6 +332,26 @@ fun DeviceScanView(appState: AppState) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete Known Device 확인 다이얼로그
|
||||||
|
pendingDelete?.let { target ->
|
||||||
|
AlertDialog(
|
||||||
|
onDismissRequest = { pendingDelete = null },
|
||||||
|
title = { Text("페어링 해제") },
|
||||||
|
text = { Text("${target.name} 의 페어링을 해제합니다. 다시 사용하려면 PIN 재입력이 필요합니다.") },
|
||||||
|
confirmButton = {
|
||||||
|
TextButton(onClick = {
|
||||||
|
bleManager.unbondAndRemoveAddress(target.address)
|
||||||
|
knownDevices = KnownDeviceStore.getAll(context)
|
||||||
|
pendingDelete = null
|
||||||
|
}) { Text("해제", color = Color.Red) }
|
||||||
|
},
|
||||||
|
dismissButton = {
|
||||||
|
TextButton(onClick = { pendingDelete = null }) { Text("취소") }
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// Scan button
|
// Scan button
|
||||||
Button(
|
Button(
|
||||||
@@ -339,6 +397,73 @@ fun DeviceScanView(appState: AppState) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
private fun SectionHeader(label: String, count: Int) {
|
||||||
|
Row(
|
||||||
|
modifier = Modifier.fillMaxWidth().padding(top = 4.dp, bottom = 4.dp),
|
||||||
|
verticalAlignment = Alignment.CenterVertically
|
||||||
|
) {
|
||||||
|
Text(label, fontSize = 13.sp, fontWeight = FontWeight.SemiBold, color = MlSecondaryText)
|
||||||
|
Spacer(modifier = Modifier.width(6.dp))
|
||||||
|
Text("$count", fontSize = 12.sp, color = MlSecondaryText.copy(alpha = 0.6f))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
private fun KnownDeviceRow(
|
||||||
|
device: KnownDevice,
|
||||||
|
isConnecting: Boolean,
|
||||||
|
enabled: Boolean,
|
||||||
|
onConnect: () -> Unit,
|
||||||
|
onDelete: () -> Unit
|
||||||
|
) {
|
||||||
|
val lastConnectedLabel = remember(device.lastConnectedAt) {
|
||||||
|
if (device.lastConnectedAt <= 0) "—"
|
||||||
|
else SimpleDateFormat("M/d HH:mm", Locale.getDefault()).format(Date(device.lastConnectedAt))
|
||||||
|
}
|
||||||
|
Card(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.shadow(4.dp, RoundedCornerShape(14.dp), spotColor = Color.Black.copy(alpha = 0.04f))
|
||||||
|
.clickable(enabled = enabled, onClick = onConnect),
|
||||||
|
shape = RoundedCornerShape(14.dp),
|
||||||
|
colors = CardDefaults.cardColors(containerColor = MlPrimary.copy(alpha = 0.06f))
|
||||||
|
) {
|
||||||
|
Row(
|
||||||
|
modifier = Modifier.padding(14.dp),
|
||||||
|
verticalAlignment = Alignment.CenterVertically
|
||||||
|
) {
|
||||||
|
Box(
|
||||||
|
modifier = Modifier
|
||||||
|
.size(40.dp)
|
||||||
|
.clip(CircleShape)
|
||||||
|
.background(MlPrimary.copy(alpha = 0.15f)),
|
||||||
|
contentAlignment = Alignment.Center
|
||||||
|
) {
|
||||||
|
Icon(Icons.Default.BluetoothConnected, null, tint = MlPrimary, modifier = Modifier.size(20.dp))
|
||||||
|
}
|
||||||
|
Spacer(modifier = Modifier.width(12.dp))
|
||||||
|
Column(modifier = Modifier.weight(1f)) {
|
||||||
|
Text(device.name, fontSize = 15.sp, fontWeight = FontWeight.SemiBold)
|
||||||
|
Text("Last: $lastConnectedLabel", fontSize = 11.sp, color = MlSecondaryText)
|
||||||
|
}
|
||||||
|
if (isConnecting) {
|
||||||
|
CircularProgressIndicator(modifier = Modifier.size(22.dp), strokeWidth = 2.dp)
|
||||||
|
} else {
|
||||||
|
IconButton(
|
||||||
|
onClick = onDelete,
|
||||||
|
enabled = enabled,
|
||||||
|
modifier = Modifier.size(36.dp)
|
||||||
|
) {
|
||||||
|
Icon(Icons.Default.Delete, "Delete pairing",
|
||||||
|
tint = Color.Red.copy(alpha = 0.7f),
|
||||||
|
modifier = Modifier.size(18.dp))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
private fun DeviceRow(device: BleDevice, isConnecting: Boolean, enabled: Boolean, onClick: () -> Unit) {
|
private fun DeviceRow(device: BleDevice, isConnecting: Boolean, enabled: Boolean, onClick: () -> Unit) {
|
||||||
Card(
|
Card(
|
||||||
|
|||||||
Reference in New Issue
Block a user