package com.tehmou.rxbookapp;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tehmou.rxbookapp.data.DataStore;
import com.tehmou.rxbookapp.viewmodels.BookViewModel;
import com.tehmou.rxbookapp.views.BookInfoView;

/**
 * Created by ttuo on 19/03/14.
 */
public class BookFragment extends Fragment {
    private BookViewModel bookViewModel;

    private BookInfoView bookInfoView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bookViewModel = new BookViewModel(DataStore.getInstance(), "436346");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        bookInfoView = (BookInfoView) view.findViewById(R.id.book_info_view);
        bookInfoView.setViewModel(bookViewModel);
    }

    @Override
    public void onResume() {
        super.onResume();
        bookViewModel.subscribeToDataStore();
    }

    @Override
    public void onPause() {
        super.onPause();
        bookViewModel.unsubscribeFromDataStore();
    }
}

@@ -1157,43 +1157,175 @@
 book
-InfoView.setViewModel(bookViewModel
+ViewModel.subscribeToDataStore();%0A    %7D%0A%0A    @Override%0A    public void onDestroyView() %7B%0A        super.onDestroyView();%0A        bookViewModel.unsubscribeFromDataStore(
 );%0A 
@@ -1415,39 +1415,43 @@
 book
-ViewModel.subscribeToDataStore(
+InfoView.setViewModel(bookViewModel
 );%0A 
@@ -1539,43 +1539,34 @@
 book
-ViewModel.unsubscribeFromDataStore(
+InfoView.setViewModel(null
 );%0A 

Update view model life cycle