23
23
import android .view .LayoutInflater ;
24
24
import android .view .View ;
25
25
import android .view .ViewGroup ;
26
-
27
- import io .techery .presenta .flow .Utils ;
28
-
29
26
import flow .Flow ;
30
- import flow .Path ;
31
- import flow .PathContainer ;
32
- import flow .PathContext ;
33
- import flow .PathContextFactory ;
27
+ import flow .path .Path ;
28
+ import flow .path .PathContainer ;
29
+ import flow .path .PathContext ;
30
+ import flow .path .PathContextFactory ;
31
+ import io .techery .presenta .addition .flow .path .Layout ;
32
+ import io .techery .presenta .flow .Utils ;
33
+ import java .util .LinkedHashMap ;
34
+ import java .util .Map ;
34
35
35
36
import static flow .Flow .Direction .REPLACE ;
36
37
37
38
/**
38
39
* Provides basic right-to-left transitions. Saves and restores view state.
39
- * Uses {@link flow. PathContext} to allow customized sub-containers.
40
+ * Uses {@link PathContext} to allow customized sub-containers.
40
41
*/
41
42
public class SimplePathContainer extends PathContainer {
43
+ private static final Map <Class , Integer > PATH_LAYOUT_CACHE = new LinkedHashMap <>();
42
44
private final PathContextFactory contextFactory ;
43
45
44
46
public SimplePathContainer (int tagKey , PathContextFactory contextFactory ) {
@@ -60,12 +62,11 @@ public SimplePathContainer(int tagKey, PathContextFactory contextFactory) {
60
62
61
63
Path to = traversalState .toPath ();
62
64
63
- ViewGroup newView ;
65
+ View newView ;
64
66
context = PathContext .create (oldPath , to , contextFactory );
65
67
int layout = getLayout (to );
66
- newView = (ViewGroup ) LayoutInflater .from (context )
67
- .cloneInContext (context )
68
- .inflate (layout , containerView , false );
68
+ newView =
69
+ LayoutInflater .from (context ).cloneInContext (context ).inflate (layout , containerView , false );
69
70
70
71
View fromView = null ;
71
72
if (traversalState .fromPath () != null ) {
@@ -83,11 +84,9 @@ public SimplePathContainer(int tagKey, PathContextFactory contextFactory) {
83
84
containerView .addView (newView );
84
85
final View finalFromView = fromView ;
85
86
Utils .waitForMeasure (newView , new Utils .OnMeasuredCallback () {
86
- @ Override
87
- public void onMeasured (View view , int width , int height ) {
87
+ @ Override public void onMeasured (View view , int width , int height ) {
88
88
runAnimation (containerView , finalFromView , view , direction , new Flow .TraversalCallback () {
89
- @ Override
90
- public void onTraversalCompleted () {
89
+ @ Override public void onTraversalCompleted () {
91
90
containerView .removeView (finalFromView );
92
91
oldPath .destroyNotIn (context , contextFactory );
93
92
callback .onTraversalCompleted ();
@@ -98,6 +97,22 @@ public void onTraversalCompleted() {
98
97
}
99
98
}
100
99
100
+ protected int getLayout (Path path ) {
101
+ Class pathType = path .getClass ();
102
+ Integer layoutResId = PATH_LAYOUT_CACHE .get (pathType );
103
+ if (layoutResId == null ) {
104
+ Layout layout = (Layout ) pathType .getAnnotation (Layout .class );
105
+ if (layout == null ) {
106
+ throw new IllegalArgumentException (
107
+ String .format ("@%s annotation not found on class %s" , Layout .class .getSimpleName (),
108
+ pathType .getName ()));
109
+ }
110
+ layoutResId = layout .value ();
111
+ PATH_LAYOUT_CACHE .put (pathType , layoutResId );
112
+ }
113
+ return layoutResId ;
114
+ }
115
+
101
116
private void runAnimation (final ViewGroup container , final View from , final View to ,
102
117
Flow .Direction direction , final Flow .TraversalCallback callback ) {
103
118
Animator animator = createSegue (from , to , direction );
0 commit comments