<commit_before>'use strict';

import React, {
  AsyncStorage,
  Component,
  Dimensions,
  Image,
  RefreshControl,
  ScrollView,
  StyleSheet,
  Text,
  TouchableOpacity,
  View,
} from 'react-native';

import Firebase from 'firebase';
import Icon from 'react-native-vector-icons/MaterialIcons';

import FriendList from './friend-list';
import FollowersList from './followers-list';
import GridView from '../components/grid-view';
import Post from '../components/post';
import TimeStamp from '../util/time-stamp';
import TitleBar from '../components/title-bar';

let database = new Firebase("poopapp1.firebaseio.com");

const windowSize = Dimensions.get('window');

class Profile extends Component {
  constructor(props) {
    super(props);
    this.state = {
      items: [],
      name: '',
      profilePic: 'default',
      numberFriends: 0,
      followers: 0,
    };
  }

  componentDidMount() {
    this.queryData();
    var loggedUserId;
    var self = this;

    AsyncStorage.getItem('user_data', (error, result) => {
      loggedUserId = JSON.parse(result).uid;
      var numFollows;
      var numFriends;
      database.child("users/").once("value", function(snapshot) {
        var isFollowing;
        var followingList = snapshot.child(loggedUserId + "/followingList");
        numFollows = snapshot.child(self.props.state + "/followers");
        numFriends = snapshot.child(self.props.state + "/friends");
        if(typeof followingList != 'undefined') {
          followingList.forEach(function(following) {
            if(following.val().userId == self.props.state) {
              isFollowing = true;
            }
          });
        }

        var isFriends = false;
        var friendsList = snapshot.child(loggedUserId + "/friendsList");
        if(typeof friendsList != 'undefined') {
          friendsList.forEach(function(friends) {
            if(friends.val().userId == self.props.state) {
              isFriends = true;
            }
          });
        }
        var youRequested = false;
        var theyRequested = false;
        if(!isFriends) {
          var yourRequests = snapshot.child(loggedUserId + "/friendRequests");
          if(typeof yourRequests != 'undefined') {
            yourRequests.forEach(function(request) {
              if(request.val().userId == self.props.state) {
                theyRequested = true;
              }
            });
          }
          if(!theyRequested) {
            var theirRequests = snapshot.child(self.props.state + "/friendRequests");
            if(typeof theirRequests != 'undefined') {
              theirRequests.forEach(function(request) {
                if(request.val().userId == loggedUserId) {
                  youRequested = true;
                }
              });
            }
          }
        }

        self.setState({
          loggedUser: loggedUserId,
          following: isFollowing,
          followers: numFollows.val(),
          numberFriends: numFriends.val(),
          friends: isFriends,
          yourRequest: youRequested,
          theirRequest: theyRequested,
        });
      });
    });

    database.child("users/").on("value", function(snapshot) {
      var isFollowing;
      var followingList = snapshot.child(loggedUserId + "/followingList");

      if(typeof followingList != 'undefined') {
        followingList.forEach(function(following) {
          if(following.val().userId == self.props.state) {
            isFollowing = true;
          }
        });
      }

      var isFriends = false;
      var friendsList = snapshot.child(loggedUserId + "/friendsList");
      if(typeof friendsList != 'undefined') {
        friendsList.forEach(function(friends) {
          if(friends.val().userId == self.props.state) {
            isFriends = true;
          }
        });
      }

      var youRequested = false;
      var theyRequested = false;
      if(!isFriends) {
        var yourRequests = snapshot.child(loggedUserId + "/friendRequests");
        if(typeof yourRequests != 'undefined') {
          yourRequests.forEach(function(request) {
            if(request.val().userId == self.props.state) {
              theyRequested = true;
            }
          });
        }
        if(!theyRequested) {
          var theirRequests = snapshot.child(self.props.state + "/friendRequests");
          if(typeof theirRequests != 'undefined') {
            theirRequests.forEach(function(request) {
              if(request.val().userId == loggedUserId) {
                youRequested = true;
              }
            });
          }
        }
      }

      var numFollows = snapshot.child(self.props.state + "/followers");
      var numFriends = snapshot.child(self.props.state + "/friends");

      self.setState({
        following: isFollowing,
        followers: numFollows.val(),
        numberFriends: numFriends.val(),
        friends: isFriends,
        yourRequest: youRequested,
        theirRequest: theyRequested,
      });
    });
  }

  queryData() {
    var myBlob = [];
    var self = this;

    database.once("value", function(snapshot) {
      // user
      var usersnapshot = snapshot.child("users/" + self.props.state);
      var proPic = usersnapshot.val().profilePic.uri;

      // posts
      var userPostsSnapshot = usersnapshot.child("postList");
      userPostsSnapshot.forEach(function(userPostSnapshot) {
        var postSnapshot = snapshot.child("posts/" + userPostSnapshot.val().postId);
        myBlob.push(postSnapshot);
      });

      self.setState({
        items: myBlob,
        name: usersnapshot.val().firstName + " " + usersnapshot.val().lastName,
        profilePic: proPic,
      });
    });
  }

