Skip to content

Commit

Permalink
small settings refactor for readability
Browse files Browse the repository at this point in the history
* added enabled condition parameter for the SingleChoiceSelector component
* overload QuickSettingUiItem to avoid repetitive code
  • Loading branch information
Kimblebee committed Aug 1, 2023
1 parent 0b525e4 commit 7bbd924
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ 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.FlashModeStatus
import kotlin.math.min


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

//TODO: Implement Set Ratio
@Composable
Expand All @@ -76,9 +77,7 @@ fun QuickSetRatio(onClick: () -> Unit, ratio: Int, currentRatio: Int) {
else -> CameraAspectRatio.ONE_ONE
}
QuickSettingUiItem(
drawableResId = enum.getDrawableResId(),
text = stringResource(id = enum.getTextResId()),
accessibilityText = stringResource(id = enum.getDescriptionResId()),
enum = enum,
onClick = { onClick() },
)
}
Expand All @@ -91,9 +90,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 @@ -113,9 +110,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 @@ -144,8 +139,22 @@ fun DropDownIcon(toggleDropDown: () -> Unit, isOpen: Boolean) {
}
}

// subcomponents. use these to build up ui
// subcomponents used these 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 @@ -204,6 +213,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 @@ -68,13 +68,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 7bbd924

Please sign in to comment.