Last active
August 29, 2015 14:01
-
-
Save coffeearmy/d9da34ac07f6380d433a to your computer and use it in GitHub Desktop.
OTTO in android
This file contains hidden or 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
import com.squareup.otto.Bus; | |
public class OttoBusHelper { | |
private static Bus _currentBus; | |
public OttoBusHelper() | |
{ | |
} | |
public static Bus getCurrentBus() | |
{ | |
if(_currentBus==null){ | |
_currentBus=new Bus(); | |
} | |
return _currentBus; | |
} | |
} |
This file contains hidden or 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
/** | |
* @see http://nick.perfectedz.com/otto-event-system/ | |
*/ | |
public abstract class AbstractEvent | |
{ | |
private Enum _type; | |
protected AbstractEvent(Enum type) | |
{ | |
this._type = type; | |
} | |
public Enum getType() | |
{ | |
return this._type; | |
} | |
} |
This file contains hidden or 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
public class OnEvent extends AbstractEvent{ | |
private Item _Item; | |
private Type _type; | |
public enum Type | |
{ | |
SAVE,DELETE | |
} | |
public OnFinishAddElectrodomesticos(Type type,Item item) { | |
super(type); | |
this._type=type; | |
this._item = item; | |
} | |
public Item getElectrodomestico() { | |
return _item; | |
} | |
} |
This file contains hidden or 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
////SENDER | |
OttoBusHelper.getCurrentBus().post(new OnEvent(OnEvent.Type.SAVE ,newItem)); | |
////RECEIVER | |
@Override | |
public void onResume() { | |
OttoBusHelper.getCurrentBus().register(this); | |
super.onResume(); | |
} | |
@Override | |
public void onPause() { | |
OttoBusHelper.getCurrentBus().unregister(this); | |
super.onPause(); | |
} | |
@Subscribe | |
public void updateResultsInUi(OnEvent event) { | |
Item electItem=event.getItem(); | |
ItemHandlerImpl.getInstance(getActivity()).insertItem(electItem); | |
listChanged(electItem); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment