Skip to content

Instantly share code, notes, and snippets.

@ljcamargo
Created October 3, 2018 20:11
Show Gist options
  • Select an option

  • Save ljcamargo/45c9f170cd676664ae1b4864e74f75cc to your computer and use it in GitHub Desktop.

Select an option

Save ljcamargo/45c9f170cd676664ae1b4864e74f75cc to your computer and use it in GitHub Desktop.
fun isTimeToSave(current: DateTime): Boolean {
if (completed) return false
val timeSpan = timeSpan ?: return false
val timeGrain = timeGrain ?: return false
val maxSpan = when (timeGrain) {
DateUtils.DAYS -> timeSpan - 1
DateUtils.WEEKS -> timeSpan -1
DateUtils.MONTHS -> (timeSpan * 4) - 1
DateUtils.YEARS -> (timeSpan * 4 * 12) - 1
else -> timeSpan
}
for (i in 1..maxSpan) {
val date = when (timeGrain) {
DateUtils.DAYS -> DateTime.now().toLocalDate().plusDays(i)
else -> DateTime.now().toLocalDate().plusWeeks(i)
}
if (date.isEqual(current.toLocalDate())) {
return true
}
}
return false
}
fun timesToSave(): String? {
var response = ""
val timeSpan = timeSpan ?: return null
val timeGrain = timeGrain ?: return null
val maxSpan = when (timeGrain) {
DateUtils.DAYS -> timeSpan - 1
DateUtils.WEEKS -> timeSpan -1
DateUtils.MONTHS -> (timeSpan * 4) - 1
DateUtils.YEARS -> (timeSpan * 4 * 12) - 1
else -> timeSpan
}
for (i in 1..maxSpan) {
val date = when (timeGrain) {
DateUtils.DAYS -> DateTime.now().toLocalDate().plusDays(i)
else -> DateTime.now().toLocalDate().plusWeeks(i)
}
response += "${date.toString(ISODateTimeFormat.dateTime())}, "
}
return response
}
fun remainingAmount(): String? {
val amount = amount ?: return null
val saved = saved ?: return null
val remain = Math.max(0.0, (amount - saved))
return CurrencyUtils.formatAmount(remain)
}
fun remainingTimeGrain(): String? {
val timeGrain = timeGrain ?: return null
val remain = remainingTime() ?: return null
Log.v("TIME REMAINING","remain $remain")
if (remain > 1) {
return when (timeGrain) {
DateUtils.DAYS -> "$remain días"
DateUtils.WEEKS -> "$remain semanas"
DateUtils.MONTHS -> "$remain meses"
DateUtils.YEARS -> "$remain años"
else -> null
}
} else {
return when (timeGrain) {
DateUtils.DAYS -> "un día"
DateUtils.WEEKS -> "una semana"
DateUtils.MONTHS -> "un mes"
DateUtils.YEARS -> "un año"
else -> null
}
}
}
fun remainingTimeGrainHuman(): String? {
val timeGrain = timeGrain ?: return null
var remain = remainingTime() ?: return null
remain += 1
Log.v("TIME REMAINING EXACT","remain $remain")
if (remain > 1) {
return when (timeGrain) {
DateUtils.DAYS -> "$remain días"
DateUtils.WEEKS -> "$remain semanas"
DateUtils.MONTHS -> "$remain meses"
DateUtils.YEARS -> "$remain años"
else -> null
}
} else {
return when (timeGrain) {
DateUtils.DAYS -> "un día"
DateUtils.WEEKS -> "una semana"
DateUtils.MONTHS -> "un mes"
DateUtils.YEARS -> "un año"
else -> null
}
}
}
fun percent(): String? {
return "${(savingRatio()*100f)}%"
}
fun percentProse(): String? {
val percent = (savingRatio() * 100f).toInt()
return "$percent%"
}
fun remainingProse(): String? {
return "Te faltan ${remainingTimeGrain()} para cumplir tu meta"
}
fun remainingProseHuman(): String {
val color = "#e7348b"
return "Te faltan menos de <font color='$color'>${remainingTimeGrainHuman()}</font> para cumplir tu meta"
}
fun remainingProseHumanEx(): String {
val color = "#e7348b"
return "Te faltan menos de " +
"<font color='$color'>${remainingTimeGrainHuman()}</font> " +
"para cumplir tu meta y aún necesitas ahorrar " +
"<font color='$color'>${remainingAmount()}</font>"
}
fun remainingTime(): Int? {
DateUtils.parse(goalDate)?.let {
return DateUtils.timeDistance(it, timeGrain)?.let {
it1 -> Math.abs(it1)
}
}
return null
}
fun savingRatio(): Float {
//Log.v("saving Ratio >>>>", "saving Ratio saved:$saved / amount:$amount")
if (saved == null || amount == null) return 0f
return saved!!.toFloat() / amount!!.toFloat()
}
fun savingRatioPercent(): Int {
//Log.v("saving Ratio per ", "saving Ratio pert ${(savingRatio() * 100f).toInt()}")
return Math.min((savingRatio() * 100f).toInt(), 100)
}
fun dateRatio(): Float {
Log.v("date Ratio >>>>", "createDate $createDate goalDate $goalDate")
val date0 = DateUtils.parse(createDate)
val date1 = DateUtils.parse(goalDate)
if (date0 == null || date1 == null) return 0f
val maxDays = Days.daysBetween(date0, date1).days
val currDays = Days.daysBetween(DateTime(), date1).days
Log.v("date Ratio2 >>>>", "maxDays $maxDays currDays $currDays")
return 1.0f - (currDays.toFloat() / maxDays.toFloat())
}
fun dateRatioPercent() : Int {
return Math.min((dateRatio() * 100f).toInt(), 100)
}
fun achieved(): Boolean? {
val saved: Double = saved ?: 0.0
val amount: Double = amount ?: 0.0
return completed && (saved >= amount)
}
open fun calcSpanGrain() {
val goalDate = goalDate ?: return
val date0 = DateUtils.parse(createDate) ?: return
val date1 = DateUtils.parse(goalDate) ?: return
timeGrain = timeGrain ?: DateUtils.DAYS
timeSpan = DateUtils.timeDistanceFrom(date0, date1, timeGrain)
}
open fun calcGoalDate() {
val span = timeSpan ?: return
val format = ISODateTimeFormat.dateTime()
var then = if (createDate != null) DateUtils.parse(createDate) else DateTime()
when (timeGrain) {
DateUtils.DAYS ->{
goalDate = then?.plusDays(span)?.toString(format)
}
DateUtils.WEEKS ->{
goalDate = then?.plusWeeks(span)?.toString(format)
}
DateUtils.MONTHS ->{
goalDate = then?.plusMonths(span)?.toString(format)
}
DateUtils.YEARS ->{
goalDate = then?.plusYears(span)?.toString(format)
}
}
}
open fun getSavingSpanRationale(): String {
var string = ""
if (goalDate == null) calcGoalDate()
goalDate?.let {
print("having goal date $it")
val goal = it
amount?.let {
val _amount = it
print("having amount $it")
DateUtils.parse(goal)?.let {
print("having parse date $it")
val days = (DateUtils.timeDistance(it, DateUtils.DAYS) ?: 0) *-1
val weeks = (DateUtils.timeDistance(it, DateUtils.WEEKS) ?: 0) *-1
val months = (DateUtils.timeDistance(it, DateUtils.MONTHS) ?: 0) *-1
val years = (DateUtils.timeDistance(it, DateUtils.YEARS) ?: 0) *-1
print("$days $weeks $months $years")
var daySave = _amount / days
var weekSave = _amount / weeks
var monthSave = _amount / months
var yearSave = _amount / years
daySave = if (daySave.isInfinite()) _amount else daySave
weekSave = if (weekSave.isInfinite()) _amount else weekSave
monthSave = if (monthSave.isInfinite()) _amount else monthSave
yearSave = if (yearSave.isInfinite()) _amount else yearSave
print("$daySave $weekSave $monthSave $yearSave")
//DAYS
string +=
boldIfTimeGrain(
"${CurrencyUtils.formatAmount(daySave)} diarios",
DateUtils.DAYS
) + "<br>"
//WEEKS
string +=
boldIfTimeGrain(
"${CurrencyUtils.formatAmount(weekSave)} semanales",
DateUtils.WEEKS
) + "<br>"
//MONTHS
string +=
boldIfTimeGrain(
"${CurrencyUtils.formatAmount(monthSave)} mensuales",
DateUtils.MONTHS
) + "<br>"
//YEARS
string +=
boldIfTimeGrain(
"${CurrencyUtils.formatAmount(yearSave)} anuales",
DateUtils.YEARS
) + "<br>"
}
}
}
return string
}
fun boldIfTimeGrain(text: String, grain: String): String {
return if (timeGrain == grain) "<b>$text</b>" else text
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment