Skip to content
This repository has been archived by the owner on Mar 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #65 from grandcentrix/feature/runonmainthread
Browse files Browse the repository at this point in the history
TiPresenter#runOnUiThread(Runnable)
  • Loading branch information
StefMa authored Feb 10, 2017
2 parents 53579df + 5d87cee commit 9d41ece
Show file tree
Hide file tree
Showing 22 changed files with 578 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.grandcentrix.thirtyinch.internal.TiLoggingTagProvider;
import net.grandcentrix.thirtyinch.internal.TiPresenterProvider;
import net.grandcentrix.thirtyinch.internal.TiViewProvider;
import net.grandcentrix.thirtyinch.internal.UiThreadExecutor;
import net.grandcentrix.thirtyinch.util.AndroidDeveloperOptions;
import net.grandcentrix.thirtyinch.util.AnnotationUtil;

Expand All @@ -39,6 +40,7 @@
import android.support.annotation.Nullable;

import java.util.List;
import java.util.concurrent.Executor;

/**
* Binds a {@link TiPresenter} to an {@link Activity}
Expand All @@ -57,6 +59,8 @@ public class TiActivityPlugin<P extends TiPresenter<V>, V extends TiView> extend

private TiActivityDelegate<P, V> mDelegate;

private final UiThreadExecutor mUiThreadExecutor = new UiThreadExecutor();

/**
* Binds a {@link TiPresenter} returned by the {@link TiPresenterProvider} to the {@link
* Activity} and all future {@link Activity} instances created due to configuration changes.
Expand Down Expand Up @@ -118,6 +122,11 @@ public P getRetainedPresenter() {
return null;
}

@Override
public Executor getUiThreadExecutor() {
return mUiThreadExecutor;
}

/**
* Invalidates the cache of the latest bound view. Forces the next binding of the view to run
* through all the interceptors (again).
Expand Down Expand Up @@ -194,11 +203,6 @@ public void onStop() {
mDelegate.onStop_afterSuper();
}

@Override
public boolean postToMessageQueue(final Runnable runnable) {
return getActivity().getWindow().getDecorView().post(runnable);
}

@SuppressWarnings("unchecked")
@NonNull
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.grandcentrix.thirtyinch.internal.TiLoggingTagProvider;
import net.grandcentrix.thirtyinch.internal.TiPresenterProvider;
import net.grandcentrix.thirtyinch.internal.TiViewProvider;
import net.grandcentrix.thirtyinch.internal.UiThreadExecutor;
import net.grandcentrix.thirtyinch.util.AndroidDeveloperOptions;
import net.grandcentrix.thirtyinch.util.AnnotationUtil;

Expand All @@ -41,6 +42,7 @@
import android.view.ViewGroup;

import java.util.List;
import java.util.concurrent.Executor;

/**
* Adds a {@link TiPresenter} to a Fragment. Can be used for both, {@link Fragment} and
Expand All @@ -58,6 +60,8 @@ public class TiFragmentPlugin<P extends TiPresenter<V>, V extends TiView> extend

private TiFragmentDelegate<P, V> mDelegate;

private final UiThreadExecutor mUiThreadExecutor = new UiThreadExecutor();

/**
* Binds a {@link TiPresenter} returned by the {@link TiPresenterProvider} to the {@link
* Fragment} and all future {@link Fragment} instances created due to configuration changes.
Expand Down Expand Up @@ -99,7 +103,6 @@ public List<BindViewInterceptor> getInterceptors(
return mDelegate.getInterceptors(predicate);
}


@Override
public String getLoggingTag() {
return TAG;
Expand All @@ -109,6 +112,11 @@ public P getPresenter() {
return mDelegate.getPresenter();
}

@Override
public Executor getUiThreadExecutor() {
return mUiThreadExecutor;
}

/**
* Invalidates the cache of the latest bound view. Forces the next binding of the view to run
* through all the interceptors (again).
Expand Down Expand Up @@ -187,11 +195,6 @@ public void onStop() {
super.onStop();
}

@Override
public boolean postToMessageQueue(final Runnable runnable) {
return getFragment().getActivity().getWindow().getDecorView().post(runnable);
}

/**
* the default implementation assumes that the fragment is the view and implements the {@link
* TiView} interface. Override this method for a different behaviour.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,40 +114,37 @@ public Observable<T> call(Observable<T> observable) {
* TiPresenter#attachView(TiView)} and before calling {@link TiPresenter#detachView()}.
*/
public static Observable<Boolean> isViewReady(final TiPresenter presenter) {
return Observable.create(
new Observable.OnSubscribe<Boolean>() {
@Override
public void call(final Subscriber<? super Boolean> subscriber) {
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(presenter.getState()
== TiPresenter.State.VIEW_ATTACHED);
}

final Removable removable = presenter
.addLifecycleObserver(new TiLifecycleObserver() {
@Override
public void onChange(final TiPresenter.State state,
final boolean hasLifecycleMethodBeenCalled) {
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(state
== TiPresenter.State.VIEW_ATTACHED);
}
}
});

subscriber.add(new Subscription() {
@Override
public boolean isUnsubscribed() {
return removable.isRemoved();
}
return Observable.create(new Observable.OnSubscribe<Boolean>() {
@Override
public void call(final Subscriber<? super Boolean> subscriber) {
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(presenter.getState() == TiPresenter.State.VIEW_ATTACHED);
}

final Removable removable = presenter
.addLifecycleObserver(new TiLifecycleObserver() {
@Override
public void unsubscribe() {
removable.remove();
public void onChange(final TiPresenter.State state,
final boolean hasLifecycleMethodBeenCalled) {
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(state == TiPresenter.State.VIEW_ATTACHED
&& hasLifecycleMethodBeenCalled);
}
}
});

subscriber.add(new Subscription() {
@Override
public boolean isUnsubscribed() {
return removable.isRemoved();
}

@Override
public void unsubscribe() {
removable.remove();
}
})
.distinctUntilChanged();
});
}
}).distinctUntilChanged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,39 @@ public class RxTiPresenterUtils {
* TiPresenter#attachView(TiView)} and before calling {@link TiPresenter#detachView()}.
*/
public static Observable<Boolean> isViewReady(final TiPresenter presenter) {
return Observable.create(
new ObservableOnSubscribe<Boolean>() {
@Override
public void subscribe(final ObservableEmitter<Boolean> emitter)
throws Exception {
if (!emitter.isDisposed()) {
emitter.onNext(presenter.getState()
== TiPresenter.State.VIEW_ATTACHED);
}

final Removable removable = presenter
.addLifecycleObserver(new TiLifecycleObserver() {
@Override
public void onChange(final TiPresenter.State state,
final boolean hasLifecycleMethodBeenCalled) {
if (!emitter.isDisposed()) {
emitter.onNext(state ==
TiPresenter.State.VIEW_ATTACHED);
}
}
});

emitter.setDisposable(new Disposable() {
@Override
public void dispose() {
removable.remove();
}
return Observable.create(new ObservableOnSubscribe<Boolean>() {
@Override
public void subscribe(final ObservableEmitter<Boolean> emitter)
throws Exception {
if (!emitter.isDisposed()) {
emitter.onNext(presenter.getState() == TiPresenter.State.VIEW_ATTACHED);
}

final Removable removable = presenter
.addLifecycleObserver(new TiLifecycleObserver() {
@Override
public boolean isDisposed() {
return removable.isRemoved();
public void onChange(final TiPresenter.State state,
final boolean hasLifecycleMethodBeenCalled) {
if (!emitter.isDisposed()) {
emitter.onNext(state == TiPresenter.State.VIEW_ATTACHED
&& hasLifecycleMethodBeenCalled);
}
}
});

emitter.setDisposable(new Disposable() {
@Override
public void dispose() {
removable.remove();
}

@Override
public boolean isDisposed() {
return removable.isRemoved();
}
})
.distinctUntilChanged();
});
}
}).distinctUntilChanged();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package net.grandcentrix.thirtyinch.sample;

import net.grandcentrix.thirtyinch.TiPresenter;
import net.grandcentrix.thirtyinch.ViewAction;
import net.grandcentrix.thirtyinch.rx.RxTiPresenterSubscriptionHandler;
import net.grandcentrix.thirtyinch.rx.RxTiPresenterUtils;

Expand Down Expand Up @@ -73,7 +74,12 @@ protected void onCreate() {
.subscribe(new Action1<Long>() {
@Override
public void call(final Long uptime) {
getView().showPresenterUpTime(uptime);
sendToView(new ViewAction<HelloWorldView>() {
@Override
public void call(final HelloWorldView view) {
view.showPresenterUpTime(uptime);
}
});
}
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public interface HelloWorldView extends TiView {

Observable<Void> onButtonClicked();

@CallOnMainThread
void showPresenterUpTime(Long uptime);

@CallOnMainThread
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import net.grandcentrix.thirtyinch.TiPresenter;
import net.grandcentrix.thirtyinch.TiView;

import java.util.concurrent.Executor;

public class TiPresenterInstructor<V extends TiView> {

private TiPresenter<V> mPresenter;
Expand All @@ -32,6 +34,12 @@ public TiPresenterInstructor(final TiPresenter<V> presenter) {
public void attachView(final V view) {
detachView();

mPresenter.setUiThreadExecutor(new Executor() {
@Override
public void execute(final Runnable action) {
action.run();
}
});
mPresenter.attachView(view);
}

Expand Down Expand Up @@ -59,6 +67,7 @@ public void detachView() {
break;
case VIEW_ATTACHED:
mPresenter.detachView();
mPresenter.setUiThreadExecutor(null);
break;
case DESTROYED:
throw new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;

import java.util.concurrent.Executor;

import static junit.framework.Assert.assertEquals;

@RunWith(AndroidJUnit4.class)
Expand Down Expand Up @@ -239,9 +241,13 @@ public boolean isDontKeepActivitiesEnabled() {
}

@Override
public boolean postToMessageQueue(final Runnable action) {
action.run();
return true;
public Executor getUiThreadExecutor() {
return new Executor() {
@Override
public void execute(@NonNull final Runnable action) {
action.run();
}
};
}
},
new TiViewProvider<TiView>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.grandcentrix.thirtyinch.internal.TiLoggingTagProvider;
import net.grandcentrix.thirtyinch.internal.TiPresenterProvider;
import net.grandcentrix.thirtyinch.internal.TiViewProvider;
import net.grandcentrix.thirtyinch.internal.UiThreadExecutor;
import net.grandcentrix.thirtyinch.util.AndroidDeveloperOptions;
import net.grandcentrix.thirtyinch.util.AnnotationUtil;

Expand All @@ -32,6 +33,7 @@
import android.support.v7.app.AppCompatActivity;

import java.util.List;
import java.util.concurrent.Executor;

/**
* Created by pascalwelsch on 9/8/15.
Expand All @@ -48,6 +50,8 @@ public abstract class TiActivity<P extends TiPresenter<V>, V extends TiView>
private final TiActivityDelegate<P, V> mDelegate
= new TiActivityDelegate<>(this, this, this, this);

private final UiThreadExecutor mUiThreadExecutor = new UiThreadExecutor();

@NonNull
@Override
public Removable addBindViewInterceptor(@NonNull final BindViewInterceptor interceptor) {
Expand Down Expand Up @@ -90,6 +94,11 @@ public P getRetainedPresenter() {
return null;
}

@Override
public Executor getUiThreadExecutor() {
return mUiThreadExecutor;
}

/**
* Invalidates the cache of the latest bound view. Forces the next binding of the view to run
* through all the interceptors (again).
Expand Down Expand Up @@ -136,11 +145,6 @@ public Object onRetainCustomNonConfigurationInstance() {
return null;
}

@Override
public boolean postToMessageQueue(final Runnable runnable) {
return getWindow().getDecorView().post(runnable);
}

@SuppressWarnings("unchecked")
@NonNull
@Override
Expand Down
Loading

0 comments on commit 9d41ece

Please sign in to comment.