// Path: app/src/main/java/com/example/offline/domain/services/SyncCommentLifecycleObserver.java
// public class SyncCommentLifecycleObserver implements LifecycleObserver {
//     private final UpdateCommentUseCase updateCommentUseCase;
//     private final DeleteCommentUseCase deleteCommentUseCase;
//     private final CompositeDisposable disposables = new CompositeDisposable();
// 
//     public SyncCommentLifecycleObserver(UpdateCommentUseCase updateCommentUseCase,
//                                         DeleteCommentUseCase deleteCommentUseCase) {
//         this.updateCommentUseCase = updateCommentUseCase;
//         this.deleteCommentUseCase = deleteCommentUseCase;
//     }
// 
//     @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
//     public void onResume() {
//         Timber.d("onResume lifecycle event.");
//         disposables.add(SyncCommentRxBus.getInstance().toObservable()
//                 .subscribe(this::handleSyncResponse, t -> Timber.e(t, "error handling sync response")));
//     }
// 
//     @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
//     public void onPause() {
//         Timber.d("onPause lifecycle event.");
//         disposables.clear();
//     }
// 
//     private void handleSyncResponse(SyncCommentResponse response) {
//         if (response.eventType == SyncResponseEventType.SUCCESS) {
//             onSyncCommentSuccess(response.comment);
//         } else {
//             onSyncCommentFailed(response.comment);
//         }
//     }
// 
//     private void onSyncCommentSuccess(Comment comment) {
//         Timber.d("received sync comment success event for comment %s", comment);
//         disposables.add(updateCommentUseCase.updateComment(comment)
//                 .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(Schedulers.io())
//                 .observeOn(AndroidSchedulers.mainThread())
//                 .subscribe(() -> Timber.d("delete comment success"),
//                         t -> Timber.e(t, "delete comment error")));
//     }
// }
// 
// Path: app/src/main/java/com/example/offline/presentation/CommentsViewModel.java
// public class CommentsViewModel extends ViewModel {
// 
//     private final GetCommentsUseCase getCommentsUseCase;
//     private final AddCommentUseCase addCommentUseCase;
//     private final CompositeDisposable disposables = new CompositeDisposable();
//     private MutableLiveData<List<Comment>> commentsLiveData = new MutableLiveData<>();
// 
//     public CommentsViewModel(GetCommentsUseCase getCommentsUseCase,
//                       AddCommentUseCase addCommentUseCase) {
//         this.getCommentsUseCase = getCommentsUseCase;
//         this.addCommentUseCase = addCommentUseCase;
// 
//         loadComments();
//     }
// 
//     @Override
//     protected void onCleared() {
//         disposables.clear();
//     }
// 
//     /**
//      * Adds new comment
//      */
//     public void addComment(String commentText) {
//         disposables.add(addCommentUseCase.addComment(commentText)
//                 .subscribeOn(Schedulers.io())
//                 .observeOn(AndroidSchedulers.mainThread())
//                 .subscribe(() -> Timber.d("add comment success"),
//                         t -> Timber.e(t, "add comment error")));
//     }
// 
//     /**
//      * Exposes the latest comments so the UI can observe it
//      */
//     public LiveData<List<Comment>> comments() {
//         return commentsLiveData;
//     }
// 
//     void loadComments() {
//         disposables.add(getCommentsUseCase.getComments()
//                 .subscribeOn(Schedulers.io())
//                 .observeOn(AndroidSchedulers.mainThread())
//                 .subscribe(commentsLiveData::setValue,
//                         t -> Timber.e(t, "get comments error")));
//     }
// }
// 
// Path: app/src/main/java/com/example/offline/presentation/CommentsViewModelFactory.java
// public class CommentsViewModelFactory implements ViewModelProvider.Factory {
// 
//     private final GetCommentsUseCase getCommentsUseCase;
//     private final AddCommentUseCase addCommentUseCase;
// 
//     public CommentsViewModelFactory(GetCommentsUseCase getCommentsUseCase,
//                              AddCommentUseCase addCommentUseCase) {
//         this.getCommentsUseCase = getCommentsUseCase;
//         this.addCommentUseCase = addCommentUseCase;
//     }
// 
//     @Override
//     public <T extends ViewModel> T create(Class<T> modelClass) {
//         if (modelClass.isAssignableFrom(CommentsViewModel.class)) {
//             return (T) new CommentsViewModel(getCommentsUseCase, addCommentUseCase);
//         }
//         throw new IllegalArgumentException("Unknown ViewModel class");
//     }
// }


import android.arch.lifecycle.LifecycleRegistry;
import android.arch.lifecycle.LifecycleRegistryOwner;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import com.example.offline.R;
import com.example.offline.domain.services.SyncCommentLifecycleObserver;
import com.example.offline.presentation.CommentsViewModel;
import com.example.offline.presentation.CommentsViewModelFactory;
import java.util.ArrayList;
import javax.inject.Inject;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dagger.android.AndroidInjection;
package com.example.offline.view;






public class CommentsActivity extends AppCompatActivity implements LifecycleRegistryOwner {

    @Inject
    CommentsViewModelFactory viewModelFactory;

    @Inject
    SyncCommentLifecycleObserver syncCommentLifecycleObserver;

    @BindView(R.id.add_comment_edittext)
    EditText addCommentEditText;

    @BindView(R.id.comments_recycler_view)
    RecyclerView recyclerView;

