mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-19 00:35:26 +02:00
Visibility modifiers and code clean up
Marked stuff as internal and private.
This commit is contained in:
parent
8b9db717c4
commit
c68c1c458a
@ -40,11 +40,12 @@ import androidx.lifecycle.viewmodel.compose.viewModel
|
|||||||
import com.sadellie.unitto.core.ui.common.AnimatedTopBarText
|
import com.sadellie.unitto.core.ui.common.AnimatedTopBarText
|
||||||
import com.sadellie.unitto.feature.R
|
import com.sadellie.unitto.feature.R
|
||||||
import com.sadellie.unitto.feature.converter.components.Keyboard
|
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 com.sadellie.unitto.feature.converter.components.TopScreenPart
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun MainScreen(
|
internal fun MainScreen(
|
||||||
navigateToLeftScreen: (String) -> Unit,
|
navigateToLeftScreen: (String) -> Unit,
|
||||||
navigateToRightScreen: (unitFrom: String, unitTo: String, input: String) -> Unit,
|
navigateToRightScreen: (unitFrom: String, unitTo: String, input: String) -> Unit,
|
||||||
navigateToSettings: () -> Unit,
|
navigateToSettings: () -> Unit,
|
||||||
|
@ -68,7 +68,7 @@ import com.sadellie.unitto.feature.converter.ConverterMode
|
|||||||
* @param converterMode
|
* @param converterMode
|
||||||
*/
|
*/
|
||||||
@Composable
|
@Composable
|
||||||
fun Keyboard(
|
internal fun Keyboard(
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
addDigit: (String) -> Unit = {},
|
addDigit: (String) -> Unit = {},
|
||||||
deleteDigit: () -> Unit = {},
|
deleteDigit: () -> Unit = {},
|
||||||
|
@ -47,7 +47,7 @@ import com.sadellie.unitto.core.ui.theme.NumbersTextStyleTitleLarge
|
|||||||
* @param onClick Action to perform when clicking this button.
|
* @param onClick Action to perform when clicking this button.
|
||||||
*/
|
*/
|
||||||
@Composable
|
@Composable
|
||||||
fun KeyboardButton(
|
internal fun KeyboardButton(
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
digit: String,
|
digit: String,
|
||||||
isPrimary: Boolean = true,
|
isPrimary: Boolean = true,
|
||||||
|
@ -64,7 +64,7 @@ import com.sadellie.unitto.feature.R
|
|||||||
* @param textToCopy Text that will be copied to clipboard when long-clicking.
|
* @param textToCopy Text that will be copied to clipboard when long-clicking.
|
||||||
*/
|
*/
|
||||||
@Composable
|
@Composable
|
||||||
fun MyTextField(
|
internal fun MyTextField(
|
||||||
modifier: Modifier,
|
modifier: Modifier,
|
||||||
primaryText: @Composable () -> String,
|
primaryText: @Composable () -> String,
|
||||||
secondaryText: String?,
|
secondaryText: String?,
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Unitto is a unit converter for Android
|
* 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
|
* 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
|
* 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/>.
|
* 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 android.content.res.Configuration
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
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].
|
* When Landscape mode will place [content1] and [content2] in a [Row].
|
||||||
*/
|
*/
|
||||||
@Composable
|
@Composable
|
||||||
fun PortraitLandscape(
|
internal fun PortraitLandscape(
|
||||||
modifier: Modifier,
|
modifier: Modifier,
|
||||||
content1: @Composable (Modifier) -> Unit,
|
content1: @Composable (Modifier) -> Unit,
|
||||||
content2: @Composable (Modifier) -> Unit,
|
content2: @Composable (Modifier) -> Unit,
|
@ -64,7 +64,7 @@ import com.sadellie.unitto.feature.converter.ConverterMode
|
|||||||
* @param converterMode [ConverterMode.BASE] doesn't use formatting for input/output.
|
* @param converterMode [ConverterMode.BASE] doesn't use formatting for input/output.
|
||||||
*/
|
*/
|
||||||
@Composable
|
@Composable
|
||||||
fun TopScreenPart(
|
internal fun TopScreenPart(
|
||||||
modifier: Modifier,
|
modifier: Modifier,
|
||||||
inputValue: String,
|
inputValue: String,
|
||||||
calculatedValue: String?,
|
calculatedValue: String?,
|
||||||
|
@ -45,7 +45,7 @@ import com.sadellie.unitto.feature.R
|
|||||||
* @param label Text on button
|
* @param label Text on button
|
||||||
*/
|
*/
|
||||||
@Composable
|
@Composable
|
||||||
fun UnitSelectionButton(
|
internal fun UnitSelectionButton(
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
onClick: () -> Unit = {},
|
onClick: () -> Unit = {},
|
||||||
label: Int?,
|
label: Int?,
|
||||||
|
@ -46,7 +46,7 @@ import com.sadellie.unitto.feature.R
|
|||||||
import com.sadellie.unitto.feature.settings.components.AlertDialogWithList
|
import com.sadellie.unitto.feature.settings.components.AlertDialogWithList
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun AboutScreen(
|
internal fun AboutScreen(
|
||||||
navigateUpAction: () -> Unit,
|
navigateUpAction: () -> Unit,
|
||||||
navigateToThirdParty: () -> Unit
|
navigateToThirdParty: () -> Unit
|
||||||
) {
|
) {
|
||||||
|
@ -56,7 +56,7 @@ import com.sadellie.unitto.feature.settings.navigation.themesRoute
|
|||||||
import com.sadellie.unitto.feature.settings.navigation.unitsGroupRoute
|
import com.sadellie.unitto.feature.settings.navigation.unitsGroupRoute
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun SettingsScreen(
|
internal fun SettingsScreen(
|
||||||
viewModel: SettingsViewModel,
|
viewModel: SettingsViewModel,
|
||||||
navigateUpAction: () -> Unit,
|
navigateUpAction: () -> Unit,
|
||||||
navControllerAction: (String) -> Unit
|
navControllerAction: (String) -> Unit
|
||||||
|
@ -31,14 +31,14 @@ import androidx.compose.material.icons.filled.Palette
|
|||||||
import androidx.compose.material3.Icon
|
import androidx.compose.material3.Icon
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.ui.res.stringResource
|
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.UnittoLargeTopAppBar
|
||||||
import com.sadellie.unitto.core.ui.common.UnittoListItem
|
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.ThemingMode
|
||||||
import io.github.sadellie.themmo.ThemmoController
|
import io.github.sadellie.themmo.ThemmoController
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun ThemesScreen(
|
internal fun ThemesScreen(
|
||||||
navigateUpAction: () -> Unit = {},
|
navigateUpAction: () -> Unit = {},
|
||||||
themmoController: ThemmoController,
|
themmoController: ThemmoController,
|
||||||
viewModel: SettingsViewModel
|
viewModel: SettingsViewModel
|
||||||
|
@ -48,7 +48,7 @@ import com.sadellie.unitto.feature.R
|
|||||||
*/
|
*/
|
||||||
@Stable
|
@Stable
|
||||||
@Composable
|
@Composable
|
||||||
fun ThirdPartyLicensesScreen(
|
internal fun ThirdPartyLicensesScreen(
|
||||||
navigateUpAction: () -> Unit = {}
|
navigateUpAction: () -> Unit = {}
|
||||||
) {
|
) {
|
||||||
val mContext = LocalContext.current
|
val mContext = LocalContext.current
|
||||||
|
@ -47,9 +47,9 @@ import androidx.compose.ui.Modifier
|
|||||||
import androidx.compose.ui.draw.clip
|
import androidx.compose.ui.draw.clip
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.unit.dp
|
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.Header
|
||||||
import com.sadellie.unitto.core.ui.common.UnittoLargeTopAppBar
|
import com.sadellie.unitto.core.ui.common.UnittoLargeTopAppBar
|
||||||
|
import com.sadellie.unitto.feature.R
|
||||||
import org.burnoutcrew.reorderable.ReorderableItem
|
import org.burnoutcrew.reorderable.ReorderableItem
|
||||||
import org.burnoutcrew.reorderable.detectReorder
|
import org.burnoutcrew.reorderable.detectReorder
|
||||||
import org.burnoutcrew.reorderable.detectReorderAfterLongPress
|
import org.burnoutcrew.reorderable.detectReorderAfterLongPress
|
||||||
@ -57,7 +57,7 @@ import org.burnoutcrew.reorderable.rememberReorderableLazyListState
|
|||||||
import org.burnoutcrew.reorderable.reorderable
|
import org.burnoutcrew.reorderable.reorderable
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun UnitGroupsScreen(
|
internal fun UnitGroupsScreen(
|
||||||
viewModel: SettingsViewModel,
|
viewModel: SettingsViewModel,
|
||||||
navigateUpAction: () -> Unit
|
navigateUpAction: () -> Unit
|
||||||
) {
|
) {
|
||||||
|
@ -51,7 +51,7 @@ import com.sadellie.unitto.feature.settings.R
|
|||||||
* @param supportText Text above list of options
|
* @param supportText Text above list of options
|
||||||
*/
|
*/
|
||||||
@Composable
|
@Composable
|
||||||
fun AlertDialogWithList(
|
internal fun AlertDialogWithList(
|
||||||
title: String,
|
title: String,
|
||||||
listItems: Map<Int, Int> = emptyMap(),
|
listItems: Map<Int, Int> = emptyMap(),
|
||||||
selectedItemIndex: Int = 0,
|
selectedItemIndex: Int = 0,
|
||||||
|
@ -32,11 +32,11 @@ import com.sadellie.unitto.feature.settings.UnitGroupsScreen
|
|||||||
import io.github.sadellie.themmo.ThemmoController
|
import io.github.sadellie.themmo.ThemmoController
|
||||||
|
|
||||||
const val settingsGraph = "settings_graph"
|
const val settingsGraph = "settings_graph"
|
||||||
const val settingsRoute = "settings_route"
|
private const val settingsRoute = "settings_route"
|
||||||
const val themesRoute = "themes_route"
|
internal const val themesRoute = "themes_route"
|
||||||
const val unitsGroupRoute = "units_group_route"
|
internal const val unitsGroupRoute = "units_group_route"
|
||||||
const val thirdPartyRoute = "third_party_route"
|
internal const val thirdPartyRoute = "third_party_route"
|
||||||
const val aboutRoute = "about_route"
|
internal const val aboutRoute = "about_route"
|
||||||
|
|
||||||
fun NavController.navigateToSettings() {
|
fun NavController.navigateToSettings() {
|
||||||
navigate(settingsRoute)
|
navigate(settingsRoute)
|
||||||
|
21
feature/unitslist/proguard-rules.pro
vendored
21
feature/unitslist/proguard-rules.pro
vendored
@ -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
|
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Unitto is a unit converter for Android
|
* 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
|
* 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
|
* 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.res.stringResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
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.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.ChipsRow
|
||||||
import com.sadellie.unitto.feature.unitslist.components.SearchBar
|
import com.sadellie.unitto.feature.unitslist.components.SearchBar
|
||||||
import com.sadellie.unitto.feature.unitslist.components.SearchPlaceholder
|
import com.sadellie.unitto.feature.unitslist.components.SearchPlaceholder
|
||||||
import com.sadellie.unitto.feature.unitslist.components.UnitGroupHeader
|
import com.sadellie.unitto.feature.unitslist.components.UnitGroupHeader
|
||||||
import com.sadellie.unitto.feature.unitslist.components.UnitListItem
|
import com.sadellie.unitto.feature.unitslist.components.UnitListItem
|
||||||
import java.math.BigDecimal
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Left side screen. Unit to convert from.
|
* 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].
|
* @param selectAction Action to perform when user clicks on [UnitListItem].
|
||||||
*/
|
*/
|
||||||
@Composable
|
@Composable
|
||||||
fun LeftSideScreen(
|
internal fun LeftSideScreen(
|
||||||
viewModel: SecondViewModel,
|
viewModel: SecondViewModel,
|
||||||
currentUnitId: String,
|
currentUnitId: String,
|
||||||
navigateUp: () -> Unit,
|
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) {
|
|
||||||
""
|
|
||||||
}
|
|
||||||
}
|
|
@ -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) {
|
||||||
|
""
|
||||||
|
}
|
||||||
|
}
|
@ -60,7 +60,7 @@ import com.sadellie.unitto.feature.unitslist.R
|
|||||||
* @param lazyListState Used for animated scroll when entering unit selection screen
|
* @param lazyListState Used for animated scroll when entering unit selection screen
|
||||||
*/
|
*/
|
||||||
@Composable
|
@Composable
|
||||||
fun ChipsRow(
|
internal fun ChipsRow(
|
||||||
items: List<UnitGroup> = ALL_UNIT_GROUPS,
|
items: List<UnitGroup> = ALL_UNIT_GROUPS,
|
||||||
chosenUnitGroup: UnitGroup?,
|
chosenUnitGroup: UnitGroup?,
|
||||||
selectAction: (UnitGroup) -> Unit,
|
selectAction: (UnitGroup) -> Unit,
|
||||||
|
@ -77,7 +77,7 @@ import com.sadellie.unitto.feature.unitslist.R
|
|||||||
* @param scrollBehavior [TopAppBarScrollBehavior] that is used for collapsing and container color
|
* @param scrollBehavior [TopAppBarScrollBehavior] that is used for collapsing and container color
|
||||||
*/
|
*/
|
||||||
@Composable
|
@Composable
|
||||||
fun SearchBar(
|
internal fun SearchBar(
|
||||||
title: String,
|
title: String,
|
||||||
value: String,
|
value: String,
|
||||||
onValueChange: (String) -> Unit,
|
onValueChange: (String) -> Unit,
|
||||||
|
@ -43,7 +43,7 @@ import com.sadellie.unitto.feature.unitslist.R
|
|||||||
* @param navigateToSettingsAction Action to perform when clicking open settings button.
|
* @param navigateToSettingsAction Action to perform when clicking open settings button.
|
||||||
*/
|
*/
|
||||||
@Composable
|
@Composable
|
||||||
fun SearchPlaceholder(navigateToSettingsAction: () -> Unit) {
|
internal fun SearchPlaceholder(navigateToSettingsAction: () -> Unit) {
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
|
@ -27,7 +27,7 @@ import com.sadellie.unitto.core.ui.common.Header
|
|||||||
import com.sadellie.unitto.data.units.UnitGroup
|
import com.sadellie.unitto.data.units.UnitGroup
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun UnitGroupHeader(modifier: Modifier, unitGroup: UnitGroup) {
|
internal fun UnitGroupHeader(modifier: Modifier, unitGroup: UnitGroup) {
|
||||||
Header(
|
Header(
|
||||||
text = stringResource(unitGroup.res),
|
text = stringResource(unitGroup.res),
|
||||||
modifier = modifier,
|
modifier = modifier,
|
||||||
|
@ -145,7 +145,7 @@ private fun BasicUnitListItem(
|
|||||||
* @param favoriteAction Function to mark unit as favorite. It's a toggle.
|
* @param favoriteAction Function to mark unit as favorite. It's a toggle.
|
||||||
*/
|
*/
|
||||||
@Composable
|
@Composable
|
||||||
fun UnitListItem(
|
internal fun UnitListItem(
|
||||||
modifier: Modifier,
|
modifier: Modifier,
|
||||||
unit: AbstractUnit,
|
unit: AbstractUnit,
|
||||||
isSelected: Boolean,
|
isSelected: Boolean,
|
||||||
@ -170,7 +170,7 @@ fun UnitListItem(
|
|||||||
* @param convertValue Function to call that will convert this unit.
|
* @param convertValue Function to call that will convert this unit.
|
||||||
*/
|
*/
|
||||||
@Composable
|
@Composable
|
||||||
fun UnitListItem(
|
internal fun UnitListItem(
|
||||||
modifier: Modifier,
|
modifier: Modifier,
|
||||||
unit: AbstractUnit,
|
unit: AbstractUnit,
|
||||||
isSelected: Boolean,
|
isSelected: Boolean,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user