<commit_before>package com.crowdshelf.app;

import android.util.Log;

import com.crowdshelf.app.io.DbEvent;
import com.crowdshelf.app.io.DbEventType;
import com.crowdshelf.app.io.network.GetBookInfoAsyncTask;
import com.crowdshelf.app.io.network.NetworkController;
import com.crowdshelf.app.models.Book;
import com.crowdshelf.app.models.BookInfo;
import com.crowdshelf.app.models.Crowd;
import com.crowdshelf.app.models.MemberId;
import com.crowdshelf.app.models.User;
import com.crowdshelf.app.ui.activities.MainTabbedActivity;
import com.squareup.otto.Subscribe;

import java.util.List;

import io.realm.Realm;
import io.realm.RealmList;

/**
 * Created by Torstein on 02.09.2015.
 */
public class MainController {
    private static Realm realm;
    private static final String TAG = "MainController";

    /*
    Must be called on application startup
     */
    public void onCreate() {
        MainTabbedActivity.getBus().register(this);
        realm = Realm.getDefaultInstance();
    }

    @Subscribe
    public void dbEventListener(DbEvent dbEvent) {
        switch (dbEvent.getDbEventType()) {
            case ON_START_USER_CROWDS_READY:
                /*
                When all the crowds for the main user is retrieved,
                retrieve all the books of the members of these crowds
                (not including the books of the main user)
                 */
                MainTabbedActivity.getBus().post(new DbEvent(DbEventType.USER_CROWDS_CHANGED, "all"));
                List<Crowd> crowds = realm.where(Crowd.class)
                        .findAll();
                for (Crowd c : crowds) {
                    for (MemberId memberId : c.getMembers()) {
                        if (!memberId.getId().equals(MainTabbedActivity.getMainUserId())) {
                            getBooks(memberId.getId(), DbEventType.USER_CROWD_BOOKS_CHANGED);
                        }
                    }
                }
                break;
        }
    }

    /*
    Users
     */

    public static void login(String username, String password, DbEventType dbEventType) {
        NetworkController.login(username, password, dbEventType);

    }

    public static void createUser(String username, String name, String email, String password, DbEventType dbEventType) {
        // This user is never stored in the database. It is sent to the server,
        // then retrieved to be stored with the correct _id
        NetworkController.createUser(username, name, email, password, dbEventType);
    }

    public static void getUser(String userId, DbEventType dbEventType) {
        NetworkController.getUser(userId, dbEventType);
    }

    public static void getUserByUsername(String userName, DbEventType dbEventType) {
        NetworkController.getUserByUsername(userName, dbEventType);
    }

    /*
    Crowds
     */

    public static void createCrowd(String name, String ownerId, List<String> members, DbEventType dbEventType){
        // This crowd is never stored in the database. It is sent to the server,
        // then retrieved to be stored with the correct _id
        Crowd crowd = new Crowd();
        crowd.setName(name);
        crowd.setOwner(ownerId);
        RealmList<MemberId> memberIds = new RealmList<MemberId>();
        for (String id: members) {
            MemberId memberId = new MemberId();
            memberId.setId(id);
            memberIds.add(memberId);
        }
        crowd.setMembers(memberIds);
        NetworkController.createCrowd(crowd, dbEventType);
    }

    public static void deleteCrowd(String crowdId, DbEventType dbEventType) {
        realm.beginTransaction();
        Crowd crowd = realm.where(Crowd.class)
                .equalTo("id", crowdId)
                .findFirst();
        crowd.removeFromRealm();
        realm.commitTransaction();
        NetworkController.deleteCrowd(crowdId, dbEventType);
        MainTabbedActivity.getBus().post(new DbEvent(dbEventType, crowdId));
    }

    public static void getCrowd(String crowdId, DbEventType dbEventType) {
        NetworkController.getCrowd(crowdId, dbEventType);
    }

    public static void addCrowdMember(String crowdId, String userId, DbEventType dbEventType){
        NetworkController.addCrowdMember(crowdId, userId, dbEventType);
    }

    public static void removeCrowdMember(String crowdId, String userId, DbEventType dbEventType){
        NetworkController.removeCrowdMember(crowdId, userId, dbEventType);
    }

    /*
    Get all the crowds which the given userId is a member of
     */
    public static void getCrowdsByMember(String userId, DbEventType dbEventType) {
        NetworkController.getCrowdsByMember(userId, dbEventType);
    }

    /*
    Books
     */

    public static void createBook(Book book, DbEventType dbEventType) {
        // This book is never stored in the database. It is sent to the server,
        // then retrieved to be stored with the correct _id
        NetworkController.createBook(book, dbEventType);
        MainTabbedActivity.getMixpanel().track("BookAdded");
    }
<commit_msg>removeCrowdMember now updates realm db<commit_after>package com.crowdshelf.app;

import android.util.Log;

import com.crowdshelf.app.io.DbEvent;
import com.crowdshelf.app.io.DbEventType;
import com.crowdshelf.app.io.network.GetBookInfoAsyncTask;
import com.crowdshelf.app.io.network.NetworkController;
import com.crowdshelf.app.models.Book;
import com.crowdshelf.app.models.BookInfo;
import com.crowdshelf.app.models.Crowd;
import com.crowdshelf.app.models.MemberId;
import com.crowdshelf.app.models.User;
import com.crowdshelf.app.ui.activities.MainTabbedActivity;
import com.squareup.otto.Subscribe;

import java.util.List;

import io.realm.Realm;
import io.realm.RealmList;

/**
 * Created by Torstein on 02.09.2015.
 */
public class MainController {
    private static Realm realm;
    private static final String TAG = "MainController";

