This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from R3PI/develop
Develop
- Loading branch information
Showing
8 changed files
with
169 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
defrag/src/main/java/com/solera/defrag/ViewStackUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package com.solera.defrag; | ||
|
||
import android.os.Bundle; | ||
import android.support.annotation.LayoutRes; | ||
import android.support.annotation.MainThread; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
import android.support.v4.util.Pair; | ||
import java.util.List; | ||
|
||
/** | ||
* A collection of utilities for ViewStacks. | ||
*/ | ||
public class ViewStackUtils { | ||
private ViewStackUtils() { | ||
} | ||
|
||
/** | ||
* Safely replace the current view as soon as the {@link ViewStack} will be in {@link | ||
* TraversingState#IDLE}. If it is already in the idle state, method is invoked immediately. | ||
*/ | ||
public static void safeReplace(@NonNull final ViewStack viewStack, @LayoutRes int layout) { | ||
safeReplaceWithParameters(viewStack, layout, null); | ||
} | ||
|
||
/** | ||
* Safely replace the current view as soon as the {@link ViewStack} will be in {@link | ||
* TraversingState#IDLE}. If it is already in the idle state, method is invoked immediately. | ||
*/ | ||
public static void safeReplaceWithParameters(@NonNull final ViewStack viewStack, | ||
@LayoutRes final int layout, @Nullable final Bundle parameters) { | ||
waitForTraversingState(viewStack, TraversingState.IDLE, new ViewStackListener() { | ||
@Override | ||
public void onTraversing(@NonNull TraversingState traversingState) { | ||
viewStack.replaceWithParameters(layout, parameters); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Safely replace the {@link ViewStack} stack as soon as the {@link ViewStack} will be in {@link | ||
* TraversingState#IDLE}. If it is already in the idle state, method is invoked immediately. | ||
*/ | ||
public static void safeReplaceStack(@NonNull final ViewStack viewStack, | ||
@NonNull final List<Pair<Integer, Bundle>> views) { | ||
waitForTraversingState(viewStack, TraversingState.IDLE, new ViewStackListener() { | ||
@Override | ||
public void onTraversing(@NonNull TraversingState traversingState) { | ||
viewStack.replaceStack(views); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Safely push a view (as soon as the {@link ViewStack} will be in {@link TraversingState#IDLE}, | ||
* if it is already in the idle state, method is invoked immediately). | ||
*/ | ||
public static void safePush(@NonNull final ViewStack viewStack, @LayoutRes int layout) { | ||
safePushWithParameters(viewStack, layout, null); | ||
} | ||
|
||
/** | ||
* Safely push a view as soon as the {@link ViewStack} will be in {@link TraversingState#IDLE}. | ||
* If it is already in the idle state, method is invoked immediately. | ||
*/ | ||
public static void safePushWithParameters(@NonNull final ViewStack viewStack, | ||
@LayoutRes final int layout, @Nullable final Bundle parameters) { | ||
waitForTraversingState(viewStack, TraversingState.IDLE, new ViewStackListener() { | ||
@Override | ||
public void onTraversing(@NonNull TraversingState traversingState) { | ||
viewStack.pushWithParameters(layout, parameters); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Safely pop the current view as soon as the {@link ViewStack} will be in {@link | ||
* TraversingState#IDLE}. If it is already in the idle state, method is invoked immediately. | ||
* <p> | ||
* Note: You cannot be sure if the pop operation has been successful. | ||
*/ | ||
public static void safePop(@NonNull final ViewStack viewStack) { | ||
safePopWithResult(viewStack, null); | ||
} | ||
|
||
/** | ||
* Safely pop the current view as soon as the {@link ViewStack} will be in {@link | ||
* TraversingState#IDLE}. If it is already in the idle state, method is invoked immediately. | ||
* <p> | ||
* Note: You cannot be sure if the pop operation has been successful. | ||
*/ | ||
public static void safePopWithResult(@NonNull final ViewStack viewStack, | ||
@Nullable final Object result) { | ||
waitForTraversingState(viewStack, TraversingState.IDLE, new ViewStackListener() { | ||
@Override | ||
public void onTraversing(@NonNull TraversingState traversingState) { | ||
viewStack.popWithResult(result); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Safely pop to the given view layout as soon as the {@link ViewStack} will be in {@link | ||
* TraversingState#IDLE}. If it is already in the idle state, method is invoked immediately. | ||
* <p> | ||
* Note: You cannot be sure if the pop operation has been successful. | ||
*/ | ||
public static void safePopBackToWithResult(@NonNull final ViewStack viewStack, | ||
@LayoutRes final int layout, @Nullable final Object result) { | ||
waitForTraversingState(viewStack, TraversingState.IDLE, new ViewStackListener() { | ||
@Override | ||
public void onTraversing(@NonNull TraversingState traversingState) { | ||
viewStack.popBackToWithResult(layout, result); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Call the given callback when viewStack will be in the given state (if it is already in the | ||
* given state, the callback is immediately invoked). | ||
* | ||
* @param viewStack the viewStack to wait on | ||
* @param desiredState the traversing state to wait on | ||
* @param callback the callback to invoke | ||
*/ | ||
@MainThread | ||
private static void waitForTraversingState(@NonNull final ViewStack viewStack, | ||
@NonNull final TraversingState desiredState, @NonNull final ViewStackListener callback) { | ||
ViewUtils.verifyMainThread(); | ||
if (desiredState == viewStack.getTraversingState()) { | ||
callback.onTraversing(desiredState); | ||
} else { | ||
viewStack.addTraversingListener(new ViewStackListener() { | ||
@Override | ||
public void onTraversing(@NonNull TraversingState traversingState) { | ||
if (traversingState == desiredState) { | ||
viewStack.removeTraversingListener(this); | ||
callback.onTraversing(desiredState); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.