Last active
October 13, 2020 18:13
-
-
Save mohamedagamy/ada3e2f8e1fb87f8c28f8e36aafa62ab to your computer and use it in GitHub Desktop.
Revisions
-
mohamedagamy revised this gist
Oct 9, 2020 . 1 changed file with 24 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -76,3 +76,27 @@ fun String.decodeBase64(): String { return Base64.decode(this.toByteArray(), Base64.NO_WRAP).toString() } @SuppressLint("SimpleDateFormat") fun calcDiffInDays(startDate: String, endDate: String): Long { //"2022-12-21 10:58:15" val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss") var startPackageDate: Long = 0 var endPackageDate: Long = 0 try { startPackageDate = dateFormat.parse(startDate)?.time ?: 0 endPackageDate = dateFormat.parse(endDate)?.time ?: 0 } catch (e: Exception) { e.printStackTrace() } var diffInMs = endPackageDate - startPackageDate diffInMs = if (diffInMs < 0) 0 else diffInMs val diffInDays = TimeUnit.MILLISECONDS.toDays(diffInMs) val diffInHours = TimeUnit.MILLISECONDS.toHours(diffInMs) val diffInMin = TimeUnit.MILLISECONDS.toMinutes(diffInMs) val diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMs) val totalDifference = Math.ceil(diffInSec.toDouble() / (60.0 * 60.0 * 24.0)) Log.e("TIMER", "diffTotal $totalDifference") Log.e("TIMER", "diffSec $diffInSec") return diffInDays } -
mohamedagamy revised this gist
Oct 9, 2020 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -31,6 +31,7 @@ fun String.formatStringLocalized(): String { //you need to use this workaround to avoid arabic comma in Numbers //so you replace all english numbers with arabic numbers //https://stackoverflow.com/questions/34266789/arabic-number-in-arabic-text-in-android fun String.convertEnglishNumbersToArabic(): String { return this.replace("1".toRegex(), "١").replace("2".toRegex(), "٢") .replace("3".toRegex(), "٣").replace("4".toRegex(), "٤") -
mohamedagamy revised this gist
Oct 9, 2020 . No changes.There are no files selected for viewing
-
mohamedagamy revised this gist
Oct 9, 2020 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -29,6 +29,8 @@ fun String.formatStringLocalized(): String { else return input.convertEnglishNumbersToArabic() } //you need to use this workaround to avoid arabic comma in Numbers //so you replace all english numbers with arabic numbers fun String.convertEnglishNumbersToArabic(): String { return this.replace("1".toRegex(), "١").replace("2".toRegex(), "٢") .replace("3".toRegex(), "٣").replace("4".toRegex(), "٤") -
mohamedagamy revised this gist
Oct 9, 2020 . 1 changed file with 32 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -41,4 +41,35 @@ private fun String.formatStringEnglish(): String { val decimalFormat = NumberFormat.getNumberInstance(Locale.ENGLISH) val result = decimalFormat.format(this.toInt()) return result } //************************************************************* fun String.textOnly(): Boolean { //not allowed number arabic/english ,not allowed special chars return this.matches("^[^0-9\u0660-\u0669^~|<>{}¥€£@#&*+=()_\$\";.?!%]+$".toRegex()) } fun String.digitsOnly(): Boolean { //arabic,english numbers only return this.matches("^[0-9\u0660-\u0669]+\$".toRegex()) } fun String.alphaNumericOnly(): Boolean { //arabic & english => numbers & characters also no spaces allowed //this.matches("^(?=.*[\\u0660-\\u0669])(?=.*[\\u0621-\\u064A0]).{6,}\$".toRegex()) return this.matches("^(?=.*[0-9])(?=.*[a-zA-Z]).{6,}\$".toRegex()) } fun String.isArabicChars(): Boolean { //arabic characters and numbers return this.matches("^[\\u0621-\\u064A\\u0660-\\u0669 ]+\$".toRegex()) } fun String.encodeBase64(): String { return Base64.encodeToString(toByteArray(), Base64.NO_WRAP) } fun String.decodeBase64(): String { return Base64.decode(this.toByteArray(), Base64.NO_WRAP).toString() } -
mohamedagamy renamed this gist
Oct 9, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
mohamedagamy created this gist
Oct 9, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ fun String.formatCreditCardNumber(): String { val delimiter = ' ' return this.replace(".{4}(?!$)".toRegex(), "$0$delimiter") } fun Float.formatPriceFloat(): String { val isEnglish = Utils.getLang() val symbols = DecimalFormatSymbols(Locale.US) val decimalFormat = DecimalFormat("#,###.000",symbols) val output = if(isEnglish) decimalFormat.format(this) else decimalFormat.format(this).convertEnglishNumbersToArabic() return output } fun Float.formatPriceInt(): String { val isEnglish = Utils.getLang() val symbols = DecimalFormatSymbols(Locale.US) val decimalFormat = DecimalFormat("#,###",symbols) val output = if(isEnglish) decimalFormat.format(this.toInt()) else decimalFormat.format(this.toInt()).convertEnglishNumbersToArabic() return output } fun String.formatStringLocalized(): String { val isEnglish = Utils.getLang() val input = this.formatStringEnglish() if (isEnglish) return input else return input.convertEnglishNumbersToArabic() } fun String.convertEnglishNumbersToArabic(): String { return this.replace("1".toRegex(), "١").replace("2".toRegex(), "٢") .replace("3".toRegex(), "٣").replace("4".toRegex(), "٤") .replace("5".toRegex(), "٥").replace("6".toRegex(), "٦") .replace("7".toRegex(), "٧").replace("8".toRegex(), "٨") .replace("9".toRegex(), "٩").replace("0".toRegex(), "٠") } private fun String.formatStringEnglish(): String { val decimalFormat = NumberFormat.getNumberInstance(Locale.ENGLISH) val result = decimalFormat.format(this.toInt()) return result }