<commit_before>package io.explod.testable.data;


import android.support.annotation.NonNull;
import android.support.v4.util.Pair;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.explod.testable.data.local.AppDatabase;
import io.explod.testable.data.local.model.Repository;
import io.explod.testable.data.local.model.User;
import io.explod.testable.data.remote.GithubService;
import io.explod.testable.data.remote.model.UserRepositoryResponse;
import io.reactivex.Observable;
import io.reactivex.Single;

import static io.explod.testable.module.ObjectGraph.getInjector;

public class AppRepo {

	@Inject
	GithubService mGithubService;

	@Inject
	AppDatabase mAppDatabase;

	public AppRepo() {
		getInjector().inject(this);
	}

	@NonNull
	public OfflineFirstObservable<Pair<User, List<Repository>>> getRepositories(@NonNull String username) {
		return new GetRepositoriesByUsername().getOfflineFirstObservable(username);
	}

	private class GetRepositoriesByUsername {

		@NonNull
		OfflineFirstObservable<Pair<User, List<Repository>>> getOfflineFirstObservable(@NonNull String username) {
			return OfflineFirstObservable.from(
				getRepositoriesOffline(username), getRepositoriesOnline(username)
			);
		}

		@NonNull
		private Single<Pair<User, List<Repository>>> getRepositoriesOffline(@NonNull String username) {
			return mAppDatabase.users()
				.getOrCreate(username)
				.flatMap(user -> mAppDatabase.repositories()
					.getAllForUser(user.getId())
					.map(repos -> Pair.create(user, repos))
				);
		}

		@NonNull
		private Single<Pair<User, List<Repository>>> getRepositoriesOnline(@NonNull String username) {
			Single<User> userCreate = mAppDatabase.users().getOrCreate(username);
			Single<List<UserRepositoryResponse>> reposFetch = mGithubService.getUserRepos(username);

			return Single.zip(userCreate, reposFetch, Pair::create)
				.flatMapObservable(userRepos -> saveRepositories(userRepos.first, userRepos.second))
				.collect(
					// collect the results in a mutable pair
					// the pair needs to be mutable so that we can save the user when the collector
					// knows who it is
					UserRepos::new,
					(results, next) -> {
						// save the user because we now know who it is
						results.user = next.first;
						// add the nest repo to the list of repos
						results.repos.add(next.second);
					}
				)
				.map(userRepos -> Pair.create(userRepos.user, userRepos.repos));
		}

		@NonNull
		private Observable<Pair<User, Repository>> saveRepositories(@NonNull User user, @NonNull List<UserRepositoryResponse> repos) {<commit_msg>[repo] fix typo in repo comment<commit_after>package io.explod.testable.data;


import android.support.annotation.NonNull;
import android.support.v4.util.Pair;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.explod.testable.data.local.AppDatabase;
import io.explod.testable.data.local.model.Repository;
import io.explod.testable.data.local.model.User;
import io.explod.testable.data.remote.GithubService;
import io.explod.testable.data.remote.model.UserRepositoryResponse;
import io.reactivex.Observable;
import io.reactivex.Single;

import static io.explod.testable.module.ObjectGraph.getInjector;

public class AppRepo {

	@Inject
	GithubService mGithubService;

	@Inject
	AppDatabase mAppDatabase;

	public AppRepo() {
		getInjector().inject(this);
	}

	@NonNull
	public OfflineFirstObservable<Pair<User, List<Repository>>> getRepositories(@NonNull String username) {
		return new GetRepositoriesByUsername().getOfflineFirstObservable(username);
	}

	private class GetRepositoriesByUsername {

		@NonNull
		OfflineFirstObservable<Pair<User, List<Repository>>> getOfflineFirstObservable(@NonNull String username) {
			return OfflineFirstObservable.from(
				getRepositoriesOffline(username), getRepositoriesOnline(username)
			);
		}

		@NonNull
		private Single<Pair<User, List<Repository>>> getRepositoriesOffline(@NonNull String username) {
			return mAppDatabase.users()
				.getOrCreate(username)
				.flatMap(user -> mAppDatabase.repositories()
					.getAllForUser(user.getId())
					.map(repos -> Pair.create(user, repos))
				);
		}

		@NonNull
		private Single<Pair<User, List<Repository>>> getRepositoriesOnline(@NonNull String username) {
			Single<User> userCreate = mAppDatabase.users().getOrCreate(username);
			Single<List<UserRepositoryResponse>> reposFetch = mGithubService.getUserRepos(username);

			return Single.zip(userCreate, reposFetch, Pair::create)
				.flatMapObservable(userRepos -> saveRepositories(userRepos.first, userRepos.second))
				.collect(
					// collect the results in a mutable pair
					// the pair needs to be mutable so that we can save the user when the collector
					// knows who it is
					UserRepos::new,
					(results, next) -> {
						// save the user because we now know who it is
						results.user = next.first;
						// add the next repo to the list of repos
						results.repos.add(next.second);
					}
				)
				.map(userRepos -> Pair.create(userRepos.user, userRepos.repos));
		}

		@NonNull
		private Observable<Pair<User, Repository>> saveRepositories(@NonNull User user, @NonNull List<UserRepositoryResponse> repos) {
{'fraction_non_alphanumeric': 0.10669216061185469, 'fraction_numerical': 0.0003824091778202677, 'mean_word_length': 4.661255411255412, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 49, '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': '411942', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1605, 'n_words': 337}