Skip to content

Commit

Permalink
feat: basic calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
aayush2622 committed May 12, 2024
1 parent 1af6514 commit 0edd1c0
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 45 deletions.
4 changes: 2 additions & 2 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3'
implementation 'androidx.preference:preference-ktx:1.2.1'
implementation 'androidx.webkit:webkit:1.10.0'

implementation 'org.mariuszgromada.math:MathParser.org-mXparser:4.4.2'
implementation 'com.google.android.material:material:1.11.0'
}
98 changes: 98 additions & 0 deletions app/src/main/java/com/aayush/calculator/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.aayush.calculator.databinding.ActivityMainBinding
import com.aayush.calculator.saving.PrefManager
import com.aayush.calculator.saving.PrefName
import org.mariuszgromada.math.mxparser.Expression

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
Expand All @@ -13,6 +15,102 @@ class MainActivity : AppCompatActivity() {
ThemeManager.apply(this)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.apply{

clearButton.setOnClickListener {
input.text = ""
output.text = ""
}

removeButton.setOnClickListener {
val text = input.text.toString()
if (text.isNotEmpty()) {
input.text = text.substring(0, text.length - 1)
}
output.text = ""
}

zeroButton.setOnClickListener {
input.text = addToInputText("0")
}
oneButton.setOnClickListener {
input.text = addToInputText("1")
}
twoButton.setOnClickListener {
input.text = addToInputText("2")
}
threeButton.setOnClickListener {
input.text = addToInputText("3")
}
fourButton.setOnClickListener {
input.text = addToInputText("4")
}
fiveButton.setOnClickListener {
input.text = addToInputText("5")
}
sixButton.setOnClickListener {
input.text = addToInputText("6")
}
sevenButton.setOnClickListener {
input.text = addToInputText("7")
}
eightButton.setOnClickListener {
input.text = addToInputText("8")
}
nineButton.setOnClickListener {
input.text = addToInputText("9")
}
decimalButton.setOnClickListener {
input.text = addToInputText(".")
}
plusButton.setOnClickListener {
input.text = addToInputText("+")
}
subtractButton.setOnClickListener {
input.text = addToInputText("-")
}
multiplyButton.setOnClickListener {
input.text = addToInputText("*")
}
divideButton.setOnClickListener {
input.text = addToInputText("/")
}
parenthesesButton.setOnClickListener {
val text = input.text.toString()
if (text.isEmpty() || text.last() == '(' || text.last() == '+' || text.last() == '-' || text.last() == '*' || text.last() == '/') {
input.text = addToInputText("(")
} else {
input.text = addToInputText(")")
}
}
equalButton.setOnClickListener {
showResult()
}

}
}
private fun addToInputText(buttonValue: String): String {

return binding.input.text.toString() + "" + buttonValue
}
private fun getInputExpression(): String {
var expression = binding.input.text.replace(Regex("÷"), "/")
expression = expression.replace(Regex("×"), "*")
return expression
}
private fun showResult() {
val input = getInputExpression()
val expression = Expression(input)
val result = expression.calculate()
val toInt = if (result % 1 == 0.0) {
result.toInt()
} else {
result
}
binding.output.text = toInt.toString()
val currentHistory = PrefManager.getCustomVal<Set<String>>("history", setOf()).plus(input)
PrefManager.setCustomVal("history", currentHistory)
}
}

22 changes: 18 additions & 4 deletions app/src/main/java/com/aayush/calculator/saving/PrefManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ object PrefManager {
if (generalPreferences != null) return
generalPreferences =
context.getSharedPreferences(Location.General.location, Context.MODE_PRIVATE)
protectedPreferences =
context.getSharedPreferences(Location.Protected.location, Context.MODE_PRIVATE)
irrelevantPreferences =
context.getSharedPreferences(Location.Irrelevant.location, Context.MODE_PRIVATE)
protectedPreferences =
context.getSharedPreferences(Location.Protected.location, Context.MODE_PRIVATE)
}

@Suppress("UNCHECKED_CAST")
Expand Down Expand Up @@ -178,7 +178,7 @@ object PrefManager {
default as? Set<String> ?: setOf()
) as T?

else -> throw IllegalArgumentException("Type not supported")
else -> deserializeClass(key, default, Location.Irrelevant)
}
} catch (e: Exception) {
default
Expand Down Expand Up @@ -206,6 +206,7 @@ object PrefManager {
is String -> putString(key, value as String)
is Set<*> -> putStringSet(key, value as Set<String>)
null -> remove(key)
else -> serializeClass(key, value, Location.Irrelevant)
}
apply()
}
Expand Down Expand Up @@ -286,6 +287,11 @@ object PrefManager {
fun SharedPreferenceLiveData<*>.asLiveStringSet(): SharedPreferenceStringSetLiveData =
this as? SharedPreferenceStringSetLiveData
?: throw ClassCastException("Cannot cast to SharedPreferenceLiveData<Set<String>>")
fun exportAllPrefs(prefLocation: List<Location>): String {
return PreferencePackager.pack(
prefLocation.associateWith { getPrefLocation(it) }
)
}


/**
Expand Down Expand Up @@ -331,8 +337,8 @@ object PrefManager {
private fun getPrefLocation(prefLoc: Location): SharedPreferences {
return when (prefLoc) {
Location.General -> generalPreferences
Location.Protected -> protectedPreferences
Location.Irrelevant -> irrelevantPreferences
Location.Protected -> protectedPreferences
}!!
}

Expand All @@ -348,6 +354,7 @@ object PrefManager {
pref.edit().putString(key, serialized).apply()
} catch (e: Exception) {
println("Error serializing preference: ${e.message}")

}
}

Expand All @@ -365,6 +372,13 @@ object PrefManager {
} else {
default
}
} catch (e: java.io.InvalidClassException) {
try {
getPrefLocation(location).edit().remove(key).apply()
default
} catch (e: Exception) {
default
}
} catch (e: Exception) {
default
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import com.aayush.calculator.saving.internal.Pref
enum class PrefName(val data: Pref) {
//General
Amoled(Pref(Location.General, Boolean::class, false)),
History(Pref(Location.General, Set::class, setOf<com.aayush.calculator.History>())),
HistoryList(Pref(Location.General, List::class, listOf<History>(History("1+1", "2")))),
}
26 changes: 26 additions & 0 deletions app/src/main/res/drawable/ic_round_tag_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:tint="?attr/colorPrimary"
android:viewportHeight="24">
<path
android:pathData="M10.28,20.25H17C19.76,20.25 22,18.01 22,15.25V8.75C22,5.99 19.76,3.75 17,3.75H10.28C8.87,3.75 7.53,4.34 6.58,5.39L3.05,9.27C1.64,10.82 1.64,13.18 3.05,14.73L6.58,18.61C7.53,19.66 8.87,20.25 10.28,20.25Z"
android:strokeLineJoin="round"
android:strokeWidth="1.5"
android:fillColor="#00000000"
android:strokeColor="#292D32"
android:strokeLineCap="round"/>
<path
android:pathData="M16,14.47L11.06,9.53"
android:strokeWidth="1.5"
android:fillColor="#00000000"
android:strokeColor="#292D32"
android:strokeLineCap="round"/>
<path
android:pathData="M11.06,14.47L16,9.53"
android:strokeWidth="1.5"
android:fillColor="#00000000"
android:strokeColor="#292D32"
android:strokeLineCap="round"/>
</vector>
Loading

0 comments on commit 0edd1c0

Please sign in to comment.