Agent Skills
› maxrave-dev/SimpMusic
› android-data-layer
android-data-layer
GitHub指导Android数据层实现,采用Repository模式、Room本地存储和Retrofit远程获取,支持离线优先同步。涵盖SSOT逻辑、Stale-While-Revalidate读取及Outbox写入策略,结合Hilt依赖注入。
Trigger Scenarios
Android数据层架构设计
实现离线优先功能
配置Room和Retrofit
处理数据同步策略
Install
npx skills add maxrave-dev/SimpMusic --skill android-data-layer -g -y
SKILL.md
Frontmatter
{
"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:
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
@Entitydata classes. - DAOs: Return
Flow<T>for observable data.
3. Remote Data (Retrofit)
- Usage: Fetching data from backend.
- Response: Use
suspendfunctions in interfaces. - Error Handling: Wrap network calls in
try-catchblocks or aResultwrapper 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
WorkManagerto push changes to server.
5. Dependency Injection
- Bind Repository interfaces to implementations in a Hilt Module.
@Binds abstract fun bindNewsRepository(impl: OfflineFirstNewsRepository): NewsRepository
Version History
- 11f5926 Current 2026-07-30 20:16


