Skip to content

Instantly share code, notes, and snippets.

@alexoro
Created February 10, 2014 19:10
Show Gist options
  • Save alexoro/8922196 to your computer and use it in GitHub Desktop.
Save alexoro/8922196 to your computer and use it in GitHub Desktop.
public class MainFragment extends Fragment {
private Model mModel;
private TextView mTextView;
@Override
@SuppressWarnings("ConstantConditions")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mModel = new Model();
mModel.setFragmentVisible(false);
// mModel.setName("");
// mModel.setCount(0);
mModel.init();
mModel.subscribe(
Arrays.asList(Model.FRAGMENT_VISIBLE),
new Runnable() {
@Override
public void run() {
if (!mModel.isFragmentVisible()) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mModel.setCount(mModel.getCount() + 1);
}
});
}
}
});
mModel.subscribe(
Arrays.asList(Model.FRAGMENT_VISIBLE),
new Runnable() {
@Override
public void run() {
if (mModel.isFragmentVisible()) {
mModel.setName("" + new Random(System.currentTimeMillis()).nextLong());
}
}
});
mModel.subscribe(
Arrays.asList(Model.FRAGMENT_VISIBLE, Model.NAME, Model.COUNT),
new Runnable() {
@Override
public void run() {
if (mModel.isFragmentVisible()) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mTextView.setText(mModel.getName() + " " + mModel.getCount());
}
});
}
}
}
);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mTextView = new TextView(getActivity());
return mTextView;
}
@Override
public void onStart() {
super.onStart();
mModel.setFragmentVisible(true);
}
@Override
public void onStop() {
super.onStop();
mModel.setFragmentVisible(false);
}
@Override
public void onDestroy() {
super.onDestroy();
mModel.deInit();
// unsubscribe
}
}
public class Model {
public static final String FRAGMENT_VISIBLE = "fragment_visible";
public static final String COUNT = "count";
public static final String NAME = "name";
private boolean mIsFragmentVisible;
private Integer mCount;
private String mName;
private ExecutorService mService;
private Collection<Pair<Collection<String>, Runnable>> mCallbacks;
private Set<String> mInvokedEvents;
public Model() {
mCallbacks = new ArrayList<Pair<Collection<String>, Runnable>>();
mInvokedEvents = new HashSet<String>();
}
public void init() {
mService = Executors.newSingleThreadExecutor();
}
public void deInit() {
mService.shutdownNow();
}
public void fireChange(String field) {
mInvokedEvents.add(field);
for (Pair<Collection<String>, Runnable> p: mCallbacks) {
if (p.first.contains(field) && mInvokedEvents.containsAll(p.first)) {
mService.submit(p.second);
}
}
}
public void subscribe(Collection<String> args, Runnable callback) {
mCallbacks.add(new Pair<Collection<String>, Runnable>(args, callback));
if (mInvokedEvents.containsAll(args)) {
mService.submit(callback);
}
}
public boolean isFragmentVisible() {
return mIsFragmentVisible;
}
public void setFragmentVisible(boolean isFragmentVisible) {
mIsFragmentVisible = isFragmentVisible;
fireChange(FRAGMENT_VISIBLE);
}
public Integer getCount() {
return mCount;
}
public void setCount(Integer count) {
mCount = count;
fireChange(COUNT);
}
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
fireChange(NAME);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment