feat: ANC visit details with visit locking [MHWC-197]#82
Conversation
…small fixes were applied: corrected AncVisitAdapter DiffUtil so multiple visits per patient are identified correctly, used the ViewModel’s last visit when deciding read-only for list clicks, ensured setUpPage uses a local recordExists flag, and capped suggested ANC visit numbers at 4 to match the form’s supported range.
- Visit locking: only last ANC visit editable; previous visits read-only from list - Pregnancy Registration first: show ANC only after LMP registration - ANC Period dropdown by visit: 1→1–4, 2→2–4, 3→3–4, 4→no dropdown - Edit by clicking visit in list; form editability updates when switching visit in dropdown - Timestamp ANC record on save (updatedDate); status/next stage via persist and lastVisitNumber - Comment out 28-day ANC gap check per product request - AncVisitAdapter: DiffUtil identity by patientID + visitNumber for multi-visit list
…visit-locking' into mhwc-197-anc-visit-details-with-visit-locking # Conflicts: # app/src/main/java/org/piramalswasthya/cho/ui/commons/fhir_visit_details/FragmentVisitDetail.kt # app/src/main/java/org/piramalswasthya/cho/ui/commons/maternal_health/pregnant_woment_anc_visits/form/PwAncFormFragment.kt # app/src/main/java/org/piramalswasthya/cho/ui/commons/maternal_health/pregnant_woment_anc_visits/form/PwAncFormViewModel.kt
|
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 You can disable this status message by setting the
📝 WalkthroughWalkthroughDynamic ANC visit handling and pregnancy-registration flows added: visit-number-dependent dropdowns, GA-based ANC visit suggestion, LMP-save-driven registration path, form editability toggles based on visit age, and isOldVisit refactored to LiveData with visit-switching support. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant FragmentVisitDetail
participant PwAncFormFragment
participant PwAncFormViewModel
participant PregnantWomanAncVisitDataset
rect rgba(135, 206, 250, 0.5)
Note over User,FragmentVisitDetail: Pregnancy Registration flow
User->>FragmentVisitDetail: Select "Pregnancy Registration"
FragmentVisitDetail->>FragmentVisitDetail: Require LMP date and save registration
FragmentVisitDetail->>PwAncFormViewModel: Observe LMP/save confirmation
FragmentVisitDetail->>FragmentVisitDetail: Update reason dropdown to ANC
end
rect rgba(144, 238, 144, 0.5)
Note over User,FragmentVisitDetail: ANC navigation with GA suggestion
User->>FragmentVisitDetail: Choose ANC
FragmentVisitDetail->>FragmentVisitDetail: Compute weeks via getWeeksOfPregnancy()
FragmentVisitDetail->>FragmentVisitDetail: Map weeks → suggestedVisitNumber (1-4)
FragmentVisitDetail->>PwAncFormFragment: Navigate(with suggestedVisitNumber, computed isOldVisit)
PwAncFormFragment->>PwAncFormViewModel: Initialize for visit
PwAncFormViewModel->>PregnantWomanAncVisitDataset: Request visit-number-dependent ancVisit entries
PregnantWomanAncVisitDataset-->>PwAncFormViewModel: Return filtered entries
PwAncFormViewModel-->>PwAncFormFragment: Emit isOldVisit/formEditable state
PwAncFormFragment->>PwAncFormFragment: Configure adapter and UI based on formEditable
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 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 |
|
@coderabbitai full review |
✅ Actions performedFull review triggered. |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In
`@app/src/main/java/org/piramalswasthya/cho/ui/commons/maternal_health/pregnant_woment_anc_visits/form/PwAncFormFragment.kt`:
- Around line 81-118: The two LiveData observers (viewModel.recordExists.observe
and viewModel.isOldVisit.observe) race and leave formEditable stale; factor the
UI update into a single function (e.g., updateFormEditable) and call it from
both observers (or use a combined observable) so visibility/adapter state is
always recomputed from the latest values; specifically, remove the early return
in the isOldVisit observer, compute formEditable from both
viewModel.recordExists.value and viewModel.isOldVisit.value, set
binding.btnSubmit and activity?.findViewById<Button>(R.id.btnSubmit) visibility
accordingly, re-create ancFormAdapter with the same
FormInputAdapter.FormValueListener (calling viewModel.updateListOnValueChanged
and hardCodedListUpdate), set binding.form.rvInputForm.adapter and submit
latestFormList if present, ensuring latestFormList / ancFormAdapter updates are
centralized to avoid the race.
In
`@app/src/main/java/org/piramalswasthya/cho/ui/commons/maternal_health/pregnant_woment_anc_visits/form/PwAncFormViewModel.kt`:
- Around line 119-136: The isOld calculation in switchToVisit incorrectly uses
inequality and marks future (unsaved) visits as old; change the logic so a visit
is considered old only when its number is less than the last saved visit.
Replace the current computation of isOld with a comparison like: isOld =
selectedVisit < max(1, lastAncVisitNumber) (i.e., use selectedVisit <
lastAncVisitNumber with lastAncVisitNumber floored to 1) so that future visits
are not treated as old; update the assignment to _isOldVisit and keep references
to switchToVisit, selectedVisit, lastAncVisitNumber, and _isOldVisit to locate
the change.
...swasthya/cho/ui/commons/maternal_health/pregnant_woment_anc_visits/form/PwAncFormFragment.kt
Show resolved
Hide resolved
...wasthya/cho/ui/commons/maternal_health/pregnant_woment_anc_visits/form/PwAncFormViewModel.kt
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In
`@app/src/main/java/org/piramalswasthya/cho/ui/commons/maternal_health/pregnant_woment_anc_visits/form/PwAncFormViewModel.kt`:
- Line 95: The assignment registerRecord =
maternalHealthRepo.getSavedRegistrationRecord(patientID)!! in PwAncFormViewModel
uses a non-null assertion that can crash; replace it with a null-safe check of
maternalHealthRepo.getSavedRegistrationRecord(patientID) and handle the null
case explicitly (e.g., log/processLogger an error, show a user-facing message,
navigate back/abort form initialization, or throw a clear IllegalStateException
with context) so the ViewModel does not NPE on unexpected navigation states;
update any consumers of registerRecord to account for the nullable type or
ensure early return after the null handling.
- Around line 119-146: switchToVisit lacks try/catch and can crash or leak DB
errors (and will throw UninitializedPropertyAccessException if called before
init due to the lateinit registerRecord). Wrap the coroutine body (inside
viewModelScope.launch/withContext) in a try/catch that catches Exception (and
optionally UninitializedPropertyAccessException) and on error posts a UI-visible
error state (e.g., a LiveData/SingleEvent) and logs the exception; ensure you
check the lateinit registerRecord before use (use ::registerRecord.isInitialized
or null/guard path) before passing it to dataset.setUpPage, and keep existing DB
calls to maternalHealthRepo.getSavedAncRecord and userRepo.getLoggedInUser()
inside the guarded block so failures are handled similarly.
🧹 Nitpick comments (1)
app/src/main/java/org/piramalswasthya/cho/ui/commons/maternal_health/pregnant_woment_anc_visits/form/PwAncFormViewModel.kt (1)
117-117: Consider extracting magic number to a named constant.
ancPeriodFormId = 3is a magic number. For maintainability, consider defining this alongside other form IDs in a shared constants file or within the dataset class.
...wasthya/cho/ui/commons/maternal_health/pregnant_woment_anc_visits/form/PwAncFormViewModel.kt
Show resolved
Hide resolved
...wasthya/cho/ui/commons/maternal_health/pregnant_woment_anc_visits/form/PwAncFormViewModel.kt
Show resolved
Hide resolved
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
app/src/main/java/org/piramalswasthya/cho/ui/commons/maternal_health/pregnant_woment_anc_visits/form/PwAncFormViewModel.kt (2)
83-120:⚠️ Potential issue | 🟠 MajorPrevent
ancCachefrom remaining uninitialized when patient display is missing.
IfpatientRepo.getPatientDisplay(patientID)returns null and no existing ANC record exists,ancCachenever initializes andsaveForm()will crash. Fail fast or initializeancCacheregardless.🛠️ Suggested fix
- ben = patientRepo.getPatientDisplay(patientID)?.also { ben -> - _benName.value = - "${ben.patient.firstName} ${ben.patient.lastName ?: ""}" - _benAgeGender.value = "${ben.patient.age} ${ben.ageUnit?.name} | ${ben.gender?.genderName}" - ancCache = PregnantWomanAncCache( - patientID = patientID, - visitNumber = visitNumber, - syncState = SyncState.UNSYNCED, - createdBy = asha.userName, - updatedBy = asha.userName - ) - } + val patient = patientRepo.getPatientDisplay(patientID) + if (patient == null) { + Timber.e("No patient display for patientID=$patientID; cannot load ANC form") + _state.postValue(State.SAVE_FAILED) + return@launch + } + ben = patient + _benName.value = + "${patient.patient.firstName} ${patient.patient.lastName ?: ""}" + _benAgeGender.value = + "${patient.patient.age} ${patient.ageUnit?.name} | ${patient.gender?.genderName}" + ancCache = PregnantWomanAncCache( + patientID = patientID, + visitNumber = visitNumber, + syncState = SyncState.UNSYNCED, + createdBy = asha.userName, + updatedBy = asha.userName + )
176-183:⚠️ Potential issue | 🟠 MajorRefresh
lastAncVisitNumberafter save to enforce visit locking in-session.
Without updating it, earlier visits can remain editable after saving a newer visit until the screen is reloaded.🛠️ Suggested fix
ancCache.updatedDate = System.currentTimeMillis() maternalHealthRepo.persistAncRecord(ancCache) + lastAncVisitNumber = maxOf(lastAncVisitNumber, ancCache.visitNumber)
DeepakKumar471
left a comment
There was a problem hiding this comment.
Looking good.
Approved.
|



📋 Description
JIRA ID: MHWC-197
As a CHO, I want to record ANC visit details and progress the woman through ANC stages while locking previous visits, so that clinical data integrity is maintained.
- The most recent ANC visit remains editable.
- The visit is timestamped.
- The woman’s ANC status is updated.
- The record progresses to the next ANC stage.
- Auto-calculated based on gestational age.
- Manually editable if required.
✅ 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.
Summary by CodeRabbit
New Features
Bug Fixes
✏️ Tip: You can customize this high-level summary in your review settings.