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 android.widget.TextView;

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

import rx.android.schedulers.AndroidSchedulers;
import rx.util.functions.Action1;

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

    private BookViewModel bookViewModel;

    @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);

        bookViewModel.subscribeToDataStore();

        final TextView bookNameTextView = (TextView) view.findViewById(R.id.book_name);
        bookViewModel.getBookName()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        bookNameTextView.setText(s);
                    }
                });

        final TextView bookAuthorTextView = (TextView) view.findViewById(R.id.book_author);
        bookViewModel.getAuthorName()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                bookAuthorTextView.setText(s);
            }
        });

        final TextView bookPriceTextView = (TextView) view.findViewById(R.id.book_price);
        bookViewModel.getBookPrice()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                bookPriceTextView.setText(s);
            }
        });
    }

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

@@ -292,34 +292,135 @@
 app.
-viewmodels.BookViewModel;%0A
+utils.SubscriptionManager;%0Aimport com.tehmou.rxbookapp.viewmodels.BookViewModel;%0A%0Aimport rx.Observable;%0Aimport rx.Subscription;
 %0Aimp
@@ -478,13 +478,8 @@
  rx.
-util.
 func
@@ -580,16 +580,103 @@
 ment %7B%0A%0A
+    final private SubscriptionManager subscriptionManager = new SubscriptionManager();%0A
     priv
@@ -1279,55 +1279,8 @@
 );%0A%0A
-        bookViewModel.subscribeToDataStore();%0A%0A
     
@@ -1375,573 +1375,498 @@
     
-bookViewModel.getBookName()%0A                .observeOn(AndroidSchedulers.mainThread())%0A                .subscribe(new Action1%3CString%3E() %7B%0A                    @Override%0A                    public void call(String s) %7B%0A                        bookNameTextView.setText(s);%0A                    %7D%0A                %7D);%0A%0A        final TextView bookAuthorTextView = (TextView) view.findViewById(R.id.book_author);%0A        bookViewModel.getAuthorName()%0A                .observeOn(AndroidSchedulers.mainThread())%0A                .subscribe(new Action1%3CString%3E() %7B
+subscriptionManager.add(subscribeTextView(bookViewModel.getBookName(), bookNameTextView));%0A%0A        final TextView bookAuthorTextView = (TextView) view.findViewById(R.id.book_author);%0A        subscriptionManager.add(subscribeTextView(bookViewModel.getAuthorName(), bookAuthorTextView));%0A%0A        final TextView bookPriceTextView = (TextView) view.findViewById(R.id.book_price);%0A        subscriptionManager.add(subscribeTextView(bookViewModel.getBookPrice(), bookPriceTextView));
 %0A    
-   
+%7D%0A%0A
     
- 
 @Ove
@@ -1867,32 +1867,24 @@
   @Override%0A
-        
     public v
@@ -1891,225 +1891,243 @@
 oid 
-call(String s) %7B%0A                bookAuthorTextView.setText(s);%0A            %7D%0A        %7D);%0A%0A        final TextView bookPriceTextView = (TextView) view.findViewById(R.id.book_price);%0A        bookViewModel.getBookPrice()
+onDestroyView() %7B%0A        super.onDestroyView();%0A        subscriptionManager.unsubscribeAll();%0A    %7D%0A%0A    static private Subscription subscribeTextView(Observable%3CString%3E observable, final TextView textView) %7B%0A        return observable
 %0A   
@@ -2241,24 +2241,32 @@
             
+        
 @Override%0A  
@@ -2259,24 +2259,32 @@
   @Override%0A
+        
             
@@ -2328,26 +2328,25 @@
         
-bookPriceT
+        t
 extView.
@@ -2369,18 +2369,34 @@
         
+   
-%7D%0A
+     %7D%0A        
         
@@ -2434,35 +2434,151 @@
 blic void on
-DestroyView
+Resume() %7B%0A        super.onResume();%0A        bookViewModel.subscribeToDataStore();%0A    %7D%0A%0A    @Override%0A    public void onPause
 () %7B%0A       
@@ -2582,35 +2582,29 @@
     super.on
-DestroyView
+Pause
 ();%0A        
@@ -2651,10 +2651,11 @@
 ;%0A    %7D%0A
+%0A
 %7D%0A

Move subscribe/unSubscribe to resume/pause