<commit_before>var utils = require('../../utils');
var helper = require('../helper');
var widgets = require('../widget/common');
var layout = require('../layout');
var menu = require('../menu');
var i18n = require('../../i18n');
var xhr = require('../../xhr');
var session = require('../../session');
var socket = require('../../socket');

var onlineFriends = [];

function renderBody() {
  return [
    m('ul.friends_list.general.scroller', {}, onlineFriends.map(function(f) {
      return m('li.list_item', {}, f);
    }))
  ];
}

module.exports = {
  controller: function() {
	  
    var friendsSocket;

    helper.analyticsTrackView('Online Friends');

    var toastName = function(name) {
      window.plugins.toast.show(name, 'short', 'center');
    }

    var requestFriends = function() {
      friendsSocket.send('following_onlines');
    }

    xhr.friends().then(function(data) {
      friendsSocket = socket.connectFriends('v1', requestFriends, {
        following_onlines: function(data) {
          // TODO - bind this to a scroller so the list updates
          onlineFriends = data;
        },
        following_enters: function(name) {
          onlineFriends.push(name);
        },
        following_leaves: function(name) {
          var nameIndex = onlineFriends.indexOf(name);
          if (nameIndex !== -1) {
            onlineFriends.splice(nameIndex, 1);
          }
        }
      });
    });

    this.onunload = function() {
      if (friendsSocket) {
        friendsSocket.destroy();
        friendsSocket = null;
      }
    }.bind(this);

  },

  view: function() {
    var header = utils.partialf(widgets.header, null,
      widgets.backButton(i18n('onlineFriends'))
    );<commit_msg>#20 added scroller and view refresh<commit_after>var utils = require('../../utils');
var helper = require('../helper');
var widgets = require('../widget/common');
var layout = require('../layout');
var menu = require('../menu');
var i18n = require('../../i18n');
var xhr = require('../../xhr');
var socket = require('../../socket');
var iScroll = require('iscroll');

var onlineFriends = [];

// scroll testing
// var onlineFriends = [
//   "friend1",
//   "friend2",
//   "friend3",
//   "friend4",
//   "friend5",
//   "friend6",
//   "friend7",
//   "friend8",
//   "friend9",
//   "friend10",
//   "friend11",
//   "friend12",
//   "friend13",
//   "friend14",
//   "friend15",
//   "friend16",
// ];

function renderBody() {
  return m('ul.friends_list', {
    config: function(el, isUpdate, context) {
      if (!isUpdate) {
        context.scroller = new iScroll(el);
        context.onunload = function() {
          if (context.croller) {
            context.scroller.destroy();
            context.scroller = null;
          }
        };
      }
      context.scroller.refresh();
    }
  }, m('div.scroller', {}, onlineFriends.map(function(f) {
    return m('li.list_item', {}, f);
  })));
}

module.exports = {
  controller: function() {
	  
    var friendsSocket;

    this.scroller = null;

    helper.analyticsTrackView('Online Friends');

    var requestFriends = function() {
      friendsSocket.send('following_onlines');
    }

    var refreshList = function() {
      // TODO sort the onlineFriends array

      // update view
      m.redraw();
    }

    xhr.friends().then(function(data) {
      friendsSocket = socket.connectFriends('v1', requestFriends, {
        following_onlines: function(data) {
          onlineFriends = data;
          refreshList();
        },
        following_enters: function(name) {
          onlineFriends.push(name);
          refreshList();
        },
        following_leaves: function(name) {
          var nameIndex = onlineFriends.indexOf(name);
          if (nameIndex !== -1) {
            onlineFriends.splice(nameIndex, 1);
            refreshList();
          }
        }
      });
    });

    this.onunload = function() {
      if (friendsSocket) {
        friendsSocket.destroy();
        friendsSocket = null;
      }
    }.bind(this);

  },

  view: function() {
    var header = utils.partialf(widgets.header, null,
      widgets.backButton(i18n('onlineFriends'))
    );
{'fraction_non_alphanumeric': 0.1715179215780611, 'fraction_numerical': 0.010343998075535241, 'mean_word_length': 2.3210862619808306, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4493409', 'n_tokens_mistral': 1300, 'n_tokens_neox': 1252, 'n_words': 243}