|
13 | 13 | import android.support.v7.app.AppCompatActivity;
|
14 | 14 | import android.support.v7.widget.LinearLayoutManager;
|
15 | 15 | import android.support.v7.widget.RecyclerView;
|
| 16 | +import android.text.TextUtils; |
16 | 17 | import android.util.Log;
|
17 | 18 | import android.util.SparseArray;
|
18 | 19 | import android.view.LayoutInflater;
|
|
39 | 40 | import com.zulip.android.networking.AsyncGetOldMessages;
|
40 | 41 | import com.zulip.android.networking.ZulipAsyncPushTask;
|
41 | 42 | import com.zulip.android.networking.response.EditResponse;
|
| 43 | +import com.zulip.android.networking.response.RawMessageResponse; |
| 44 | +import com.zulip.android.networking.util.DefaultCallback; |
| 45 | +import com.zulip.android.util.CommonProgressDialog; |
42 | 46 | import com.zulip.android.util.Constants;
|
43 | 47 | import com.zulip.android.util.MessageListener;
|
44 | 48 | import com.zulip.android.util.MutedTopics;
|
@@ -174,7 +178,7 @@ public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
174 | 178 | }
|
175 | 179 | }
|
176 | 180 | });
|
177 |
| - mListener.setLayoutBehaviour(linearLayoutManager,adapter); |
| 181 | + mListener.setLayoutBehaviour(linearLayoutManager, adapter); |
178 | 182 | return view;
|
179 | 183 | }
|
180 | 184 |
|
@@ -398,68 +402,110 @@ private void editMessage(final Message message, final int position) {
|
398 | 402 | if (timeSinceMessageSend > maxMessageContentEditLimit) {
|
399 | 403 | Toast.makeText(getContext(), R.string.maximum_time_limit_error, Toast.LENGTH_SHORT).show();
|
400 | 404 | } else {
|
401 |
| - final View dialogView = View.inflate(getContext(), |
402 |
| - R.layout.message_edit_dialog, null); |
403 |
| - //Pop up a dialog box with previous message content |
404 |
| - final AlertDialog dialog = new AlertDialog.Builder(getContext()) |
405 |
| - .setTitle(R.string.edit_message) |
406 |
| - .setView(dialogView) |
407 |
| - .setPositiveButton(android.R.string.ok, null) |
408 |
| - .setNegativeButton(android.R.string.cancel, null) |
409 |
| - .create(); |
410 |
| - dialog.getWindow().setSoftInputMode( |
411 |
| - WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); |
412 |
| - dialog.show(); |
413 |
| - |
414 |
| - final EditText dialogMessageEditText = (EditText) dialogView.findViewById(R.id.message_content); |
415 |
| - dialogMessageEditText.setText(message.getContent().trim()); |
416 |
| - |
417 |
| - //Move cursor to end of text |
418 |
| - dialogMessageEditText.setSelection(dialogMessageEditText.getText().length()); |
419 |
| - |
420 |
| - //OK button listener |
421 |
| - dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { |
422 |
| - //Show edit option only on if current user send it. |
423 |
| - @Override |
424 |
| - public void onClick(View view) { |
425 |
| - dialog.cancel(); |
426 |
| - InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); |
427 |
| - imm.hideSoftInputFromWindow(view.getWindowToken(), 0); |
428 |
| - //Start a progress dialog indicating editing message |
429 |
| - final ProgressDialog progress = new ProgressDialog(getActivity()); |
430 |
| - progress.setCancelable(false); |
431 |
| - progress.setMessage(app.getString(R.string.editing_message)); |
432 |
| - progress.show(); |
433 |
| - final String editedMessageContent = |
434 |
| - dialogMessageEditText.getText().toString().length() == 0 ? getString(R.string.default_delete_text) : dialogMessageEditText.getText().toString(); |
435 |
| - |
436 |
| - app.getZulipServices() |
437 |
| - .editMessage(String.valueOf(message.getID()), editedMessageContent) |
438 |
| - .enqueue(new Callback<EditResponse>() { |
439 |
| - @Override |
440 |
| - public void onResponse(Call<EditResponse> call, Response<EditResponse> response) { |
441 |
| - if (response.isSuccessful()) { |
442 |
| - message.setContent(editedMessageContent); |
443 |
| - message.setFormattedContent(editedMessageContent); |
444 |
| - adapter.notifyItemChanged(position); |
445 |
| - progress.dismiss(); |
446 |
| - Toast.makeText(getActivity(), R.string.message_edited, Toast.LENGTH_SHORT).show(); |
447 |
| - } else { |
448 |
| - progress.dismiss(); |
449 |
| - Toast.makeText(getActivity(), R.string.message_edit_failed, Toast.LENGTH_SHORT).show(); |
450 |
| - } |
451 |
| - } |
| 405 | + final CommonProgressDialog commonProgressDialog = new CommonProgressDialog(getContext()); |
| 406 | + commonProgressDialog.showWithMessage(getString(R.string.fetch_edit_message)); |
| 407 | + app.getZulipServices() |
| 408 | + .fetchRawMessage(message.getID()) |
| 409 | + .enqueue(new DefaultCallback<RawMessageResponse>() { |
| 410 | + @Override |
| 411 | + public void onSuccess(Call<RawMessageResponse> call, Response<RawMessageResponse> response) { |
| 412 | + RawMessageResponse messageResponse = response.body(); |
| 413 | + commonProgressDialog.dismiss(); |
| 414 | + showEditMessageDialog(message, messageResponse.getRawContent(), position); |
| 415 | + } |
| 416 | + |
| 417 | + @Override |
| 418 | + public void onError(Call<RawMessageResponse> call, Response<RawMessageResponse> response) { |
| 419 | + RawMessageResponse messageResponse = response.body(); |
| 420 | + if (messageResponse != null) { |
| 421 | + Toast.makeText(getActivity(), (TextUtils.isEmpty(messageResponse.getMsg())) ? getString(R.string.message_edit_failed) : |
| 422 | + messageResponse.getMsg(), Toast.LENGTH_SHORT).show(); |
| 423 | + } else { |
| 424 | + Toast.makeText(getActivity(), R.string.message_edit_failed, Toast.LENGTH_SHORT).show(); |
| 425 | + } |
| 426 | + //If something fails msg will have something to display |
| 427 | + commonProgressDialog.dismiss(); |
| 428 | + } |
| 429 | + |
| 430 | + @Override |
| 431 | + public void onFailure(Call<RawMessageResponse> call, Throwable t) { |
| 432 | + super.onFailure(call, t); |
| 433 | + commonProgressDialog.dismiss(); |
| 434 | + Toast.makeText(getActivity(), R.string.message_edit_failed, Toast.LENGTH_SHORT).show(); |
| 435 | + } |
| 436 | + }); |
| 437 | + } |
| 438 | + } |
| 439 | + |
| 440 | + private void showEditMessageDialog(final Message message, final String rawContent, final int position) { |
| 441 | + |
| 442 | + final View dialogView = View.inflate(getContext(), |
| 443 | + R.layout.message_edit_dialog, null); |
| 444 | + //Pop up a dialog box with previous message content |
| 445 | + final AlertDialog dialog = new AlertDialog.Builder(getContext()) |
| 446 | + .setTitle(R.string.edit_message) |
| 447 | + .setView(dialogView) |
| 448 | + .setPositiveButton(android.R.string.ok, null) |
| 449 | + .setNegativeButton(android.R.string.cancel, null) |
| 450 | + .create(); |
| 451 | + dialog.getWindow().setSoftInputMode( |
| 452 | + WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); |
| 453 | + dialog.show(); |
| 454 | + |
| 455 | + final EditText dialogMessageEditText = (EditText) dialogView.findViewById(R.id.message_content); |
| 456 | + dialogMessageEditText.setText(rawContent); |
452 | 457 |
|
453 |
| - @Override |
454 |
| - public void onFailure(Call<EditResponse> call, Throwable t) { |
| 458 | + //Move cursor to end of text |
| 459 | + dialogMessageEditText.setSelection(dialogMessageEditText.getText().length()); |
| 460 | + |
| 461 | + //OK button listener |
| 462 | + dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { |
| 463 | + //Show edit option only on if current user send it. |
| 464 | + @Override |
| 465 | + public void onClick(View view) { |
| 466 | + dialog.cancel(); |
| 467 | + InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); |
| 468 | + imm.hideSoftInputFromWindow(view.getWindowToken(), 0); |
| 469 | + //Start a progress dialog indicating editing message |
| 470 | + final ProgressDialog progress = new ProgressDialog(getActivity()); |
| 471 | + progress.setCancelable(false); |
| 472 | + progress.setMessage(app.getString(R.string.editing_message)); |
| 473 | + progress.show(); |
| 474 | + final String editedMessageContent = |
| 475 | + dialogMessageEditText.getText().toString().length() == 0 ? getString(R.string.default_delete_text) : dialogMessageEditText.getText().toString(); |
| 476 | + |
| 477 | + if (editedMessageContent.equals(rawContent)) { |
| 478 | + Toast.makeText(getActivity(), R.string.no_edit, Toast.LENGTH_SHORT).show(); |
| 479 | + progress.dismiss(); |
| 480 | + return; |
| 481 | + } |
| 482 | + |
| 483 | + app.getZulipServices() |
| 484 | + .editMessage(String.valueOf(message.getID()), editedMessageContent) |
| 485 | + .enqueue(new Callback<EditResponse>() { |
| 486 | + @Override |
| 487 | + public void onResponse(Call<EditResponse> call, Response<EditResponse> response) { |
| 488 | + if (response.isSuccessful()) { |
| 489 | + message.setContent(editedMessageContent); |
| 490 | + message.setFormattedContent(editedMessageContent); |
| 491 | + adapter.notifyItemChanged(position); |
| 492 | + progress.dismiss(); |
| 493 | + Toast.makeText(getActivity(), R.string.message_edited, Toast.LENGTH_SHORT).show(); |
| 494 | + } else { |
455 | 495 | progress.dismiss();
|
456 |
| - ZLog.logException(t); |
457 | 496 | Toast.makeText(getActivity(), R.string.message_edit_failed, Toast.LENGTH_SHORT).show();
|
458 | 497 | }
|
459 |
| - }); |
460 |
| - } |
461 |
| - }); |
462 |
| - } |
| 498 | + } |
| 499 | + |
| 500 | + @Override |
| 501 | + public void onFailure(Call<EditResponse> call, Throwable t) { |
| 502 | + progress.dismiss(); |
| 503 | + ZLog.logException(t); |
| 504 | + Toast.makeText(getActivity(), R.string.message_edit_failed, Toast.LENGTH_SHORT).show(); |
| 505 | + } |
| 506 | + }); |
| 507 | + } |
| 508 | + }); |
463 | 509 | }
|
464 | 510 |
|
465 | 511 | @SuppressWarnings("deprecation")
|
@@ -713,7 +759,8 @@ public interface Listener {
|
713 | 759 |
|
714 | 760 | void clearChatBox();
|
715 | 761 |
|
716 |
| - void setLayoutBehaviour(LinearLayoutManager linearLayoutManager , RecyclerMessageAdapter adapter); |
| 762 | + void setLayoutBehaviour(LinearLayoutManager linearLayoutManager, RecyclerMessageAdapter adapter); |
| 763 | + |
717 | 764 | }
|
718 | 765 |
|
719 | 766 | public void showLatestMessages() {
|
|
0 commit comments