Overview
Threadly follows the MVVM (Model-View-ViewModel) architectural pattern, which provides clear separation of concerns between UI logic and business logic. This pattern is recommended by Google for Android development and integrates seamlessly with Android Architecture Components.MVVM Components
View Layer (Activities & Fragments)
The View layer consists of Activities and Fragments responsible for:- Rendering UI components
- Handling user interactions
- Observing ViewModel data changes
- Updating UI based on LiveData emissions
Example: HomeActivity
activities/HomeActivity.java:61
ViewModel Layer
ViewModels manage UI-related data and survive configuration changes (like screen rotations). They expose LiveData objects that the View layer observes.ProfileViewModel Example
The ProfileViewModel manages user profile data and user posts:viewmodels/ProfileViewModel.java:24
MessagesViewModel Example
The MessagesViewModel demonstrates Room Database integration:viewmodels/MessagesViewModel.java:14
Model Layer
The Model layer consists of:- Data Models (POJOs): Plain Old Java Objects representing data structures
- Network Managers: Handle API communication
- Room Database Entities: Local data persistence
Available ViewModels
Threadly includes the following ViewModels, each managing specific feature data:ProfileViewModel
Manages user profile data and user posts with pagination support
MessagesViewModel
Handles real-time messaging data from Room Database
CommentsViewModel
Manages post comments and interactions
SearchViewModel
Handles user and content search functionality
StoriesViewModel
Manages stories feed and viewing
ExplorePostsViewModel
Handles explore/discover feed with various post types
InteractionNotificationViewModel
Manages notification data and unread counts
SuggestUsersViewModel
Provides user suggestions for following
LiveData Usage
What is LiveData?
LiveData is an observable data holder class that is lifecycle-aware. It ensures UI updates only happen when the UI is in an active state.Benefits in Threadly
- Automatic UI Updates: When data changes, observers are notified automatically
- Lifecycle Awareness: No memory leaks or crashes due to stopped activities
- Configuration Change Handling: Data persists across screen rotations
- Room Integration: DAOs return LiveData for reactive database queries
Common Patterns
Lazy Loading
Pagination
viewmodels/ProfileViewModel.java:137
Room Database LiveData
Data Flow
Best Practices Implemented
1. No Context References in ViewModel
ViewModels useAndroidViewModel which provides Application context safely:
2. ViewModels Don’t Know About Views
ViewModels never reference UI components directly - they only expose data:3. Single Responsibility
Each ViewModel focuses on a specific feature:ProfileViewModel: User profile and postsMessagesViewModel: Messaging dataCommentsViewModel: Comment interactions
4. Thread Safety
LiveData updates usepostValue() for background thread safety:
Related Documentation
Project Structure
See where ViewModels fit in the overall project
Database
Learn how ViewModels integrate with Room
Architecture Overview
Understand the complete architecture