Skip to content

eduhmik/Clean-MVVM-ArchComponents

Β 
Β 

Repository files navigation

home

Β  Β  codebeat badge

The-Force

An Android app consuming a Star Wars API to display Movie Characters it has been built with clean architecture principles, Repository Pattern and MVVM pattern as well as Architecture Components.

Min Api Level : 21 Supports Over 87% Devices

Build System : Gradle

Table of Contents

Prerequisite

Before running the project create a firebase project and replace the google-services.json with yours in the app directory for a successful build and enable crashlytics.

package-name: com.k0d4black.theforce

To run the release build successfully in travis you will need to replace the default values in keystore.properties.samplefile with your own in a keystore.properties file.

Next archive your keystore file and the properties file with the following command

 tar cvf secrets.tar keystore.properties theforce.jks

Next encrypt the archive and add config to travis with the following command

travis encrypt-file --pro secrets.tar --add

Verify that in your travis.yml in the before_install it looks something like

before_install:
  - openssl aes-256-cbc -K $encrypted_5880cf525281_key -iv $encrypted_5880cf525281_iv -in secrets.tar.enc -out secrets.tar -d
  - tar xvf secrets.tar

Make sure to add only the *.enc file to git,leave out the keystore.properties and *.jks file. Add the following to the root .gitignore just to be sure

*.jks
*.tar
keystore.properties

If you encounter any error check this site out.

Architecture

The Application is split into a three layer architecture:

  • Presentation
  • Domain
  • Data

Architecture Flow Diagram

This provides better abstractions between framework implementations and the underlying business logic.It requires a number of classes to get things running but the pros outweigh the cons in terms of building an app that should scale.

The 3 layered architectural approach is majorly guided by clean architecture which provides a clear separation of concerns with its Abstraction Principle.

The domain and data layers are java module libraries as the business logic does not rely on the Android frameworks concrete implementations.

Presentation

The application presentation layer contains the Activity,Fragments and Viewmodels and handles Dependency Injection.

The UI layer feature package contains character_detail and character_search which contain an activity and corresponding viewmodel as well as other UI related classes.

The viewmodels are provided by the Koin that uses Kotlin's DSLs to lazily resolve dependency graph at runtime

The ViewModel then receives data from the use case and updates the LiveData being observed by the activity,the Activity then makes updates to the UI as need be depending on the current view state.

The UI utilises a State pattern by representing expected view states using sealed classes. This aids with delegating logic operations to the Viewmodel and makes testing in isolation easier.

Domain

The domain layer contains domain model classes which represent the data we will be handling across presentation and data layer.

Use cases are also provided in the domain layer and orchestrate the flow of data from the data layer onto the presentation layer and a split into modular pieces serving one particular purpose.

The UseCases use a BaseUseCase interface that defines the parameters its taking in and output this helps in creating fakes using in testing.

Data

The Data layer using the Repository Pattern will be able to provide data to the defined use cases which in this case is searching for characters and viewing details of selected characters.The use-cases are based on the single responsibility rule.

This provides a more decoupled system,as it is isolated from changes to the db by abstracting low level implementation details of data sources and changes to the UI.

The repository classes delegate access of data to the data source of interest either local data source or a remote data source.

This layer also handles mapping of data entities to their domain representations which when eventually passed to the presentation layer the domain will be mapped to the presentation model.

Testing

Testing has been done based on the architectural layers.

1.Domain

Contains tests that encompass domain models and uses mockito to verify use case behavior.

2.Data

Tests in the data inherit from a base test that provides a mock web server with the api interface to request paths the routing of paths to responses is handled by a custom mock web server dispatcher.

Json responses have also been provided in the test resource folder they are similar to the response that will be received from the api The repository tests serve as integration tests between the data sources and mappers to the domain models. Currently the data source tests serve as unit tests verifying the appropriate responses are received from remote source.

3.Presentation

The Presentation layer contains robolectric jvm tests on for menu items and instrumentation tests checking on system behaviour as per user expectation.

The UI tests display data served from a mock web server running from the devices localhost,this removes flakiness compared to relying on actual data from the real server aspects such as internet connection or network service might bring up issues.

View models testing on live data were guided by this article

Libraries

Libraries used in the whole application are:

  • JetpackπŸš€
    • Viewmodel - Manage UI related data in a lifecycle conscious way and act as a channel between use cases and ui
    • Data Binding - support library that allows binding of UI components in layouts to data sources,binds character details and search results to UI
  • Retrofit - type safe http client and supports coroutines out of the box.
  • Moshi - JSON Parser,used to parse requests on the data layer for Entities and understands Kotlin non-nullable and default parameters
  • okhttp-logging-interceptor - logs HTTP request and response data.
  • Mockito - Mocking framework used to provide mocks to verify behaviour in domain usecases tests.
  • kotlinx.coroutines - Library Support for coroutines,provides runBlocking coroutine builder used in tests
  • Truth - Assertions Library,provides readability as far as assertions are concerned
  • MockWebServer - web server for testing HTTP clients ,verify requests and responses on the star wars api with the retrofit client.
  • Leak Canary - Leak Detection Library
  • Material Design - build awesome beautiful UIs.πŸ”₯πŸ”₯
  • Firebase - Backend As A Service for faster mobile development.
    • Crashylitics - Provide Realtime crash reports from users end.
  • Koin - A pragmatic lightweight dependency injection framework for Kotlin
  • Robolectric - Unit test on android framework.
  • Espresso - Test framework to write UI Tests
  • recyclerview-animators - Recycler View Animations
  • AboutLibraries -provide info on used open source libraries.

Contributors

  • Thanks to Zafer Celaloglu for the Dagger to Koin Refactor and additional test cases.

Feel free to contribute in any way to the project.

Demo

Get it on Google Play

Google Play and the Google Play logo are trademarks of Google LLC.

Copyright Notice

Star Wars and all associated names are copyright Lucasfilm ltd.

Related Posts

Handling Dynamic Urls

License

  Copyright 2019 David Odari
  
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

About

πŸ‘½ Android app consuming Star Wars API.Built with clean architecture ,MVVM pattern, Koin , Coroutines + Flows ,Architecture Components, Data Binding , Firebase + Unit/UI Tests

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • Kotlin 100.0%