Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup toPersianNumber extension #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class PersianLinearDatePicker(context: Context, attr: AttributeSet?) : LinearLay
constructor(context: Context) : this(context, null)

init {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
inflater.inflate(R.layout.layout_persian_linear_date_picker, this)
LayoutInflater.from(context).inflate(R.layout.layout_persian_linear_date_picker, this)

val typedArray =
context.obtainStyledAttributes(attr, R.styleable.PersianLinearDatePicker)
Expand Down Expand Up @@ -52,16 +51,17 @@ class PersianLinearDatePicker(context: Context, attr: AttributeSet?) : LinearLay
setDays(31)
}

private fun setDays(maxDay: Int) {
dayPicker.minValue = 1
dayPicker.maxValue = maxDay
private fun setDays(maxDay: Int) = dayPicker.run {
minValue = 1
maxValue = maxDay
}

private fun setMonths() {
monthPicker.minValue = 1
monthPicker.maxValue = 12
private fun setMonths() = monthPicker.run {
minValue = 1
maxValue = 12
}


private fun changeShowingNumbersToPersian() {
yearPicker.setFormatter {
it.toString().toPersianNumber()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@ package com.pouyaheydari.lineardatepicker.utils
* Turns all English numbers to Persian numbers
*/
fun String.toPersianNumber(): String {
var newValue = replace("1", "۱")
newValue = newValue.replace("2", "۲")
newValue = newValue.replace("3", "۳")
newValue = newValue.replace("4", "۴")
newValue = newValue.replace("5", "۵")
newValue = newValue.replace("6", "۶")
newValue = newValue.replace("7", "۷")
newValue = newValue.replace("8", "۸")
newValue = newValue.replace("9", "۹")
newValue = newValue.replace("0", "۰")
// var newValue = replace("1", "۱")
// newValue = newValue.replace("2", "۲")
// newValue = newValue.replace("3", "۳")
// newValue = newValue.replace("4", "۴")
// newValue = newValue.replace("5", "۵")
// newValue = newValue.replace("6", "۶")
// newValue = newValue.replace("7", "۷")
// newValue = newValue.replace("8", "۸")
// newValue = newValue.replace("9", "۹")
// newValue = newValue.replace("0", "۰")
// return newValue

// change latin char to arabic char by ascii code
// 1728 is difference of latin char and arabic char
var newValue = this
forEach {
val newChar = (it.toInt() + 1728).toChar()
newValue = newValue.replace(it, newChar)
}

return newValue
}