FLW-857: Fix age format display across all modules#342
FLW-857: Fix age format display across all modules#342umaseershika45 wants to merge 2 commits intoPSMRI:release-2.8from
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
📝 WalkthroughWalkthroughModified the Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@CodeRabbit review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
app/src/main/java/org/piramalswasthya/sakhi/model/Ben.kt (1)
795-819:⚠️ Potential issue | 🟠 MajorThe month-end borrow can still produce negative day counts.
With a DOB on January 31 and a current date of March 1, this path leaves
days == -2, so the UI can render0 Years 1 Months -2 Days. Please switch this to elapsed-date calculation instead of subtracting calendar fields directly, and add a regression test for month-end/leap-year cases.🐛 Proposed fix
fun getAgeDisplayString(dob: Long): String { - val calDob = Calendar.getInstance().apply { timeInMillis = dob } - val calNow = Calendar.getInstance() - - var years = calNow.get(Calendar.YEAR) - calDob.get(Calendar.YEAR) - var months = calNow.get(Calendar.MONTH) - calDob.get(Calendar.MONTH) - var days = calNow.get(Calendar.DAY_OF_MONTH) - calDob.get(Calendar.DAY_OF_MONTH) - - if (days < 0) { - months-- - val tempCal = calNow.clone() as Calendar - tempCal.add(Calendar.MONTH, -1) - days += tempCal.getActualMaximum(Calendar.DAY_OF_MONTH) - } - - if (months < 0) { - years-- - months += 12 - } + val calDob = Calendar.getInstance().apply { + timeInMillis = dob + set(Calendar.HOUR_OF_DAY, 0) + set(Calendar.MINUTE, 0) + set(Calendar.SECOND, 0) + set(Calendar.MILLISECOND, 0) + } + val calNow = Calendar.getInstance().apply { + set(Calendar.HOUR_OF_DAY, 0) + set(Calendar.MINUTE, 0) + set(Calendar.SECOND, 0) + set(Calendar.MILLISECOND, 0) + } + if (calDob.after(calNow)) return "0 Years 0 Months 0 Days" + + val cursor = calDob.clone() as Calendar + var years = 0 + while (true) { + val next = cursor.clone() as Calendar + next.add(Calendar.YEAR, 1) + if (next.after(calNow)) break + cursor.add(Calendar.YEAR, 1) + years++ + } + + var months = 0 + while (true) { + val next = cursor.clone() as Calendar + next.add(Calendar.MONTH, 1) + if (next.after(calNow)) break + cursor.add(Calendar.MONTH, 1) + months++ + } + + var days = 0 + while (true) { + val next = cursor.clone() as Calendar + next.add(Calendar.DAY_OF_MONTH, 1) + if (next.after(calNow)) break + cursor.add(Calendar.DAY_OF_MONTH, 1) + days++ + } return if (years < 6) { "$years Years $months Months $days Days" } else { "$years Years"🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@app/src/main/java/org/piramalswasthya/sakhi/model/Ben.kt` around lines 795 - 819, The current getAgeDisplayString(dob: Long) computes years/months/days by subtracting Calendar fields which can yield negative day results for month-end dates; replace that logic with an elapsed-date calculation using java.time.LocalDate and java.time.Period (convert dob millis to Instant->ZoneId.systemDefault()->LocalDate), then derive years/months/days from Period to guarantee non-negative day/month components; update the function getAgeDisplayString to return "Y Years M Months D Days" for ages <6 and "Y Years" otherwise, and add regression tests covering cases like DOB=Jan 31 to current=Mar 1 and leap-year boundaries to assert no negative days are produced.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@app/src/main/java/org/piramalswasthya/sakhi/model/Ben.kt`:
- Around line 815-818: BenBasicDomainForForm.age is still returning the legacy
"$ageInt $ageUnit" string while BenBasicDomain.age uses
getAgeDisplayString(dob), so form-backed models are inconsistent; update the age
getter/logic in BenBasicDomainForForm (and any similar form model) to call
getAgeDisplayString(dob) (or reuse the same helper) instead of building "$ageInt
$ageUnit" so the form modules use the same display format (including the years<6
months/days behavior) as BenBasicDomain.
---
Outside diff comments:
In `@app/src/main/java/org/piramalswasthya/sakhi/model/Ben.kt`:
- Around line 795-819: The current getAgeDisplayString(dob: Long) computes
years/months/days by subtracting Calendar fields which can yield negative day
results for month-end dates; replace that logic with an elapsed-date calculation
using java.time.LocalDate and java.time.Period (convert dob millis to
Instant->ZoneId.systemDefault()->LocalDate), then derive years/months/days from
Period to guarantee non-negative day/month components; update the function
getAgeDisplayString to return "Y Years M Months D Days" for ages <6 and "Y
Years" otherwise, and add regression tests covering cases like DOB=Jan 31 to
current=Mar 1 and leap-year boundaries to assert no negative days are produced.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 64eaceea-3b26-4093-91f0-f4e9a637cf47
📒 Files selected for processing (1)
app/src/main/java/org/piramalswasthya/sakhi/model/Ben.kt
|
@umaseershika45 please fix coderabbit suggestion also do unit testing |
|
Sure sir, I have fixed the CodeRabbit suggestion by updating the form models also. I have verified the changes and completed unit testing as well. |



📋 Description
JIRA ID: FLW-857
Please provide a summary of the change and the motivation behind it. Include relevant context and details.
Implemented age display logic for beneficiary cards as per requirement.
For beneficiaries below 6 years, age is displayed in Years, Months, and Days.
For beneficiaries 6 years and above, age is displayed in Years only.
This change ensures proper and consistent age representation across all modules.
✅ Type of Change
ℹ️ Additional Information
Please describe how the changes were tested, and include any relevant screenshots, logs, or other information that provides additional context.
Updated getAgeDisplayString(dob) method in Ben.kt.
Verified changes in beneficiary list screens.
No UI changes involved, only logic update.
Ensured age is displayed correctly for both below and above 6 years cases.
Summary by CodeRabbit
Bug Fixes