Skip to content

Commit

Permalink
included actions.
Browse files Browse the repository at this point in the history
besides requesting, we want to allow the active fragment what is the
intended action. This is also applicable when suggesting a fragment.
  • Loading branch information
Juan Mendez committed Jul 2, 2017
1 parent af6ce55 commit a1631e8
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,23 +17,47 @@
public class ShoeRack {

ShoeModel shoeModel;
HashMap<String, String> requestActionMap = new HashMap<>();

List<ShoeModel> history = new ArrayList<>();

private PublishSubject<List<ShoeModel>> 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();

/**
Expand Down Expand Up @@ -124,6 +150,44 @@ public boolean suggest (String... tags ){
return anySuccess;
}

public boolean suggestIdsWithTags( HashMap<Integer, String> idsWithActions ){

HashMap<String, String> tagsWithActions = new HashMap<>();

for (Map.Entry<Integer, String> entry : idsWithActions.entrySet()) {
tagsWithActions.put( Integer.toString(entry.getKey()), entry.getValue() );
}

return suggest( tagsWithActions );
}



public boolean suggest( HashMap<String,String> tagsWithActions ){

ShoeModel shoeModel, shoeParent;
ShoeBox shoeBox;
Boolean anySuccess = false;

for (Map.Entry<String, String> 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<List<ShoeModel>> asObservable() {
return publishSubject.hide();
}
Expand Down Expand Up @@ -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(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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<String,String> 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<Integer, String> 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<String> strings = new ArrayList<>(Arrays.asList(new String[]{"a","b","c","d"}));
Expand Down

0 comments on commit a1631e8

Please sign in to comment.