KSurvey
Architecture
This project follow CLean Architecture.
The project contains 3 modules:
-
Domain
- This module contains business logic models of the application
- This module define the Repository interfaces, then the higher modules can implement these interfaces
- It also define Use Cases that handle business logic of the application using Repository interfaces
-
Data
- This module contains Repository implementations
- This module contains Android components (such as Network, Data Soures..) that can be used in Repository implementation
-
Presentation
- This module handles UI parts of the application
- This module follows MVVM pattern
The project use Hilt for Dependency Injection
Libraries used
The application uses following external libraries:
- Coil: to load image from remote
- Retrofit: to handle HTTP requests
- Room: to handle CRUD actions
- Lottie: to display animation
Implementation
1. UI
The application has 4 screens:
-
Splash screen
- To display a welcome screen to user
- To verify user's status, then navigate user to Home screen or Login screen
-
Login screen
- To display input fields and Login button
- This screen calls APIs to login and fetch user's info (includes tokens), then save to local storage
-
Home screen
- To display user's info
- To display surveys page by page
- User can logout using the left menu
- User can navigate to Thank you screen by clicking on button Take This Survey
-
Thank you screen
- To display an animation
- This page will finish in 5 seconds and back to the Home screen
2. Details implementation
2.1. Storage of access tokens securely
- To secure the Access Token and Refresh Token, the application uses Secret Key from AndroidKeyStore to encrypt and decrypt these tokens.
- After getting the tokens from API, the application encrypts the tokens first, then save them into local storage.
- When the application need User's info (including tokens), the application will take User's info from local storage and decrypts tokens.
The implementation can be found at .data.security.CryptoUtil
2.2. Automatic usage of refresh tokens
- When the application launches, it checks if there are any valid Access Token (the token must be non-empty and not expires - by checking
createdAt
and expiresIn
).
- If NO, navigate users to Login screen.
- If YES, the application check if the Access Token will expire soon.
- If NO, navigate users to Home screen.
- If YES, the application calls API to refresh the new Access Token (using
refreshToken
). Then the application save the new Access Token and Refresh Token into local storage (need to encrypt before saving). Then navigate users to Home Screen
The implementation can be found at .domain.usecase.UserUseCase#validateUserToken
2.3. Caching of surveys
To display the surveys when there is no network connection
- The application get surveys from local storage first
- If there is no record, the application fetch surveys from the API and save them into local storage
The implementation can be found at .data.repository.SurveyRepositoryImpl.getSurveys
2.4. Pull-to-refresh
The implementation can be found at .data.repository.SurveyRepositoryImpl.getSurveys
3. Unit Test
The project includes Unit Test for module Data (locate at data/src/test
)
The Unit Test is done with JUnit
, Mockito
, Mockk
LoginApiTest
LogoutApiTest
RefershTokenApiTest
FetchProfileApiTest
FetchSurveysApiTest
FetchUserDetailsApiTest
RoomUserStoreTest
RoomSurveyStoreTest
UserRepositoryImplTest
SurveyRepositoryImplTest
Screen record