feat: implement focal length visibility toggling and filtering

This commit is contained in:
zhoubinjia committed 2026-05-09 15:08:10 +08:00
1 parent 6f8ee4fd13
commit 04c5e9c4e5
5 files changed
+183 -74

No files matched your search

@@ -134,7 +134,8 @@ data class UserPreferences(
val phantomSaveAsNew: Boolean = false,
val defaultVirtualAperture: Float = 0f, // 默认虚化光圈,0表示关闭
val customFocalLengths: List<Float> = emptyList(), // 自定义焦段 (35mm等效),最多8个
val customLensIds: List<String> = emptyList() // 自定义镜头 ID,逗号分隔存储
val customLensIds: List<String> = emptyList(), // 自定义镜头 ID,逗号分隔存储
val hiddenFocalLengths: List<Float> = emptyList() // 隐藏的焦段 (35mm等效)
)
/**
@@ -241,6 +242,7 @@ class UserPreferencesRepository(private val context: Context) {
private val DEFAULT_VIRTUAL_APERTURE = floatPreferencesKey("default_virtual_aperture")
private val CUSTOM_FOCAL_LENGTHS = stringPreferencesKey("custom_focal_lengths")
private val CUSTOM_LENS_IDS = stringPreferencesKey("custom_lens_ids")
private val HIDDEN_FOCAL_LENGTHS = stringPreferencesKey("hidden_focal_lengths")
}
/**
@@ -363,7 +365,11 @@ class UserPreferencesRepository(private val context: Context) {
?.split(",")?.filter { it.isNotEmpty() }
?.mapNotNull { it.toFloatOrNull() }
?: listOf(35f, 50f, 85f, 200f),
customLensIds = parseCustomLensIds(preferences[CUSTOM_LENS_IDS])
customLensIds = parseCustomLensIds(preferences[CUSTOM_LENS_IDS]),
hiddenFocalLengths = preferences[HIDDEN_FOCAL_LENGTHS]
?.split(",")?.filter { it.isNotEmpty() }
?.mapNotNull { it.toFloatOrNull() }
?: emptyList()
)
}
@@ -812,6 +818,12 @@ class UserPreferencesRepository(private val context: Context) {
}
}
suspend fun saveHiddenFocalLengths(focalLengths: List<Float>) {
context.dataStore.edit { preferences ->
preferences[HIDDEN_FOCAL_LENGTHS] = focalLengths.joinToString(",")
}
}
suspend fun saveCustomLensIds(lensIds: List<String>) {
context.dataStore.edit { preferences ->
preferences[CUSTOM_LENS_IDS] = lensIds
@@ -77,6 +77,7 @@ fun ZoomControlBar(
// 根据可用相机计算变焦档位
val customFocalLengths by viewModel.customFocalLengths.collectAsState(initial = emptyList())
val hiddenFocalLengths by viewModel.hiddenFocalLengths.collectAsState(initial = emptyList())
val lensZoomStops = viewModel.calculateLensZoomStops(availableCameras, currentCamera)
val customLensZoomStops = remember(availableCameras, currentCamera) {
availableCameras
@@ -90,7 +91,7 @@ fun ZoomControlBar(
}
.map { it.intrinsicZoomRatio }
}
val zoomStops = viewModel.allZoomStops(lensZoomStops, mainCamera, currentCamera, customFocalLengths)
val zoomStops = viewModel.allZoomStops(lensZoomStops, mainCamera, currentCamera, customFocalLengths, hiddenFocalLengths)
val cameraState by viewModel.state.collectAsState()
val minZoom = remember(cameraState.availableCameras) {
@@ -67,6 +67,7 @@ fun ZoomControlBarVerticel(
// 根据可用相机计算变焦档位
val customFocalLengths by viewModel.customFocalLengths.collectAsState(initial = emptyList())
val hiddenFocalLengths by viewModel.hiddenFocalLengths.collectAsState(initial = emptyList())
val lensZoomStops = viewModel.calculateLensZoomStops(availableCameras, currentCamera)
val customLensZoomStops = remember(availableCameras, currentCamera) {
availableCameras
@@ -80,7 +81,7 @@ fun ZoomControlBarVerticel(
}
.map { it.intrinsicZoomRatio }
}
val zoomStops = viewModel.allZoomStops(lensZoomStops, mainCamera, currentCamera, customFocalLengths)
val zoomStops = viewModel.allZoomStops(lensZoomStops, mainCamera, currentCamera, customFocalLengths, hiddenFocalLengths)
val cameraState by viewModel.state.collectAsState()
val minZoom = remember(cameraState.availableCameras) {
@@ -16,6 +16,7 @@ import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import kotlinx.coroutines.launch
import kotlin.math.abs
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
@@ -45,6 +46,8 @@ import androidx.compose.material.icons.filled.ExpandLess
import androidx.compose.material.icons.filled.ExpandMore
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.Visibility
import androidx.compose.material.icons.filled.VisibilityOff
import androidx.compose.material.icons.filled.FilterNone
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Card
@@ -2456,6 +2459,7 @@ fun DefaultFocalLengthSetting(
var showAddDialog by remember { mutableStateOf(false) }
var inputValue by remember { mutableStateOf("") }
val customFocalLengths by viewModel.customFocalLengths.collectAsState(initial = emptyList())
val hiddenFocalLengths by viewModel.hiddenFocalLengths.collectAsState(initial = emptyList())
Column(modifier = modifier.fillMaxWidth()) {
Text(
@@ -2502,62 +2506,34 @@ fun DefaultFocalLengthSetting(
// Device focal length chips
availableFLs.forEach { fl ->
val isSelected = kotlin.math.abs(currentFocalLength - fl) < 0.5f
Box(
modifier = Modifier
.width(64.dp)
.height(40.dp)
.clip(RoundedCornerShape(8.dp))
.background(if (isSelected) Color(0xFFFF6B35) else Color.White.copy(alpha = 0.1f))
.clickable { onFocalLengthSelected(fl) },
contentAlignment = Alignment.Center
) {
Text(
text = "${fl.roundToInt()}mm",
color = if (isSelected) Color.White else Color.White.copy(alpha = 0.7f),
fontSize = 11.sp,
fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Normal,
textAlign = TextAlign.Center
)
}
FocalLengthChip(
focalLength = fl,
isCustom = false,
isSelected = abs(currentFocalLength - fl) < 0.5f,
isHidden = hiddenFocalLengths.any { abs(it - fl) < 0.5f },
onSelect = { onFocalLengthSelected(fl) },
onToggleVisibility = { viewModel.toggleFocalLengthVisibility(fl) }
)
}
// Custom focal length chips (with remove button)
// Custom focal length chips
customFocalLengths.forEach { fl ->
val isSelected = kotlin.math.abs(currentFocalLength - fl) < 0.5f
Box(
modifier = Modifier
.height(40.dp)
.clip(RoundedCornerShape(8.dp))
.background(if (isSelected) Color(0xFFFF6B35) else Color(0xFF2A3A5C))
.clickable { onFocalLengthSelected(fl) },
contentAlignment = Alignment.Center
) {
Row(
modifier = Modifier.padding(horizontal = 10.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(2.dp)
) {
Text(
text = "${fl.roundToInt()}mm*",
color = if (isSelected) Color.White else Color.White.copy(alpha = 0.7f),
fontSize = 11.sp,
fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Normal
)
Icon(
imageVector = Icons.Default.Close,
contentDescription = null,
tint = if (isSelected) Color.White else Color.White.copy(alpha = 0.5f),
modifier = Modifier
.size(14.dp)
.clickable { viewModel.removeCustomFocalLength(fl) }
)
}
}
FocalLengthChip(
focalLength = fl,
isCustom = true,
isSelected = abs(currentFocalLength - fl) < 0.5f,
isHidden = false,
onSelect = { onFocalLengthSelected(fl) },
onToggleVisibility = { },
onRemove = { viewModel.removeCustomFocalLength(fl) }
)
}
// Add button (shown when total physical + custom < 8)
if (availableFLs.size + customFocalLengths.size < 8) {
// Add button (shown when total visible < 8)
val visibleFLCount = availableFLs.count { fl ->
hiddenFocalLengths.none { abs(it - fl) < 0.5f }
} + customFocalLengths.size
if (visibleFLCount < 8) {
Box(
modifier = Modifier
.width(40.dp)
@@ -2767,3 +2743,73 @@ private fun CustomBackgroundItem(
}
}
}
@Composable
private fun FocalLengthChip(
focalLength: Float,
isCustom: Boolean,
isSelected: Boolean,
isHidden: Boolean,
onSelect: () -> Unit,
onToggleVisibility: () -> Unit,
onRemove: (() -> Unit)? = null,
modifier: Modifier = Modifier
) {
Box(
modifier = modifier
.height(40.dp)
.clip(RoundedCornerShape(8.dp))
.background(
if (isSelected) Color(0xFFFF6B35)
else if (isHidden) Color.White.copy(alpha = 0.05f)
else if (isCustom) Color(0xFF2A3A5C)
else Color.White.copy(alpha = 0.1f)
)
.border(
width = 1.dp,
color = if (isHidden && !isSelected) Color.White.copy(alpha = 0.1f) else Color.Transparent,
shape = RoundedCornerShape(8.dp)
)
.clickable {
if (isHidden) {
onToggleVisibility()
}
onSelect()
},
contentAlignment = Alignment.Center
) {
Row(
modifier = Modifier.padding(horizontal = 10.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(6.dp)
) {
Text(
text = "${focalLength.roundToInt()}mm${if (isCustom) "*" else ""}",
color = if (isSelected) Color.White else if (isHidden) Color.White.copy(alpha = 0.3f) else Color.White.copy(alpha = 0.7f),
fontSize = 11.sp,
fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Normal,
textAlign = TextAlign.Center
)
if (!isCustom) {
Icon(
imageVector = if (isHidden) Icons.Default.VisibilityOff else Icons.Default.Visibility,
contentDescription = null,
tint = if (isSelected) Color.White else if (isHidden) Color.White.copy(alpha = 0.3f) else Color.White.copy(alpha = 0.5f),
modifier = Modifier
.size(16.dp)
.clickable { onToggleVisibility() }
)
}
if (onRemove != null) {
Icon(
imageVector = Icons.Default.Close,
contentDescription = null,
tint = if (isSelected) Color.White else Color.White.copy(alpha = 0.5f),
modifier = Modifier
.size(16.dp)
.clickable { onRemove() }
)
}
}
}
}
@@ -220,6 +220,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
val defaultFocalLength: Flow<Float> = userPreferencesRepository.userPreferences.map { it.defaultFocalLength }
val customFocalLengths: Flow<List<Float>> = userPreferencesRepository.userPreferences.map { it.customFocalLengths }
val hiddenFocalLengths: Flow<List<Float>> = userPreferencesRepository.userPreferences.map { it.hiddenFocalLengths }
val customLensIds: Flow<List<String>> = userPreferencesRepository.userPreferences.map { it.customLensIds }
val userPreferences: StateFlow<com.hinnka.mycamera.data.UserPreferences> = userPreferencesRepository.userPreferences
.stateIn(viewModelScope, SharingStarted.Eagerly, com.hinnka.mycamera.data.UserPreferences())
@@ -2269,7 +2270,13 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
// 2. 计算变焦档位 (逻辑同步自 ZoomControlBar.kt)
val lensZoomStops = calculateLensZoomStops(availableCameras, currentCamera)
val zoomStops = allZoomStops(lensZoomStops, mainCamera, currentCamera)
val zoomStops = allZoomStops(
lensZoomStops,
mainCamera,
currentCamera,
userPreferences.value.customFocalLengths,
userPreferences.value.hiddenFocalLengths
)
if (zoomStops.isEmpty()) return
@@ -2337,20 +2344,33 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
lensZoomStops: List<Float>,
mainCamera: CameraInfo?,
currentCamera: CameraInfo?,
customFocalLengths: List<Float> = emptyList()
customFocalLengths: List<Float> = emptyList(),
hiddenFocalLengths: List<Float> = emptyList()
): List<Float> {
val stops = mutableListOf<Float>()
stops.addAll(lensZoomStops)
if (currentCamera?.lensType == LensType.FRONT) {
stops.addAll(lensZoomStops)
if (stops.none { abs(it - 2f) <= 0.1f }) {
stops.add(2f)
}
return stops.sorted()
}
mainCamera ?: return stops.sorted()
mainCamera ?: return lensZoomStops.sorted()
// 1. 添加并过滤原生镜头焦段
if (mainCamera.focalLength35mmEquivalent > 0) {
val filteredLensStops = lensZoomStops.filter { zoom ->
val fl = zoom * mainCamera.focalLength35mmEquivalent
hiddenFocalLengths.none { abs(it - fl) < 0.5f }
}
stops.addAll(filteredLensStops)
} else {
stops.addAll(lensZoomStops)
}
// 2. 添加自定义焦段 (不参与隐藏过滤)
if (mainCamera.focalLength35mmEquivalent > 0) {
customFocalLengths.forEach { fl ->
val zoom = fl / mainCamera.focalLength35mmEquivalent
@@ -2359,6 +2379,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
}
}
}
return stops.sorted()
}
@@ -2391,6 +2412,49 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
}
}
fun addCustomFocalLength(focalLength: Float) {
viewModelScope.launch {
val prefs = userPreferencesRepository.userPreferences.first()
val list = prefs.customFocalLengths.toMutableList()
if (list.none { abs(it - focalLength) < 0.5f }) {
list.add(focalLength)
userPreferencesRepository.saveCustomFocalLengths(list.sorted())
}
}
}
fun removeCustomFocalLength(focalLength: Float) {
viewModelScope.launch {
val prefs = userPreferencesRepository.userPreferences.first()
val list = prefs.customFocalLengths.toMutableList()
list.removeAll { abs(it - focalLength) < 0.5f }
userPreferencesRepository.saveCustomFocalLengths(list)
// 如果删除了当前的默认焦段,重置为0
if (abs(prefs.defaultFocalLength - focalLength) < 0.5f) {
userPreferencesRepository.saveDefaultFocalLength(0f)
}
}
}
fun toggleFocalLengthVisibility(focalLength: Float) {
viewModelScope.launch {
val prefs = userPreferencesRepository.userPreferences.first()
val list = prefs.hiddenFocalLengths.toMutableList()
val index = list.indexOfFirst { abs(it - focalLength) < 0.5f }
if (index != -1) {
list.removeAt(index)
} else {
list.add(focalLength)
// 如果隐藏了当前的默认焦段,重置默认焦段为0
if (abs(prefs.defaultFocalLength - focalLength) < 0.5f) {
userPreferencesRepository.saveDefaultFocalLength(0f)
}
}
userPreferencesRepository.saveHiddenFocalLengths(list)
}
}
/**
* 设置 RAW 色彩空间
*/
@@ -2501,21 +2565,6 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
}
}
fun addCustomFocalLength(fl: Float) {
viewModelScope.launch {
val current = userPreferencesRepository.userPreferences.firstOrNull()?.customFocalLengths ?: emptyList()
val physicalCount = getAvailableFocalLengths().size
if (physicalCount + current.size >= 8 || current.any { abs(it - fl) < 0.5f }) return@launch
userPreferencesRepository.saveCustomFocalLengths((current + fl).sortedBy { it })
}
}
fun removeCustomFocalLength(fl: Float) {
viewModelScope.launch {
val current = userPreferencesRepository.userPreferences.firstOrNull()?.customFocalLengths ?: emptyList()
userPreferencesRepository.saveCustomFocalLengths(current.filter { abs(it - fl) >= 0.5f })
}
}
fun setCustomLensIds(value: String) {
viewModelScope.launch {