Skip to content

Commit

Permalink
Merge pull request #38 from jedi1150/update_2.2.2
Browse files Browse the repository at this point in the history
Update 2.2.2
  • Loading branch information
jedi1150 authored Jul 31, 2024
2 parents 08d248f + 71825f1 commit 3e10756
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 25 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ android {
minSdk = 21
targetSdk = 34
versionCode = 1
versionName = "2.2.1"
versionName = "2.2.2"
}
buildTypes {
named("release") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class NumberSystemDataSource @Inject constructor(

if (from.value.isEmpty()) return@coroutineScope null

return@coroutineScope withContext(Dispatchers.Default) { numSys.convert(numberSystem = from.asInternalModel(), targetRadix = toRadix).asExternalModel() }
return@coroutineScope withContext(Dispatchers.Default) {
numSys.convert(
numberSystem = from.asInternalModel(),
targetRadix = toRadix,
ignoreCase = toRadix.value in Radix.BIN.value..Radix.HEX.value,
).asExternalModel()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,11 @@ class CalculatorViewModel @Inject constructor(private val numSys: NumSys) : View
}
}.map { toRadix ->
try {
numSys.convert(numberSystem = from, targetRadix = toRadix)
numSys.convert(
numberSystem = from,
targetRadix = toRadix,
ignoreCase = toRadix.value in Radix.BIN.value..Radix.HEX.value,
)
} catch (exception: IllegalArgumentException) {
when (calculatorOperandType) {
OperandCustom1 -> numberSystem1Temp.value = NumberSystem(String(), numberSystem1Temp.value.radix)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ class ExplanationViewModel @Inject constructor(private val numSys: NumSys) : Vie
}

viewModelScope.launch {
numSys.convert(numberSystem = updatedFrom.asInternalModel(), targetRadix = toRadix).let { convertedData ->
numSys.convert(
numberSystem = updatedFrom.asInternalModel(),
targetRadix = toRadix,
ignoreCase = toRadix.value in Radix.BIN.value..Radix.HEX.value,
).let { convertedData ->
var finalFrom: NumberSystem = explanationUiState.value.from
var finalTo: NumberSystem = explanationUiState.value.to

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ fun ExplanationFractionalMultiplierContent(from: NumberSystem, to: NumberSystem)

do {
if (fractionMultiplierList.isEmpty()) {
fractionMultiplierList.add(fractionMultiplier(multiplier = getFractional(NumberSystem(fromFractional, from.radix).asInternalModel().toRadix(Radix.DEC).value), multiplicand = to.radix.value))
fractionMultiplierList.add(fractionMultiplier(multiplier = getFractional(NumberSystem(fromFractional, from.radix).asInternalModel().toRadix(Radix.DEC, ignoreCase = true).value), multiplicand = to.radix.value))
} else {
fractionMultiplierList.add(fractionMultiplier(multiplier = getFractional(fractionMultiplierList.last().product), multiplicand = to.radix.value))
}
iterations++
} while (fractionMultiplierList.last().product.toBigDecimal().scale() > 0 && (iterations < getFractional(NumberSystem(fromFractional, from.radix).asInternalModel().toRadix(Radix.DEC).value).toBigDecimal().scale() && iterations < maxIterations))
} while (fractionMultiplierList.last().product.toBigDecimal().scale() > 0 && (iterations < getFractional(NumberSystem(fromFractional, from.radix).asInternalModel().toRadix(Radix.DEC, ignoreCase = true).value).toBigDecimal().scale() && iterations < maxIterations))

Column(
modifier = Modifier.padding(bottom = 8.dp),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fun ExplanationIntegerDivisionContent(from: NumberSystem, to: NumberSystem) {

do {
if (divisionList.isEmpty()) {
divisionList.add(longDivision(dividend = NumberSystem(fromDecimal, from.radix).asInternalModel().toRadix(Radix.DEC).value.toBigDecimal(), divisor = to.radix.value))
divisionList.add(longDivision(dividend = NumberSystem(fromDecimal, from.radix).asInternalModel().toRadix(Radix.DEC, ignoreCase = true).value.toBigDecimal(), divisor = to.radix.value))
} else {
divisionList.add(longDivision(dividend = divisionList.last().quotient, divisor = divisionList.last().divisor))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fun ExplanationToDecimalContent(from: NumberSystem) {
val position = from.value.substringBefore(NS_DELIMITER).length
val filteredValue = from.value.toList().filterNot { char -> char == NS_DELIMITER }

val result = NumSys.convert(numberSystem = from.asInternalModel(), targetRadix = Radix.DEC)
val result = NumSys.convert(numberSystem = from.asInternalModel(), targetRadix = Radix.DEC, ignoreCase = true)

Column(
modifier = Modifier.padding(bottom = 8.dp),
Expand All @@ -58,7 +58,7 @@ fun ExplanationToDecimalContent(from: NumberSystem) {
filteredValue.forEachIndexed { index, value ->
append(value)
if (value.isLetter()) {
val decimalNumber = NumberSystem(value = value.toString(), radix = integerPart.radix).asInternalModel().toRadix(Radix.DEC).value
val decimalNumber = NumberSystem(value = value.toString(), radix = integerPart.radix).asInternalModel().toRadix(Radix.DEC, ignoreCase = true).value
withStyle(SpanStyle(fontFamily = RobotoFamily)) {
append("(")
}
Expand Down
9 changes: 6 additions & 3 deletions lib/api/lib.api
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
public final class numsys/NumSys {
public static final field INSTANCE Lnumsys/NumSys;
public static final fun convert (Ljava/lang/String;II)Ljava/lang/String;
public static final fun convert (Lnumsys/model/NumberSystem;Lnumsys/model/Radix;)Lnumsys/model/NumberSystem;
public static final fun convert (Ljava/lang/String;IIZ)Ljava/lang/String;
public static final fun convert (Lnumsys/model/NumberSystem;Lnumsys/model/Radix;Z)Lnumsys/model/NumberSystem;
public static synthetic fun convert$default (Ljava/lang/String;IIZILjava/lang/Object;)Ljava/lang/String;
public static synthetic fun convert$default (Lnumsys/model/NumberSystem;Lnumsys/model/Radix;ZILjava/lang/Object;)Lnumsys/model/NumberSystem;
public static final fun getFractionalLength ()I
public static final fun setFractionalLength (I)V
public static final fun toRadix (Lnumsys/model/NumberSystem;Lnumsys/model/Radix;)Lnumsys/model/NumberSystem;
public static final fun toRadix (Lnumsys/model/NumberSystem;Lnumsys/model/Radix;Z)Lnumsys/model/NumberSystem;
public static synthetic fun toRadix$default (Lnumsys/model/NumberSystem;Lnumsys/model/Radix;ZILjava/lang/Object;)Lnumsys/model/NumberSystem;
}

public final class numsys/NumSys$Constants {
Expand Down
25 changes: 12 additions & 13 deletions lib/src/main/kotlin/numsys/NumSys.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public object NumSys {
* @return value of target radix.
*/
@JvmStatic
public fun convert(value: String, sourceRadix: Int, targetRadix: Int): String {
public fun convert(value: String, sourceRadix: Int, targetRadix: Int, ignoreCase: Boolean = false): String {
require(value.isNotBlank()) { "Source value must not be empty" }
require(value.replace(".", "").all { char -> char.isLetterOrDigit() }) { "Incorrect source value" }
require(sourceRadix in 2..62 && targetRadix in 2..62) { "Source and target radixes must be in the range from 2 to 62" }

val parts = value.split(".")
val parts = if (ignoreCase) value.lowercase().split(".") else value.split(".")
val integerPart = parts[0]
val fractionalPart = if (parts.size > 1) parts[1] else ""

Expand Down Expand Up @@ -55,16 +55,15 @@ public object NumSys {
* @see Radix
*/
@JvmStatic
public fun convert(numberSystem: NumberSystem, targetRadix: Radix): NumberSystem {
return NumberSystem(
value = convert(
value = numberSystem.value,
sourceRadix = numberSystem.radix.value,
targetRadix = targetRadix.value,
),
radix = targetRadix,
)
}
public fun convert(numberSystem: NumberSystem, targetRadix: Radix, ignoreCase: Boolean = false): NumberSystem = NumberSystem(
value = convert(
value = numberSystem.value,
sourceRadix = numberSystem.radix.value,
targetRadix = targetRadix.value,
ignoreCase = ignoreCase,
),
radix = targetRadix,
)

/**
* Converts [NumberSystem] to another [NumberSystem].
Expand All @@ -75,7 +74,7 @@ public object NumSys {
* @see Radix
*/
@JvmStatic
public fun NumberSystem.toRadix(value: Radix): NumberSystem = convert(this, value)
public fun NumberSystem.toRadix(value: Radix, ignoreCase: Boolean = false): NumberSystem = convert(this, value, ignoreCase)

@JvmStatic
private fun convertIntegerPart(value: String, sourceRadix: Int, targetRadix: Int): String {
Expand Down

0 comments on commit 3e10756

Please sign in to comment.