Skip to content

Commit fd4377c

Browse files
committed
Version 1.0.51
1 parent 523bd8d commit fd4377c

File tree

11 files changed

+270
-102
lines changed

11 files changed

+270
-102
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ dependencies {
3333
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
3434
implementation 'androidx.core:core-ktx:1.3.2'
3535
implementation 'androidx.appcompat:appcompat:1.2.0'
36-
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
37-
implementation 'com.android.support.constraint:constraint-layout:2.0.1'
36+
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
37+
implementation 'com.android.support.constraint:constraint-layout:2.0.2'
3838
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
3939
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
4040
testImplementation 'junit:junit:4.12'
Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,128 @@
11
package justjump.coding_calculator
22

3-
import androidx.appcompat.app.AppCompatActivity
43
import android.os.Bundle
4+
import android.text.Editable
5+
import android.text.TextWatcher
6+
import android.view.View
7+
import android.widget.RadioButton
8+
import androidx.appcompat.app.AppCompatActivity
9+
import androidx.lifecycle.Observer
10+
import androidx.lifecycle.ViewModelProviders
11+
import justjump.coding_calculator.viewmodel.NumericalSystemViewModel
12+
import kotlinx.android.synthetic.main.activity_numerical_systems.*
13+
514

615
class NumericalSystems : AppCompatActivity() {
16+
17+
lateinit var cViewModel: NumericalSystemViewModel
18+
719
override fun onCreate(savedInstanceState: Bundle?) {
820
super.onCreate(savedInstanceState)
921
setContentView(R.layout.activity_numerical_systems)
22+
23+
cViewModel = ViewModelProviders.of(this).get(NumericalSystemViewModel::class.java)
24+
25+
/***************************************************************************/
26+
// observer
27+
/***************************************************************************/
28+
val myObserverColor = Observer<String> {
29+
30+
val intSelectButton: Int = radioGroup!!.checkedRadioButtonId
31+
val radioButton = findViewById<RadioButton>(intSelectButton)
32+
33+
when (radioButton.text) {
34+
"Dec" -> {
35+
cViewModel.getSystemNumber(radioButton.text.toString())
36+
}
37+
"Oct" -> {
38+
cViewModel.getSystemNumber(radioButton.text.toString())
39+
}
40+
"Bin" -> {
41+
cViewModel.getSystemNumber(radioButton.text.toString())
42+
}
43+
"Hex" -> {
44+
cViewModel.getSystemNumber(radioButton.text.toString())
45+
}
46+
}
47+
48+
resultBinary.text = cViewModel.resultBinary.value
49+
resultOctal.text = cViewModel.resultOctal.value
50+
resultHex.text = cViewModel.resultHex.value
51+
resultDecimal.text = cViewModel.resultDecimal.value
52+
}
53+
54+
// this observer works when the expression change
55+
cViewModel.dataNumber.observe(this@NumericalSystems, myObserverColor)
56+
57+
radioGroup.setOnCheckedChangeListener { group, checkedId ->
58+
59+
// checkedId is the RadioButton selected
60+
val intSelectButton: Int = radioGroup!!.checkedRadioButtonId
61+
val radioButton = findViewById<RadioButton>(intSelectButton)
62+
63+
cViewModel.dataNumber.value = cViewModel.dataNumber.value
64+
65+
when(radioButton.text){
66+
"Dec" -> {
67+
titleDecimal.visibility = View.GONE
68+
resultDecimal.visibility = View.GONE
69+
70+
titleOctal.visibility = View.VISIBLE
71+
resultOctal.visibility = View.VISIBLE
72+
titleBinary.visibility = View.VISIBLE
73+
resultBinary.visibility = View.VISIBLE
74+
titleHex.visibility = View.VISIBLE
75+
resultHex.visibility = View.VISIBLE
76+
}
77+
"Oct" -> {
78+
titleOctal.visibility = View.GONE
79+
resultOctal.visibility = View.GONE
80+
81+
titleDecimal.visibility = View.VISIBLE
82+
resultDecimal.visibility = View.VISIBLE
83+
titleBinary.visibility = View.VISIBLE
84+
resultBinary.visibility = View.VISIBLE
85+
titleHex.visibility = View.VISIBLE
86+
resultHex.visibility = View.VISIBLE
87+
}
88+
"Bin" -> {
89+
titleBinary.visibility = View.GONE
90+
resultBinary.visibility = View.GONE
91+
92+
titleDecimal.visibility = View.VISIBLE
93+
resultDecimal.visibility = View.VISIBLE
94+
titleOctal.visibility = View.VISIBLE
95+
resultOctal.visibility = View.VISIBLE
96+
titleHex.visibility = View.VISIBLE
97+
resultHex.visibility = View.VISIBLE
98+
}
99+
"Hex" -> {
100+
titleHex.visibility = View.GONE
101+
resultHex.visibility = View.GONE
102+
103+
titleDecimal.visibility = View.VISIBLE
104+
resultDecimal.visibility = View.VISIBLE
105+
titleOctal.visibility = View.VISIBLE
106+
resultOctal.visibility = View.VISIBLE
107+
titleBinary.visibility = View.VISIBLE
108+
resultBinary.visibility = View.VISIBLE
109+
}
110+
}
111+
}
112+
113+
fieldNumber.addTextChangedListener(object : TextWatcher {
114+
115+
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
116+
117+
}
118+
119+
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
120+
121+
}
122+
123+
override fun afterTextChanged(p0: Editable?) {
124+
cViewModel.dataNumber.value = fieldNumber.text.toString()
125+
}
126+
})
10127
}
11128
}

