Skip to content

Instantly share code, notes, and snippets.

@mohamedagamy
Last active October 13, 2020 18:13
Show Gist options
  • Save mohamedagamy/ada3e2f8e1fb87f8c28f8e36aafa62ab to your computer and use it in GitHub Desktop.
Save mohamedagamy/ada3e2f8e1fb87f8c28f8e36aafa62ab to your computer and use it in GitHub Desktop.

Revisions

  1. mohamedagamy revised this gist Oct 9, 2020. 1 changed file with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions Formatting Numbers Locale
    Original 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
    }

  2. mohamedagamy revised this gist Oct 9, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Formatting Numbers Locale
    Original 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(), "٤")
  3. mohamedagamy revised this gist Oct 9, 2020. No changes.
  4. mohamedagamy revised this gist Oct 9, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Formatting Numbers Locale
    Original 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(), "٤")
  5. mohamedagamy revised this gist Oct 9, 2020. 1 changed file with 32 additions and 1 deletion.
    33 changes: 32 additions & 1 deletion Formatting Numbers Locale
    Original 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()
    }

  6. mohamedagamy renamed this gist Oct 9, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. mohamedagamy created this gist Oct 9, 2020.
    44 changes: 44 additions & 0 deletions Formatting Numbers
    Original 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
    }