Skip to content

Commit

Permalink
Improve Gson extensions and refactor date and strings extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
jeluchu committed Jan 26, 2023
1 parent 42f5104 commit 13d7a28
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

package com.jeluchu.jchucomponents.ktx.date

import android.provider.Settings.System.DATE_FORMAT
import com.jeluchu.jchucomponents.ktx.numbers.orEmpty
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

Expand Down Expand Up @@ -139,39 +136,15 @@ fun Date.add(field: Int, amount: Int): Date {
}
}

fun Date.addYears(years: Int): Date {
return add(Calendar.YEAR, years)
}

fun Date.addMonths(months: Int): Date {
return add(Calendar.MONTH, months)
}

fun Date.addDays(days: Int): Date {
return add(Calendar.DAY_OF_MONTH, days)
}

fun Date.addHours(hours: Int): Date {
return add(Calendar.HOUR_OF_DAY, hours)
}

fun Date.addMinutes(minutes: Int): Date {
return add(Calendar.MINUTE, minutes)
}

fun Date.addSeconds(seconds: Int): Date {
return add(Calendar.SECOND, seconds)
}
fun Date.addYears(years: Int): Date = add(Calendar.YEAR, years)
fun Date.addMonths(months: Int): Date = add(Calendar.MONTH, months)
fun Date.addDays(days: Int): Date = add(Calendar.DAY_OF_MONTH, days)
fun Date.addHours(hours: Int): Date = add(Calendar.HOUR_OF_DAY, hours)
fun Date.addMinutes(minutes: Int): Date = add(Calendar.MINUTE, minutes)
fun Date.addSeconds(seconds: Int): Date = add(Calendar.SECOND, seconds)

/** ---- CALENDAR ------------------------------------------------------------------------------ **/

fun Date.addYear(years: Int): Date =
this.toCalendar()
.run {
add(Calendar.YEAR, years)
this.time
}

fun Date.toCalendar(): Calendar {
val calendar = Calendar.getInstance()
calendar.time = this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ inline fun <T, reified R> T.convert(): R = gson.toJson(this).toObject()
/**
* Helps to get Map, List, Set or other generic type from Json using Gson.
*/
inline fun <reified T : Any> Gson.fromJsonToGeneric(json: String): T {
val type = object : TypeToken<T>() {}.type
return fromJson(json, type)
inline fun <reified T> Gson.fromJson(json: String): T? = try {
fromJson<T>(json, object : TypeToken<T>() {}.type)
} catch (e: Exception) {
e.printStackTrace()
null
}

inline fun <reified T : Any> Gson.fromJsonList(json: String?): List<T>? = try {
fromJson<List<T>>(json, object : TypeToken<List<T>>() {}.type)
} catch (e: Exception) {
e.printStackTrace()
null
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@ package com.jeluchu.jchucomponents.ktx.numbers

import java.math.RoundingMode
import java.text.DecimalFormat
import java.text.SimpleDateFormat
import java.util.*

fun Float.roundTo(n: Int): Float = toBigDecimal().setScale(n, RoundingMode.UP).toFloat()

fun Double.roundTo(n: Int): Double = toBigDecimal().setScale(n, RoundingMode.UP).toDouble()

fun String.toPriceAmount(): String {
val dec = DecimalFormat("###,###,###.00")
return dec.format(this.toDouble())
/**
* Return date in specified format.
* @param milliSeconds Date in milliseconds
* @param dateFormat Date format
* @return String representing date in specified format
*/
fun Long.getDate(dateFormat: String?): String? {
val formatter = SimpleDateFormat(dateFormat, Locale.getDefault())
val calendar: Calendar = Calendar.getInstance().apply { timeInMillis = this@getDate }
return formatter.format(calendar.time)
}

fun Double.toPriceAmount(): String {
val dec = DecimalFormat("###,###,###.00")
return dec.format(this)
}
fun Double.toPriceAmount(): String = DecimalFormat("###,###,###.00").format(this)
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import java.net.HttpURLConnection
import java.net.URL
import java.net.URLEncoder
import java.text.DateFormat
import java.text.DecimalFormat
import java.text.Format
import java.text.SimpleDateFormat
import java.util.*
Expand Down Expand Up @@ -350,3 +351,8 @@ fun String.getCurrencyString() =
"RUB" -> ""
else -> ""
}

fun String.toPriceAmount(): String {
val dec = DecimalFormat("###,###,###.00")
return dec.format(this.toDouble())
}

0 comments on commit 13d7a28

Please sign in to comment.