<commit_before>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();
    }

}<commit_msg>Make view model unsubscribe properly<commit_after>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.Subscription;
import rx.subjects.BehaviorSubject;
import rx.subjects.Subject;
import rx.functions.Action1;
import rx.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() {
        subscriptionManager.add(createBookSubscription());
        subscriptionManager.add(createBookPriceSubscription());
    }

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

    private Subscription createBookSubscription() {
        return 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);
                    }
                });
    }

    private Subscription createBookPriceSubscription() {
        return dataStore.getBookPrice(bookId)
                .map(new rx.functions.Func1<Integer, String>() {
                    @Override
                    public String call(Integer integer) {
                        return "Price: " + integer + " EUR";
                    }
                })
                .subscribe(bookPrice);
    }
}
{'fraction_non_alphanumeric': 0.09238057639163048, 'fraction_numerical': 0.0043426766679826295, 'mean_word_length': 2.1928166351606806, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3835661', 'n_tokens_mistral': 1337, 'n_tokens_neox': 1272, 'n_words': 280}