    private CommentListAdapter recyclerViewAdapter;

// Path: app/src/main/java/com/example/offline/domain/services/SyncCommentLifecycleObserver.java
// public class SyncCommentLifecycleObserver implements LifecycleObserver {
//     private final UpdateCommentUseCase updateCommentUseCase;
//     private final DeleteCommentUseCase deleteCommentUseCase;
//     private final CompositeDisposable disposables = new CompositeDisposable();
// 
//     public SyncCommentLifecycleObserver(UpdateCommentUseCase updateCommentUseCase,
//                                         DeleteCommentUseCase deleteCommentUseCase) {
//         this.updateCommentUseCase = updateCommentUseCase;
//         this.deleteCommentUseCase = deleteCommentUseCase;
//     }
// 
//     @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
//     public void onResume() {
//         Timber.d("onResume lifecycle event.");
//         disposables.add(SyncCommentRxBus.getInstance().toObservable()
//                 .subscribe(this::handleSyncResponse, t -> Timber.e(t, "error handling sync response")));
//     }
// 
//     @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
//     public void onPause() {
//         Timber.d("onPause lifecycle event.");
//         disposables.clear();
//     }
// 
//     private void handleSyncResponse(SyncCommentResponse response) {
//         if (response.eventType == SyncResponseEventType.SUCCESS) {
//             onSyncCommentSuccess(response.comment);
//         } else {
//             onSyncCommentFailed(response.comment);
//         }
//     }
// 
//     private void onSyncCommentSuccess(Comment comment) {
//         Timber.d("received sync comment success event for comment %s", comment);
//         disposables.add(updateCommentUseCase.updateComment(comment)
//                 .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(Schedulers.io())
//                 .observeOn(AndroidSchedulers.mainThread())
//                 .subscribe(() -> Timber.d("delete comment success"),
//                         t -> Timber.e(t, "delete comment error")));
//     }
// }
// 
// Path: app/src/main/java/com/example/offline/presentation/CommentsViewModel.java
// public class CommentsViewModel extends ViewModel {
// 
//     private final GetCommentsUseCase getCommentsUseCase;
//     private final AddCommentUseCase addCommentUseCase;
//     private final CompositeDisposable disposables = new CompositeDisposable();
//     private MutableLiveData<List<Comment>> commentsLiveData = new MutableLiveData<>();
// 
//     public CommentsViewModel(GetCommentsUseCase getCommentsUseCase,
//                       AddCommentUseCase addCommentUseCase) {
//         this.getCommentsUseCase = getCommentsUseCase;
//         this.addCommentUseCase = addCommentUseCase;
// 
//         loadComments();
//     }
// 
//     @Override
//     protected void onCleared() {
//         disposables.clear();
//     }
// 
//     /**
//      * Adds new comment
//      */
//     public void addComment(String commentText) {
//         disposables.add(addCommentUseCase.addComment(commentText)
//                 .subscribeOn(Schedulers.io())
//                 .observeOn(AndroidSchedulers.mainThread())
//                 .subscribe(() -> Timber.d("add comment success"),
//                         t -> Timber.e(t, "add comment error")));
//     }
// 
//     /**
//      * Exposes the latest comments so the UI can observe it
//      */
//     public LiveData<List<Comment>> comments() {
//         return commentsLiveData;
//     }
// 
//     void loadComments() {
//         disposables.add(getCommentsUseCase.getComments()
//                 .subscribeOn(Schedulers.io())
//                 .observeOn(AndroidSchedulers.mainThread())
//                 .subscribe(commentsLiveData::setValue,
//                         t -> Timber.e(t, "get comments error")));
//     }
// }
// 
// Path: app/src/main/java/com/example/offline/presentation/CommentsViewModelFactory.java
// public class CommentsViewModelFactory implements ViewModelProvider.Factory {
// 
//     private final GetCommentsUseCase getCommentsUseCase;
//     private final AddCommentUseCase addCommentUseCase;
// 
//     public CommentsViewModelFactory(GetCommentsUseCase getCommentsUseCase,
//                              AddCommentUseCase addCommentUseCase) {
//         this.getCommentsUseCase = getCommentsUseCase;
//         this.addCommentUseCase = addCommentUseCase;
//     }
// 
//     @Override
//     public <T extends ViewModel> T create(Class<T> modelClass) {
//         if (modelClass.isAssignableFrom(CommentsViewModel.class)) {
//             return (T) new CommentsViewModel(getCommentsUseCase, addCommentUseCase);
//         }
//         throw new IllegalArgumentException("Unknown ViewModel class");
//     }
// }


// Path: app/src/main/java/com/example/offline/view/CommentsActivity.java
import android.arch.lifecycle.LifecycleRegistry;
import android.arch.lifecycle.LifecycleRegistryOwner;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import com.example.offline.R;
import com.example.offline.domain.services.SyncCommentLifecycleObserver;
import com.example.offline.presentation.CommentsViewModel;
import com.example.offline.presentation.CommentsViewModelFactory;
import java.util.ArrayList;
import javax.inject.Inject;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dagger.android.AndroidInjection;

package com.example.offline.view;






public class CommentsActivity extends AppCompatActivity implements LifecycleRegistryOwner {

    @Inject
    CommentsViewModelFactory viewModelFactory;

    @Inject
    SyncCommentLifecycleObserver syncCommentLifecycleObserver;

    @BindView(R.id.add_comment_edittext)
    EditText addCommentEditText;

    @BindView(R.id.comments_recycler_view)
    RecyclerView recyclerView;

    private CommentListAdapter recyclerViewAdapter;

    private CommentsViewModel viewModel;