    /*
    Must be called on application startup
     */
    public void onCreate() {
        MainTabbedActivity.getBus().register(this);
        realm = Realm.getDefaultInstance();
    }

    @Subscribe
    public void dbEventListener(DbEvent dbEvent) {
        switch (dbEvent.getDbEventType()) {
            case ON_START_USER_CROWDS_READY:
                /*
                When all the crowds for the main user is retrieved,
                retrieve all the books of the members of these crowds
                (not including the books of the main user)
                 */
                MainTabbedActivity.getBus().post(new DbEvent(DbEventType.USER_CROWDS_CHANGED, "all"));
                List<Crowd> crowds = realm.where(Crowd.class)
                        .findAll();
                for (Crowd c : crowds) {
                    for (MemberId memberId : c.getMembers()) {
                        if (!memberId.getId().equals(MainTabbedActivity.getMainUserId())) {
                            getBooks(memberId.getId(), DbEventType.USER_CROWD_BOOKS_CHANGED);
                        }
                    }
                }
                break;
        }
    }

    /*
    Users
     */

    public static void login(String username, String password, DbEventType dbEventType) {
        NetworkController.login(username, password, dbEventType);

    }

    public static void createUser(String username, String name, String email, String password, DbEventType dbEventType) {
        // This user is never stored in the database. It is sent to the server,
        // then retrieved to be stored with the correct _id
        NetworkController.createUser(username, name, email, password, dbEventType);
    }

    public static void getUser(String userId, DbEventType dbEventType) {
        NetworkController.getUser(userId, dbEventType);
    }

    public static void getUserByUsername(String userName, DbEventType dbEventType) {
        NetworkController.getUserByUsername(userName, dbEventType);
    }

    /*
    Crowds
     */

    public static void createCrowd(String name, String ownerId, List<String> members, DbEventType dbEventType){
        // This crowd is never stored in the database. It is sent to the server,
        // then retrieved to be stored with the correct _id
        Crowd crowd = new Crowd();
        crowd.setName(name);
        crowd.setOwner(ownerId);
        RealmList<MemberId> memberIds = new RealmList<MemberId>();
        for (String id: members) {
            MemberId memberId = new MemberId();
            memberId.setId(id);
            memberIds.add(memberId);
        }
        crowd.setMembers(memberIds);
        NetworkController.createCrowd(crowd, dbEventType);
    }

    public static void deleteCrowd(String crowdId, DbEventType dbEventType) {
        realm.beginTransaction();
        Crowd crowd = realm.where(Crowd.class)
                .equalTo("id", crowdId)
                .findFirst();
        crowd.removeFromRealm();
        realm.commitTransaction();
        NetworkController.deleteCrowd(crowdId, dbEventType);
        MainTabbedActivity.getBus().post(new DbEvent(dbEventType, crowdId));
    }

    public static void getCrowd(String crowdId, DbEventType dbEventType) {
        NetworkController.getCrowd(crowdId, dbEventType);
    }

    public static void addCrowdMember(String crowdId, String userId, DbEventType dbEventType){
        NetworkController.addCrowdMember(crowdId, userId, dbEventType);
    }

    public static void removeCrowdMember(String crowdId, String userId, DbEventType dbEventType){
        realm.beginTransaction();
        Crowd crowd = realm.where(Crowd.class)
                .equalTo("id", crowdId)
                .findFirst();
        for (MemberId memderId: crowd.getMembers()) {
            if (memderId.getId().equals(userId)) {
                memderId.removeFromRealm();
            }
        }
        realm.commitTransaction();
        NetworkController.removeCrowdMember(crowdId, userId, dbEventType);
        MainTabbedActivity.getBus().post(new DbEvent(dbEventType, userId));
    }

    /*
    Get all the crowds which the given userId is a member of
     */
    public static void getCrowdsByMember(String userId, DbEventType dbEventType) {
        NetworkController.getCrowdsByMember(userId, dbEventType);
    }

    /*
    Books
     */

    public static void createBook(Book book, DbEventType dbEventType) {
        // This book is never stored in the database. It is sent to the server,
        // then retrieved to be stored with the correct _id
        NetworkController.createBook(book, dbEventType);
        MainTabbedActivity.getMixpanel().track("BookAdded");
    }

{'fraction_non_alphanumeric': 0.0875919290967377, 'fraction_numerical': 0.0015085800490288515, 'mean_word_length': 2.55700871898055, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '252918', 'n_tokens_mistral': 3088, 'n_tokens_neox': 2963, 'n_words': 738}