Skip to content

Latest commit

Β 

History

23 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Modern Android MVVM Authentication Starter

Platform Language Architecture Networking UI License

A production-structured Android starter project implementing clean MVVM (Model-View-ViewModel) architecture, token-based REST authentication, Kotlin Coroutines, ViewBinding, persistent session storage, and interactive dashboard UI.


πŸ“– Overview

The MVVM-implementation project is an architectural reference blueprint demonstrating how to build robust, decoupled Android applications. By strictly isolating UI controllers (Activities) from business logic (ViewModels) and data sources (Repositories), the codebase achieves clean separation of concerns, high testability, and seamless session state management.

🎯 Core Architectural Objectives

  • MVVM + Repository Pattern: Distinct layering that keeps Views purely declarative, ViewModels lifecycle-aware, and Repositories as the single source of truth for remote data.
  • RESTful Network Layer: Asynchronous HTTP communication with remote REST endpoints via Google Volley dispatches inside Kotlin Coroutines (viewModelScope).
  • Secure Session Lifecycle: Automatic session verification on startup (Splash screen) with persistent authentication token storage in SharedPreferences.
  • Type-Safe View Binding: Zero boilerplate findViewById calls with Android Jetpack ViewBinding.
  • Rich User Experience: Smooth Lottie vector animations during startup and dynamic accordion/dropdown dashboard layouts.

πŸ—οΈ Architecture & Data Flow

graph TD
    classDef view fill:#2D3748,stroke:#4FD1C5,stroke-width:2px,color:#fff;
    classDef vm fill:#1A365D,stroke:#63B3ED,stroke-width:2px,color:#fff;
    classDef repo fill:#234E52,stroke:#38B2AC,stroke-width:2px,color:#fff;
    classDef storage fill:#744210,stroke:#F6E05E,stroke-width:2px,color:#fff;
    classDef remote fill:#7B341E,stroke:#ED8936,stroke-width:2px,color:#fff;

    subgraph ViewLayer ["View Layer (Activities & ViewBinding)"]
        Splash["MainActivitySplash<br/>(Lottie Animation)"]:::view
        Login["MainActivityLogin"]:::view
        Register["MainActivityRegister"]:::view
        Home["MainActivityHome<br/>(Interactive Dashboard)"]:::view
    end

    subgraph ViewModelLayer ["ViewModel Layer (Lifecycle Aware)"]
        AuthVM["AuthViewModel<br/>(viewModelScope.launch)"]:::vm
    end

    subgraph RepositoryLayer ["Data & Repository Layer"]
        AuthRepo["AuthRepository"]:::repo
        Prefs["SharedPreferences<br/>('PrefsDatabase' / user_token)"]:::storage
        BackendAPI["Remote REST API<br/>(cricdex.enfotrix.com/api)"]:::remote
    end

    Splash -->|1. Check user_token| Prefs
    Splash -->|Token Present| Home
    Splash -->|Token Null| Login

    Login -->|2. Trigger loginUser| AuthVM
    Register -->|Trigger registerUser| AuthVM

    AuthVM -->|3. Dispatch network request| AuthRepo
    AuthRepo -->|4. Volley JsonObjectRequest POST| BackendAPI
    BackendAPI -->|5. JSON Response + Token| AuthRepo

    AuthRepo -->|6. Callback onSuccess| AuthVM
    AuthVM -->|7. Deliver Token| Login
    Login -->|8. Save token| Prefs
    Login -->|9. Route to| Home

    Home -->|Logout Action| Prefs
    Home -->|Clear Session & Redirect| Login
Loading

✨ Core Features & Technical Highlights

1. Robust Repository Pattern

AuthRepository encapsulates all HTTP headers, JSON serialization, and Volley RequestQueue dispatches:

class AuthRepository(private val context: Context) {
    private val loginUrl = "https://cricdex.enfotrix.com/api/login"

    fun loginUser(phone: String, password: String, onSuccess: (String) -> Unit, onError: (String) -> Unit) {
        val jsonBody = JSONObject().apply {
            put("phone_number", phone)
            put("password", password)
        }

        val jsonObjectRequest = object : JsonObjectRequest(
            Method.POST, loginUrl, jsonBody,
            Response.Listener { response ->
                if (response.optBoolean("success", false)) {
                    val token = response.optJSONObject("data")?.optString("token", "")
                    if (!token.isNullOrEmpty()) onSuccess(token)
                    else onError("Login failed: no token received")
                } else {
                    onError(response.optString("message", "Login Failed!"))
                }
            },
            Response.ErrorListener { error -> onError("Error: ${error.message}") }
        ) {
            override fun getHeaders(): Map<String, String> =
                mapOf("Content-Type" to "application/json")
        }
        Volley.newRequestQueue(context.applicationContext).add(jsonObjectRequest)
    }
}

