Skip to content

Commit

Permalink
settings refactor for readability (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kimblebee authored Aug 14, 2023
1 parent 62187a1 commit 1b72881
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.google.jetpackcamera.feature.quicksettings

import android.util.Rational
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
Expand Down Expand Up @@ -143,36 +142,38 @@ private fun ExpandedQuickSettingsUi(
) {
// if no setting is chosen, display the grid of settings
// to change the order of display just move these lines of code above or below each other
if (shouldShowQuickSetting == IsExpandedQuickSetting.NONE) {
val displayedQuickSettings: Array<@Composable () -> Unit> = arrayOf(
when (shouldShowQuickSetting) {
IsExpandedQuickSetting.NONE -> {
val displayedQuickSettings: Array<@Composable () -> Unit> = arrayOf(

{
QuickSetFlash(
onClick = { f: FlashModeStatus -> onFlashModeClick(f) },
currentFlashMode = currentCameraSettings.flash_mode_status
)
},
{
QuickFlipCamera(
flipCamera = { b: Boolean -> onLensFaceClick(b) },
currentFacingFront = currentCameraSettings.default_front_camera
)
},
{
QuickSetRatio(
onClick = { setVisibleQuickSetting(IsExpandedQuickSetting.ASPECT_RATIO) },
ratio = currentCameraSettings.aspect_ratio,
currentRatio = currentCameraSettings.aspect_ratio
)
},
)
QuickSettingsGrid(quickSettingsButtons = displayedQuickSettings)

}
// if a setting that can be expanded is selected, show it
else {
if (shouldShowQuickSetting == IsExpandedQuickSetting.ASPECT_RATIO) {
ExpandedQuickSetRatio(setRatio = onAspectRatioClick, currentRatio = currentCameraSettings.aspect_ratio)
{
QuickSetFlash(
onClick = { f: FlashModeStatus -> onFlashModeClick(f) },
currentFlashMode = currentCameraSettings.flash_mode_status
)
},
{
QuickFlipCamera(
flipCamera = { b: Boolean -> onLensFaceClick(b) },
currentFacingFront = currentCameraSettings.default_front_camera
)
},
{
QuickSetRatio(
onClick = { setVisibleQuickSetting(IsExpandedQuickSetting.ASPECT_RATIO) },
ratio = currentCameraSettings.aspect_ratio,
currentRatio = currentCameraSettings.aspect_ratio
)
},
)
QuickSettingsGrid(quickSettingsButtons = displayedQuickSettings)
}
// if a setting that can be expanded is selected, show it
IsExpandedQuickSetting.ASPECT_RATIO -> {
ExpandedQuickSetRatio(
setRatio = onAspectRatioClick,
currentRatio = currentCameraSettings.aspect_ratio
)
}
}
Spacer(modifier = Modifier.height(dimensionResource(id = R.dimen.quick_settings_spacer_height)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.google.jetpackcamera.feature.quicksettings.ui

import android.util.Rational
import androidx.annotation.DrawableRes
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
Expand Down Expand Up @@ -45,13 +44,14 @@ import androidx.compose.ui.unit.dp
import com.google.jetpackcamera.feature.quicksettings.CameraAspectRatio
import com.google.jetpackcamera.feature.quicksettings.CameraFlashMode
import com.google.jetpackcamera.feature.quicksettings.CameraLensFace
import com.google.jetpackcamera.feature.quicksettings.QuickSettingsEnum
import com.google.jetpackcamera.quicksettings.R
import com.google.jetpackcamera.settings.model.AspectRatio
import com.google.jetpackcamera.settings.model.FlashModeStatus
import kotlin.math.min


// completed components
// completed components ready to go into preview screen

//TODO: Implement Set Ratio
@Composable
Expand Down Expand Up @@ -103,9 +103,7 @@ fun QuickSetRatio(
else -> CameraAspectRatio.ONE_ONE
}
QuickSettingUiItem(
drawableResId = enum.getDrawableResId(),
text = stringResource(id = enum.getTextResId()),
accessibilityText = stringResource(id = enum.getDescriptionResId()),
enum = enum,
onClick = { onClick() },
isHighLighted = isHighlightEnabled && (ratio == currentRatio)
)
Expand All @@ -119,9 +117,7 @@ fun QuickSetFlash(onClick: (FlashModeStatus) -> Unit, currentFlashMode: FlashMod
FlashModeStatus.ON -> CameraFlashMode.ON
}
QuickSettingUiItem(
drawableResId = enum.getDrawableResId(),
text = stringResource(id = enum.getTextResId()),
accessibilityText = stringResource(id = enum.getDescriptionResId()),
enum = enum,
isHighLighted = currentFlashMode == FlashModeStatus.ON,
onClick =
{
Expand All @@ -141,9 +137,7 @@ fun QuickFlipCamera(flipCamera: (Boolean) -> Unit, currentFacingFront: Boolean)
false -> CameraLensFace.BACK
}
QuickSettingUiItem(
drawableResId = enum.getDrawableResId(),
text = stringResource(id = enum.getTextResId()),
accessibilityText = stringResource(id = enum.getDescriptionResId()),
enum = enum,
onClick = { flipCamera(!currentFacingFront) }
)
}
Expand Down Expand Up @@ -172,7 +166,22 @@ fun DropDownIcon(toggleDropDown: () -> Unit, isOpen: Boolean) {
}
}

// subcomponents. use these to build up ui
// subcomponents used to build completed components

@Composable
fun QuickSettingUiItem(
enum: QuickSettingsEnum,
onClick: () -> Unit,
isHighLighted: Boolean = false
) {
QuickSettingUiItem(
drawableResId = enum.getDrawableResId(),
text = stringResource(id = enum.getTextResId()),
accessibilityText = stringResource(id = enum.getDescriptionResId()),
onClick = { onClick() },
isHighLighted = isHighLighted,
)
}

/**
* The itemized UI component representing each button in quick settings.
Expand Down Expand Up @@ -232,6 +241,9 @@ fun ExpandedQuickSetting(vararg quickSettingButtons: @Composable () -> Unit) {
}
}

/**
* Algorithm to determine dimensions of QuickSettings Icon layout
*/
@Composable
fun QuickSettingsGrid(vararg quickSettingsButtons: @Composable () -> Unit) {
val initialNumOfColumns =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ private const val TAG = "SettingsScreen"
@Composable
fun SettingsScreen(
viewModel: SettingsViewModel = hiltViewModel(),
onNavigateToPreview: () -> Unit ) {
onNavigateToPreview: () -> Unit
) {
val settingsUiState by viewModel.settingsUiState.collectAsState()

Column(
Expand All @@ -65,13 +66,13 @@ fun SettingsList(uiState: SettingsUiState, viewModel: SettingsViewModel) {
)

FlashModeSetting(
uiState = uiState,
currentFlashMode = uiState.cameraAppSettings.flash_mode_status,
setFlashMode = viewModel::setFlashMode
)

SectionHeader(title = stringResource(id = R.string.section_title_app_settings))
DarkModeSetting(
uiState = uiState,
currentDarkModeStatus = uiState.cameraAppSettings.dark_mode_status,
setDarkMode = viewModel::setDarkMode
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ import androidx.compose.ui.semantics.Role
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.google.jetpackcamera.settings.R
import com.google.jetpackcamera.settings.SettingsUiState
import com.google.jetpackcamera.settings.model.DarkModeStatus
import com.google.jetpackcamera.settings.model.CameraAppSettings
import com.google.jetpackcamera.settings.model.DarkModeStatus
import com.google.jetpackcamera.settings.model.FlashModeStatus


Expand Down Expand Up @@ -92,32 +91,32 @@ fun DefaultCameraFacing(cameraAppSettings: CameraAppSettings, onClick: () -> Uni
leadingIcon = null,
onClick = { onClick() },
settingValue = cameraAppSettings.default_front_camera,
enabled = cameraAppSettings.back_camera_available && cameraAppSettings.front_camera_available
enabled = cameraAppSettings.back_camera_available && cameraAppSettings.front_camera_available
)
}

@Composable
fun DarkModeSetting(uiState: SettingsUiState, setDarkMode: (DarkModeStatus) -> Unit) {
fun DarkModeSetting(currentDarkModeStatus: DarkModeStatus, setDarkMode: (DarkModeStatus) -> Unit) {
BasicPopupSetting(
title = stringResource(id = R.string.dark_mode_title),
leadingIcon = null,
description = when (uiState.cameraAppSettings.dark_mode_status) {
description = when (currentDarkModeStatus) {
DarkModeStatus.SYSTEM -> stringResource(id = R.string.dark_mode_status_system)
DarkModeStatus.DARK -> stringResource(id = R.string.dark_mode_status_dark)
DarkModeStatus.LIGHT -> stringResource(id = R.string.dark_mode_status_light)
},
popupContents = {
Column(Modifier.selectableGroup()) {
ChoiceRow(text = stringResource(id = R.string.dark_mode_selector_dark),
selected = uiState.cameraAppSettings.dark_mode_status == DarkModeStatus.DARK,
SingleChoiceSelector(text = stringResource(id = R.string.dark_mode_selector_dark),
selected = currentDarkModeStatus == DarkModeStatus.DARK,
onClick = { setDarkMode(DarkModeStatus.DARK) }
)
ChoiceRow(text = stringResource(id = R.string.dark_mode_selector_light),
selected = uiState.cameraAppSettings.dark_mode_status == DarkModeStatus.LIGHT,
SingleChoiceSelector(text = stringResource(id = R.string.dark_mode_selector_light),
selected = currentDarkModeStatus == DarkModeStatus.LIGHT,
onClick = { setDarkMode(DarkModeStatus.LIGHT) }
)
ChoiceRow(text = stringResource(id = R.string.dark_mode_selector_system),
selected = uiState.cameraAppSettings.dark_mode_status == DarkModeStatus.SYSTEM,
SingleChoiceSelector(text = stringResource(id = R.string.dark_mode_selector_system),
selected = currentDarkModeStatus == DarkModeStatus.SYSTEM,
onClick = { setDarkMode(DarkModeStatus.SYSTEM) }
)
}
Expand All @@ -126,27 +125,27 @@ fun DarkModeSetting(uiState: SettingsUiState, setDarkMode: (DarkModeStatus) -> U
}

@Composable
fun FlashModeSetting(uiState: SettingsUiState, setFlashMode: (FlashModeStatus) -> Unit) {
fun FlashModeSetting(currentFlashMode: FlashModeStatus, setFlashMode: (FlashModeStatus) -> Unit) {
BasicPopupSetting(
title = stringResource(id = R.string.flash_mode_title),
leadingIcon = null,
description = when (uiState.cameraAppSettings.flash_mode_status) {
description = when (currentFlashMode) {
FlashModeStatus.AUTO -> stringResource(id = R.string.flash_mode_status_auto)
FlashModeStatus.ON -> stringResource(id = R.string.flash_mode_status_on)
FlashModeStatus.OFF -> stringResource(id = R.string.flash_mode_status_off)
},
popupContents = {
Column(Modifier.selectableGroup()) {
ChoiceRow(text = stringResource(id = R.string.flash_mode_selector_auto),
selected = uiState.cameraAppSettings.flash_mode_status == FlashModeStatus.AUTO,
SingleChoiceSelector(text = stringResource(id = R.string.flash_mode_selector_auto),
selected = currentFlashMode == FlashModeStatus.AUTO,
onClick = { setFlashMode(FlashModeStatus.AUTO) }
)
ChoiceRow(text = stringResource(id = R.string.flash_mode_selector_on),
selected = uiState.cameraAppSettings.flash_mode_status == FlashModeStatus.ON,
SingleChoiceSelector(text = stringResource(id = R.string.flash_mode_selector_on),
selected = currentFlashMode == FlashModeStatus.ON,
onClick = { setFlashMode(FlashModeStatus.ON) }
)
ChoiceRow(text = stringResource(id = R.string.flash_mode_selector_off),
selected = uiState.cameraAppSettings.flash_mode_status == FlashModeStatus.OFF,
SingleChoiceSelector(text = stringResource(id = R.string.flash_mode_selector_off),
selected = currentFlashMode == FlashModeStatus.OFF,
onClick = { setFlashMode(FlashModeStatus.OFF) }
)
}
Expand Down Expand Up @@ -265,13 +264,14 @@ fun SettingSwitch(settingValue: Boolean, onClick: () -> Unit, enabled: Boolean =
}

/**
* A component for a single-choice selector
* A component for a single-choice selector for a multiple choice list
*/
@Composable
fun ChoiceRow(
fun SingleChoiceSelector(
text: String,
selected: Boolean,
onClick: () -> Unit,
enabled: Boolean = true
) {
Row(
Modifier
Expand All @@ -287,6 +287,7 @@ fun ChoiceRow(
RadioButton(
selected = selected,
onClick = null,
enabled = enabled
)
Spacer(Modifier.width(8.dp))
Text(text)
Expand Down

0 comments on commit 1b72881

Please sign in to comment.