Skip to content

Latest commit

 

History

History
111 lines (89 loc) · 4.83 KB

File metadata and controls

111 lines (89 loc) · 4.83 KB

Jetpack Compose Example

Platform Kotlin Compose License

A modern Android application built with Jetpack Compose, demonstrating a clean architecture with seamless integration of local (Room) and remote (Retrofit) data sources. The app allows users to view a list of posts, access post details, and create new posts, with offline support and reactive UI updates.

Table of Contents

Features

  • Home Screen: Displays a scrollable list of posts using Material 3 cards in a LazyColumn.
  • Post Detail Screen: Shows detailed post information with loading and error states.
  • Add Post Screen: Enables users to create new posts, stored locally.
  • Local Storage: Caches posts in a Room database for offline access.
  • Remote Data: Fetches posts from a REST API (JSONPlaceholder for demo purposes).
  • Reactive UI: Leverages Kotlin Flow and StateFlow for real-time UI updates. Ascending/descending navigation.
  • Error Handling: Displays user-friendly loading, error, and retry options.
  • Dependency Injection: Uses Hilt for modular dependency management.

Architecture

The app follows Clean Architecture principles, organized into three layers:

  • Presentation Layer:
    • UI: Built with Jetpack Compose, utilizing Material 3 components (Scaffold, TopAppBar, Card, FloatingActionButton, etc.).
    • ViewModels: HomeViewModel and PostDetailViewModel manage UI state using StateFlow for reactive updates.
  • Domain Layer:
    • Use Cases: GetPostsUseCase and GetPostUseCase encapsulate business logic.
    • Models: Post data class (id: Int, title: String, body: String, userId: Int?).
  • Data Layer:
    • Repository: PostRepositoryImpl integrates local (Room) and remote (Retrofit) data sources.
    • Local: Room database with PostDao for persistent storage and reactive queries.
    • Remote: Retrofit with ApiService for API calls to JSONPlaceholder (https://jsonplaceholder.typicode.com).

Data flows reactively using Flow from Room and API calls, converted to StateFlow in ViewModels to drive UI updates.

Tech Stack

  • Kotlin: Primary programming language.
  • Jetpack Compose: Declarative UI framework for modern Android UI.
  • Hilt: Dependency injection for modular and testable code.
  • Room: Local database for caching and offline support.
  • Retrofit: HTTP client for REST API communication.
  • Kotlin Coroutines: Asynchronous programming for smooth performance.
  • Flow/StateFlow: Reactive streams for real-time data updates.
  • Material 3: Design system for consistent, modern UI components.
  • Navigation Compose: Handles screen navigation.

Setup

Prerequisites

  • Android Studio (latest stable version recommended, e.g., Koala or later).
  • JDK 17 or higher.
  • Android device or emulator running API 26 or higher.

Steps

  1. Clone the Repository:
    git clone https://github.com/arifbd/jetpack-compose-example.git
    cd jetpack-compose-example
  2. Open in Android Studio:
  • Open the project in Android Studio.
  • Allow Gradle to sync and download dependencies.
  1. Build and Run:
  • Connect an Android device or start an emulator.
  • Click Run in Android Studio.

Usage

  1. Home Screen:
  • Shows a list of posts fetched from the JSONPlaceholder API (https://jsonplaceholder.typicode.com/posts) or cached locally in the Room database.
  • Tap a post to view its details.
  • Click the Floating Action Button to add a new post.
  1. Post Detail Screen:
  • Displays the title and body of the selected post.
  • Handles loading states and errors (e.g., "No internet connection") with a retry button.
  1. Add Post Screen:
  • Allows users to input a title and body to create a new post, stored in the local database.

Custom API Integration

The app uses JSONPlaceholder for demonstration. To integrate a custom API:

  • Update the base URL in NetworkModule.kt.
  • Adjust ApiService.kt to match your API's endpoint structure.
  • Modify PostResponse and mapping logic in PostRepositoryImpl to align with your API's response format.

Contributing

Contributions are welcome! To contribute:

  • Fork the repository.
  • Create a feature branch (git checkout -b feature/your-feature).
  • Commit your changes (git commit -m "Add your feature").
  • Push to the branch (git push origin feature/your-feature).
  • Open a Pull Request.

Please follow Kotlin coding conventions and include tests where applicable.

License

This project is licensed under the MIT License.