Skip to content

Commit

Permalink
Merge pull request #91 from Yet-Zio/v1.1.1
Browse files Browse the repository at this point in the history
V1.1.1
  • Loading branch information
Yet-Zio authored Nov 5, 2023
2 parents b9e0265 + d8ab540 commit ad39699
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 38 deletions.
10 changes: 10 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This configuration file was automatically generated by Gitpod.
# Please adjust to your needs (see https://www.gitpod.io/docs/introduction/learn-gitpod/gitpod-yaml)
# and commit this file to your remote git repository to share the goodness with others.

# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart

tasks:
- init: ./gradlew build


12 changes: 6 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ android {
applicationId "yetzio.yetcalc"
minSdk 21
targetSdk 34
versionCode 11
versionName "1.1.0"
versionCode 12
versionName "1.1.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down Expand Up @@ -46,11 +46,11 @@ apply plugin: 'kotlin-kapt'

dependencies {

coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.0.3")
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.0.4")

implementation 'androidx.core:core-ktx:1.12.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'com.google.android.material:material:1.10.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

implementation("org.mariuszgromada.math:MathParser.org-mXparser:5.2.1")
Expand All @@ -61,12 +61,12 @@ dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3'

implementation 'com.google.android.material:material:1.9.0'
implementation 'com.google.android.material:material:1.10.0'
implementation 'io.github.muddz:styleabletoast:2.4.0'

implementation 'androidx.preference:preference-ktx:1.2.1'

implementation 'com.fasterxml.jackson.module:jackson-module-kotlin:2.15.2'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin:2.15.3'
implementation 'com.afollestad.material-dialogs:core:3.3.0'

implementation 'com.airbnb.android:paris:2.0.2'
Expand Down
62 changes: 57 additions & 5 deletions app/src/main/java/yetzio/yetcalc/component/History.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package yetzio.yetcalc.component

import android.content.Context
import android.os.Build
import androidx.preference.PreferenceManager
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import yetzio.yetcalc.model.HistoryItem
Expand All @@ -16,11 +16,20 @@ class History{
var ctx: Context? = null
val m_Mapper = jacksonObjectMapper()

val dateFormats = listOf(
FormatStyle.MEDIUM, // Example: Nov 5, 2023 3:30:00 PM
DateTimeFormatter.ofPattern("dd, MMMM yyyy HH:mm:ss"), // Example: 05, November 2023 15:30:00
DateTimeFormatter.ofPattern("yyyy, MMMM dd HH:mm:ss") // Example: 2023, November 05 15:30:00
)

fun addToDb(ex: String, res: String){
val datefmr = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.MEDIUM)
.withZone(ZoneOffset.systemDefault())
.format(Instant.now()).toString()
val prefMgr = ctx?.let { PreferenceManager.getDefaultSharedPreferences(it) }

val datefmr = when(prefMgr?.getString("datehistkey", "Default format")){
"dd, MMMM yyyy HH:mm:ss" -> DateTimeFormatter.ofPattern("dd, MMMM yyyy HH:mm:ss").withZone(ZoneOffset.systemDefault()).format(Instant.now()).toString()
"yyyy, MMMM dd HH:mm:ss" -> DateTimeFormatter.ofPattern("yyyy, MMMM dd HH:mm:ss").withZone(ZoneOffset.systemDefault()).format(Instant.now()).toString()
else -> DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withZone(ZoneOffset.systemDefault()).format(Instant.now()).toString()
}

val histout = HistoryItem(datefmr, ex, res)

Expand Down Expand Up @@ -57,6 +66,7 @@ class History{

var ret_list = arrayListOf<HistoryItem>()
val dbfile = File(ctx?.filesDir, mdb_name)
val prefMgr = ctx?.let { PreferenceManager.getDefaultSharedPreferences(it) }

if(dbfile.exists()){
val dbcontent = ctx?.openFileInput(mdb_name)?.bufferedReader().use {
Expand All @@ -65,6 +75,23 @@ class History{

try{
ret_list = m_Mapper.readValue(dbcontent, object: TypeReference<ArrayList<HistoryItem>>(){})
for(histItem in ret_list){
val datefmr = when(detectDateFormat(histItem.timestamp, dateFormats)){
"dd, MMMM yyyy HH:mm:ss" -> DateTimeFormatter.ofPattern("dd, MMMM yyyy HH:mm:ss").withZone(ZoneOffset.systemDefault())
"yyyy, MMMM dd HH:mm:ss" -> DateTimeFormatter.ofPattern("yyyy, MMMM dd HH:mm:ss").withZone(ZoneOffset.systemDefault())
else -> DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withZone(ZoneOffset.systemDefault())
}

val parsedInstant = datefmr.parse(histItem.timestamp, Instant::from)
val desFmr = when(prefMgr?.getString("datehistkey", "Default format")){
"dd, MMMM yyyy HH:mm:ss" -> DateTimeFormatter.ofPattern("dd, MMMM yyyy HH:mm:ss").withZone(ZoneOffset.systemDefault())
"yyyy, MMMM dd HH:mm:ss" -> DateTimeFormatter.ofPattern("yyyy, MMMM dd HH:mm:ss").withZone(ZoneOffset.systemDefault())
else -> DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withZone(ZoneOffset.systemDefault())
}

val formattedDate = desFmr.format(parsedInstant)
histItem.timestamp = formattedDate
}
}
catch (e: Exception){
println("history get list read error occurred")
Expand All @@ -82,4 +109,29 @@ class History{
dbfile.createNewFile()
}
}

private fun detectDateFormat(dateString: String, dateFormats: List<Any>): String? {
for (format in dateFormats) {
try {
if (format is FormatStyle) {
Instant.from(DateTimeFormatter.ofLocalizedDateTime(format).parse(dateString))
if (format == FormatStyle.MEDIUM) {
return "FormatStyle.MEDIUM"
}
} else if (format is DateTimeFormatter) {
Instant.from(format.parse(dateString))
if (format.toString() == "dd, MMMM yyyy HH:mm:ss") {
return "dd, MMMM yyyy HH:mm:ss"
} else if (format.toString() == "yyyy, MMMM dd HH:mm:ss") {
return "yyyy, MMMM dd HH:mm:ss"
}
}
} catch (e: Exception) {
// println("date time format didn't match")
}
}
return null
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package yetzio.yetcalc.model
import androidx.lifecycle.ViewModel
import yetzio.yetcalc.component.NumberSystem
import yetzio.yetcalc.component.Operator
import java.math.BigInteger

class ProgramCalcViewModel: ViewModel(){
var initialized = false
Expand All @@ -11,7 +12,7 @@ class ProgramCalcViewModel: ViewModel(){
var numberSys: NumberSystem = NumberSystem.DEC
var currentOp: Operator? = null
var isCalcPending = false
var prevResult = 0
var prevResult: BigInteger = BigInteger("0")
var clearInput = false
var divByZero = false
var opPresent = false
Expand Down
33 changes: 17 additions & 16 deletions app/src/main/java/yetzio/yetcalc/views/ProgramCalcActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import yetzio.yetcalc.utils.getModesList
import yetzio.yetcalc.utils.getScreenOrientation
import yetzio.yetcalc.utils.setVibOnClick
import yetzio.yetcalc.utils.showThemeDialog
import java.math.BigInteger

class ProgramCalcActivity : AppCompatActivity(), View.OnClickListener {

Expand Down Expand Up @@ -570,7 +571,7 @@ class ProgramCalcActivity : AppCompatActivity(), View.OnClickListener {
R.id.inputBtnAC -> {
tvExp.text = ""
tvResult.text = "0"
mViewModel.prevResult = 0
mViewModel.prevResult = BigInteger("0")
setResultText()
if (mViewModel.currentOp != null) {
mViewModel.currentOp = null
Expand All @@ -586,7 +587,7 @@ class ProgramCalcActivity : AppCompatActivity(), View.OnClickListener {
mViewModel.isCalcPending = false
}
else{
mViewModel.prevResult = tvResult.text.toString().toInt(mViewModel.numberSys.radix)
mViewModel.prevResult = tvResult.text.toString().toBigInteger(mViewModel.numberSys.radix)
setCurrentNumberSystem(mViewModel.numberSys)
}
}
Expand All @@ -603,12 +604,12 @@ class ProgramCalcActivity : AppCompatActivity(), View.OnClickListener {
if (operator == Operator.NOT) {
var temp = tvResult.text.toString().toInt(mViewModel.numberSys.radix)
tvResult.text = temp.inv().toString(mViewModel.numberSys.radix)
mViewModel.prevResult = tvResult.text.toString().toInt(mViewModel.numberSys.radix)
mViewModel.prevResult = tvResult.text.toString().toBigInteger(mViewModel.numberSys.radix)
setResultText()
mViewModel.clearInput = true
return
}
mViewModel.prevResult = tvResult.text.toString().toInt(mViewModel.numberSys.radix)
mViewModel.prevResult = tvResult.text.toString().toBigInteger(mViewModel.numberSys.radix)
} else if (mViewModel.currentOp != null) {
mViewModel.clearInput = true
if (operator == Operator.NOT) {
Expand All @@ -631,47 +632,47 @@ class ProgramCalcActivity : AppCompatActivity(), View.OnClickListener {
when (mViewModel.currentOp) {

Operator.ADD -> {
mViewModel.prevResult += tvResult.text.toString().toInt(mViewModel.numberSys.radix)
mViewModel.prevResult += tvResult.text.toString().toBigInteger(mViewModel.numberSys.radix)
}

Operator.SUB -> {
mViewModel.prevResult -= tvResult.text.toString().toInt(mViewModel.numberSys.radix)
mViewModel.prevResult -= tvResult.text.toString().toBigInteger(mViewModel.numberSys.radix)
}

Operator.MUL -> {
mViewModel.prevResult *= tvResult.text.toString().toInt(mViewModel.numberSys.radix)
mViewModel.prevResult *= tvResult.text.toString().toBigInteger(mViewModel.numberSys.radix)
}

Operator.DIV -> {
if(tvResult.text.toString().toInt(mViewModel.numberSys.radix) == 0){
mViewModel.divByZero = true
mViewModel.prevResult = 0
mViewModel.prevResult = BigInteger("0")
}
else{
mViewModel.prevResult /= tvResult.text.toString().toInt(mViewModel.numberSys.radix)
mViewModel.prevResult /= tvResult.text.toString().toBigInteger(mViewModel.numberSys.radix)
}
}

Operator.AND -> {
mViewModel.prevResult = mViewModel.prevResult and tvResult.text.toString().toInt(mViewModel.numberSys.radix)
mViewModel.prevResult = mViewModel.prevResult and tvResult.text.toString().toBigInteger(mViewModel.numberSys.radix)
}

Operator.OR -> {
mViewModel.prevResult = mViewModel.prevResult or tvResult.text.toString().toInt(mViewModel.numberSys.radix)
mViewModel.prevResult = mViewModel.prevResult or tvResult.text.toString().toBigInteger(mViewModel.numberSys.radix)
}

Operator.NAND -> {
mViewModel.prevResult =
(mViewModel.prevResult and tvResult.text.toString().toInt(mViewModel.numberSys.radix)).inv()
(mViewModel.prevResult and tvResult.text.toString().toBigInteger(mViewModel.numberSys.radix)).inv()
}

Operator.NOR -> {
mViewModel.prevResult =
(mViewModel.prevResult or tvResult.text.toString().toInt(mViewModel.numberSys.radix)).inv()
(mViewModel.prevResult or tvResult.text.toString().toBigInteger(mViewModel.numberSys.radix)).inv()
}

Operator.XOR -> {
mViewModel.prevResult = mViewModel.prevResult xor tvResult.text.toString().toInt(mViewModel.numberSys.radix)
mViewModel.prevResult = mViewModel.prevResult xor tvResult.text.toString().toBigInteger(mViewModel.numberSys.radix)
}

Operator.LSH -> {
Expand Down Expand Up @@ -773,8 +774,8 @@ class ProgramCalcActivity : AppCompatActivity(), View.OnClickListener {
}
}

if (mViewModel.prevResult == 0 && tvResult.text.toString() != "0") {
mViewModel.prevResult = tvResult.text.toString().toInt(mViewModel.numberSys.radix)
if (mViewModel.prevResult == BigInteger("0") && tvResult.text.toString() != "0") {
mViewModel.prevResult = tvResult.text.toString().toBigInteger(mViewModel.numberSys.radix)
}

mViewModel.numberSys = numSys
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.view.ViewGroup
import android.widget.*
import androidx.core.text.HtmlCompat
import androidx.fragment.app.Fragment
import androidx.preference.PreferenceManager
import com.airbnb.paris.Paris
import com.chivorn.smartmaterialspinner.SmartMaterialSpinner
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -55,6 +56,7 @@ class CurrencyFragment : Fragment() {
private lateinit var pTheme: String
private var pDark by Delegates.notNull<Boolean>()
private var pLight by Delegates.notNull<Boolean>()
private lateinit var selectedDateFormat: String

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -69,6 +71,10 @@ class CurrencyFragment : Fragment() {
pDark = (activity as? UnitConvActivity)?.dark!!
pLight = (activity as? UnitConvActivity)?.light!!

val prefMgr = context?.let { PreferenceManager.getDefaultSharedPreferences(it.applicationContext) }
selectedDateFormat = prefMgr?.getString("dateFormatKey", "YYYY-MM-DD").toString()


var v: View?
if(isNetworkAvailable(context?.applicationContext)){
v = createConv(inflater, container)
Expand Down Expand Up @@ -152,7 +158,7 @@ class CurrencyFragment : Fragment() {

if(dateText != null){
if(dateText!!.text.isNotEmpty() && dateText!!.text.isNotBlank()){
API = "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/${dateText!!.text}/currencies/${baseCur.lowercase()}/$lowerConv.json"
API = "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/${pViewModel.current_date}/currencies/${baseCur.lowercase()}/$lowerConv.json"
}
}

Expand Down Expand Up @@ -292,6 +298,10 @@ class CurrencyFragment : Fragment() {
})
}

private fun formatDate(dayormonth: String): String {
return if (dayormonth.length > 1) dayormonth else "0$dayormonth"
}

private fun setupDatePicker(){
dateinfoTV?.movementMethod = LinkMovementMethod.getInstance()
dateinfoTV?.setText(HtmlCompat.fromHtml(getString(R.string.datecurrhint), HtmlCompat.FROM_HTML_MODE_LEGACY))
Expand All @@ -306,16 +316,30 @@ class CurrencyFragment : Fragment() {
activity?.let { it1 ->
if(pDark){
DatePickerDialog(it1, R.style.DatePickerTheme, DatePickerDialog.OnDateSetListener{ _, mYear, mMonth, mDay ->
pViewModel.current_date = "$mYear-${mMonth+1}-$mDay"
pViewModel.current_date = "$mYear-${formatDate((mMonth+1).toString())}-${formatDate((mDay).toString())}"
println("Date pick: ${pViewModel.current_date}")
dateText?.text = "$mYear-${mMonth+1}-$mDay"
dateText?.text = when(selectedDateFormat){
"DD-MM-YYYY" -> "${formatDate((mDay).toString())}-${formatDate((mMonth+1).toString())}-$mYear"
"DD-YYYY-MM" -> "${formatDate((mDay).toString())}-$mYear-${formatDate((mMonth+1).toString())}"
"YYYY-DD-MM" -> "$mYear-${formatDate((mDay).toString())}-${formatDate((mMonth+1).toString())}"
"MM-DD-YYYY" -> "${formatDate((mMonth+1).toString())}-${formatDate((mDay).toString())}-$mYear"
"MM-YYYY-DD" -> "${formatDate((mMonth+1).toString())}-$mYear-${formatDate((mDay).toString())}"
else -> "$mYear-${formatDate((mMonth+1).toString())}-${formatDate((mDay).toString())}"
}
}, year, month, day)
}
else{
DatePickerDialog(it1, R.style.DatePickerThemeLight, DatePickerDialog.OnDateSetListener{ _, mYear, mMonth, mDay ->
pViewModel.current_date = "$mYear-${mMonth+1}-$mDay"
pViewModel.current_date = "$mYear-${formatDate((mMonth+1).toString())}-${formatDate((mDay).toString())}"
println("Date pick: ${pViewModel.current_date}")
dateText?.text = "$mYear-${mMonth+1}-$mDay"
dateText?.text = when(selectedDateFormat){
"DD-MM-YYYY" -> "${formatDate((mDay).toString())}-${formatDate((mMonth+1).toString())}-$mYear"
"DD-YYYY-MM" -> "${formatDate((mDay).toString())}-$mYear-${formatDate((mMonth+1).toString())}"
"YYYY-DD-MM" -> "$mYear-${formatDate((mDay).toString())}-${formatDate((mMonth+1).toString())}"
"MM-DD-YYYY" -> "${formatDate((mMonth+1).toString())}-${formatDate((mDay).toString())}-$mYear"
"MM-YYYY-DD" -> "${formatDate((mMonth+1).toString())}-$mYear-${formatDate((mDay).toString())}"
else -> "$mYear-${formatDate((mMonth+1).toString())}-${formatDate((mDay).toString())}"
}
}, year, month, day)
}
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout-land/activity_program_calc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
android:gravity="end"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:textIsSelectable="true"
android:text="0"
style="@style/yetCalcTVDark"
android:textSize="35sp" />
Expand Down Expand Up @@ -431,4 +432,4 @@
</TableRow>


</LinearLayout>
</LinearLayout>
3 changes: 2 additions & 1 deletion app/src/main/res/layout/activity_program_calc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
android:layout_height="wrap_content"
android:ems="10"
android:gravity="end"
android:textIsSelectable="true"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:text="0"
Expand Down Expand Up @@ -490,4 +491,4 @@
style="@style/programCalcBorderlessButton"/>
</TableRow>

</LinearLayout>
</LinearLayout>
Loading

0 comments on commit ad39699

Please sign in to comment.