Application Core
Documentation about the core application architecture
Last updated
Documentation about the core application architecture
Last updated
The AniTrend architecture is built around a structural design pattern MVVMP with some customisation that best suit the use case for this application, the architecture is something I came up with and may violate some pattern standards but as we all know these are guidelines, not commandments.
The goal for the application architecture is to keep components independent and self managed or self aware of their own state to promote decoupling. All communication between non attached classes is achieved by EventBus
The app makes use of an application class called App
which can be found in com.mxt.anitrend.App
and servers as the initial application starting point. It is responsible for configuring things like the database or any other application related configurations that will be used throughout application instance
Most of the core building blocks of the android classes have extended to base classes that offer the most most commonly used functions in any android application, making use of generics and inheritance allows easier code reuse and extending.
The ViewModel
class is designed to store and manage UI-related data in a life cycle conscious way. The ViewModel
class allows data to survive configuration changes such as screen rotations.
Looking at the class signature you'll notice that the class is generic and implements RetroCallback
which is an extension interface for Retrofit callbacks, and the shared generic type represents our model type that will be used by any of your activities or fragments.
Press Ctrl
and hover your cursor over a class name or property to navigate to it from android studio, you can also hit Shift
twice to search for any file in the current project
Now that we've got that out of the way, let's talk about responsibilities of our view model, in this case our view model will be used by multiple Fragments and Activities by simply calling the setViewModel(true)
in your onCreate
method
The setViewModel(true)
method is available in the ActivityBase
& FragmentBase
classes and it creates a view model and sets all the subscription methods and callbacks for you. We'll get to this later
The view model handles dispatching of network requests and the corresponding responses, you'll see that the ActivityBase
class implements Observer<M>
which is how our view model notifies either of these about the data model changes
ViewModelBase
uses Bundle instead of plain objects, which is used by our RequestHandler
that handles all our network requests (it is also a multi threaded execution task).
Since we're using both view models and presenters, we've moved all our models into our view model and only left the presenters with the task of providing some core objects to our activities, fragments and views. Every presenter that needs to provide extra functionality is derived from the base presenter class:
The common presenter exposes a couple of objects to any classes that use it and the class itself is mainly responsible to binding and unbinding Shared Preferences Change Listeners, exposing the Database Helper
The presenter also extends a class called RecyclerScrollListener
that represents a custom OnScrollListener for RecyclerView which allow us to pre-fetch data when user reaches the bottom in the list.
A common super Activity class has been provided for general consumption found in com.mxt.anitrend.base.custom.activity
This is a generic abstract class that extends AppCompatActivity
which should be used on any activity in the this application.
The ActivityBase
contains a lot of helpful methods that are commonly used in this application, it handles many changes such as:
Theme selection based on what the user chose ActivityBase#configureActivity()
Deep Linking identification vial the IntentBundleUtil
Configuring the search view if the current child activity is using one
Requesting application permissions
Getting the set presenter and view model
Showing the bottom sheet
Example Usage
Let's look at a basic example from the splash activity com.mxt.anitrend.view.activity.index.SplashActivity
which extends from ActivityBase<VersionBase, BasePresenter>
The generic parameters passed are the ActivityBase<Model, Presenter>
for the current class, the model will be used by the View Model as it handles requests, it needs to know the type to expect and use it on the onChanged(M model)
method. The given presenter type is utilised when using setPresenter(new BasePresenter(this));
and when using getPresnter()
Note that we are using Butter Knife to bind our view ids to our view objects. Also looking at the ActivityBase<VersionBase, BasePresenter>
has set VersionBase
as it's model for the class and BasePresenter
as our presenter type since ActivityBase
has a P getPresnter()
and a void setPresenter(P presenter)
method