package com.tehmou.rxbookapp.viewmodels;

import com.tehmou.rxbookapp.data.DataStore;
import com.tehmou.rxbookapp.pojo.Author;
import com.tehmou.rxbookapp.pojo.Book;
import com.tehmou.rxbookapp.utils.SubscriptionManager;

import rx.Observable;
import rx.subjects.BehaviorSubject;
import rx.subjects.Subject;
import rx.util.functions.Action1;
import rx.util.functions.Func1;

/**
 * Created by ttuo on 19/03/14.
 */
public class BookViewModel {
    final private String bookId;
    final private DataStore dataStore;
    final private SubscriptionManager subscriptionManager = new SubscriptionManager();

    final private Subject<String, String> bookName = BehaviorSubject.create("Loading name..");
    final private Subject<String, String> bookPrice = BehaviorSubject.create("Loading book price..");
    final private Subject<String, String> authorName = BehaviorSubject.create("Loading author name..");

    public Observable<String> getBookName() {
        return bookName;
    }
    public Observable<String> getBookPrice() {
        return bookPrice;
    }
    public Observable<String> getAuthorName() {
        return authorName;
    }

    public BookViewModel(final DataStore dataStore, final String bookId) {
        this.bookId = bookId;
        this.dataStore = dataStore;
    }

    public void subscribeToDataStore() {
        dataStore.getBook(bookId)
                .flatMap(new Func1<Book, Observable<Author>>() {
                    @Override
                    public Observable<Author> call(Book book) {
                        bookName.onNext(book.name);
                        return dataStore.getAuthor(book.authorId);
                    }
                })
                .subscribe(new Action1<Author>() {
                    @Override
                    public void call(Author author) {
                        authorName.onNext(author.name);
                    }
                });
        dataStore.getBookPrice(bookId)
                .map(new rx.functions.Func1<Integer, String>() {
                    @Override
                    public String call(Integer integer) {
                        return "Price: " + integer + " EUR";
                    }
                })
                .subscribe(bookPrice);
    }

    public void unsubscribeFromDataStore() {
        subscriptionManager.unsubscribeAll();
    }

}

@@ -237,16 +237,40 @@
 rvable;%0A
+import rx.Subscription;%0A
 import r
@@ -327,37 +327,32 @@
 ject;%0Aimport rx.
-util.
 functions.Action
@@ -368,13 +368,8 @@
  rx.
-util.
 func
@@ -1340,32 +1340,319 @@
 ore() %7B%0A        
+subscriptionManager.add(createBookSubscription());%0A        subscriptionManager.add(createBookPriceSubscription());%0A    %7D%0A%0A    public void unsubscribeFromDataStore() %7B%0A        subscriptionManager.unsubscribeAll();%0A    %7D%0A%0A    private Subscription createBookSubscription() %7B%0A        return 
 dataStore.getBoo
@@ -2217,19 +2217,90 @@
 %7D);%0A    
-   
+%7D%0A%0A    private Subscription createBookPriceSubscription() %7B%0A        return
  dataSto
@@ -2627,105 +2627,6 @@
   %7D%0A
-%0A    public void unsubscribeFromDataStore() %7B%0A        subscriptionManager.unsubscribeAll();%0A    %7D%0A%0A
 %7D%0A

Make view model unsubscribe properly