/**
   * This function sets the view data. Any changes between the old and the new data is applied as a
   * diff in an efficient way.
   */
  private synchronized void setData(Chat chat) {
    if (!viewCreated.get()) {
      return;
    }

    messageAdapter.setNotifyOnChange(false);
    messageAdapter.clear();
    messageAdapter.addAll(chat.messages);
    messageAdapter.notifyDataSetChanged();
    setSelection(messageAdapter.getCount() - 1);
    Log.v(TAG, "updated view with: " + messageAdapter.getCount() + " messages");

    // todo: implement in place updates as below as an optimization
    //        boolean notifyDataSetChanged = false;
    //
    //        // apply changes to existing messages in case any of them was changed
    //        // diffing here is easy as we're always using immutable objects, we can just compare
    // references to get changed ones
    //        for (int i = 0; i < messages.size(); i++) {
    //            if (messages.get(i) != chat.messages.get(i)) {
    //                // update the check mark on the updated item only as per:
    //                //
    // http://stackoverflow.com/questions/3724874/how-can-i-update-a-single-row-in-a-listview
    //                View v = messageListView.getChildAt(location -
    // messageListView.getFirstVisiblePosition());
    //                if (v != null) {
    //                    if (msg.status == Message.Status.SENT_TO_SERVER) {
    //                        ((TextView)v.findViewById(R.id.check)).setText("✓");
    //                    }
    //                    v.findViewById(R.id.check).setVisibility(View.VISIBLE);
    //                }
    //
    //            }
    //        }
  }
 @Override
 public void onViewCreated(View view, Bundle savedInstanceState) {
   super.onViewCreated(view, savedInstanceState);
   viewCreated.set(true);
   setSelection(messageAdapter.getCount() - 1);
 }