diff --git a/shoeboxes/src/main/java/info/juanmendez/shoeboxes/models/ShoeRack.java b/shoeboxes/src/main/java/info/juanmendez/shoeboxes/models/ShoeRack.java index c647f59..fd41412 100644 --- a/shoeboxes/src/main/java/info/juanmendez/shoeboxes/models/ShoeRack.java +++ b/shoeboxes/src/main/java/info/juanmendez/shoeboxes/models/ShoeRack.java @@ -1,7 +1,9 @@ package info.juanmendez.shoeboxes.models; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import info.juanmendez.shoeboxes.utils.ShoeUtils; import io.reactivex.Observable; @@ -15,23 +17,47 @@ public class ShoeRack { ShoeModel shoeModel; + HashMap requestActionMap = new HashMap<>(); + List history = new ArrayList<>(); private PublishSubject> publishSubject = PublishSubject.create(); public boolean request( int requestId ){ - return request( Integer.toString( requestId) ); + return request( requestId, "" ); + } + + public boolean request( int requestId, String action ){ + return request( Integer.toString( requestId), action ); } public boolean request(String requestedTag) { return request(search( requestedTag )); } - public boolean request( ShoeModel shoeModel){ + public boolean request(String requestedTag, String action ) { + + /** + * keep a reference of the action based on the tag in the shoeBox + */ + ShoeBox shoeBox = (ShoeBox) search( requestedTag ); + + if( action == null || action.isEmpty() ){ + requestActionMap.remove( shoeBox.getFragmentTag() ); + }else{ + requestActionMap.put( shoeBox.getFragmentTag(), action ); + } + + return request( shoeBox ); + } + + public boolean request( ShoeModel shoeModel ){ if( shoeModel != null ){ - //going forward can mean also steping back to a previous shoeModel - //lets find the shoeModel within.. + /* + going forward can mean also steping back to a previous shoeModel + lets find the shoeModel within.. + */ ShoeModel parent = shoeModel.getParent(); /** @@ -124,6 +150,44 @@ public boolean suggest (String... tags ){ return anySuccess; } + public boolean suggestIdsWithTags( HashMap idsWithActions ){ + + HashMap tagsWithActions = new HashMap<>(); + + for (Map.Entry entry : idsWithActions.entrySet()) { + tagsWithActions.put( Integer.toString(entry.getKey()), entry.getValue() ); + } + + return suggest( tagsWithActions ); + } + + + + public boolean suggest( HashMap tagsWithActions ){ + + ShoeModel shoeModel, shoeParent; + ShoeBox shoeBox; + Boolean anySuccess = false; + + for (Map.Entry entry : tagsWithActions.entrySet()) { + + shoeModel = search( entry.getKey() ); + + if( shoeModel != null && shoeModel instanceof ShoeBox ){ + shoeBox = (ShoeBox) shoeModel; + shoeParent = shoeBox.getParent(); + + if( !ShoeUtils.anyChildActive( shoeParent ) ){ + if( request( entry.getKey(), entry.getValue() ) ){ + anySuccess = true; + } + } + } + } + + return anySuccess; + } + public Observable> asObservable() { return publishSubject.hide(); } @@ -250,6 +314,16 @@ public boolean goBack(){ */ public void clearHistory(){ history.clear(); + requestActionMap.clear(); + } + + + public String getActionByTag( String tag ){ + return requestActionMap.get( tag ); + } + + public String getActionById( int id ){ + return getActionByTag( Integer.toString(id)); } public boolean isHistoryEmpty(){ diff --git a/shoeboxes/src/test/java/info/juanmendez/shoeboxes/BasicShoeStorageTest.java b/shoeboxes/src/test/java/info/juanmendez/shoeboxes/BasicShoeStorageTest.java index 819c324..834c296 100644 --- a/shoeboxes/src/test/java/info/juanmendez/shoeboxes/BasicShoeStorageTest.java +++ b/shoeboxes/src/test/java/info/juanmendez/shoeboxes/BasicShoeStorageTest.java @@ -5,6 +5,7 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import info.juanmendez.shoeboxes.adapters.ShoeFragment; @@ -18,6 +19,7 @@ import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertTrue; /** @@ -146,6 +148,77 @@ public void shouldHistoryByFlowWorkFine(){ } + + @Test + public void testRequestAction(){ + + shoeRack.clearHistory(); + ShoeBox shoeBoxA = ShoeBox.build(fragmentA); + ShoeBox shoeBoxB = ShoeBox.build(fragmentB); + ShoeBox shoeBoxC = ShoeBox.build(fragmentC); + + shoeRack.populate( ShoeStack.build(shoeBoxA, shoeBoxB, shoeBoxC) ); + + + shoeRack.request( tagB, "B" ); + + //lets go to first one! + shoeRack.request( tagC, "C" ); + + assertEquals( shoeRack.getActionByTag( tagC ), "C" ); + assertNull( shoeRack.getActionByTag( tagA )); + + shoeRack.goBack(); + assertTrue( fragmentB.isActive() ); + assertEquals( shoeRack.getActionByTag(tagB), "B"); + } + + + @Test + public void testSuggestByTag(){ + shoeRack.clearHistory(); + ShoeBox shoeBoxA = ShoeBox.build(fragmentA); + ShoeBox shoeBoxB = ShoeBox.build(fragmentB); + ShoeBox shoeBoxC = ShoeBox.build(fragmentC); + + shoeRack.populate( ShoeStack.build(shoeBoxA, shoeBoxB, shoeBoxC) ); + + HashMap tagsWithActions = new HashMap<>(); + tagsWithActions.put( tagC, "C"); + shoeRack.suggest( tagsWithActions ); + } + + @Test + public void testSuggestById(){ + shoeRack.clearHistory(); + + int a=1,b=2,c=3,d=4,e=5,f=6; + + fragmentA = new TestShoeFragment(a); + fragmentB = new TestShoeFragment(b); + fragmentC = new TestShoeFragment(c); + + ShoeBox shoeBoxA = ShoeBox.build(fragmentA); + ShoeBox shoeBoxB = ShoeBox.build(fragmentB); + ShoeBox shoeBoxC = ShoeBox.build(fragmentC); + + shoeRack.populate( shoeBoxA, shoeBoxB, shoeBoxC ); + + HashMap idsWithActions = new HashMap<>(); + idsWithActions.put( a, "A"); + idsWithActions.put( b, "B"); + idsWithActions.put( c, "C"); + + shoeRack.suggestIdsWithTags( idsWithActions ); + + assertTrue( shoeBoxA.isActive() ); + assertTrue( shoeBoxC.isActive() ); + assertTrue( shoeBoxB.isActive() ); + + assertEquals( shoeRack.getActionById(a), "A"); + } + + @Test public void slideArrayList(){ List strings = new ArrayList<>(Arrays.asList(new String[]{"a","b","c","d"}));