Skip to content

Commit

Permalink
remove SchedulersFacade abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
jshvarts committed Oct 2, 2017
1 parent 6308c4b commit 0ca3e9b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.example.offline.domain.SyncCommentUseCase;
import com.example.offline.domain.UpdateCommentUseCase;
import com.example.offline.presentation.CommentsViewModelFactory;
import com.example.offline.rx.SchedulersFacade;

import dagger.Module;
import dagger.Provides;
Expand All @@ -21,16 +20,14 @@
public class CommentsActivityModule {
@Provides
CommentsViewModelFactory provideCommentsViewModelFactory(GetCommentsUseCase getCommentsUseCase,
AddCommentUseCase addCommentUseCase,
SchedulersFacade schedulersFacade) {
return new CommentsViewModelFactory(getCommentsUseCase, addCommentUseCase, schedulersFacade);
AddCommentUseCase addCommentUseCase) {
return new CommentsViewModelFactory(getCommentsUseCase, addCommentUseCase);
}

@Provides
SyncCommentLifecycleObserver provideSyncCommentLifecycleObserver(UpdateCommentUseCase updateCommentUseCase,
DeleteCommentUseCase deleteCommentUseCase,
SchedulersFacade schedulersFacade) {
return new SyncCommentLifecycleObserver(updateCommentUseCase, deleteCommentUseCase, schedulersFacade);
DeleteCommentUseCase deleteCommentUseCase) {
return new SyncCommentLifecycleObserver(updateCommentUseCase, deleteCommentUseCase);
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import com.example.offline.domain.DeleteCommentUseCase;
import com.example.offline.domain.UpdateCommentUseCase;
import com.example.offline.model.Comment;
import com.example.offline.rx.SchedulersFacade;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;

/**
Expand All @@ -18,16 +19,12 @@
public class SyncCommentLifecycleObserver implements LifecycleObserver {
private final UpdateCommentUseCase updateCommentUseCase;
private final DeleteCommentUseCase deleteCommentUseCase;
private final SchedulersFacade schedulersFacade;

private final CompositeDisposable disposables = new CompositeDisposable();

public SyncCommentLifecycleObserver(UpdateCommentUseCase updateCommentUseCase,
DeleteCommentUseCase deleteCommentUseCase,
SchedulersFacade schedulersFacade) {
DeleteCommentUseCase deleteCommentUseCase) {
this.updateCommentUseCase = updateCommentUseCase;
this.deleteCommentUseCase = deleteCommentUseCase;
this.schedulersFacade = schedulersFacade;
}

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
Expand All @@ -54,17 +51,17 @@ private void handleSyncResponse(SyncCommentResponse response) {
private void onSyncCommentSuccess(Comment comment) {
Timber.d("received sync comment success event for comment %s", comment);
disposables.add(updateCommentUseCase.updateComment(comment)
.subscribeOn(schedulersFacade.io())
.observeOn(schedulersFacade.ui())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(() -> Timber.d("update comment success"),
t -> Timber.e(t, "update comment error")));
}

private void onSyncCommentFailed(Comment comment) {
Timber.d("received sync comment failed event for comment %s", comment);
disposables.add(deleteCommentUseCase.deleteComment(comment)
.subscribeOn(schedulersFacade.io())
.observeOn(schedulersFacade.ui())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(() -> Timber.d("delete comment success"),
t -> Timber.e(t, "delete comment error")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,25 @@
import com.example.offline.domain.AddCommentUseCase;
import com.example.offline.domain.GetCommentsUseCase;
import com.example.offline.model.Comment;
import com.example.offline.rx.SchedulersFacade;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;

public class CommentsViewModel extends ViewModel {

private final GetCommentsUseCase getCommentsUseCase;
private final AddCommentUseCase addCommentUseCase;
private final SchedulersFacade schedulersFacade;
private final CompositeDisposable disposables = new CompositeDisposable();
private MutableLiveData<List<Comment>> commentsLiveData = new MutableLiveData<>();

public CommentsViewModel(GetCommentsUseCase getCommentsUseCase,
AddCommentUseCase addCommentUseCase,
SchedulersFacade schedulersFacade) {
AddCommentUseCase addCommentUseCase) {
this.getCommentsUseCase = getCommentsUseCase;
this.addCommentUseCase = addCommentUseCase;
this.schedulersFacade = schedulersFacade;

loadComments();
}
Expand All @@ -39,16 +37,16 @@ protected void onCleared() {

public void loadComments() {
disposables.add(getCommentsUseCase.getComments()
.subscribeOn(schedulersFacade.io())
.observeOn(schedulersFacade.ui())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(commentList -> commentsLiveData.setValue(commentList),
t -> Timber.e(t, "get comments error")));
}

public void addComment(String commentText) {
disposables.add(addCommentUseCase.addComment(commentText)
.subscribeOn(schedulersFacade.io())
.observeOn(schedulersFacade.ui())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(() -> Timber.d("add comment success"),
t -> Timber.e(t, "add comment error")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,22 @@

import com.example.offline.domain.AddCommentUseCase;
import com.example.offline.domain.GetCommentsUseCase;
import com.example.offline.rx.SchedulersFacade;

public class CommentsViewModelFactory implements ViewModelProvider.Factory {

private final GetCommentsUseCase getCommentsUseCase;
private final AddCommentUseCase addCommentUseCase;
private final SchedulersFacade schedulersFacade;

public CommentsViewModelFactory(GetCommentsUseCase getCommentsUseCase,
AddCommentUseCase addCommentUseCase,
SchedulersFacade schedulersFacade) {
AddCommentUseCase addCommentUseCase) {
this.getCommentsUseCase = getCommentsUseCase;
this.addCommentUseCase = addCommentUseCase;
this.schedulersFacade = schedulersFacade;
}

@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
if (modelClass.isAssignableFrom(CommentsViewModel.class)) {
return (T) new CommentsViewModel(getCommentsUseCase, addCommentUseCase, schedulersFacade);
return (T) new CommentsViewModel(getCommentsUseCase, addCommentUseCase);
}
throw new IllegalArgumentException("Unknown ViewModel class");
}
Expand Down
39 changes: 0 additions & 39 deletions app/src/main/java/com/example/offline/rx/SchedulersFacade.java

This file was deleted.

0 comments on commit 0ca3e9b

Please sign in to comment.