  renderRow(post) {
    return (
      <Post
        id = {post}
        navigator = {this.props.navigator}<commit_msg>Added sorting by date on profile.<commit_after>'use strict';

import React, {
  AsyncStorage,
  Component,
  Dimensions,
  Image,
  RefreshControl,
  ScrollView,
  StyleSheet,
  Text,
  TouchableOpacity,
  View,
} from 'react-native';

import Firebase from 'firebase';
import Icon from 'react-native-vector-icons/MaterialIcons';

import FriendList from './friend-list';
import FollowersList from './followers-list';
import GridView from '../components/grid-view';
import Post from '../components/post';
import TimeStamp from '../util/time-stamp';
import TitleBar from '../components/title-bar';

let database = new Firebase("poopapp1.firebaseio.com");

const windowSize = Dimensions.get('window');

class Profile extends Component {
  constructor(props) {
    super(props);
    this.state = {
      items: [],
      name: '',
      profilePic: 'default',
      numberFriends: 0,
      followers: 0,
    };
  }

  componentDidMount() {
    this.queryData();
    var loggedUserId;
    var self = this;

    AsyncStorage.getItem('user_data', (error, result) => {
      loggedUserId = JSON.parse(result).uid;
      var numFollows;
      var numFriends;
      database.child("users/").once("value", function(snapshot) {
        var isFollowing;
        var followingList = snapshot.child(loggedUserId + "/followingList");
        numFollows = snapshot.child(self.props.state + "/followers");
        numFriends = snapshot.child(self.props.state + "/friends");
        if(typeof followingList != 'undefined') {
          followingList.forEach(function(following) {
            if(following.val().userId == self.props.state) {
              isFollowing = true;
            }
          });
        }

        var isFriends = false;
        var friendsList = snapshot.child(loggedUserId + "/friendsList");
        if(typeof friendsList != 'undefined') {
          friendsList.forEach(function(friends) {
            if(friends.val().userId == self.props.state) {
              isFriends = true;
            }
          });
        }
        var youRequested = false;
        var theyRequested = false;
        if(!isFriends) {
          var yourRequests = snapshot.child(loggedUserId + "/friendRequests");
          if(typeof yourRequests != 'undefined') {
            yourRequests.forEach(function(request) {
              if(request.val().userId == self.props.state) {
                theyRequested = true;
              }
            });
          }
          if(!theyRequested) {
            var theirRequests = snapshot.child(self.props.state + "/friendRequests");
            if(typeof theirRequests != 'undefined') {
              theirRequests.forEach(function(request) {
                if(request.val().userId == loggedUserId) {
                  youRequested = true;
                }
              });
            }
          }
        }

        self.setState({
          loggedUser: loggedUserId,
          following: isFollowing,
          followers: numFollows.val(),
          numberFriends: numFriends.val(),
          friends: isFriends,
          yourRequest: youRequested,
          theirRequest: theyRequested,
        });
      });
    });

    database.child("users/").on("value", function(snapshot) {
      var isFollowing;
      var followingList = snapshot.child(loggedUserId + "/followingList");

      if(typeof followingList != 'undefined') {
        followingList.forEach(function(following) {
          if(following.val().userId == self.props.state) {
            isFollowing = true;
          }
        });
      }

      var isFriends = false;
      var friendsList = snapshot.child(loggedUserId + "/friendsList");
      if(typeof friendsList != 'undefined') {
        friendsList.forEach(function(friends) {
          if(friends.val().userId == self.props.state) {
            isFriends = true;
          }
        });
      }

      var youRequested = false;
      var theyRequested = false;
      if(!isFriends) {
        var yourRequests = snapshot.child(loggedUserId + "/friendRequests");
        if(typeof yourRequests != 'undefined') {
          yourRequests.forEach(function(request) {
            if(request.val().userId == self.props.state) {
              theyRequested = true;
            }
          });
        }
        if(!theyRequested) {
          var theirRequests = snapshot.child(self.props.state + "/friendRequests");
          if(typeof theirRequests != 'undefined') {
            theirRequests.forEach(function(request) {
              if(request.val().userId == loggedUserId) {
                youRequested = true;
              }
            });
          }
        }
      }

      var numFollows = snapshot.child(self.props.state + "/followers");
      var numFriends = snapshot.child(self.props.state + "/friends");

      self.setState({
        following: isFollowing,
        followers: numFollows.val(),
        numberFriends: numFriends.val(),
        friends: isFriends,
        yourRequest: youRequested,
        theirRequest: theyRequested,
      });
    });
  }

  queryData() {
    var myBlob = [];
    var self = this;

    database.once("value", function(snapshot) {
      // user
      var usersnapshot = snapshot.child("users/" + self.props.state);
      var proPic = usersnapshot.val().profilePic.uri;

      // posts
      var userPostsSnapshot = usersnapshot.child("postList");
      userPostsSnapshot.forEach(function(userPostSnapshot) {
        var postSnapshot = snapshot.child("posts/" + userPostSnapshot.val().postId);
        myBlob.push(postSnapshot);
      });
      
      myBlob.sort((a, b) => {
        return b.val().date - a.val().date;
      });

      self.setState({
        items: myBlob,
        name: usersnapshot.val().firstName + " " + usersnapshot.val().lastName,
        profilePic: proPic,
      });
    });
  }

  renderRow(post) {
    return (
      <Post
        id = {post}
        navigator = {this.props.navigator}
{'fraction_non_alphanumeric': 0.12799726939158632, 'fraction_numerical': 0.0005119890775663453, 'mean_word_length': 2.083399105498553, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '5248246', 'n_tokens_mistral': 3256, 'n_tokens_neox': 3245, 'n_words': 599}