Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gfl.linewise.ui.fragments;
- import android.app.Activity;
- import android.app.Dialog;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Handler;
- import android.provider.MediaStore;
- import android.support.annotation.Nullable;
- import android.support.v4.widget.SwipeRefreshLayout;
- import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
- import android.text.TextUtils;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.Window;
- import android.view.inputmethod.InputMethodManager;
- import android.webkit.WebView;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ExpandableListView;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.alshvets.core.helpers.ViewHelper;
- import com.alshvets.core.structure.helpers.Logger;
- import com.alshvets.core.structure.ui.fragments.abs.BaseFragment;
- import com.gfl.linewise.R;
- import com.gfl.linewise.api.models.Poster;
- import com.gfl.linewise.api.models.responses.ReplyOn;
- import com.gfl.linewise.api.models.responses.SoundbiteWallResponse;
- import com.gfl.linewise.chat.MessageService;
- import com.gfl.linewise.chat.ui.MessagingActivity;
- import com.gfl.linewise.helpers.DateFormatHelper;
- import com.gfl.linewise.managers.SoundBiteFeedManager;
- import com.gfl.linewise.managers.SoundBiteFeedManager.OnSoundBiteWallListener;
- import com.gfl.linewise.models.abs.ActionBarListener;
- import com.gfl.linewise.preferences.AppPreference;
- import com.gfl.linewise.ui.activities.MainActivity;
- import com.gfl.linewise.ui.adapters.SoundBiteExpandableAdapter;
- import com.gfl.linewise.ui.adapters.SoundBiteExpandableAdapter.OnMainPostClickListener;
- import com.paging.listview.PagingExpandableListView;
- import com.paging.listview.PagingExpandableListView.Pagingable;
- import com.parse.FindCallback;
- import com.parse.ParseException;
- import com.parse.ParseQuery;
- import com.parse.ParseUser;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.List;
- import static com.alshvets.core.helpers.BitmapHelper.base64Encode;
- import static com.alshvets.core.helpers.BitmapHelper.getResizedBitmap;
- /**
- * Created by kvarivoda on 8/28/2015.
- */
- public class SoundBiteFeed extends BaseFragment implements View.OnClickListener,
- OnMainPostClickListener, OnSoundBiteWallListener, OnRefreshListener, Pagingable {
- private ActionBarListener actionBarListener;
- private static final String SOUND_BITE_FEED_ID = "0";
- private List<SoundbiteWallResponse> mainPosts = Collections.synchronizedList(new ArrayList<SoundbiteWallResponse>());
- //<SoundbiteWallResponse id> <List<ReplyOn>
- private HashMap<String, List<ReplyOn>> replyToMainPostMap = new HashMap<>();
- private PagingExpandableListView listView;
- private SoundBiteExpandableAdapter soundBiteExpandableAdapter;
- private String soundbiteWallId;
- private SoundBiteFeedManager soundBiteFeedManager;
- private EditText messageBodyField;
- private Button addPhoto;
- private String imagePath = "";
- private String imageBase64 = "";
- private ArrayList<String> names;
- private boolean isFirstLoad;
- private boolean isRefresh;
- // private SwipeRefreshLayout mSwipeRefreshLayout;
- private int count = 10;
- private int pager = 10;
- private SwipeRefreshLayout mSwipeRefreshLayout;
- @Override
- protected View initView(LayoutInflater inflater, ViewGroup container) {
- return inflater.inflate(R.layout.fragment_soundbite_feed, container, false);
- }
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- try {
- actionBarListener = (ActionBarListener) activity;
- } catch (ClassCastException castException) {
- Logger.error(getClass(), "The activity does not implement the listener.");
- }
- }
- @Override
- public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- if (actionBarListener != null)
- actionBarListener.changeActionBarBehavior(getString(R.string.soundBiteFeed));
- mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh);
- mSwipeRefreshLayout.setOnRefreshListener(this);
- mSwipeRefreshLayout.setColorScheme(R.color.blue, R.color.green);
- TextView dateText = (TextView) view.findViewById(R.id.dateText);
- String eventDateText = DateFormatHelper.getDateTextFromMillis(System.currentTimeMillis());
- dateText.setText(eventDateText);
- String[] soundBiteWallLines = getResources().getStringArray(R.array.soundBiteWallLines);
- Collections.shuffle(Arrays.asList(soundBiteWallLines));
- StringBuilder builder = new StringBuilder();
- for (String value : soundBiteWallLines) {
- builder.append(" ");
- builder.append(value);
- }
- TextView shareTextLine = (TextView) view.findViewById(R.id.shareTextLine);
- shareTextLine.setText(builder);
- shareTextLine.setSelected(true);
- listView = (PagingExpandableListView) view.findViewById(R.id.soundBiteListView);
- listView.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE);
- soundBiteExpandableAdapter = new SoundBiteExpandableAdapter(getActivity(), mainPosts, replyToMainPostMap, this);
- listView.setAdapter(soundBiteExpandableAdapter);
- listView.setPagingableListener(this);
- View shareLayout = view.findViewById(R.id.shareLayout);
- shareLayout.setOnClickListener(this);
- addPhoto = (Button) view.findViewById(R.id.addPhoto);
- addPhoto.setOnClickListener(this);
- messageBodyField = (EditText) view.findViewById(R.id.editTextComment);
- Button btnAddComment = (Button) view.findViewById(R.id.btnAddComment);
- btnAddComment.setOnClickListener(this);
- mProgressDialog.show();
- mProgressDialog.setCancelable(true);
- soundBiteFeedManager = new SoundBiteFeedManager(this);
- isFirstLoad = true;
- int offset = 0;
- soundBiteFeedManager.getSoundBiteWallByEventId(SOUND_BITE_FEED_ID, count, offset);
- }
- @Override
- public void onPause() {
- if (actionBarListener != null)
- actionBarListener.changeActionBarBehavior("");
- super.onPause();
- }
- @Override
- public void onResume() {
- super.onResume();
- if (actionBarListener != null)
- actionBarListener.changeActionBarBehavior(getString(R.string.soundBiteFeed));
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.btnAddComment:
- String messageBody = messageBodyField.getText().toString();
- if(!TextUtils.isEmpty(imagePath)){
- sendMessage();
- return;
- } else if (messageBody.isEmpty()) {
- Toast.makeText(getActivity(), "Please enter a message", Toast.LENGTH_LONG).show();
- Logger.debug(getClass(), "id of event " + soundBiteExpandableAdapter.getSelectedMainPostId());
- Logger.debug(getClass(), "isPicked " + soundBiteExpandableAdapter.isPostPicked());
- return;
- }
- sendMessage();
- break;
- case R.id.addPhoto:
- ((MainActivity)getActivity()).startPickImageWithoutCropping();
- break;
- case R.id.shareLayout:
- showShareLineDialog();
- break;
- }
- }
- private void sendMessage() {
- if (soundBiteExpandableAdapter.isPostPicked()) {
- mProgressDialog.show();
- final String selectedMainPostId = soundBiteExpandableAdapter.getSelectedMainPostId();
- final String selectedMainAccountId = soundBiteExpandableAdapter.getSelectedMainAccountId();
- if(!TextUtils.isEmpty(imagePath)){
- messageBodyField.setText("");
- }
- soundBiteFeedManager.replyOnComment(selectedMainPostId, selectedMainAccountId,
- messageBodyField.getText().toString(), imagePath, imageBase64);
- } else {
- mProgressDialog.show();
- if(!TextUtils.isEmpty(imagePath)){
- messageBodyField.setText("");
- }
- soundBiteFeedManager.postComment(soundbiteWallId, messageBodyField.getText().toString(),
- imagePath, imageBase64);
- }
- }
- @Override
- public void onMainPostClickListener(String id) {
- }
- @Override
- public void onOpenKeyboardListener() {
- InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
- }
- @Override
- public void onOpenChatListener(String email, String accountId, String points, String photo) {
- startChat(email, accountId, points, photo);
- }
- private void hideKeyboard() {
- View view = getActivity().getCurrentFocus();
- ViewHelper.hideKeyboard(getActivity(), view);
- }
- public void deliverImagePath(String path) {
- Logger.debug(getClass(), "SoundBiteFeed imageDelivered " + path);
- messageBodyField.setText("");
- imagePath = path;
- messageBodyField.setFocusable(false);
- messageBodyField.setText("File is ready to send...");
- Uri imageUri = Uri.fromFile(new File(path));
- try {
- Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), imageUri);
- Bitmap imageBitmap = getResizedBitmap(bitmap);
- imageBase64 = base64Encode(imageBitmap);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onSoundBiteWallListener(final List<SoundbiteWallResponse> soundbiteWallResponse, String soundbiteWallId) {
- Logger.debug(getClass(), "GetSoundbiteWallRequest: OK");
- this.soundbiteWallId = soundbiteWallId;
- if (soundbiteWallResponse != null) {
- if (soundbiteWallResponse.size() == 0 && isFirstLoad) {
- Logger.debug(getClass(), "first load size == 0");
- listView.setHasMoreItems(false);
- safeClose(mProgressDialog);
- mSwipeRefreshLayout.setRefreshing(false);
- return;
- } else if (isRefresh) {
- Logger.debug(getClass(), "is refresh");
- mainPosts.clear();
- Logger.debug(getClass(), "clear");
- // listView.setHasMoreItems(true);
- Logger.debug(getClass(), "setHasMoreItems");
- } else {
- Logger.debug(getClass(), "else setHasMoreItems...");
- // listView.setHasMoreItems(true);
- }
- Logger.debug(getClass(), "addAll...");
- for (SoundbiteWallResponse soundbiteWallItem : soundbiteWallResponse) {
- replyToMainPostMap.put(soundbiteWallItem.getId(), soundbiteWallItem.getSoundbiteWallReplyOns());
- }
- Logger.debug(getClass(), "notifyDataSetChanged...");
- Logger.debug(getClass(), "isRefresh.. " + isRefresh);
- getActivity().runOnUiThread(new Runnable() {
- public void run() {
- mainPosts.addAll(soundbiteWallResponse);
- soundBiteExpandableAdapter.notifyDataSetChanged();
- if (!isRefresh) {
- listView.onFinishLoading(true, mainPosts);
- }
- startUpdateTimer();
- }
- });
- Logger.debug(getClass(), "listView.onFinishLoading(true, mainPosts...");
- if (isFirstLoad) {
- isFirstLoad = false;
- pager = 0;
- }
- pager += 10;
- if(soundbiteWallResponse.size() == 0){
- Logger.debug(getClass(), "size == 0");
- // listView.onFinishLoading(false, mainPosts);
- listView.setHasMoreItems(false);
- // listView.setHasMoreItems(false);
- }
- }
- isRefresh = false;
- safeClose(mProgressDialog);
- Logger.debug(getClass(), " mSwipeRefreshLayout.setRefreshing(false);");
- mSwipeRefreshLayout.setRefreshing(false);
- }
- private void startUpdateTimer() {
- Handler handler = new Handler();
- handler.postDelayed(new Runnable() {
- @Override
- public void run() {
- getRefreshedPage();
- }
- }, 10000);
- }
- @Override
- public void onSoundBiteWallError(String getErrorMessage) {
- safeClose(mProgressDialog);
- listView.onFinishLoading(true, mainPosts);
- mSwipeRefreshLayout.setRefreshing(false);
- }
- @Override
- public void onSoundBitePostCommentListener(String soundbiteWallResponseId, String comment,
- String imageJsonPath) {
- Logger.debug(getClass(), "CreateSoundBitePoster: OK");
- safeClose(mProgressDialog);
- // save index and top position of listView
- final int index = listView.getFirstVisiblePosition();
- View v = listView.getChildAt(0);
- final int top = (v == null) ? 0 : (v.getTop() - listView.getPaddingTop());
- //updating UI with comment
- SoundbiteWallResponse soundbiteWallResponse = new SoundbiteWallResponse();
- soundbiteWallResponse.setId(soundbiteWallResponseId);
- soundbiteWallResponse.setComment(messageBodyField.getText().toString());
- soundbiteWallResponse.setPhoto(imageJsonPath);
- Poster poster1 = new Poster(AppPreference.getInstance().getProfileId());
- poster1.setPhoto(AppPreference.getInstance().getProfileResponse().getPhoto());
- poster1.setAchievements(AppPreference.getInstance().getUserInfoResponse());
- soundbiteWallResponse.setPoster(poster1);
- mainPosts.add(0, soundbiteWallResponse);
- replyToMainPostMap.put(soundbiteWallResponse.getId(), new ArrayList<ReplyOn>());
- soundBiteExpandableAdapter.notifyDataSetChanged();
- this.imagePath = "";
- this.imageBase64 = "";
- messageBodyField.setText("");
- hideKeyboard();
- }
- @Override
- public void onSoundBitePostCommentError(String getErrorMessage) {
- safeClose(mProgressDialog);
- }
- @Override
- public void onSoundBiteReplyOnCommentListener(String selectedMainPostId, String selectedMainAccountId,
- String replyOnId, String comment, String imageJsonPath) {
- Logger.debug(getClass(), "CreateSoundBiteReplyOn: OK");
- Logger.debug(getClass(), "lets log everything ");
- Logger.debug(getClass(), "lets log selectedMainPostId " + selectedMainPostId);
- Logger.debug(getClass(), "lets log selectedMainAccountId " + selectedMainAccountId);
- Logger.debug(getClass(), "lets log replyOnId " + replyOnId);
- Logger.debug(getClass(), "lets log comment " + comment);
- Logger.debug(getClass(), "lets log messageBodyField.getText().toString() " + messageBodyField.getText().toString());
- Logger.debug(getClass(), "lets log imageBase64 " + imageJsonPath);
- safeClose(mProgressDialog);
- // save index and top position of listView
- final int index = listView.getFirstVisiblePosition();
- View v = listView.getChildAt(0);
- final int top = (v == null) ? 0 : (v.getTop() - listView.getPaddingTop());
- List<ReplyOn> soundbiteWallReplyOns = replyToMainPostMap.get(selectedMainPostId);
- ReplyOn replyOn = new ReplyOn();
- replyOn.setId(replyOnId);
- replyOn.setComment(messageBodyField.getText().toString());
- replyOn.setPhoto(imageJsonPath);
- Poster poster1 = new Poster(AppPreference.getInstance().getProfileId());
- poster1.setPhoto(AppPreference.getInstance().getProfileResponse().getPhoto());
- poster1.setAchievements(AppPreference.getInstance().getUserInfoResponse());
- replyOn.setReplyOn(poster1);
- soundbiteWallReplyOns.add(replyOn);
- replyToMainPostMap.put(selectedMainPostId, soundbiteWallReplyOns);
- getActivity().runOnUiThread(new Runnable() {
- public void run() {
- hideKeyboard();
- }
- });
- soundBiteExpandableAdapter.notifyDataSetChanged();
- listView.setSelection(index);
- messageBodyField.setText("");
- this.imagePath = "";
- this.imageBase64 = "";
- safeClose(mProgressDialog);
- }
- @Override
- public void onSoundBiteReplyOnCommentError(String getErrorMessage) {
- safeClose(mProgressDialog);
- }
- @Override
- public void onRefresh() {
- mSwipeRefreshLayout.setRefreshing(true);
- getRefreshedPage();
- }
- public void getRefreshedPage(){
- soundBiteFeedManager = new SoundBiteFeedManager(this);
- isFirstLoad = true;
- isRefresh = true;
- pager = 0;
- soundBiteFeedManager.getSoundBiteWallByEventId(SOUND_BITE_FEED_ID, count, pager);
- }
- @Override
- public void onLoadMoreItems() {
- Logger.debug(getClass(), "onLoadMoreItems");
- soundBiteFeedManager.getSoundBiteWallByEventId(SOUND_BITE_FEED_ID, count, pager);
- }
- private void showShareLineDialog() {
- final Dialog dialog = new Dialog(getActivity());
- dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
- dialog.setContentView(R.layout.share_line_dialog);
- WebView licenceWebView = (WebView) dialog.findViewById(R.id.licenceWebView);
- licenceWebView.getSettings();
- licenceWebView.setBackgroundColor(0x00000000);
- licenceWebView.loadUrl("file:///android_asset/share_line_text.html");
- dialog.show();
- }
- private void startChat(final String email, final String accountId, final String points, final String photo) {
- if (email == null) {
- Logger.debug(getClass(), "email null!");
- return;
- }
- Logger.debug(getClass(), "startChat()");
- ParseUser currentUser = ParseUser.getCurrentUser();
- if (currentUser != null) {
- String currentUserId = ParseUser.getCurrentUser().getObjectId();
- //don't include yourself in the list
- names = new ArrayList<>();
- ParseQuery<ParseUser> query = ParseUser.getQuery();
- query.whereNotEqualTo("objectId", currentUserId);
- query.findInBackground(new FindCallback<ParseUser>() {
- public void done(List<ParseUser> userList, ParseException e) {
- if (e == null) {
- for (int i = 0; i < userList.size(); i++) {
- names.add(userList.get(i).getUsername());
- if (email.equals(userList.get(i).getUsername())) {
- Logger.debug(getClass(), "Chat: this is your user " + userList.get(i).getUsername());
- openChatConversation(userList.get(i),accountId, points, photo );
- }
- }
- } else {
- Toast.makeText(getApplicationContext(),
- "Chat: error loading user list",
- Toast.LENGTH_LONG).show();
- }
- }
- });
- } else {
- Logger.debug(getClass(), "currentUser==null");
- }
- }
- //open a chat conversation with one person
- public void openChatConversation(ParseUser username, final String accountId, final String points, final String photo) {
- Logger.debug(getClass(), "openConversation");
- ParseQuery<ParseUser> query = ParseUser.getQuery();
- query.whereEqualTo("username", username.getUsername());
- query.findInBackground(new FindCallback<ParseUser>() {
- public void done(List<ParseUser> user, ParseException e) {
- if (e == null) {
- startChatService();
- //start the messaging activity
- Intent intent = new Intent(getApplicationContext(), MessagingActivity.class);
- intent.putExtra(MessagingActivity.RECIPIENT_ID, user.get(0).getObjectId());
- intent.putExtra(MessagingActivity.USER_ID, accountId);
- intent.putExtra(MessagingActivity.POINTS, points);
- intent.putExtra(MessagingActivity.PHOTO_URL, photo);
- startActivity(intent);
- Logger.debug(getClass(), "RECIPIENT_ID " + user.get(0).getObjectId() );
- } else {
- Toast.makeText(getApplicationContext(),
- "Chat: error finding that user",
- Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- private void startChatService() {
- ParseUser currentUser = ParseUser.getCurrentUser();
- if (currentUser != null) {
- Logger.debug(getClass(), "currentUser !=null " + ParseUser.getCurrentUser().getUsername());
- Logger.debug(getClass(), "start chat service");
- Intent serviceIntent = new Intent(getApplicationContext(), MessageService.class);
- getActivity().startService(serviceIntent);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement