Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
terrakok committed Jul 17, 2017
2 parents 2f6f445 + 7337901 commit d8266b0
Show file tree
Hide file tree
Showing 32 changed files with 769 additions and 34 deletions.
36 changes: 25 additions & 11 deletions README.md
Expand Up @@ -21,6 +21,11 @@ It was designed to be used with the MVP pattern (try [Moxy](https://github.com/A
+ 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:
```groovy
Expand Down Expand Up @@ -140,30 +145,39 @@ This commands set will fulfill the needs of the most applications. But if you ne
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.
```java
private Navigator navigator = new SupportFragmentNavigator(
getSupportFragmentManager(), R.id.main_container) {
private Navigator navigator = new SupportAppNavigator(this, R.id.container) {
@Override
protected Fragment createFragment(String screenKey, Object data) {
return SampleFragment.getNewInstance((int) data);
protected Intent createActivityIntent(String screenKey, Object data) {
return null;
}

@Override
protected void showSystemMessage(String message) {
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
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 exit() {
finish();
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.

![](https://habrastorage.org/files/16d/2ee/6e3/16d2ee6e33a0428eb4f0dcab8ce6b294.gif)

![](https://hsto.org/files/867/638/c33/867638c338704489b3107a6d7cb28c2d.gif)
![](https://habrastorage.org/web/a94/d73/653/a94d736534694d9daa994e0c260fca28.gif)
![](https://habrastorage.org/web/6dd/a19/15c/6dda1915cdcf4f14bed16fcffb3fd938.gif)
![](https://habrastorage.org/web/a63/881/7f8/a638817f8bba49daacc4fa427987fabb.gif)

## Participants
+ idea and code - Konstantin Tskhovrebov (@terrakok)
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-all.zip
2 changes: 1 addition & 1 deletion library/build.gradle
Expand Up @@ -15,7 +15,7 @@ ext {
bintrayName = 'cicerone'
publishedGroupId = 'ru.terrakok.cicerone'
artifact = 'cicerone'
libraryVersion = '1.2.1'
libraryVersion = '2.0.0'
gitUrl = 'https://github.com/terrakok/Cicerone'
allLicenses = ['MIT']
}
Expand Down
53 changes: 53 additions & 0 deletions library/src/main/java/ru/terrakok/cicerone/Router.java
@@ -1,10 +1,13 @@
package ru.terrakok.cicerone;

import java.util.HashMap;

import ru.terrakok.cicerone.commands.Back;
import ru.terrakok.cicerone.commands.BackTo;
import ru.terrakok.cicerone.commands.Forward;
import ru.terrakok.cicerone.commands.Replace;
import ru.terrakok.cicerone.commands.SystemMessage;
import ru.terrakok.cicerone.result.ResultListener;

/**
* Created by Konstantin Tckhovrebov (aka @terrakok)
Expand All @@ -19,10 +22,49 @@
*/
public class Router extends BaseRouter {

private HashMap<Integer, ResultListener> resultListeners = new HashMap<>();

public Router() {
super();
}

/**
* Subscribe to the screen result.<br>
* <b>Note:</b> only one listener can subscribe to a unique resultCode!<br>
* You must call a <b>removeResultListener()</b> to avoid a memory leak.
*
* @param resultCode key for filter results
* @param listener result listener
*/
public void setResultListener(Integer resultCode, ResultListener listener) {
resultListeners.put(resultCode, listener);
}

/**
* Unsubscribe from the screen result.
*
* @param resultCode key for filter results
*/
public void removeResultListener(Integer resultCode) {
resultListeners.remove(resultCode);
}

/**
* Send result data to subscriber.
*
* @param resultCode result data key
* @param result result data
* @return TRUE if listener was notified and FALSE otherwise
*/
protected boolean sendResult(Integer resultCode, Object result) {
ResultListener resultListener = resultListeners.get(resultCode);
if (resultListener != null) {
resultListener.onResult(result);
return true;
}
return false;
}

/**
* Open new screen and add it to the screens chain.
*
Expand Down Expand Up @@ -138,6 +180,17 @@ public void exit() {
executeCommand(new Back());
}

/**
* Return to the previous screen in the chain and send result data.
*
* @param resultCode result data key
* @param result result data
*/
public void exitWithResult(Integer resultCode, Object result) {
exit();
sendResult(resultCode, result);
}

/**
* Return to the previous screen in the chain and show system message.
*
Expand Down
Expand Up @@ -2,6 +2,7 @@

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;

import ru.terrakok.cicerone.Navigator;
import ru.terrakok.cicerone.commands.Back;
Expand Down Expand Up @@ -42,6 +43,21 @@ public FragmentNavigator(FragmentManager fragmentManager, int containerId) {
this.containerId = containerId;
}

/**
* Override this method for setup custom fragment transaction animation.
*
* @param command current navigation command. Maybe only {@link Forward} and {@link Replace}
* @param currentFragment current fragment in container
* (for {@link Replace} command it will be previous screen, NOT replaced screen)
* @param nextFragment next screen fragment
* @param fragmentTransaction fragment transaction
*/
protected void setupFragmentTransactionAnimation(Command command,
Fragment currentFragment,
Fragment nextFragment,
FragmentTransaction fragmentTransaction) {
}

@Override
public void applyCommand(Command command) {
if (command instanceof Forward) {
Expand All @@ -51,8 +67,16 @@ public void applyCommand(Command command) {
unknownScreen(command);
return;
}
fragmentManager
.beginTransaction()

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
setupFragmentTransactionAnimation(
command,
fragmentManager.findFragmentById(containerId),
fragment,
fragmentTransaction
);

fragmentTransaction
.replace(containerId, fragment)
.addToBackStack(forward.getScreenKey())
.commit();
Expand All @@ -71,14 +95,29 @@ public void applyCommand(Command command) {
}
if (fragmentManager.getBackStackEntryCount() > 0) {
fragmentManager.popBackStackImmediate();
fragmentManager
.beginTransaction()

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
setupFragmentTransactionAnimation(
command,
fragmentManager.findFragmentById(containerId),
fragment,
fragmentTransaction
);

fragmentTransaction
.replace(containerId, fragment)
.addToBackStack(replace.getScreenKey())
.commit();
} else {
fragmentManager
.beginTransaction()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
setupFragmentTransactionAnimation(
command,
fragmentManager.findFragmentById(containerId),
fragment,
fragmentTransaction
);

fragmentTransaction
.replace(containerId, fragment)
.commit();
}
Expand Down Expand Up @@ -106,10 +145,7 @@ public void applyCommand(Command command) {
}

private void backToRoot() {
for (int i = 0; i < fragmentManager.getBackStackEntryCount(); i++) {
fragmentManager.popBackStack();
}
fragmentManager.executePendingTransactions();
fragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}

/**
Expand Down
Expand Up @@ -2,6 +2,7 @@

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import ru.terrakok.cicerone.Navigator;
import ru.terrakok.cicerone.commands.Back;
Expand Down Expand Up @@ -42,6 +43,21 @@ public SupportFragmentNavigator(FragmentManager fragmentManager, int containerId
this.containerId = containerId;
}

/**
* Override this method for setup custom fragment transaction animation.
*
* @param command current navigation command. Maybe only {@link Forward} and {@link Replace}
* @param currentFragment current fragment in container
* (for {@link Replace} command it will be previous screen, NOT replaced screen)
* @param nextFragment next screen fragment
* @param fragmentTransaction fragment transaction
*/
protected void setupFragmentTransactionAnimation(Command command,
Fragment currentFragment,
Fragment nextFragment,
FragmentTransaction fragmentTransaction) {
}

@Override
public void applyCommand(Command command) {
if (command instanceof Forward) {
Expand All @@ -51,8 +67,16 @@ public void applyCommand(Command command) {
unknownScreen(command);
return;
}
fragmentManager
.beginTransaction()

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
setupFragmentTransactionAnimation(
command,
fragmentManager.findFragmentById(containerId),
fragment,
fragmentTransaction
);

fragmentTransaction
.replace(containerId, fragment)
.addToBackStack(forward.getScreenKey())
.commit();
Expand All @@ -71,14 +95,29 @@ public void applyCommand(Command command) {
}
if (fragmentManager.getBackStackEntryCount() > 0) {
fragmentManager.popBackStackImmediate();
fragmentManager
.beginTransaction()

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
setupFragmentTransactionAnimation(
command,
fragmentManager.findFragmentById(containerId),
fragment,
fragmentTransaction
);

fragmentTransaction
.replace(containerId, fragment)
.addToBackStack(replace.getScreenKey())
.commit();
} else {
fragmentManager
.beginTransaction()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
setupFragmentTransactionAnimation(
command,
fragmentManager.findFragmentById(containerId),
fragment,
fragmentTransaction
);

fragmentTransaction
.replace(containerId, fragment)
.commit();
}
Expand Down Expand Up @@ -106,10 +145,7 @@ public void applyCommand(Command command) {
}

private void backToRoot() {
for (int i = 0; i < fragmentManager.getBackStackEntryCount(); i++) {
fragmentManager.popBackStack();
}
fragmentManager.executePendingTransactions();
fragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}

/**
Expand Down
@@ -0,0 +1,15 @@
package ru.terrakok.cicerone.result;

/**
* @author Konstantin Tskhovrebov (aka terrakok) on 04.07.17.
*/

public interface ResultListener {

/**
* Received result from screen.
*
* @param resultData
*/
void onResult(Object resultData);
}
Expand Up @@ -4,6 +4,7 @@
* Created by laomo on 2016-11-21.
*/
public class FragmentManager {
public static final int POP_BACK_STACK_INCLUSIVE = 1<<0;

public FragmentTransaction beginTransaction() {
throw new RuntimeException("Stub!");
Expand Down Expand Up @@ -33,6 +34,10 @@ public BackStackEntry getBackStackEntryAt(int index) {
throw new RuntimeException("Stub!");
}

public Fragment findFragmentById(int id) {
throw new RuntimeException("Stub!");
}

public interface BackStackEntry {
int getId();

Expand Down

0 comments on commit d8266b0

Please sign in to comment.