-
Notifications
You must be signed in to change notification settings - Fork 129
MVP Guidelines
The project has a package name base
.
It contains mainly 6 files
base
- BaseActivity
- BaseFragment
- BaseMvpPresenter
- BasePresenter
- BaseView
- PresenterFactory
BaseView
public interface BaseView {
.........
}
An base interface used to communicate with view
BaseMvpPresenter
public interface BaseMvpPresenter<V extends BaseView> {
.........
}
An interface used to assign view to the presenter
BasePresenter
public class BasePresenter<V extends BaseView> implements BaseMvpPresenter<V> {
.........
}
A class used to connect presenter to the view
BaseActivity
public abstract class BaseActivity<T extends BaseMvpPresenter, K extends ViewDataBinding> extends AppCompatActivity implements BaseView {
.........
}
Our base class for the activity. It has 2 generics which connect the Databinder and presenter to this activity.
BaseFragment
public abstract class BaseFragment<T extends BaseMvpPresenter, K extends ViewDataBinding> extends Fragment implements BaseView {
.........
}
Our base class for the fragment. It has 2 generics which connect the Databinder and presenter to this activity.
PresenterFactory
public class PresenterFactory{
.........
}
This class is used to generate presenter models.
- It creates the simplicity to write the code for Unit Testing ( Unit Testing encourages developers to modify the source code).
- We will be able to keep track of the logic, which is inside the class.
- Easy to divide the work.
Whenever we create a new activity. First create a package with your activity name .Remeber to create 3 main files. (For this context I am assuming the activity name as foo)
- FooActivity ( extend BaseActivity and should implement FooContract.View. Has view Methods)
- FooContract ( Has the presenter and view definitions)
- FooPresenter ( Has the presenter methods. Should implement FooContract.Presenter)
Example : After creating you package it should look something like this.
foo
- FooActivity
- FooContract
- FooPresenter
Then in your layout <activity_foo> add the lines for data binding.
<xml........>
<layout>
<data> </data>
/// Your layout code
</layout>
Remeber to put the layout
only at the begining of the file excatly beneath the <xml...>
definition or else the complier sometimes might not recognise the layout leading to not generating the classes.
Rebuild the project once to generate the binding class . IMPORTANT
Next define your classes as follows
- FooContract
interface FooContract{
public interface Presenter extends BaseMvpPresenter<View>{
void checkView();
}
public interface View extends BaseView{
void checkData();
}
}
- FooActivity
public class FooActivity extends BaseActivity<FooActivity.Presenter, ActivityFooBining> implements HomeContract.View {
...
}
Remember that the ActivityFooBining
wount generate until you rebuild after defining the layout.
- FooPresenter
public class FooPresenter extends BasePresenter<FooContract.View> implements FooContract.Presenter {
...
}
Now we can access the view methods in FooPresenter and presenter methods from FooActivity.
Side note: Always remember keep all your logic in the presenter and make your activity code look as dumb as possible. And everything related to view system must never be accessed in the presenter. That's it HAPPY CODING