<commit_before>package com.cloudycrew.cloudycar.summarycontainer;

import com.cloudycrew.cloudycar.ViewController;
import com.cloudycrew.cloudycar.controllers.UserController;
import com.cloudycrew.cloudycar.models.requests.CompletedRequest;
import com.cloudycrew.cloudycar.models.requests.PendingRequest;
import com.cloudycrew.cloudycar.models.requests.Request;
import com.cloudycrew.cloudycar.observables.IObserver;
import com.cloudycrew.cloudycar.requeststorage.IRequestStore;

import java.util.Date;

import rx.Observable;

/**
 * Created by George on 2016-11-23.
 */

public class SummaryMenuController extends ViewController<ISummaryMenuView> {
    private IRequestStore requestStore;
    private UserController userController;

    public SummaryMenuController(UserController userController, IRequestStore requestStore) {
        this.userController = userController;
        this.requestStore = requestStore;
    }

    @Override
    public void attachView(ISummaryMenuView view) {
        super.attachView(view);
        requestStore.addObserver(requestStoreObserver);
    }

    @Override
    public void detachView() {
        super.detachView();
        requestStore.removeObserver(requestStoreObserver);
    }

    private IObserver<IRequestStore> requestStoreObserver = store -> {
        dispatchDisplayUnreadRiderRequests(getTotalUnreadRiderRequests());
        dispatchDisplayUnreadDriverRequests(getTotalUnreadDriverRequests());
    };

    private int getTotalUnreadRiderRequests() {
        return Observable.from(requestStore.getRequests())
                         .filter(r -> r.getRider().equals(getCurrentUsername()))
                         .filter(r -> !isRequestRead(r))
                         .count()
                         .toBlocking()
                         .first();
    }

    private int getTotalUnreadDriverRequests() {
        return Observable.from(requestStore.getRequests())
                         .filter(r -> isCurrentUserADriverForRequest(r))
                         .filter(r -> !isRequestRead(r))
                         .count()
                         .toBlocking()
                         .first();
    }

    private boolean isCurrentUserADriverForRequest(Request request) {
        if (request instanceof PendingRequest) {
            PendingRequest pendingRequest = (PendingRequest) request;
            return pendingRequest.hasBeenAcceptedBy(getCurrentUsername());
        } else if (request instanceof CompletedRequest) {
            CompletedRequest completedRequest = (CompletedRequest) request;
            return completedRequest.getDriverUsername().equals(getCurrentUsername());
        }
        return false;
    }

    private boolean isRequestRead(Request request) {
        Date lastUserReadTime = userController.getLastReadTime(request.getId());

        return lastUserReadTime == null ||
                request.getLastUpdated().compareTo(lastUserReadTime) > 0;
    }

    private String getCurrentUsername() {
        return userController.getCurrentUser().getUsername();
    }

    private void dispatchDisplayUnreadRiderRequests(int numUnread) {
        if (isViewAttached()) {
            getView().displayTotalUnreadRiderRequests(numUnread);
        }
    }

    private void dispatchDisplayUnreadDriverRequests(int numUnread) {
        if (isViewAttached()) {
            getView().displayTotalUnreadDriverRequests(numUnread);
        }
    }
}<commit_msg>Address issues in the SummaryMenuController<commit_after>package com.cloudycrew.cloudycar.summarycontainer;

import com.cloudycrew.cloudycar.ViewController;
import com.cloudycrew.cloudycar.controllers.UserController;
import com.cloudycrew.cloudycar.models.requests.CompletedRequest;
import com.cloudycrew.cloudycar.models.requests.ConfirmedRequest;
import com.cloudycrew.cloudycar.models.requests.Request;
import com.cloudycrew.cloudycar.observables.IObserver;
import com.cloudycrew.cloudycar.requeststorage.IRequestStore;

import java.util.Date;

import rx.Observable;

/**
 * Created by George on 2016-11-23.
 */

public class SummaryMenuController extends ViewController<ISummaryMenuView> {
    private IRequestStore requestStore;
    private UserController userController;

    public SummaryMenuController(UserController userController, IRequestStore requestStore) {
        this.userController = userController;
        this.requestStore = requestStore;
    }

    @Override
    public void attachView(ISummaryMenuView view) {
        super.attachView(view);
        requestStore.addObserver(requestStoreObserver);
    }

    @Override
    public void detachView() {
        super.detachView();
        requestStore.removeObserver(requestStoreObserver);
    }

    private IObserver<IRequestStore> requestStoreObserver = store -> {
        dispatchDisplayUnreadRiderRequests(getTotalUnreadRiderRequests());
        dispatchDisplayUnreadDriverRequests(getTotalUnreadDriverRequests());
    };

    private int getTotalUnreadRiderRequests() {
        return Observable.from(requestStore.getRequests())
                         .filter(r -> r.getRider().equals(getCurrentUsername()))
                         .filter(r -> !isRequestRead(r))
                         .count()
                         .toBlocking()
                         .first();
    }

    private int getTotalUnreadDriverRequests() {
        return Observable.from(requestStore.getRequests())
                         .filter(r -> shouldDriverRequestBeNotified(r))
                         .filter(r -> !isRequestRead(r))
                         .count()
                         .toBlocking()
                         .first();
    }

    private boolean shouldDriverRequestBeNotified(Request request) {
        if (request instanceof ConfirmedRequest) {
            ConfirmedRequest confirmedRequest = (ConfirmedRequest) request;
            return confirmedRequest.getDriverUsername().equals(getCurrentUsername());
        }
        return false;
    }

    private boolean isRequestRead(Request request) {
        Date lastUserReadTime = userController.getLastReadTime(request.getId());

        return lastUserReadTime != null &&
                lastUserReadTime.compareTo(request.getLastUpdated()) > 0;
    }

    private String getCurrentUsername() {
        return userController.getCurrentUser().getUsername();
    }

    private void dispatchDisplayUnreadRiderRequests(int numUnread) {
        if (isViewAttached()) {
            getView().displayTotalUnreadRiderRequests(numUnread);
        }
    }

    private void dispatchDisplayUnreadDriverRequests(int numUnread) {
        if (isViewAttached()) {
            getView().displayTotalUnreadDriverRequests(numUnread);
        }
    }
}
{'fraction_non_alphanumeric': 0.08669745270370922, 'fraction_numerical': 0.0026813645166095636, 'mean_word_length': 2.81044267877412, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '2659032', 'n_tokens_mistral': 1733, 'n_tokens_neox': 1701, 'n_words': 285}