2. Lifecycle-Aware ViewModel (AuthViewModel)

Leverages AndroidViewModel and viewModelScope to handle network triggers safely without leaking Activity contexts during device configuration changes:

class AuthViewModel(application: Application) : AndroidViewModel(application) {
    private val authRepository = AuthRepository(application)

    fun loginUser(phone: String, password: String, onSuccess: (String) -> Unit, onError: (String) -> Unit) {
        viewModelScope.launch {
            authRepository.loginUser(phone, password, onSuccess, onError)
        }
    }
}

3. Automatic Session Routing

MainActivitySplash inspects local persistence before navigation, ensuring authenticated users skip the login wall automatically.

4. Interactive Dashboard

MainActivityHome incorporates expandable collapsible accordion cards (e.g., iCloud & Outlook containers) and an action overflow popup menu with session termination / log-out capabilities.


πŸ“± Key Components & Project Structure

MVVM-implementation/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ src/main/java/com/shayan/mvvm_login/
β”‚   β”‚   β”œβ”€β”€ model/
β”‚   β”‚   β”‚   └── ModelUser.kt              # Data class representing user credentials
β”‚   β”‚   β”œβ”€β”€ repository/
β”‚   β”‚   β”‚   └── AuthRepository.kt         # Remote network datasource & Volley requests
β”‚   β”‚   β”œβ”€β”€ view/
β”‚   β”‚   β”‚   β”œβ”€β”€ MainActivitySplash.kt     # Splashscreen with Lottie & session check
β”‚   β”‚   β”‚   β”œβ”€β”€ MainActivityLogin.kt      # Login Activity with input validation
β”‚   β”‚   β”‚   β”œβ”€β”€ MainActivityRegister.kt   # User registration activity
β”‚   β”‚   β”‚   └── MainActivityHome.kt       # Dashboard activity with expandable cards
β”‚   β”‚   └── viewmodel/
β”‚   β”‚       └── AuthViewModel.kt          # ViewModel orchestrating business logic
β”‚   β”œβ”€β”€ src/main/res/
β”‚   β”‚   β”œβ”€β”€ layout/                       # XML layouts with ViewBinding support
β”‚   β”‚   β”œβ”€β”€ menu/                         # Toolbar dropdown menu definitions
β”‚   β”‚   β”œβ”€β”€ raw/                          # Lottie animation JSON file
β”‚   β”‚   └── values/                       # Colors, styles, and string resources
β”‚   └── build.gradle.kts                  # App dependencies & Kotlin DSL configuration
└── gradle/
    └── libs.versions.toml                # Centralized version catalog

πŸ› οΈ Technology Stack Matrix

Layer / Category Technology Purpose / Notes
Platform Android (API 24+ to 35) Min SDK 24, Target SDK 34, Compile SDK 35
Language Kotlin 2.0+ Modern Android language
Architecture MVVM + Repository Clean Architecture separation of concerns
Concurrency Kotlinx Coroutines viewModelScope & background task execution
Networking Google Volley Asynchronous HTTP / REST JSON request queue
View Binding AndroidX ViewBinding Null-safe compile-time view references
Animations Airbnb Lottie Android High-performance vector animations
Local Storage Android SharedPreferences Persistent token and session caching

πŸš€ Getting Started

Prerequisites

  • Android Studio Ladybug (2024.2.1+) or newer.
  • JDK 11 or JDK 17.
  • Android SDK 34 / 35.

Build & Run

  1. Clone the repository:
    git clone https://github.com/shayann07/MVVM-implementation.git
    cd MVVM-implementation
  2. Open in Android Studio: Let Gradle sync the project dependencies.
  3. Build the project:
    ./gradlew assembleDebug
  4. Deploy: Run on an emulator or physical device.

πŸ“„ License

This project is licensed under the MIT License β€” Copyright (c) 2026 shayann07.

About

Clean Android MVVM architecture starter featuring token-based authentication, Volley REST networking, Coroutines, ViewBinding, and Lottie animations.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages