RainbowMVP


Source link: https://github.com/Ne1c/RainbowMVP

RainbowMVP

Lightweight MVP library(for personal using) with easy lifecycle.

For good understanding approach read this article.

  • Really lighweight library
  • Easy integrate to project
  • Minimum actions to implement MVP
  • Doesn't destroy presenter after rotation device
  • You can cached data in presenter for restore data after rotate device

In presenter you have 3 methods:

  • bindView(V view) - you need call this method for attach your view to presenter.
  • unbindView() - you need call this method when view not available already.
  • onDestoy() - call, when your presenter will destroy.

Important thing: You can bind your view to presenter ONLY after onStart() in Activity, and after onResume() in Fragment.

#Dependency

Step 1. Add it in your root build.gradle at the end of repositories:

allprojects {
  repositories {

...
maven {
 url "https://jitpack.io" 
}
  
}
 
}

Step 2. Add the dependency

dependencies {

  compile 'com.github.ne1c:rainbowmvp:1.2.2'  
}
 
}

Step 1

Create you View interface:

public interface MyView {

  void showResult(String text);

 void showError(@StringRes int resId);
 
}

Step 2

Create presenter. You need inherit of BasePresenter and add tag:

public class MyPresenter extends BasePresenter<MyView> {

  public static final TAG = MyPresenter.class.getName();

  ...
  public void makeParty() {

// some actions

boolean success = ...

if (success) {

 getView().showResult(...);

}
 else {

 getView().showError(R.string.error);

}

  
}

  ... 
}

Step 3

  • Implement PresenterStorage:
public class MyPresenterStorage implements PresenterStorage {

  public MyPresenterStorage(...) {

...
  
}

@Override
  public BasePresenter create(String tag) {

if (tag.equals(MyPresenter.TAG)) {

 return new MyPresenter(...);

}

 return null;
  
}
 
}

Step 4

  • Init PresenterFactory, before use any presenter. You can do it in onCreate() of Applicaton or splash screen:
public class MyApplication extends android.app.Application {

  @Override
  public void onCreate() {

...

PresenterFactory.init(new MyPresenterStorage(...));

  
}
 
}

Step 5

Your activity or fragment need to inherit of BaseActivity/BaseFragment and override getPresenterTag():

public class MyActivity extends BaseActivity<MyPresenter> implements MyView {

  ...
  @Ovveride
  public void onStart() {

super.onStart();

getPresenter().bindView(this);

  
}

 @Ovveride
  public void onStop() {

super.onStop();

getPresenter().unbindView();

  
}

 @Override
  public String getPresenterTag() {

return MyPresenter.TAG;
  
}

  ... 
}

ViewState

You can use ViewState for saving state of view, and than restore after bindView(...). For it action you need implement ViewStateListener in your presenter or another place. Method stateChanged(...) will call after every change of ViewState or after call method bindView(...). Example how it works:

public class MyPresenter extends BasePresenter<MyView> implements ViewStateListener {

  public MyPresenter(...) {

...

addViewStateListener(this);

  
}

  ...
  public void makeParty() {

setViewState(ViewState.IN_PROGRESS);

getApi().loadData(response -> {

  if (response.isSuccess()) {

setViewState(ViewState.SUCCESS);

if (getView() != null) {

 getView().showResult(response.getData());

}

  
}
 else if (response.isError()) {

setViewState(ViewState.ERROR);

if (getView() != null) {

 getView().showErrorLoadData();

}

  
}

 
}
);

  
}

 @Override
  public void stateChanged(ViewState state) {

if (state == ViewState.IN_PROGRESS) {

 getView().showProgress();

}

 if (state == ViewState.SUCCESS) {

 getView().showRepos(mCachedData);

 getView().hideProgress();

}

if (state == ViewState.ERROR) {

 getView().showErrorLoadData()

}

setViewState(ViewState.NOTHING);

  
}

  ... 
}

License

Copyright 2016 Nikolay Kucheriaviy 

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.

Resources

LabelLayout is a powerful layout to display label on top of another view

Use builder to define a selector button.

This is a collection of custom Views and animations.

CaptchaImageView is a custom imageview to generate captcha text.

A RecyclerView adapter that can handle holding hetrogeneuous data types, and provides the ability to set up sections in your adatper.

Kotlin Language Integrated Production System (KLIPS)

A rule based AI engine to simplify design of AIs for games in particular. DSL included!

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes