Visibility modifiers and code clean up

Marked stuff as internal and private.
This commit is contained in:
Sad Ellie 2023-01-14 00:14:57 +04:00
parent 8b9db717c4
commit c68c1c458a
22 changed files with 181 additions and 162 deletions

View File

@ -40,11 +40,12 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import com.sadellie.unitto.core.ui.common.AnimatedTopBarText
import com.sadellie.unitto.feature.R
import com.sadellie.unitto.feature.converter.components.Keyboard
import com.sadellie.unitto.feature.converter.components.PortraitLandscape
import com.sadellie.unitto.feature.converter.components.TopScreenPart
import kotlinx.coroutines.delay
@Composable
fun MainScreen(
internal fun MainScreen(
navigateToLeftScreen: (String) -> Unit,
navigateToRightScreen: (unitFrom: String, unitTo: String, input: String) -> Unit,
navigateToSettings: () -> Unit,

View File

@ -68,7 +68,7 @@ import com.sadellie.unitto.feature.converter.ConverterMode
* @param converterMode
*/
@Composable
fun Keyboard(
internal fun Keyboard(
modifier: Modifier = Modifier,
addDigit: (String) -> Unit = {},
deleteDigit: () -> Unit = {},

View File

@ -47,7 +47,7 @@ import com.sadellie.unitto.core.ui.theme.NumbersTextStyleTitleLarge
* @param onClick Action to perform when clicking this button.
*/
@Composable
fun KeyboardButton(
internal fun KeyboardButton(
modifier: Modifier = Modifier,
digit: String,
isPrimary: Boolean = true,

View File

@ -64,7 +64,7 @@ import com.sadellie.unitto.feature.R
* @param textToCopy Text that will be copied to clipboard when long-clicking.
*/
@Composable
fun MyTextField(
internal fun MyTextField(
modifier: Modifier,
primaryText: @Composable () -> String,
secondaryText: String?,

View File

@ -1,6 +1,6 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2022 Elshan Agaev
* Copyright (c) 2022-2023 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.sadellie.unitto.feature.converter
package com.sadellie.unitto.feature.converter.components
import android.content.res.Configuration
import androidx.compose.foundation.layout.Arrangement
@ -36,7 +36,7 @@ import androidx.compose.ui.unit.dp
* When Landscape mode will place [content1] and [content2] in a [Row].
*/
@Composable
fun PortraitLandscape(
internal fun PortraitLandscape(
modifier: Modifier,
content1: @Composable (Modifier) -> Unit,
content2: @Composable (Modifier) -> Unit,

View File

@ -64,7 +64,7 @@ import com.sadellie.unitto.feature.converter.ConverterMode
* @param converterMode [ConverterMode.BASE] doesn't use formatting for input/output.
*/
@Composable
fun TopScreenPart(
internal fun TopScreenPart(
modifier: Modifier,
inputValue: String,
calculatedValue: String?,

View File

@ -45,7 +45,7 @@ import com.sadellie.unitto.feature.R
* @param label Text on button
*/
@Composable
fun UnitSelectionButton(
internal fun UnitSelectionButton(
modifier: Modifier = Modifier,
onClick: () -> Unit = {},
label: Int?,

View File

@ -46,7 +46,7 @@ import com.sadellie.unitto.feature.R
import com.sadellie.unitto.feature.settings.components.AlertDialogWithList
@Composable
fun AboutScreen(
internal fun AboutScreen(
navigateUpAction: () -> Unit,
navigateToThirdParty: () -> Unit
) {

View File

@ -56,7 +56,7 @@ import com.sadellie.unitto.feature.settings.navigation.themesRoute
import com.sadellie.unitto.feature.settings.navigation.unitsGroupRoute
@Composable
fun SettingsScreen(
internal fun SettingsScreen(
viewModel: SettingsViewModel,
navigateUpAction: () -> Unit,
navControllerAction: (String) -> Unit

View File

@ -31,14 +31,14 @@ import androidx.compose.material.icons.filled.Palette
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import com.sadellie.unitto.feature.R
import com.sadellie.unitto.core.ui.common.UnittoLargeTopAppBar
import com.sadellie.unitto.core.ui.common.UnittoListItem
import com.sadellie.unitto.feature.R
import io.github.sadellie.themmo.ThemingMode
import io.github.sadellie.themmo.ThemmoController
@Composable
fun ThemesScreen(
internal fun ThemesScreen(
navigateUpAction: () -> Unit = {},
themmoController: ThemmoController,
viewModel: SettingsViewModel

View File

@ -48,7 +48,7 @@ import com.sadellie.unitto.feature.R
*/
@Stable
@Composable
fun ThirdPartyLicensesScreen(
internal fun ThirdPartyLicensesScreen(
navigateUpAction: () -> Unit = {}
) {
val mContext = LocalContext.current

View File

@ -47,9 +47,9 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.sadellie.unitto.feature.R
import com.sadellie.unitto.core.ui.common.Header
import com.sadellie.unitto.core.ui.common.UnittoLargeTopAppBar
import com.sadellie.unitto.feature.R
import org.burnoutcrew.reorderable.ReorderableItem
import org.burnoutcrew.reorderable.detectReorder
import org.burnoutcrew.reorderable.detectReorderAfterLongPress
@ -57,7 +57,7 @@ import org.burnoutcrew.reorderable.rememberReorderableLazyListState
import org.burnoutcrew.reorderable.reorderable
@Composable
fun UnitGroupsScreen(
internal fun UnitGroupsScreen(
viewModel: SettingsViewModel,
navigateUpAction: () -> Unit
) {

View File

@ -51,7 +51,7 @@ import com.sadellie.unitto.feature.settings.R
* @param supportText Text above list of options
*/
@Composable
fun AlertDialogWithList(
internal fun AlertDialogWithList(
title: String,
listItems: Map<Int, Int> = emptyMap(),
selectedItemIndex: Int = 0,

View File

@ -32,11 +32,11 @@ import com.sadellie.unitto.feature.settings.UnitGroupsScreen
import io.github.sadellie.themmo.ThemmoController
const val settingsGraph = "settings_graph"
const val settingsRoute = "settings_route"
const val themesRoute = "themes_route"
const val unitsGroupRoute = "units_group_route"
const val thirdPartyRoute = "third_party_route"
const val aboutRoute = "about_route"
private const val settingsRoute = "settings_route"
internal const val themesRoute = "themes_route"
internal const val unitsGroupRoute = "units_group_route"
internal const val thirdPartyRoute = "third_party_route"
internal const val aboutRoute = "about_route"
fun NavController.navigateToSettings() {
navigate(settingsRoute)

View File

@ -1,21 +0,0 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

View File

@ -1,6 +1,6 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2022-2023 Elshan Agaev
* Copyright (c) 2023 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -45,16 +45,12 @@ import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.sadellie.unitto.core.ui.Formatter
import com.sadellie.unitto.data.units.AbstractUnit
import com.sadellie.unitto.data.units.NumberBaseUnit
import com.sadellie.unitto.data.units.UnitGroup
import com.sadellie.unitto.feature.unitslist.components.ChipsRow
import com.sadellie.unitto.feature.unitslist.components.SearchBar
import com.sadellie.unitto.feature.unitslist.components.SearchPlaceholder
import com.sadellie.unitto.feature.unitslist.components.UnitGroupHeader
import com.sadellie.unitto.feature.unitslist.components.UnitListItem
import java.math.BigDecimal
/**
* Left side screen. Unit to convert from.
@ -66,7 +62,7 @@ import java.math.BigDecimal
* @param selectAction Action to perform when user clicks on [UnitListItem].
*/
@Composable
fun LeftSideScreen(
internal fun LeftSideScreen(
viewModel: SecondViewModel,
currentUnitId: String,
navigateUp: () -> Unit,
@ -154,110 +150,3 @@ fun LeftSideScreen(
}
}
}
/**
* Right side screen. Unit to convert to.
*
* @param viewModel [SecondViewModel].
* @param currentUnit Currently selected [AbstractUnit].
* @param navigateUp Action to navigate up. Called when user click back button.
* @param navigateToSettingsAction Action to perform when clicking settings chip at the end.
* @param selectAction Action to perform when user clicks on [UnitListItem].
* @param inputValue Current input value (upper text field on MainScreen)
* @param unitFrom Unit we are converting from. Need it for conversion.
*/
@Composable
fun RightSideScreen(
viewModel: SecondViewModel,
currentUnit: String,
navigateUp: () -> Unit,
navigateToSettingsAction: () -> Unit,
selectAction: (AbstractUnit) -> Unit,
inputValue: String,
unitFrom: AbstractUnit
) {
val uiState = viewModel.mainFlow.collectAsStateWithLifecycle()
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(rememberTopAppBarState())
val focusManager = LocalFocusManager.current
val inputAsBigDecimal: BigDecimal? = try {
inputValue.toBigDecimal()
} catch (e: NumberFormatException) {
null
}
val convertMethod: (AbstractUnit) -> String = when {
unitFrom.group == UnitGroup.NUMBER_BASE -> {{
convertForSecondaryNumberBase(inputValue, unitFrom as NumberBaseUnit, it as NumberBaseUnit)
}}
inputAsBigDecimal != null -> {{
convertForSecondary(inputAsBigDecimal, unitFrom, it)
}}
else -> {{""}}
}
Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
SearchBar(
title = stringResource(R.string.units_screen_to),
value = uiState.value.searchQuery,
onValueChange = {
viewModel.onSearchQueryChange(it, false)
},
favoritesOnly = uiState.value.favoritesOnly,
favoriteAction = {
viewModel.toggleFavoritesOnly(false)
},
navigateUpAction = navigateUp,
focusManager = focusManager,
scrollBehavior = scrollBehavior
)
}
) { paddingValues ->
Crossfade(
targetState = uiState.value.unitsToShow.isEmpty(),
modifier = Modifier.padding(paddingValues)
) { noUnits ->
if (noUnits) {
SearchPlaceholder(navigateToSettingsAction = navigateToSettingsAction)
} else {
LazyColumn(Modifier.fillMaxSize()) {
uiState.value.unitsToShow.forEach { (unitGroup, listOfUnits) ->
item(unitGroup.name) {
UnitGroupHeader(Modifier.animateItemPlacement(), unitGroup)
}
items(listOfUnits, { it.unitId }) { unit ->
UnitListItem(
modifier = Modifier.animateItemPlacement(),
unit = unit,
isSelected = currentUnit == unit.unitId,
selectAction = {
selectAction(it)
viewModel.onSearchQueryChange("")
focusManager.clearFocus(true)
navigateUp()
},
favoriteAction = { viewModel.favoriteUnit(it) },
convertValue = convertMethod
)
}
}
}
}
}
}
}
internal fun convertForSecondary(inputValue: BigDecimal, unitFrom: AbstractUnit, unitTo: AbstractUnit): String {
return Formatter.format(
unitFrom.convert(unitTo, inputValue, 3).toPlainString()
) + " "
}
internal fun convertForSecondaryNumberBase(inputValue: String, unitFrom: NumberBaseUnit, unitTo: NumberBaseUnit): String {
return try {
unitFrom.convertToBase(inputValue, unitTo.base) + " "
} catch (e: NumberFormatException) {
""
}
}

View File

@ -0,0 +1,150 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2022-2023 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.sadellie.unitto.feature.unitslist
import androidx.compose.animation.Crossfade
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Scaffold
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.sadellie.unitto.core.ui.Formatter
import com.sadellie.unitto.data.units.AbstractUnit
import com.sadellie.unitto.data.units.NumberBaseUnit
import com.sadellie.unitto.data.units.UnitGroup
import com.sadellie.unitto.feature.unitslist.components.SearchBar
import com.sadellie.unitto.feature.unitslist.components.SearchPlaceholder
import com.sadellie.unitto.feature.unitslist.components.UnitGroupHeader
import com.sadellie.unitto.feature.unitslist.components.UnitListItem
import java.math.BigDecimal
/**
* Right side screen. Unit to convert to.
*
* @param viewModel [SecondViewModel].
* @param currentUnit Currently selected [AbstractUnit].
* @param navigateUp Action to navigate up. Called when user click back button.
* @param navigateToSettingsAction Action to perform when clicking settings chip at the end.
* @param selectAction Action to perform when user clicks on [UnitListItem].
* @param inputValue Current input value (upper text field on MainScreen)
* @param unitFrom Unit we are converting from. Need it for conversion.
*/
@Composable
internal fun RightSideScreen(
viewModel: SecondViewModel,
currentUnit: String,
navigateUp: () -> Unit,
navigateToSettingsAction: () -> Unit,
selectAction: (AbstractUnit) -> Unit,
inputValue: String,
unitFrom: AbstractUnit
) {
val uiState = viewModel.mainFlow.collectAsStateWithLifecycle()
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(rememberTopAppBarState())
val focusManager = LocalFocusManager.current
val inputAsBigDecimal: BigDecimal? = try {
inputValue.toBigDecimal()
} catch (e: NumberFormatException) {
null
}
val convertMethod: (AbstractUnit) -> String = when {
unitFrom.group == UnitGroup.NUMBER_BASE -> {{
convertForSecondaryNumberBase(inputValue, unitFrom as NumberBaseUnit, it as NumberBaseUnit)
}}
inputAsBigDecimal != null -> {{
convertForSecondary(inputAsBigDecimal, unitFrom, it)
}}
else -> {{""}}
}
Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
SearchBar(
title = stringResource(R.string.units_screen_to),
value = uiState.value.searchQuery,
onValueChange = {
viewModel.onSearchQueryChange(it, false)
},
favoritesOnly = uiState.value.favoritesOnly,
favoriteAction = {
viewModel.toggleFavoritesOnly(false)
},
navigateUpAction = navigateUp,
focusManager = focusManager,
scrollBehavior = scrollBehavior
)
}
) { paddingValues ->
Crossfade(
targetState = uiState.value.unitsToShow.isEmpty(),
modifier = Modifier.padding(paddingValues)
) { noUnits ->
if (noUnits) {
SearchPlaceholder(navigateToSettingsAction = navigateToSettingsAction)
} else {
LazyColumn(Modifier.fillMaxSize()) {
uiState.value.unitsToShow.forEach { (unitGroup, listOfUnits) ->
item(unitGroup.name) {
UnitGroupHeader(Modifier.animateItemPlacement(), unitGroup)
}
items(listOfUnits, { it.unitId }) { unit ->
UnitListItem(
modifier = Modifier.animateItemPlacement(),
unit = unit,
isSelected = currentUnit == unit.unitId,
selectAction = {
selectAction(it)
viewModel.onSearchQueryChange("")
focusManager.clearFocus(true)
navigateUp()
},
favoriteAction = { viewModel.favoriteUnit(it) },
convertValue = convertMethod
)
}
}
}
}
}
}
}
private fun convertForSecondary(inputValue: BigDecimal, unitFrom: AbstractUnit, unitTo: AbstractUnit): String {
return Formatter.format(
unitFrom.convert(unitTo, inputValue, 3).toPlainString()
) + " "
}
private fun convertForSecondaryNumberBase(inputValue: String, unitFrom: NumberBaseUnit, unitTo: NumberBaseUnit): String {
return try {
unitFrom.convertToBase(inputValue, unitTo.base) + " "
} catch (e: NumberFormatException) {
""
}
}

View File

@ -60,7 +60,7 @@ import com.sadellie.unitto.feature.unitslist.R
* @param lazyListState Used for animated scroll when entering unit selection screen
*/
@Composable
fun ChipsRow(
internal fun ChipsRow(
items: List<UnitGroup> = ALL_UNIT_GROUPS,
chosenUnitGroup: UnitGroup?,
selectAction: (UnitGroup) -> Unit,

View File

@ -77,7 +77,7 @@ import com.sadellie.unitto.feature.unitslist.R
* @param scrollBehavior [TopAppBarScrollBehavior] that is used for collapsing and container color
*/
@Composable
fun SearchBar(
internal fun SearchBar(
title: String,
value: String,
onValueChange: (String) -> Unit,

View File

@ -43,7 +43,7 @@ import com.sadellie.unitto.feature.unitslist.R
* @param navigateToSettingsAction Action to perform when clicking open settings button.
*/
@Composable
fun SearchPlaceholder(navigateToSettingsAction: () -> Unit) {
internal fun SearchPlaceholder(navigateToSettingsAction: () -> Unit) {
Column(
modifier = Modifier
.fillMaxWidth()

View File

@ -27,7 +27,7 @@ import com.sadellie.unitto.core.ui.common.Header
import com.sadellie.unitto.data.units.UnitGroup
@Composable
fun UnitGroupHeader(modifier: Modifier, unitGroup: UnitGroup) {
internal fun UnitGroupHeader(modifier: Modifier, unitGroup: UnitGroup) {
Header(
text = stringResource(unitGroup.res),
modifier = modifier,

View File

@ -145,7 +145,7 @@ private fun BasicUnitListItem(
* @param favoriteAction Function to mark unit as favorite. It's a toggle.
*/
@Composable
fun UnitListItem(
internal fun UnitListItem(
modifier: Modifier,
unit: AbstractUnit,
isSelected: Boolean,
@ -170,7 +170,7 @@ fun UnitListItem(
* @param convertValue Function to call that will convert this unit.
*/
@Composable
fun UnitListItem(
internal fun UnitListItem(
modifier: Modifier,
unit: AbstractUnit,
isSelected: Boolean,