app/src/main/java/justjump/coding_calculator/utilities/Functions.kt

Lines changed: 38 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package justjump.coding_calculator.utilities
22

33
import com.github.zieiony.calc.Calc
44
import java.util.regex.Pattern
5+
import kotlin.math.pow
56

67
class Functions {
78

@@ -38,8 +39,10 @@ class Functions {
3839
return false
3940
}
4041

41-
fun validateHexNumber(hexNumber: String): Boolean
42-
{
42+
/**********************************************************************/
43+
// Function to validate hexadecimal number
44+
/**********************************************************************/
45+
fun validateHexNumber(hexNumber: String): Boolean {
4346
val p = Pattern.compile("[0-9a-fA-F]+")
4447
val m = p.matcher(hexNumber)
4548
val b = m.matches()
@@ -71,6 +74,23 @@ class Functions {
7174
return ((b * c) / a)
7275
}
7376

77+
/**********************************************************************/
78+
// Function to discount calculator
79+
/**********************************************************************/
80+
fun discountCalculator(quantity: Double, discount: Double): Array<String> {
81+
val n1: Double = quantity - ((discount / 100) * quantity)
82+
val n2: Double = (discount / 100) * quantity
83+
84+
return arrayOf(n1.toString(), n2.toString())
85+
}
86+
87+
/**********************************************************************/
88+
// Function to calculate color in RGB to hex
89+
/**********************************************************************/
90+
fun colorGenerator(R: Int, G: Int, B: Int): String {
91+
return "#" + convertToHex(R) + convertToHex(G) + convertToHex(B)
92+
}
93+
7494
/**********************************************************************/
7595
// Function to convert integer to binary
7696
/**********************************************************************/
@@ -90,6 +110,14 @@ class Functions {
90110
return binary.reversed()
91111
}
92112

113+
/**********************************************************************/
114+
// Function to convert integer to binary
115+
/**********************************************************************/
116+
fun convertToOctal(dataNumber: Int): String? {
117+
118+
return Integer.toOctalString(dataNumber)
119+
}
120+
93121
/**********************************************************************/
94122
// Function to convert integer to hexadecimal
95123
/**********************************************************************/
@@ -157,80 +185,22 @@ class Functions {
157185
// Function to convert integer to hexadecimal
158186
/**********************************************************************/
159187
fun convertHexToDecimal(dataNumber: String): Int {
160-
return Integer.parseInt(dataNumber,16);
188+
return Integer.parseInt(dataNumber, 16);
161189
}
162190

163191
/**********************************************************************/
164-
// Function to discount calculator
192+
// Function to convert octal to decimal
165193
/**********************************************************************/
166-
fun discountCalculator(quantity: Double, discount: Double): Array<String> {
167-
val n1: Double = quantity - ((discount / 100) * quantity)
168-
val n2: Double = (discount / 100) * quantity
169-
170-
return arrayOf(n1.toString(), n2.toString())
194+
fun convertOctalToDecimal(value: String): Int {
195+
return Integer.parseInt(value, 8)
171196
}
172197

173198
/**********************************************************************/
174-
// Function to calculate color in RGB to hex
199+
// Function to convert binary to decimal
175200
/**********************************************************************/
176-
fun colorGenerator(R: Int, G: Int, B: Int): String {
177-
return "#" + convertToHex(R) + convertToHex(G) + convertToHex(B)
201+
fun convertBinaryToDecimal(value: String): Int {
202+
return Integer.parseInt(value, 2)
178203
}
179204

180-
/**********************************************************************/
181-
// Function to calculate color in hex to RGB
182-
// function incomplete
183-
// function incomplete
184-
// function incomplete
185-
// function incomplete
186-
/**********************************************************************/
187-
fun colorGenerator(colorHex: String): String {
188-
/*var cont: Int = 1
189-
var R = 0
190-
var G = 0
191-
var B = 0
192-
var result: Int = 0
193-
194-
195-
for (item in colorHex.toCharArray()) {
196-
result = 0
197-
if (cont == 2 || cont == 3) {
198-
println("grupo 1 " + item)
199-
200-
when (item) {
201-
'A' -> {
202-
result = (10 * 16)
203-
}
204-
'B' -> {
205-
result = (11 * 16)
206-
}
207-
'C' -> {
208-
result = (12 * 16)
209-
}
210-
'D' -> {
211-
result = (13 * 16)
212-
}
213-
'E' -> {
214-
result = (14 * 16)
215-
}
216-
'F' -> {
217-
result = (15 * 16)
218-
}
219-
else -> {
220-
println("KKKKKKKKKKKKKKKKKKKK-> " + item)
221-
result = item.toInt() * 16
222-
}
223-
}
224-
println("grupo 1 " + result)
225-
}
226-
if (cont == 4 || cont == 5) {
227-
println("grupo 2 " + item)
228-
}
229-
if (cont == 6 || cont == 7) {
230-
println("grupo 3 " + item)
231-
}
232-
cont++
233-
}*/
234-
return "#ffffff"
235-
}
205+
236206
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package justjump.coding_calculator.viewmodel
2+
3+
import androidx.lifecycle.MutableLiveData
4+
import androidx.lifecycle.ViewModel
5+
import justjump.coding_calculator.utilities.Functions
6+
7+
class NumericalSystemViewModel: ViewModel() {
8+
9+
var dataNumber = MutableLiveData<String>()
10+
var resultDecimal = MutableLiveData<String>()
11+
var resultBinary = MutableLiveData<String>()
12+
var resultOctal = MutableLiveData<String>()
13+
var resultHex = MutableLiveData<String>()
14+
15+
fun getSystemNumber(typeSNumber: String) {
16+
17+
when(typeSNumber) {
18+
"Dec" -> {
19+
if (dataNumber.value!!.isNotEmpty()){
20+
resultBinary.value = Functions().convertToBinary(dataNumber.value!!.toInt())
21+
resultOctal.value = Functions().convertToOctal(dataNumber.value!!.toInt())
22+
resultHex.value = Functions().convertToHex(dataNumber.value!!.toInt())
23+
}
24+
else
25+
{
26+
resultBinary.value = ""
27+
resultOctal.value = ""
28+
resultHex.value = ""
29+
}
30+
}
31+
32+
"Oct" -> {
33+
if (dataNumber.value!!.isNotEmpty()) {
34+
35+
//here I'm gonna change the value of dataNumber to decimal
36+
var newDecimalNumber = Functions().convertOctalToDecimal(dataNumber.value!!)
37+
38+
resultBinary.value = Functions().convertToBinary(newDecimalNumber)
39+
resultDecimal.value = newDecimalNumber.toString()
40+
resultHex.value = Functions().convertToHex(newDecimalNumber)
41+
} else {
42+
resultBinary.value = ""
43+
resultDecimal.value = ""
44+
resultHex.value = ""
45+
}
46+
}
47+
48+
"Bin" -> {
49+
if (dataNumber.value!!.isNotEmpty()){
50+
51+
//here I'm gonna change the value of dataNumber to decimal
52+
var newDecimalNumber = Functions().convertBinaryToDecimal(dataNumber.value!!)
53+
54+
resultOctal.value = Functions().convertToOctal(newDecimalNumber)
55+
resultDecimal.value = newDecimalNumber.toString()
56+
resultHex.value = Functions().convertToHex(newDecimalNumber)
57+
}
58+
else
59+
{
60+
resultOctal.value = ""
61+
resultDecimal.value = ""
62+
resultHex.value = ""
63+
}
64+
}
65+
66+
"Hex" -> {
67+
if (dataNumber.value!!.isNotEmpty()){
68+
69+
//here I'm gonna change the value of dataNumber to decimal
70+
var newDecimalNumber = Functions().convertHexToDecimal(dataNumber.value!!)
71+
72+
resultBinary.value = Functions().convertToBinary(newDecimalNumber)
73+
resultOctal.value = Functions().convertToOctal(newDecimalNumber)
74+
resultDecimal.value = newDecimalNumber.toString()
75+
}
76+
else
77+
{
78+
resultBinary.value = ""
79+
resultOctal.value = ""
80+
resultDecimal.value = ""
81+
}
82+
}
83+
}
84+
}
85+
}

app/src/main/res/drawable/background_grey_corner.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<item>
88
<shape android:shape="rectangle">
99
<solid android:color="#646464"/>
10-
<corners android:radius="10dp"/>
10+
<corners android:radius="5dp"/>
1111
</shape>
1212
</item>
1313

app/src/main/res/drawable/background_white_corner.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<item>
88
<shape android:shape="rectangle">
99
<solid android:color="@color/material_white"/>
10-
<corners android:radius="10dp"/>
10+
<corners android:radius="5dp"/>
1111
<stroke android:color="#E8E9ED" android:width="1dp"/>
1212
</shape>
1313
</item>

0 commit comments

Comments
 (0)