Advertisement
Guest User

Untitled

a guest
Jul 12th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.24 KB | None | 0 0
  1.     private fun updateFeedback() {
  2.         val now = DateTime.now()
  3.         val dayOfWeek = now.dayOfWeek
  4.         val isMonday = dayOfWeek == 1
  5.  
  6.         val faceIcon: Int
  7.         val message: String
  8.         if (isMonday) {
  9.             val nowWeekdayIndex = dayOfWeek - 1
  10.             val last4WeekCalories = (1..4).sumByDouble { model.days[model.days.size - ((model.weekDays * (it + 1)) - nowWeekdayIndex)].intakeCalories.toDouble() }.toFloat()
  11.             val average4WeeksCalories = last4WeekCalories / 4
  12.             faceIcon = R.drawable.feedback_logging_info
  13.             message = repository.getString(R.string.logging_cal_and_activities_feedback_info, Math.round(average4WeeksCalories))
  14.         } else {
  15.             val acceptedKcalOffside = 50f
  16.             val thisWeekStart = DateUtils.getWeekStart(now)
  17.             val thisWeekEnd = DateUtils.getWeekEnd(thisWeekStart).withMillisOfDay(0).plusDays(1)
  18.  
  19.             val daysLoggedThisWeek = model.days
  20.                     .filter { it.dateTime.isAfter(thisWeekStart.minus(1)) && it.dateTime.isBefore(thisWeekStart.plusWeeks(1)) }
  21.                     .filter { it.intakeCalories > 0f }
  22.                     .count()
  23.  
  24.             val daysInRecommendedRange = model.days
  25.                     .filter { it.dateTime.isAfter(thisWeekStart.minus(1)) && it.dateTime.isBefore(thisWeekEnd) }
  26.                     .filter {
  27.                         val intake = it.intakeCalories
  28.                         val min = it.recommendCalories - acceptedKcalOffside
  29.                         val max = it.recommendCalories + acceptedKcalOffside
  30.                         intake in min..max
  31.                     }
  32.                     .count()
  33.  
  34.             val value = daysInRecommendedRange.toFloat() / now.dayOfWeek.toFloat()
  35.             if (value >= 0.5f) {
  36.                 faceIcon = R.drawable.feedback_logging_positive
  37.                 message = repository.getString(R.string.logging_cal_and_activities_feedback_positive, daysInRecommendedRange, daysLoggedThisWeek)
  38.             } else {
  39.                 faceIcon = R.drawable.feedback_logging_negative
  40.                 message = repository.getString(R.string.logging_cal_and_activities_feedback_negative, daysInRecommendedRange, daysLoggedThisWeek)
  41.             }
  42.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement