You got tired because of fact that your Activities and Fragments were becoming god classes, so you have migrated to MVP. Now you're tired of your god-class presenters and you just want to stop continuously wondering if you should pass a context to your presenters and make them platform-dependent and harder to mock and test, or maybe you should let your view-activity manage the system connected work. That's why you're here. My Android VIPER interpretation allows you keep your code clean, neat, and more SRP with minimal effort.
To avoid manually creating all VIPER class files and managing their dependencies every time you want to create a new screen I created a generator that does all of necessary work for you. You can find it here.
You are able to use all of the Mosby's MVP Views with Moviper. MvpFragment, MvpLceActivity, ViewStateFragment etc. are fully compatibile with Moviper presenters.
dependencies {
compile 'com.mateuszkoslacz.moviper:moviper:1.1.0-alpha'
}
First of all, check out the samples. After that, just create a VIPER files set using Moviper Template Generator, fill up the contract, generate missing methods using Android Studio autofix and implement them. Most probably you will want to check out a sample module provided in this repository to see how to use Moviper. You have two flavours of Moviper. The "regular" one — to use with callbacks, and the Rx one, to use with RxJava.
You can easily pass extras from your Activity or Fragment to the presenter using Moviper Args Bundle. You can check out how to use it in the Sample's FullscreenPhotoPresenter
constructor and its call.
Enable IPC in your Application
class onCreate()
method.
Moviper.getInstance().setConfig(
new Config.Builder()
.withPresenterAccessUtilEnabled(true) // plain IPC
.withInstancePresentersEnabled(false) // acces to specific presenters
.build());
You can access all alive Presenters of a given class from any place in your app like this:
Moviper.getInstance().getPresenters(SomePresenter.class)
.subscribe(somePresenter -> somePresenter.someMethod(false)); // stream of all Presenters goes here
For readability mark your external methods in the Presenter using the @ExternalCall
annotation.
If you set the withInstancePresentersEnabled
in the config to true you can use Instance Presenter Access. After that you must ensure that every Presenter of given class has an unique name. To do so you have to override Presenter String getName()
method (in default it returns "default").
After that you can access any Presenter Instance like this:
Moviper.getInstance().getPresenterInstance(SomePresenter.class, "someName")
.subscribe(somePresenter -> somePresenter.someMethod(false)); // exactly one or zero Presenters with given name and class goes here
For the basic usage just check out the rxsample
(or sample
, if you aren't familiar with RxJava) module in this repo. For the IPC usage check out the ipcsample
module.
This library is built on top of a great Mosby library by Hannes Dorfmann. Check out the project website.
I've also used a Lucas Urbas Interactor implementation idea presented in his Search viper architecture example.
I have followed Publish AAR to jCenter and Maven Central by Lopez Mikhael to publish Moviper to jCenter.
Copyright 2016 Mateusz Koślacz
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.