2020-10-12 20:11:23 +02:00
|
|
|
package org.mian.gitnex.fragments;
|
|
|
|
|
|
|
|
import android.animation.ValueAnimator;
|
2020-10-29 18:37:06 +01:00
|
|
|
import android.annotation.SuppressLint;
|
2020-10-12 20:11:23 +02:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.text.Editable;
|
|
|
|
import android.text.TextWatcher;
|
|
|
|
import android.view.LayoutInflater;
|
2020-10-29 18:37:06 +01:00
|
|
|
import android.view.MotionEvent;
|
2020-10-12 20:11:23 +02:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.ImageButton;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2020-11-02 16:17:00 +01:00
|
|
|
import androidx.core.content.ContextCompat;
|
2020-10-29 18:37:06 +01:00
|
|
|
import com.google.android.material.bottomsheet.BottomSheetBehavior;
|
2020-10-12 20:11:23 +02:00
|
|
|
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
|
|
|
|
import org.mian.gitnex.R;
|
|
|
|
import org.mian.gitnex.actions.ActionResult;
|
|
|
|
import org.mian.gitnex.actions.IssueActions;
|
|
|
|
import org.mian.gitnex.activities.MainActivity;
|
|
|
|
import org.mian.gitnex.database.api.DraftsApi;
|
2021-01-26 16:10:31 +01:00
|
|
|
import org.mian.gitnex.databinding.BottomSheetReplyLayoutBinding;
|
2020-10-12 20:11:23 +02:00
|
|
|
import org.mian.gitnex.helpers.StaticGlobalVariables;
|
|
|
|
import org.mian.gitnex.helpers.TinyDB;
|
|
|
|
import org.mian.gitnex.helpers.Toasty;
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author opyale
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class BottomSheetReplyFragment extends BottomSheetDialogFragment {
|
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
private enum Mode { EDIT, SEND }
|
|
|
|
private Mode mode = Mode.SEND;
|
|
|
|
|
2020-10-12 20:11:23 +02:00
|
|
|
private TinyDB tinyDB;
|
|
|
|
private DraftsApi draftsApi;
|
|
|
|
|
|
|
|
private int repositoryId;
|
|
|
|
private int currentActiveAccountId;
|
|
|
|
private int issueNumber;
|
|
|
|
private long draftId;
|
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
private OnInteractedListener onInteractedListener;
|
2020-10-12 20:11:23 +02:00
|
|
|
private TextView draftsHint;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAttach(@NonNull Context context) {
|
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
tinyDB = TinyDB.getInstance(context);
|
2020-10-12 20:11:23 +02:00
|
|
|
draftsApi = new DraftsApi(context);
|
|
|
|
|
|
|
|
repositoryId = (int) tinyDB.getLong("repositoryId", 0);
|
|
|
|
currentActiveAccountId = tinyDB.getInt("currentActiveAccountId");
|
|
|
|
issueNumber = Integer.parseInt(tinyDB.getString("issueNumber"));
|
|
|
|
|
|
|
|
super.onAttach(context);
|
|
|
|
}
|
|
|
|
|
2020-10-29 18:37:06 +01:00
|
|
|
@SuppressLint("ClickableViewAccessibility")
|
2020-10-12 20:11:23 +02:00
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
|
|
|
|
2021-01-26 16:10:31 +01:00
|
|
|
BottomSheetReplyLayoutBinding bottomSheetReplyLayoutBinding = BottomSheetReplyLayoutBinding.inflate(inflater, container, false);
|
2020-10-12 20:11:23 +02:00
|
|
|
Bundle arguments = requireArguments();
|
|
|
|
|
2021-01-26 16:10:31 +01:00
|
|
|
draftsHint = bottomSheetReplyLayoutBinding.draftsHint;
|
2020-10-12 20:11:23 +02:00
|
|
|
|
2021-01-26 16:10:31 +01:00
|
|
|
EditText comment = bottomSheetReplyLayoutBinding.comment;
|
|
|
|
TextView toolbarTitle = bottomSheetReplyLayoutBinding.toolbarTitle;
|
|
|
|
ImageButton close = bottomSheetReplyLayoutBinding.close;
|
|
|
|
ImageButton drafts = bottomSheetReplyLayoutBinding.drafts;
|
|
|
|
ImageButton send = bottomSheetReplyLayoutBinding.send;
|
2020-10-12 20:11:23 +02:00
|
|
|
|
|
|
|
send.setEnabled(false);
|
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
if(Objects.equals(arguments.getString("commentAction"), "edit") &&
|
|
|
|
arguments.getString("draftId") == null) {
|
|
|
|
|
|
|
|
send.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.ic_save));
|
|
|
|
mode = Mode.EDIT;
|
2020-10-12 20:11:23 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(arguments.getString("draftId") != null) {
|
|
|
|
|
|
|
|
draftId = Long.parseLong(arguments.getString("draftId"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!tinyDB.getString("issueTitle").isEmpty()) {
|
|
|
|
|
|
|
|
toolbarTitle.setText(tinyDB.getString("issueTitle"));
|
|
|
|
}
|
|
|
|
else if(arguments.getString("draftTitle") != null) {
|
|
|
|
|
|
|
|
toolbarTitle.setText(arguments.getString("draftTitle"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(arguments.getString("commentBody") != null) {
|
|
|
|
|
|
|
|
send.setEnabled(true);
|
|
|
|
send.setAlpha(1f);
|
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
comment.setText(arguments.getString("commentBody"));
|
2020-10-12 20:11:23 +02:00
|
|
|
|
|
|
|
if(arguments.getBoolean("cursorToEnd", false)) {
|
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
comment.setSelection(comment.length());
|
2020-10-12 20:11:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
comment.requestFocus();
|
|
|
|
comment.setOnTouchListener((v, event) -> {
|
2020-10-29 18:37:06 +01:00
|
|
|
|
2021-01-26 16:10:31 +01:00
|
|
|
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from((View) bottomSheetReplyLayoutBinding.getRoot().getParent());
|
2020-10-29 18:37:06 +01:00
|
|
|
|
|
|
|
switch(event.getAction()) {
|
|
|
|
|
|
|
|
case MotionEvent.ACTION_DOWN:
|
|
|
|
case MotionEvent.ACTION_SCROLL:
|
|
|
|
bottomSheetBehavior.setDraggable(false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
bottomSheetBehavior.setDraggable(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
comment.addTextChangedListener(new TextWatcher() {
|
2020-10-12 20:11:23 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
String text = comment.getText().toString();
|
2020-10-12 20:11:23 +02:00
|
|
|
|
|
|
|
if(text.isEmpty()) {
|
|
|
|
|
|
|
|
send.setEnabled(false);
|
|
|
|
send.setAlpha(0.5f);
|
|
|
|
saveDraft(null, true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
send.setEnabled(true);
|
|
|
|
send.setAlpha(1f);
|
|
|
|
saveDraft(text, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
|
|
|
@Override public void onTextChanged(CharSequence s, int start, int before, int count) {}
|
2020-11-02 16:17:00 +01:00
|
|
|
|
2020-10-12 20:11:23 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
close.setOnClickListener(v -> dismiss());
|
|
|
|
|
|
|
|
drafts.setOnClickListener(v -> {
|
|
|
|
|
|
|
|
Intent intent = new Intent(getContext(), MainActivity.class);
|
|
|
|
intent.putExtra("launchFragment", "drafts");
|
|
|
|
startActivity(intent);
|
|
|
|
|
|
|
|
dismiss();
|
2020-11-02 16:17:00 +01:00
|
|
|
|
2020-10-12 20:11:23 +02:00
|
|
|
});
|
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
send.setOnClickListener(v -> {
|
2020-10-12 20:11:23 +02:00
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
if(mode == Mode.SEND) {
|
2020-10-12 20:11:23 +02:00
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
IssueActions
|
|
|
|
.reply(getContext(), comment.getText().toString(), issueNumber)
|
|
|
|
.accept((status, result) -> {
|
2020-10-12 20:11:23 +02:00
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
if(status == ActionResult.Status.SUCCESS) {
|
2020-10-12 20:11:23 +02:00
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
Toasty.success(getContext(), getString(R.string.commentSuccess));
|
2020-10-12 20:11:23 +02:00
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
if(draftId != 0 && tinyDB.getBoolean("draftsCommentsDeletionEnabled")) {
|
|
|
|
draftsApi.deleteSingleDraft((int) draftId);
|
|
|
|
}
|
2020-10-12 20:11:23 +02:00
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
tinyDB.putBoolean("commentPosted", true);
|
|
|
|
tinyDB.putBoolean("resumeIssues", true);
|
|
|
|
tinyDB.putBoolean("resumePullRequests", true);
|
2020-10-12 20:11:23 +02:00
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
if(onInteractedListener != null) {
|
|
|
|
onInteractedListener.onInteracted();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
Toasty.error(getContext(), getString(R.string.commentError));
|
|
|
|
}
|
|
|
|
|
|
|
|
dismiss();
|
|
|
|
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
|
|
|
|
IssueActions
|
|
|
|
.edit(getContext(), comment.getText().toString(), arguments.getInt("commentId"))
|
|
|
|
.accept((status, result) -> {
|
|
|
|
|
|
|
|
if(status == ActionResult.Status.SUCCESS) {
|
|
|
|
|
|
|
|
if(draftId != 0 && tinyDB.getBoolean("draftsCommentsDeletionEnabled")) {
|
|
|
|
draftsApi.deleteSingleDraft((int) draftId);
|
|
|
|
}
|
|
|
|
|
|
|
|
tinyDB.putBoolean("commentEdited", true);
|
|
|
|
|
|
|
|
if(onInteractedListener != null) {
|
|
|
|
onInteractedListener.onInteracted();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
Toasty.error(getContext(), getString(R.string.genericError));
|
|
|
|
}
|
|
|
|
|
|
|
|
dismiss();
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2020-10-12 20:11:23 +02:00
|
|
|
|
2021-01-26 16:10:31 +01:00
|
|
|
return bottomSheetReplyLayoutBinding.getRoot();
|
2020-10-12 20:11:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void saveDraft(String text, boolean remove) {
|
|
|
|
|
|
|
|
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 1f);
|
|
|
|
valueAnimator.setDuration(500);
|
|
|
|
valueAnimator.addUpdateListener(animation -> {
|
|
|
|
|
|
|
|
float value = (Float) animation.getAnimatedValue();
|
|
|
|
|
|
|
|
if(value == 0f) {
|
|
|
|
draftsHint.setVisibility((remove) ? View.GONE : View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
draftsHint.setAlpha(value);
|
|
|
|
});
|
|
|
|
|
|
|
|
if(remove) {
|
|
|
|
|
|
|
|
draftsApi.deleteSingleDraft((int) draftId);
|
|
|
|
draftId = 0;
|
|
|
|
|
|
|
|
valueAnimator.reverse();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
String draftType;
|
|
|
|
if(tinyDB.getString("issueType").equalsIgnoreCase("Issue")) {
|
|
|
|
|
|
|
|
draftType = StaticGlobalVariables.draftTypeIssue;
|
|
|
|
}
|
|
|
|
else if(tinyDB.getString("issueType").equalsIgnoreCase("Pull")) {
|
|
|
|
|
|
|
|
draftType = StaticGlobalVariables.draftTypePull;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
draftType = "";
|
|
|
|
}
|
|
|
|
|
2020-10-12 20:11:23 +02:00
|
|
|
if(draftId == 0) {
|
2020-11-02 16:17:00 +01:00
|
|
|
|
|
|
|
draftId = draftsApi.insertDraft(repositoryId, currentActiveAccountId, issueNumber, text, draftType, "TODO", tinyDB.getString("issueType"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
2020-10-12 20:11:23 +02:00
|
|
|
DraftsApi.updateDraft(text, (int) draftId, "TODO");
|
|
|
|
}
|
|
|
|
|
|
|
|
draftsHint.setVisibility(View.VISIBLE);
|
|
|
|
valueAnimator.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static BottomSheetReplyFragment newInstance(Bundle bundle) {
|
|
|
|
|
|
|
|
BottomSheetReplyFragment fragment = new BottomSheetReplyFragment();
|
|
|
|
fragment.setArguments(bundle);
|
|
|
|
|
|
|
|
return fragment;
|
|
|
|
}
|
2020-11-02 16:17:00 +01:00
|
|
|
|
|
|
|
public void setOnInteractedListener(OnInteractedListener onInteractedListener) {
|
|
|
|
|
|
|
|
this.onInteractedListener = onInteractedListener;
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface OnInteractedListener { void onInteracted(); }
|
|
|
|
|
2020-10-12 20:11:23 +02:00
|
|
|
}
|