Min SDK Version 21
Target SDK Version 28
Check the compileSdkVersion, and buildToolsVersion to be the latest
Build Tools Version 28.0.3
Java 1.8
- Download or cline this project then change package id (id.example.mvp) to your package id
- You can change package ID easily using this script
- Open the project in Android Studio
- Profit
Imagine you have to implement a sign in screen.
- Create a new package under
feature
directory calledsignin
- Create an new Activity called
SignInActivity
- Create a new interface called
SignInView
that extendsMvpView
. Add functions likefun showSignInSuccessfull()
- Create a
SignInPresenter
class that extendsBasePresenter<SignInView>
. - Create a
SignInModule
class that will provide presenter class. Sample code:
@Module
class SignInModule {
@PerFeature
@Provides
fun providesSignInPresenter(dataManager: DataManager): SignInPresenter = SignInPresenter(dataManager)
}
- Implement functions in
SignInPresenter
that your Activity requires to perform the necessary actions, e.g.signIn(email: String)
. Once the sign in action finishes you should callmvpView?.showSignInSuccessful()
. - Create a
SignInPresenterTest
and write unit tests forsignIn(email)
. Remember to mock theSignInView
and also the DataManager. - Make your activity implement
SignInView
and implement the required functions likeshowSignInSuccessful()
- In your activity, inject a new instance of
SignInPresenter
and callpresenter.attachView(this)
from override functionattachView()
andpresenter.detachView()
from override functiondetachView()
. InonCreate()
inject that activity, like this:AndroidInjection.inject(this)
- Open file
FeatureModule.kt
in directorycore/di/module
and add abstract function for SignInActivity.
@PerFeature
@ContributesAndroidInjector(modules = [SignInModule::class])
abstract fun bindSignInActivity(): SignInActivity