<commit_before>package ca.ualberta.cs.xpertsapp.model;

import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ca.ualberta.cs.xpertsapp.MyApplication;
import ca.ualberta.cs.xpertsapp.interfaces.IObservable;
import ca.ualberta.cs.xpertsapp.interfaces.IObserver;
import ca.ualberta.cs.xpertsapp.model.es.SearchHit;
import ca.ualberta.cs.xpertsapp.model.es.SearchResponse;

/**
 * Manages the loaded users to allow circular references without getting stuck in a loading loop
 */
public class UserManager implements IObserver {
	private Map<String, User> users = new HashMap<String, User>();

	// Get/Set

	/**
	 * @return A List of loaded Users
	 */
	public List<User> getUsers() {
		return new ArrayList<User>(this.users.values());
	}

	/**
	 * @param user Registers this as an observer on User and adds it to the List
	 */
	private void addUser(User user) {
		user.addObserver(this);
		this.users.put(user.getEmail(), user);
	}

	/**
	 * @param email The email of the user
	 * @return The User with that email or null
	 */
	public User getUser(String email) {
		// If we have the user loaded
		if (this.users.containsKey(email)) {
			return this.users.get(email);
		}
		// TODO: Only return a user if one existed
		try {
			SearchHit<User> loadedUser = IOManager.sharedManager().fetchData(Constants.serverUserExtension() + email, new TypeToken<SearchHit<User>>() {
			});
			if (loadedUser.isFound()) {
				this.addUser(loadedUser.getSource());
				return loadedUser.getSource();
			} else {
				return null;
			}
		} catch (JsonIOException e) {
			throw new RuntimeException(e);
		} catch (JsonSyntaxException e) {
			throw new RuntimeException(e);
		} catch (IllegalStateException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * @return The User that is currently signed in or null
	 */
	public User localUser() {
		String email = MyApplication.getPreferences().getString("email", null);
		if (email == null) {
			return null;
		}
		User user = this.getUser(email);
		if (user == null) {
			user = this.registerUser(email);
		}
		return user;
	}

	/**
	 * @param email The email to register the user as
	 * @return The registered user or null if the email is taken
	 */
	public User registerUser(String email) {
		User foundUser = this.getUser(email);
		if (foundUser == null) {
			User newUser = new User(email);
			this.addUser(newUser);
			return newUser;
		}
		return null;
	}

	/**
	 * Get a list of users sorted by match relevance
	 * <p/>
	 * Uses the url [...]/_search?q='%s'
	 *
	 * @param meta What to search for
	 * @return The list of matching users with the most relevant first
	 */
	public List<User> findUsers(String meta) {
		List<SearchHit<User>> found = IOManager.sharedManager().searchData(Constants.serverUserExtension() + Constants.serverSearchExtension() + meta, new TypeToken<SearchResponse<User>>() {
		});
		List<User> users = new ArrayList<User>();
		for (SearchHit<User> user : found) {
			users.add(this.getUser(user.getSource().getEmail()));
		}
		return users;
	}
<commit_msg>Now auto-saves the email upon successful register.<commit_after>package ca.ualberta.cs.xpertsapp.model;

import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ca.ualberta.cs.xpertsapp.MyApplication;
import ca.ualberta.cs.xpertsapp.interfaces.IObservable;
import ca.ualberta.cs.xpertsapp.interfaces.IObserver;
import ca.ualberta.cs.xpertsapp.model.es.SearchHit;
import ca.ualberta.cs.xpertsapp.model.es.SearchResponse;

/**
 * Manages the loaded users to allow circular references without getting stuck in a loading loop
 */
public class UserManager implements IObserver {
	private Map<String, User> users = new HashMap<String, User>();

	// Get/Set

	/**
	 * @return A List of loaded Users
	 */
	public List<User> getUsers() {
		return new ArrayList<User>(this.users.values());
	}

	/**
	 * @param user Registers this as an observer on User and adds it to the List
	 */
	private void addUser(User user) {
		user.addObserver(this);
		this.users.put(user.getEmail(), user);
	}

	/**
	 * @param email The email of the user
	 * @return The User with that email or null
	 */
	public User getUser(String email) {
		// If we have the user loaded
		if (this.users.containsKey(email)) {
			return this.users.get(email);
		}
		// TODO: Only return a user if one existed
		try {
			SearchHit<User> loadedUser = IOManager.sharedManager().fetchData(Constants.serverUserExtension() + email, new TypeToken<SearchHit<User>>() {
			});
			if (loadedUser.isFound()) {
				this.addUser(loadedUser.getSource());
				return loadedUser.getSource();
			} else {
				return null;
			}
		} catch (JsonIOException e) {
			throw new RuntimeException(e);
		} catch (JsonSyntaxException e) {
			throw new RuntimeException(e);
		} catch (IllegalStateException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * @return The User that is currently signed in or null
	 */
	public User localUser() {
		String email = MyApplication.getPreferences().getString("email", null);
		if (email == null) {
			return null;
		}
		User user = this.getUser(email);
		if (user == null) {
			user = this.registerUser(email);
		}
		return user;
	}

	/**
	 * @param email The email to register the user as
	 * @return The registered user or null if the email is taken
	 */
	public User registerUser(String email) {
		User foundUser = this.getUser(email);
		if (foundUser == null) {
			User newUser = new User(email);
			this.addUser(newUser);
			// Save the email to the settings
			MyApplication.getPreferences().edit().putString("email", email).apply();
			return newUser;
		}
		return null;
	}

	/**
	 * Get a list of users sorted by match relevance
	 * <p/>
	 * Uses the url [...]/_search?q='%s'
	 *
	 * @param meta What to search for
	 * @return The list of matching users with the most relevant first
	 */
	public List<User> findUsers(String meta) {
		List<SearchHit<User>> found = IOManager.sharedManager().searchData(Constants.serverUserExtension() + Constants.serverSearchExtension() + meta, new TypeToken<SearchResponse<User>>() {
		});
		List<User> users = new ArrayList<User>();
		for (SearchHit<User> user : found) {
			users.add(this.getUser(user.getSource().getEmail()));
		}
		return users;
	}

{'fraction_non_alphanumeric': 0.12511471397981033, 'fraction_numerical': 0.0, 'mean_word_length': 3.8508902077151337, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, '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': '6274518', 'n_tokens_mistral': 2228, 'n_tokens_neox': 2043, 'n_words': 578}