Search in sources :

Example 1 with NetworkException

use of com.android.example.devsummit.archdemo.job.NetworkException in project dev-summit-architecture-demo by yigit.

the class FetchFeedJob method onRun.

@Override
public void onRun() throws Throwable {
    long timestamp = mFeedModel.getLatestTimestamp(mUserId);
    final Call<FeedResponse> feed;
    if (mUserId == null) {
        feed = mApiService.feed(timestamp);
    } else {
        feed = mApiService.userFeed(mUserId, timestamp);
    }
    Response<FeedResponse> response = feed.execute();
    if (response.isSuccess()) {
        Post oldest = handleResponse(response.body());
        mEventBus.post(new FetchedFeedEvent(true, mUserId, oldest));
    } else {
        throw new NetworkException(response.code());
    }
}
Also used : Post(com.android.example.devsummit.archdemo.vo.Post) FeedResponse(com.android.example.devsummit.archdemo.api.FeedResponse) FetchedFeedEvent(com.android.example.devsummit.archdemo.event.feed.FetchedFeedEvent) NetworkException(com.android.example.devsummit.archdemo.job.NetworkException)

Example 2 with NetworkException

use of com.android.example.devsummit.archdemo.job.NetworkException in project dev-summit-architecture-demo by yigit.

the class SaveNewPostJob method onRun.

@Override
public void onRun() throws Throwable {
    Post post = mPostModel.loadByClientIdAndUserId(mClientId, mUserId);
    if (post != null && !post.isPending()) {
        // looks like post probably arrived from somewhere else. Good Job!
        mEventBus.post(new UpdatedPostEvent(post));
        return;
    }
    Response<NewPostResponse> response = mApiService.sendPost(mText, mClientId, mUserId).execute();
    if (response.isSuccess()) {
        NewPostResponse body = response.body();
        body.getPost().setPending(false);
        mPostModel.save(body.getPost());
        mUserModel.save(body.getUser());
        mEventBus.post(new UpdatedPostEvent(body.getPost()));
    } else {
        throw new NetworkException(response.code());
    }
}
Also used : NewPostResponse(com.android.example.devsummit.archdemo.api.NewPostResponse) Post(com.android.example.devsummit.archdemo.vo.Post) UpdatedPostEvent(com.android.example.devsummit.archdemo.event.post.UpdatedPostEvent) NetworkException(com.android.example.devsummit.archdemo.job.NetworkException)

Aggregations

NetworkException (com.android.example.devsummit.archdemo.job.NetworkException)2 Post (com.android.example.devsummit.archdemo.vo.Post)2 FeedResponse (com.android.example.devsummit.archdemo.api.FeedResponse)1 NewPostResponse (com.android.example.devsummit.archdemo.api.NewPostResponse)1 FetchedFeedEvent (com.android.example.devsummit.archdemo.event.feed.FetchedFeedEvent)1 UpdatedPostEvent (com.android.example.devsummit.archdemo.event.post.UpdatedPostEvent)1