Skip to content

Commit

Permalink
v3.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
sosauce committed Jan 29, 2025
1 parent 9105cbe commit db2a2e4
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 54 deletions.
8 changes: 6 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
applicationId = "com.sosauce.cutecalc"
minSdk = 21
targetSdk = 35
versionCode = 37
versionName = "3.3.1"
versionCode = 38
versionName = "3.3.2"
}

buildTypes {
Expand All @@ -41,6 +41,10 @@ android {
buildFeatures {
compose = true
}
dependenciesInfo {
includeInApk = false
includeInBundle = false
}
}

dependencies {
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/com/sosauce/cutecalc/AppBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -32,7 +31,6 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -65,7 +63,6 @@ fun AppBar(
}
}
},
colors = TopAppBarDefaults.topAppBarColors(Color.Transparent),
actions = {
if (!showBackArrow) {
Spacer(Modifier.width(5.dp))
Expand Down
48 changes: 48 additions & 0 deletions app/src/main/java/com/sosauce/cutecalc/components/CuteText.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.sosauce.cutecalc.components

import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextLayoutResult
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.TextUnit
import com.sosauce.cutecalc.logic.rememberUseSystemFont
import com.sosauce.cutecalc.ui.theme.GlobalFont

@Composable
fun CuteText(
text: String,
modifier: Modifier = Modifier,
color: Color = Color.Unspecified,
fontSize: TextUnit = TextUnit.Unspecified,
textAlign: TextAlign? = null,
maxLines: Int = Int.MAX_VALUE,
style: TextStyle = LocalTextStyle.current,
onTextLayout: ((TextLayoutResult) -> Unit)? = null,
overflow: TextOverflow = TextOverflow.Clip,
) {
val useSystemFont by rememberUseSystemFont()
val fontFamily = if (useSystemFont) {
null
} else {
GlobalFont
}

Text(
text = text,
modifier = modifier,
color = color,
fontSize = fontSize,
textAlign = textAlign,
maxLines = maxLines,
fontFamily = fontFamily,
style = style,
onTextLayout = onTextLayout,
overflow = overflow
)
}
25 changes: 17 additions & 8 deletions app/src/main/java/com/sosauce/cutecalc/components/Switches.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.sosauce.cutecalc.logic.rememberUseAmoledMode
import com.sosauce.cutecalc.logic.rememberUseButtonsAnimation
import com.sosauce.cutecalc.logic.rememberUseDarkMode
import com.sosauce.cutecalc.logic.rememberUseHistory
import com.sosauce.cutecalc.logic.rememberUseSystemFont
import com.sosauce.cutecalc.logic.rememberVibration
import com.sosauce.cutecalc.ui.theme.GlobalFont

Expand Down Expand Up @@ -66,6 +67,7 @@ fun Misc() {
var decimalSetting by rememberDecimal()
var buttonVibrationSetting by rememberVibration()
var useButtonsAnimation by rememberUseButtonsAnimation()
var useSystemFont by rememberUseSystemFont()

Column {
Text(
Expand All @@ -75,17 +77,24 @@ fun Misc() {
modifier = Modifier.padding(horizontal = 34.dp, vertical = 8.dp)
)

// SettingsCards(
// checked = decimalSetting,
// onCheckedChange = { decimalSetting = !decimalSetting },
// topDp = 24.dp,
// bottomDp = 4.dp,
// text = "Decimal Formatting"
// )
SettingsCards(
checked = decimalSetting,
onCheckedChange = { decimalSetting = !decimalSetting },
topDp = 24.dp,
bottomDp = 4.dp,
text = stringResource(R.string.decimal_formatting)
)
SettingsCards(
checked = useSystemFont,
onCheckedChange = { useSystemFont = !useSystemFont },
topDp = 4.dp,
bottomDp = 4.dp,
text = stringResource(R.string.use_sys_font)
)
SettingsCards(
checked = useButtonsAnimation,
onCheckedChange = { useButtonsAnimation = !useButtonsAnimation },
topDp = 24.dp,
topDp = 4.dp,
bottomDp = 4.dp,
text = stringResource(R.string.buttons_anim)
)
Expand Down
16 changes: 11 additions & 5 deletions app/src/main/java/com/sosauce/cutecalc/history/HistoryViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ class HistoryViewModel(
fun onEvent(event: HistoryEvents) {
when (event) {
is HistoryEvents.AddCalculation -> {
val calculation = Calculation(
operation = state.value.operation.value,
result = state.value.result.value
)
viewModelScope.launch { dao.insertCalculation(calculation) }

// Only save to history calculations that are not any kind of errors
if (state.value.result.value.all { it.isDigit() || it == '.' || it == '-' || it == ','}) {
val calculation = Calculation(
operation = state.value.operation.value,
result = state.value.result.value
)
viewModelScope.launch { dao.insertCalculation(calculation) }
} else {
return
}
_state.update {
it.copy(
operation = mutableStateOf(""),
Expand Down
24 changes: 14 additions & 10 deletions app/src/main/java/com/sosauce/cutecalc/logic/FormatNumber.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.sosauce.cutecalc.logic

import java.text.DecimalFormat
import java.text.NumberFormat
import java.util.Locale

fun formatNumber(numberString: String): String {
val number = try {
numberString.toDouble()
} catch (e: NumberFormatException) {
return numberString // Return as is if parsing to double fails
}
// A bit wonky ik
fun formatOrNot(
expression: String,
shouldFormat: Boolean
): String {
val numberFormat = NumberFormat.getNumberInstance(Locale.getDefault())
val regex = """\d+(\.\d+)?""".toRegex()

val formatter = DecimalFormat("#,###.##")

return formatter.format(number)
return if (shouldFormat) {
regex.replace(expression) { matchResult ->
numberFormat.format(matchResult.value.toDouble())
}
} else expression
}
5 changes: 5 additions & 0 deletions app/src/main/java/com/sosauce/cutecalc/logic/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ data object PreferencesKeys {
val ENABLE_HISTORY = booleanPreferencesKey("enable_history")
val SORT_HISTORY_ASC = booleanPreferencesKey("sort_history_asc")
val USE_BUTTONS_ANIMATIONS = booleanPreferencesKey("use_buttons_animation")
val USE_SYSTEM_FONT = booleanPreferencesKey("use_system_font")
}

@Composable
Expand Down Expand Up @@ -53,3 +54,7 @@ fun rememberSortHistoryASC() =
fun rememberUseButtonsAnimation() =
rememberPreference(key = PreferencesKeys.USE_BUTTONS_ANIMATIONS, defaultValue = true)

@Composable
fun rememberUseSystemFont() =
rememberPreference(key = PreferencesKeys.USE_SYSTEM_FONT, defaultValue = false)

25 changes: 11 additions & 14 deletions app/src/main/java/com/sosauce/cutecalc/screens/Calculator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.platform.InterceptPlatformTextInput
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
Expand All @@ -44,10 +45,11 @@ import com.sosauce.cutecalc.history.HistoryViewModel
import com.sosauce.cutecalc.logic.CalcAction
import com.sosauce.cutecalc.logic.CalcViewModel
import com.sosauce.cutecalc.logic.Evaluator
import com.sosauce.cutecalc.logic.formatOrNot
import com.sosauce.cutecalc.logic.navigation.Screens
import com.sosauce.cutecalc.logic.rememberDecimal
import com.sosauce.cutecalc.logic.rememberUseHistory
import com.sosauce.cutecalc.ui.theme.GlobalFont
import kotlinx.coroutines.awaitCancellation


@SuppressLint("NewApi")
Expand All @@ -68,6 +70,7 @@ fun CalculatorUI(
val fifthRow = listOf("1", "2", "3", "+")
val sixthRow = listOf("0", ".")
val textColor = MaterialTheme.colorScheme.onBackground
val decimalSetting by rememberDecimal()


if (portraitMode != Configuration.ORIENTATION_PORTRAIT) {
Expand Down Expand Up @@ -123,8 +126,9 @@ fun CalculatorUI(
}
},
update = { view ->
view.setText(viewModel.preview)
view.setSelection(viewModel.preview.length)
view.setText(formatOrNot(viewModel.preview, decimalSetting))
// Added decimal points and comma need to be taken account for !
view.setSelection(formatOrNot(viewModel.preview, decimalSetting).length)
}
)
}
Expand All @@ -134,7 +138,7 @@ fun CalculatorUI(
) {
DisableSoftKeyboard {
BasicTextField(
value = viewModel.displayText,
value = TextFieldValue(formatOrNot(viewModel.displayText.text, decimalSetting)),
onValueChange = { viewModel.displayText = it },
singleLine = true,
textStyle = TextStyle(
Expand Down Expand Up @@ -320,18 +324,11 @@ fun CalculatorUI(
// https://stackoverflow.com/a/78720287
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun DisableSoftKeyboard(
disable: Boolean = true,
content: @Composable () -> Unit,
) {
fun DisableSoftKeyboard(content: @Composable () -> Unit) {
InterceptPlatformTextInput(
interceptor = { request, nextHandler ->
if (!disable) {
nextHandler.startInputMethod(request)
} else {
awaitCancellation()
}
nextHandler.startInputMethod(request)
},
content = content,
content = content
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ import com.sosauce.cutecalc.R
import com.sosauce.cutecalc.history.Calculation
import com.sosauce.cutecalc.history.HistoryEvents
import com.sosauce.cutecalc.history.HistoryState
import com.sosauce.cutecalc.logic.formatOrNot
import com.sosauce.cutecalc.logic.navigation.Screens
import com.sosauce.cutecalc.logic.rememberDecimal
import com.sosauce.cutecalc.logic.rememberSortHistoryASC
import com.sosauce.cutecalc.logic.rememberUseHistory
import com.sosauce.cutecalc.ui.theme.GlobalFont
Expand Down Expand Up @@ -141,6 +143,8 @@ private fun CalculationItem(
bottomDp: Dp,
modifier: Modifier = Modifier
) {
val decimalSetting by rememberDecimal()

Card(
modifier = modifier
.padding(horizontal = 10.dp, vertical = 3.dp)
Expand Down Expand Up @@ -169,13 +173,13 @@ private fun CalculationItem(
horizontalAlignment = Alignment.Start
) {
Text(
text = calculation.operation,
text = formatOrNot(calculation.operation, decimalSetting),
fontSize = 20.sp,
modifier = Modifier.basicMarquee(),
fontFamily = GlobalFont
)
Text(
text = "= ${calculation.result}",
text = "= ${formatOrNot(calculation.result, decimalSetting)}",
fontSize = 22.sp,
color = MaterialTheme.colorScheme.onBackground.copy(alpha = 0.5f),
modifier = Modifier.basicMarquee(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
Expand All @@ -34,6 +35,8 @@ import com.sosauce.cutecalc.history.HistoryViewModel
import com.sosauce.cutecalc.logic.CalcAction
import com.sosauce.cutecalc.logic.CalcViewModel
import com.sosauce.cutecalc.logic.Evaluator
import com.sosauce.cutecalc.logic.formatOrNot
import com.sosauce.cutecalc.logic.rememberDecimal
import com.sosauce.cutecalc.logic.rememberUseHistory
import com.sosauce.cutecalc.ui.theme.GlobalFont

Expand All @@ -45,6 +48,8 @@ fun LandscapeLayout(
historyState: HistoryState
) {
val saveToHistory by rememberUseHistory()
val decimalSetting by rememberDecimal()


Box(
modifier = Modifier
Expand All @@ -66,7 +71,7 @@ fun LandscapeLayout(
) {
DisableSoftKeyboard {
BasicTextField(
value = viewModel.displayText,
value = TextFieldValue(formatOrNot(viewModel.displayText.text, decimalSetting)),
onValueChange = { viewModel.displayText = it },
singleLine = true,
textStyle = TextStyle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.sosauce.cutecalc.screens
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -40,6 +42,7 @@ fun SettingsScreen(
Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(rememberScrollState())
.padding(values),
horizontalAlignment = Alignment.CenterHorizontally
) {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<string name="update">Check updates</string>
<string name="cc_by_sosauce">CuteCalc by sosauce</string>
<string name="follow_sys">Follow system</string>
<string name="use_sys_font">Use system font</string>
<string name="buttons_anim">Buttons animation</string>
<string name="history">History</string>
<string name="use_history">Use history</string>
Expand All @@ -17,4 +18,5 @@
<string name="haptic_feedback">Haptic feedback</string>
<string name="ascending">Ascending</string>
<string name="descending">Descending</string>
<string name="decimal_formatting">Decimal formatting</string>
</resources>
Loading

0 comments on commit db2a2e4

Please sign in to comment.