Cicerone


Source link: https://github.com/terrakok/Cicerone

Cicerone

Cicerone (a guide, one who conducts sightseers) is a lightweight library that makes the navigation in an Android app easy.
It was designed to be used with the MVP pattern (try Moxy), but will work great with any architecture.

Main advantages

  • is not tied to Fragments
  • not a framework
  • short navigation calls (no builders)
  • lifecycle-safe!
  • functionality is simple to extend
  • suitable for Unit Testing

Version 2.+

  • easy screen result subscription
  • predefined navigator ready for setup transition animation
    See the sample application

How to add

Add the dependency in your build.gradle:

dependencies {

  //Cicerone
  compile 'ru.terrakok.cicerone:cicerone:X.X.X' 
}

Initialize the library (for example in your Application class):

public class SampleApplication extends MvpApplication {

  public static SampleApplication INSTANCE;
  private Cicerone<Router> cicerone;

@Override
  public void onCreate() {

super.onCreate();

INSTANCE = this;

 initCicerone();

  
}

private void initCicerone() {

cicerone = Cicerone.create();

  
}

public NavigatorHolder getNavigatorHolder() {

return cicerone.getNavigatorHolder();

  
}

public Router getRouter() {

return cicerone.getRouter();

  
}
 
}

How it works?

Presenter calls navigation method of Router.

public class SamplePresenter extends Presenter<SampleView> {

  private Router router;

public SamplePresenter() {

router = SampleApplication.INSTANCE.getRouter();

  
}

public void onBackCommandClick() {

router.exit();

  
}

public void onForwardCommandClick() {

router.navigateTo("Some screen");

  
}
 
}

Router converts the navigation call to the set of Commands and sends them to CommandBuffer.

CommandBuffer checks whether there are "active" Navigator:
If yes, it passes the commands to the Navigator. Navigator will process them to achive the desired transition.
If no, then CommandBuffer saves the commands in a queue, and will apply them as soon as new "active" Navigator will appear.

protected void executeCommand(Command command) {

  if (navigator != null) {

navigator.applyCommand(command);

  
}
 else {

pendingCommands.add(command);

  
}
 
}

Navigator processes the navigation commands. Usually it is an anonymous class inside the Activity.
Activity provides Navigator to the CommandBuffer in onResume and removes it in onPause.

Attention: Use onResumeFragments() with FragmentActivity ( more info)

@Override protected void onResume() {

  super.onResume();

  SampleApplication.INSTANCE.getNavigatorHolder().setNavigator(navigator);
 
}
  @Override protected void onPause() {

  super.onPause();

  SampleApplication.INSTANCE.getNavigatorHolder().removeNavigator();
 
}
  private Navigator navigator = new Navigator() {

  @Override
  public void applyCommand(Command command) {

//implement commands logic
  
}
 
}
;

Navigation commands

This commands set will fulfill the needs of the most applications. But if you need something special - just add it!

  • Forward - Opens new screen
  • Back - Rolls back the last transition
  • BackTo - Rolls back to the needed screen in the screens chain
  • Replace - Replaces the current screen
  • SystemMessage - Shows system message (Alert, Toast, Snack, etc.)

Predefined navigators

The library provides predefined navigators for Fragments to use inside Activity.
To use, just provide it with the container and FragmentManager and override few simple methods.

private Navigator navigator = new SupportAppNavigator(this, R.id.container) {

  @Override
  protected Intent createActivityIntent(String screenKey, Object data) {

return null;
  
}

@Override
  protected Fragment createFragment(String screenKey, Object data) {

switch (screenKey) {

 case Screens.PROFILE_SCREEN:

  return new ProfileFragment();

 case Screens.SELECT_PHOTO_SCREEN:

  return SelectPhotoFragment.getNewInstance((int) data);

}

return null;
  
}

@Override
  protected void setupFragmentTransactionAnimation(

  Command command,

  Fragment currentFragment,

  Fragment nextFragment,

  FragmentTransaction fragmentTransaction) {

//setup animation
  
}
 
}
;

Sample

To see how to add, initialize and use the library and predefined navigators check out the sample.

Participants

  • idea and code - Konstantin Tskhovrebov (@terrakok)
  • architecture advice, documentation and publication - Vasili Chyrvon (@Jeevuz)

License

The MIT License (MIT)  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 

Resources

This library provides some expressions, IF, Match like Scala. IF enables java if-statement to return a value as Scala if-expression. Match also enables java to use pattern-match expression.

Pull down, and execute more action!

Beautiful and customizable Android Activity that shows web pages within an app.

Simplify your work with SQLiteDatabase on Android.

AutoCompleteEditTextWithContact that shows suggestions automatically while the user is entering characters. The list of suggestions is displayed from which the user can choose an item.

RxSnappy is a thread safe rxjava wrapper for the great SnappyDB fast key-value database for Android.

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