Android Open Source - ChitChat Chats Fragment






From Project

Back to project page ChitChat.

License

The source code is released under:

MIT License

If you think the Android project ChitChat listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.shivamb7.chitchat.fragments;
//from ww  w . j a  v a2 s . c om
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.shivamb7.chitchat.ImageMessageActivity;
import com.shivamb7.chitchat.R;
import com.shivamb7.chitchat.adapters.ChatsAdapter;
import com.shivamb7.chitchat.workers.Constants;

public class ChatsFragment extends Fragment {

  ParseUser currentUser = ParseUser.getCurrentUser();
  List<ParseObject> messages;
  ListView mMessageList;
  TextView mEmptyText;
  ImageView mEmptyImage;
  SwipeRefreshLayout mSwipeRefreshLayout;
  ChatsAdapter mAdapter;
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    getActivity().setProgressBarIndeterminateVisibility(false);
    View rootView = inflater.inflate(R.layout.fragment_chats, container,
        false);
    mSwipeRefreshLayout = (SwipeRefreshLayout)rootView.findViewById(R.id.swipeRefresh);
    mSwipeRefreshLayout.setColorScheme(R.color.orange_200, R.color.orange_400, R.color.orange_600, R.color.orange_800);
    mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
      
      @Override
      public void onRefresh() {
        // TODO Auto-generated method stub
        retrieveMessages();
      }
    });
    mMessageList = (ListView) rootView.findViewById(R.id.messages_list);
    mEmptyText = (TextView) rootView.findViewById(R.id.empty_state_text);
    mEmptyImage = (ImageView) rootView.findViewById(R.id.empty_state_image);
    mMessageList.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
        // TODO Auto-generated method stub
        ParseObject msg = messages.get(position);
        String msgType = msg.getString(Constants.FILE_TYPE);
        ParseFile file = msg.getParseFile(Constants.FILE);
        Uri fileUri = Uri.parse(file.getUrl());
        if(msgType.equals(Constants.TYPE_PICTURE))
        {
          Intent mPictureMessageIntent = new Intent(getActivity(),ImageMessageActivity.class);
          mPictureMessageIntent.setData(fileUri);
          startActivity(mPictureMessageIntent);
        }
        else if(msgType.equals(Constants.TYPE_VIDEO))
        {
          Intent mVideoMessageIntent = new Intent(Intent.ACTION_VIEW,fileUri);
          mVideoMessageIntent.setDataAndType(fileUri, "video/*");
          startActivity(mVideoMessageIntent);
        }
        
        //deletion of message
        List<String> rIDs = msg.getList(Constants.RECIPIENT_IDS);
        if(rIDs.size()==1)
        {
          msg.deleteInBackground();
        }
        else
        {
          rIDs.remove(currentUser.getObjectId());
          ArrayList<String> delIds = new ArrayList<String>();
          delIds.add(currentUser.getObjectId());
          msg.removeAll(Constants.RECIPIENT_IDS, delIds);
          msg.saveInBackground();
        }
      }
    });
    return rootView;
  }
  
  

  @Override
  public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    getActivity().setProgressBarIndeterminateVisibility(true);
    //List<ParseQuery<ParseObject>> queries = new ArrayList<ParseQuery<ParseObject>>();
    retrieveMessages();
  }

  public void retrieveMessages() {
    ParseQuery<ParseObject> messageQuery = new ParseQuery<ParseObject>("Messages");
    messageQuery.whereEqualTo(
        com.shivamb7.chitchat.workers.Constants.RECIPIENT_IDS,
        currentUser.getObjectId());
    messageQuery.addDescendingOrder("createdAt");
    messageQuery.findInBackground(new FindCallback<ParseObject>() {

      @Override
      public void done(List<ParseObject> pobjects, ParseException e) {
        // TODO Auto-generated method stub
        getActivity().setProgressBarIndeterminateVisibility(false);
        if(mSwipeRefreshLayout.isRefreshing())
        {
          mSwipeRefreshLayout.setRefreshing(false);
        }
        if (e == null) {
          messages = pobjects;
          String[] usernames = new String[messages.size()];
          int i = 0;
          for (ParseObject pobject : messages) {
            usernames[i] = pobject
                .getString(com.shivamb7.chitchat.workers.Constants.SENDER_NAME);
            i++;
          }
          if (usernames.length == 0) {
            mEmptyImage.setVisibility(View.VISIBLE);
            mEmptyText.setVisibility(View.VISIBLE);
            mMessageList.setVisibility(View.GONE);
          } else {
            
            mEmptyImage.setVisibility(View.GONE);
            mEmptyText.setVisibility(View.GONE);
            mMessageList.setVisibility(View.VISIBLE);
            if(mMessageList.getAdapter()==null)
            {
            mAdapter = new ChatsAdapter(getActivity(), messages);
            mMessageList.setAdapter(mAdapter);
            }
            else
            {
              mAdapter.refreshAdapter(messages);
            }
          }
        }
      }
    });
  }

}




Java Source Code List

com.pkmmte.circularimageview.CircularImageView.java
com.shivamb7.chitchat.AddFriendsActivity.java
com.shivamb7.chitchat.ChatsActivity.java
com.shivamb7.chitchat.ComposeTextActivity.java
com.shivamb7.chitchat.ImageMessageActivity.java
com.shivamb7.chitchat.MainActivity.java
com.shivamb7.chitchat.ProfileActivity.java
com.shivamb7.chitchat.RecipientsActivity.java
com.shivamb7.chitchat.RegisterActivity2.java
com.shivamb7.chitchat.RegisterActivity.java
com.shivamb7.chitchat.adapters.ChatsAdapter.java
com.shivamb7.chitchat.adapters.FriendGridAdapter.java
com.shivamb7.chitchat.adapters.LevelAdapter.java
com.shivamb7.chitchat.fragments.ChatsFragment.java
com.shivamb7.chitchat.fragments.ContactsFragment.java
com.shivamb7.chitchat.fragments.ProfileFragment.java
com.shivamb7.chitchat.workers.ChitChatApplication.java
com.shivamb7.chitchat.workers.CircularImageView.java
com.shivamb7.chitchat.workers.Constants.java
com.shivamb7.chitchat.workers.Fab.java
com.shivamb7.chitchat.workers.FileHelper.java
com.shivamb7.chitchat.workers.ImageResizer.java
com.shivamb7.chitchat.workers.Level.java
com.shivamb7.chitchat.workers.RippleView.java
com.shivamb7.chitchat.workers.TypefaceSpan.java