Skip to content

Refactor: moved business logic to Use Cases and added unit test suite - #1080

Merged
PranshulGG merged 9 commits into
PranshulGG:mainfrom
gietabhi10:main
Aug 26, 2026
Merged

Refactor: moved business logic to Use Cases and added unit test suite#1080
PranshulGG merged 9 commits into
PranshulGG:mainfrom
gietabhi10:main

Conversation

@gietabhi10

Copy link
Copy Markdown

🏗️ Architectural Refactor

Issue: [Feature Request] Refactor Business Logic to Use Cases and Setup Testing Suite
Problem Statement: The WeatherViewModel has grown to over 450 lines, taking on too many responsibilities including coordinate reconciliation, parallel data fetching orchestration, and layout management. This high coupling makes it difficult to unit test business logic without mocking the entire UI state. Furthermore, layout management logic is currently duplicated between WeatherViewModel and DailyScreenViewModel, making the codebase harder to maintain. There is also no established infrastructure for modern unit testing (MockK, Coroutine testing).

Proposed Solution:

  1. Introduce a Domain Layer with focused Use Cases (GetWeatherUseCase, SaveWeatherBlocksUseCase, etc.) to encapsulate business logic.
  2. Refactor existing ViewModels to delegate to these Use Cases, removing code duplication and slimming down the ViewModels.
  3. Integrate MockK, Turbine, and kotlinx-coroutines-test into the build configuration.
  4. Establish a baseline of unit tests for the new Use Cases to ensure reliability.

Alternatives Considered:
Keeping logic in Repositories: Rejected because complex orchestration (like coordinate updates combined with weather fetching) belongs in a Use Case to keep repositories focused on data mapping.

Hilt-only testing: Rejected in favor of pure unit tests for the domain layer for faster execution.
Acknowledgements:
[x] I have searched the existing issues.
[x] I understand that submitting a request does not guarantee it will be implemented.
[x] I have provided all necessary information.

@PranshulGG

PranshulGG commented Aug 24, 2026

Copy link
Copy Markdown
Owner

@gietabhi10 Indeed, this was needed. The ViewModel was growing :P

Also, did you follow CONTRIBUTING.md? I don't see any attribution

Also, be sure that the app still behaves the same

Thanks! I'll wait for the feedback

@gietabhi10

Copy link
Copy Markdown
Author

@PranshulGG Thanks for the feedback! I've updated the PR to address your points:

  1. Attribution Added: Added the required attribution to the new domain layer files following   CONTRIBUTING.md.
  2. Behavior Verified: I've verified the build and logic. Specifically, I fixed a subtle bug in the auto-refresh cycle to ensure it always uses the current location, and updated the UI call-sites to match the new Domain signatures.
  3. Infrastructure: The new Unit Test suite (using MockK/Turbine) provides coverage for the core orchestration logic to ensure stability moving forward.

Ready for a final review! 🌦️

@gietabhi10 gietabhi10 closed this Aug 24, 2026
@gietabhi10 gietabhi10 reopened this Aug 24, 2026
@PranshulGG

Copy link
Copy Markdown
Owner

@gietabhi10 Can you rebase and fix up the conflicts?

@gietabhi10

gietabhi10 commented Aug 25, 2026

Copy link
Copy Markdown
Author

@PranshulGG Thanks for the review! I've updated the PR with the following:

  1. Rebased & Resolved Conflicts: I've successfully rebased on the latest main branch. I resolved the conflicts in WeatherViewModel.kt by integrating the new WeatherBackgroundUpdateScheduler while maintaining the Use Case refactor.
  2. Attributions Added: Added the required attribution headers to all new files in domain/usecase/ as per   CONTRIBUTING.md.
  3. Behavior Verified: Confirmed the app builds and behaves correctly. The new architecture is now fully synced with your recent changes.

@PranshulGG
PranshulGG merged commit 6c892df into PranshulGG:main Aug 26, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants