The ViewModel Manager Pattern: How We Tamed a 2000+ Line ViewModel Into Clean, Testable Code
Article Summary
Chamod Lakmal from PickMe Engineering turned a 2,100-line ViewModel nightmare into 350 lines of clean, testable code. His team's test coverage jumped from 12% to 87%, and onboarding time dropped from 2 weeks to 2 days.
PickMe's checkout feature had become unmaintainable: a single ViewModel with 18 dependencies, 50+ StateFlows, and 2,000+ lines of tangled business logic. The team introduced the ViewModel Manager Pattern, extracting domain-specific logic into focused, injectable classes that share the ViewModel's lifecycle while maintaining clear separation of concerns.
Key Takeaways
- Reduced ViewModel from 2,100 to 350 lines (83% reduction) using focused Manager classes
- Test setup dropped from 85 lines to 12 lines with isolated, easily mockable Managers
- Each Manager owns single domain concern: ProductManager, CartManager, PaymentManager
- ViewModelScoped annotation ensures Managers share ViewModel lifecycle without tight coupling
- Team onboarding accelerated from 2 weeks to 2 days with clearer code organization
The ViewModel Manager Pattern uses composition to extract domain logic into ViewModelScoped classes, transforming massive ViewModels into clean coordinators that are 86% easier to test and dramatically faster to understand.
About This Article
PickMe's checkout ViewModel grew to over 2,000 lines with 18 dependencies and 50+ StateFlows scattered throughout. Testing became nearly impossible with only 12% coverage, and the team dealt with constant merge conflicts.
Chamod Lakmal split the code using the ViewModel Manager Pattern. Six focused classes took shape: ProductManager, CartManager, PaymentManager, OrderManager, ShippingManager, and AnalyticsManager. Each one handles a single responsibility.
Test setup went from 85 lines down to 12 lines, an 86% reduction. Test coverage jumped to 87%. New developers got up to speed in two days instead of two weeks, and the code became much easier to understand.