Skip to main content

android-data-layer

Guidance on implementing the Data Layer using Repository pattern, Room (Local), and Retrofit (Remote) with offline-first synchronization.

Source facts

Repository
maxrave-dev/SimpMusic
Last source activity
March 5, 2026 at 02:43
Detected SKILL.md language
English
Stars
13,045
Forks
734

Install options

The review-first prompt is selected by default. You can switch to a direct command or download a local copy.

Review the source files

Read SKILL.md and any companion files shown by SkillsMP before deciding whether to install.

Showing SKILL.md

SKILL.md
Source instructions · Read-only preview
name
android-data-layer
description
Guidance on implementing the Data Layer using Repository pattern, Room (Local), and Retrofit (Remote) with offline-first synchronization.
# Android Data Layer & Offline-First ## Instructions The Data Layer coordinates data from multiple sources. ### 1. Repository Pattern * **Role**: Single Source of Truth (SSOT). * **Logic**: The repository decides whether to return cached data or fetch fresh data. * **Implementation**: ```kotlin class NewsRepository @Inject constructor( private val newsDao: NewsDao, private val newsApi: NewsApi ) { // Expose data from Local DB as the source of truth val newsStream: Flow<List<News>> = newsDao.getAllNews() // Sync operation suspend fun refreshNews() { val remoteNews = newsApi.fetchLatest() newsDao.insertAll(remoteNews) } } ``` ### 2. Local Persistence (Room) * **Usage**: Primary cache and offline storage. * **Entities**: Define `@Entity` data classes. * **DAOs**: Return `Flow<T>` for observable data. ### 3. Remote Data (Retrofit) * **Usage**: Fetching data from backend. * **Response**: Use `suspend` functions in interfaces. * **Error Handling**: Wrap network calls in `try-catch` blocks or a `Result` wrapper to handle exceptions (NoInternet, 404, etc.) gracefully. ### 4. Synchronization * **Read**: "Stale-While-Revalidate". Show local data immediately, trigger a background refresh. * **Write**: "Outbox Pattern" (Advanced). Save local change immediately, mark as "unsynced", use `WorkManager` to push changes to server. ### 5. Dependency Injection * Bind Repository interfaces to implementations in a Hilt Module. ```kotlin @Binds abstract fun bindNewsRepository(impl: OfflineFirstNewsRepository): NewsRepository ```
View on GitHub