2019-01-23 18:03:33 +01:00
|
|
|
package org.telegram.ui;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Canvas;
|
2020-01-23 07:15:40 +01:00
|
|
|
import android.graphics.PorterDuff;
|
|
|
|
import android.graphics.PorterDuffColorFilter;
|
2019-01-23 18:03:33 +01:00
|
|
|
import android.graphics.Rect;
|
2020-01-23 07:15:40 +01:00
|
|
|
import android.graphics.drawable.Drawable;
|
2019-01-23 18:03:33 +01:00
|
|
|
import android.text.Editable;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.text.TextWatcher;
|
2020-05-01 04:08:26 +02:00
|
|
|
import android.view.ActionMode;
|
2019-01-23 18:03:33 +01:00
|
|
|
import android.view.Gravity;
|
|
|
|
import android.view.KeyEvent;
|
2020-05-01 04:08:26 +02:00
|
|
|
import android.view.Menu;
|
2019-01-23 18:03:33 +01:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.view.inputmethod.EditorInfo;
|
|
|
|
import android.widget.FrameLayout;
|
2020-06-25 17:12:58 +02:00
|
|
|
import android.widget.TextView;
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
import org.telegram.messenger.AndroidUtilities;
|
2020-01-23 07:15:40 +01:00
|
|
|
import org.telegram.messenger.ChatObject;
|
2019-01-23 18:03:33 +01:00
|
|
|
import org.telegram.messenger.LocaleController;
|
|
|
|
import org.telegram.messenger.R;
|
2020-01-23 07:15:40 +01:00
|
|
|
import org.telegram.messenger.Utilities;
|
|
|
|
import org.telegram.tgnet.SerializedData;
|
2019-01-23 18:03:33 +01:00
|
|
|
import org.telegram.tgnet.TLRPC;
|
|
|
|
import org.telegram.ui.ActionBar.ActionBar;
|
|
|
|
import org.telegram.ui.ActionBar.ActionBarMenu;
|
|
|
|
import org.telegram.ui.ActionBar.ActionBarMenuItem;
|
|
|
|
import org.telegram.ui.ActionBar.AlertDialog;
|
|
|
|
import org.telegram.ui.ActionBar.BaseFragment;
|
|
|
|
import org.telegram.ui.ActionBar.SimpleTextView;
|
|
|
|
import org.telegram.ui.ActionBar.Theme;
|
|
|
|
import org.telegram.ui.ActionBar.ThemeDescription;
|
|
|
|
import org.telegram.ui.Cells.HeaderCell;
|
|
|
|
import org.telegram.ui.Cells.PollEditTextCell;
|
|
|
|
import org.telegram.ui.Cells.ShadowSectionCell;
|
2020-01-23 07:15:40 +01:00
|
|
|
import org.telegram.ui.Cells.TextCell;
|
|
|
|
import org.telegram.ui.Cells.TextCheckCell;
|
2019-01-23 18:03:33 +01:00
|
|
|
import org.telegram.ui.Cells.TextInfoPrivacyCell;
|
2019-09-10 12:56:11 +02:00
|
|
|
import org.telegram.ui.Components.AlertsCreator;
|
2020-04-24 12:30:21 +02:00
|
|
|
import org.telegram.ui.Components.ChatAttachAlertPollLayout;
|
2020-01-23 07:15:40 +01:00
|
|
|
import org.telegram.ui.Components.CombinedDrawable;
|
2019-01-23 18:03:33 +01:00
|
|
|
import org.telegram.ui.Components.EditTextBoldCursor;
|
2020-01-23 07:15:40 +01:00
|
|
|
import org.telegram.ui.Components.HintView;
|
2019-01-23 18:03:33 +01:00
|
|
|
import org.telegram.ui.Components.LayoutHelper;
|
|
|
|
import org.telegram.ui.Components.RecyclerListView;
|
|
|
|
|
2020-04-24 11:21:58 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
2020-01-23 07:15:40 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
2019-05-14 14:08:05 +02:00
|
|
|
import androidx.recyclerview.widget.DefaultItemAnimator;
|
|
|
|
import androidx.recyclerview.widget.ItemTouchHelper;
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
2019-01-23 18:03:33 +01:00
|
|
|
public class PollCreateActivity extends BaseFragment {
|
|
|
|
|
|
|
|
private ActionBarMenuItem doneItem;
|
|
|
|
private ListAdapter listAdapter;
|
|
|
|
private RecyclerListView listView;
|
2019-09-10 12:56:11 +02:00
|
|
|
private ChatActivity parentFragment;
|
2020-01-23 07:15:40 +01:00
|
|
|
private HintView hintView;
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
private String[] answers = new String[10];
|
2020-01-23 07:15:40 +01:00
|
|
|
private boolean[] answersChecks = new boolean[10];
|
2019-01-23 18:03:33 +01:00
|
|
|
private int answersCount = 1;
|
|
|
|
private String questionString;
|
2020-05-01 04:08:26 +02:00
|
|
|
private CharSequence solutionString;
|
2020-01-23 07:15:40 +01:00
|
|
|
private boolean anonymousPoll = true;
|
|
|
|
private boolean multipleChoise;
|
|
|
|
private boolean quizPoll;
|
|
|
|
private boolean hintShowed;
|
|
|
|
private int quizOnly;
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
private PollCreateActivityDelegate delegate;
|
|
|
|
|
|
|
|
private int requestFieldFocusAtPosition = -1;
|
|
|
|
|
|
|
|
private int questionHeaderRow;
|
|
|
|
private int questionRow;
|
2020-04-24 11:21:58 +02:00
|
|
|
private int solutionRow;
|
|
|
|
private int solutionInfoRow;
|
2019-01-23 18:03:33 +01:00
|
|
|
private int questionSectionRow;
|
|
|
|
private int answerHeaderRow;
|
|
|
|
private int answerStartRow;
|
|
|
|
private int addAnswerRow;
|
|
|
|
private int answerSectionRow;
|
2020-01-23 07:15:40 +01:00
|
|
|
private int settingsHeaderRow;
|
|
|
|
private int anonymousRow;
|
|
|
|
private int multipleRow;
|
|
|
|
private int quizRow;
|
|
|
|
private int settingsSectionRow;
|
2019-01-23 18:03:33 +01:00
|
|
|
private int rowCount;
|
|
|
|
|
|
|
|
private static final int done_button = 1;
|
|
|
|
|
|
|
|
public interface PollCreateActivityDelegate {
|
2020-01-23 07:15:40 +01:00
|
|
|
void sendPoll(TLRPC.TL_messageMediaPoll poll, HashMap<String, String> params, boolean notify, int scheduleDate);
|
2019-01-23 18:03:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public class TouchHelperCallback extends ItemTouchHelper.Callback {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isLongPressDragEnabled() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
|
|
|
|
if (viewHolder.getItemViewType() != 5) {
|
|
|
|
return makeMovementFlags(0, 0);
|
|
|
|
}
|
|
|
|
return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {
|
|
|
|
if (source.getItemViewType() != target.getItemViewType()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
listAdapter.swapElements(source.getAdapterPosition(), target.getAdapterPosition());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
|
|
|
|
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
|
|
|
|
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
|
|
|
|
listView.cancelClickRunnables(false);
|
|
|
|
viewHolder.itemView.setPressed(true);
|
|
|
|
}
|
|
|
|
super.onSelectedChanged(viewHolder, actionState);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
|
|
|
|
super.clearView(recyclerView, viewHolder);
|
|
|
|
viewHolder.itemView.setPressed(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-23 07:15:40 +01:00
|
|
|
public PollCreateActivity(ChatActivity chatActivity, Boolean quiz) {
|
2019-09-10 12:56:11 +02:00
|
|
|
super();
|
|
|
|
parentFragment = chatActivity;
|
2020-01-23 07:15:40 +01:00
|
|
|
if (quiz != null) {
|
|
|
|
quizPoll = quiz;
|
|
|
|
quizOnly = quizPoll ? 1 : 2;
|
|
|
|
}
|
2019-09-10 12:56:11 +02:00
|
|
|
}
|
|
|
|
|
2019-01-23 18:03:33 +01:00
|
|
|
@Override
|
|
|
|
public boolean onFragmentCreate() {
|
|
|
|
super.onFragmentCreate();
|
|
|
|
updateRows();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View createView(Context context) {
|
|
|
|
actionBar.setBackButtonImage(R.drawable.ic_ab_back);
|
2020-01-23 07:15:40 +01:00
|
|
|
if (quizOnly == 1) {
|
|
|
|
actionBar.setTitle(LocaleController.getString("NewQuiz", R.string.NewQuiz));
|
|
|
|
} else {
|
|
|
|
actionBar.setTitle(LocaleController.getString("NewPoll", R.string.NewPoll));
|
|
|
|
}
|
2019-01-23 18:03:33 +01:00
|
|
|
if (AndroidUtilities.isTablet()) {
|
|
|
|
actionBar.setOccupyStatusBar(false);
|
|
|
|
}
|
|
|
|
actionBar.setAllowOverlayTitle(true);
|
|
|
|
actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
|
|
|
|
@Override
|
|
|
|
public void onItemClick(int id) {
|
|
|
|
if (id == -1) {
|
|
|
|
if (checkDiscard()) {
|
|
|
|
finishFragment();
|
|
|
|
}
|
|
|
|
} else if (id == done_button) {
|
2020-01-23 07:15:40 +01:00
|
|
|
if (quizPoll && doneItem.getAlpha() != 1.0f) {
|
|
|
|
int checksCount = 0;
|
|
|
|
for (int a = 0; a < answersChecks.length; a++) {
|
2020-05-01 04:08:26 +02:00
|
|
|
if (!TextUtils.isEmpty(ChatAttachAlertPollLayout.getFixedString(answers[a])) && answersChecks[a]) {
|
2020-01-23 07:15:40 +01:00
|
|
|
checksCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (checksCount <= 0) {
|
|
|
|
showQuizHint();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-01-23 18:03:33 +01:00
|
|
|
TLRPC.TL_messageMediaPoll poll = new TLRPC.TL_messageMediaPoll();
|
|
|
|
poll.poll = new TLRPC.TL_poll();
|
2020-01-23 07:15:40 +01:00
|
|
|
poll.poll.multiple_choice = multipleChoise;
|
|
|
|
poll.poll.quiz = quizPoll;
|
|
|
|
poll.poll.public_voters = !anonymousPoll;
|
2020-05-01 04:08:26 +02:00
|
|
|
poll.poll.question = ChatAttachAlertPollLayout.getFixedString(questionString).toString();
|
2020-01-23 07:15:40 +01:00
|
|
|
SerializedData serializedData = new SerializedData(10);
|
2019-01-23 18:03:33 +01:00
|
|
|
for (int a = 0; a < answers.length; a++) {
|
2020-05-01 04:08:26 +02:00
|
|
|
if (TextUtils.isEmpty(ChatAttachAlertPollLayout.getFixedString(answers[a]))) {
|
2019-01-23 18:03:33 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
TLRPC.TL_pollAnswer answer = new TLRPC.TL_pollAnswer();
|
2020-05-01 04:08:26 +02:00
|
|
|
answer.text = ChatAttachAlertPollLayout.getFixedString(answers[a]).toString();
|
2019-01-23 18:03:33 +01:00
|
|
|
answer.option = new byte[1];
|
2019-03-03 21:40:48 +01:00
|
|
|
answer.option[0] = (byte) (48 + poll.poll.answers.size());
|
2019-01-23 18:03:33 +01:00
|
|
|
poll.poll.answers.add(answer);
|
2020-01-23 07:15:40 +01:00
|
|
|
if ((multipleChoise || quizPoll) && answersChecks[a]) {
|
|
|
|
serializedData.writeByte(answer.option[0]);
|
|
|
|
}
|
2019-01-23 18:03:33 +01:00
|
|
|
}
|
2020-01-23 07:15:40 +01:00
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
params.put("answers", Utilities.bytesToHex(serializedData.toByteArray()));
|
2019-01-23 18:03:33 +01:00
|
|
|
poll.results = new TLRPC.TL_pollResults();
|
2020-05-01 04:08:26 +02:00
|
|
|
CharSequence solution = ChatAttachAlertPollLayout.getFixedString(solutionString);
|
2020-05-01 12:16:27 +02:00
|
|
|
if (solution != null) {
|
|
|
|
poll.results.solution = solution.toString();
|
|
|
|
CharSequence[] message = new CharSequence[]{solution};
|
|
|
|
ArrayList<TLRPC.MessageEntity> entities = getMediaDataController().getEntities(message, true);
|
|
|
|
if (entities != null && !entities.isEmpty()) {
|
|
|
|
poll.results.solution_entities = entities;
|
|
|
|
}
|
|
|
|
if (!TextUtils.isEmpty(poll.results.solution)) {
|
|
|
|
poll.results.flags |= 16;
|
|
|
|
}
|
2020-04-24 11:21:58 +02:00
|
|
|
}
|
2019-09-10 12:56:11 +02:00
|
|
|
if (parentFragment.isInScheduleMode()) {
|
2019-12-31 14:08:08 +01:00
|
|
|
AlertsCreator.createScheduleDatePickerDialog(getParentActivity(), parentFragment.getDialogId(), (notify, scheduleDate) -> {
|
2020-01-23 07:15:40 +01:00
|
|
|
delegate.sendPoll(poll, params, notify, scheduleDate);
|
2019-09-10 12:56:11 +02:00
|
|
|
finishFragment();
|
|
|
|
});
|
|
|
|
} else {
|
2020-01-23 07:15:40 +01:00
|
|
|
delegate.sendPoll(poll, params, true, 0);
|
2019-09-10 12:56:11 +02:00
|
|
|
finishFragment();
|
|
|
|
}
|
2019-01-23 18:03:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ActionBarMenu menu = actionBar.createMenu();
|
2020-01-23 07:15:40 +01:00
|
|
|
doneItem = menu.addItem(done_button, LocaleController.getString("Create", R.string.Create).toUpperCase());
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
listAdapter = new ListAdapter(context);
|
|
|
|
|
|
|
|
fragmentView = new FrameLayout(context);
|
|
|
|
fragmentView.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundGray));
|
|
|
|
FrameLayout frameLayout = (FrameLayout) fragmentView;
|
|
|
|
|
|
|
|
listView = new RecyclerListView(context) {
|
2020-01-23 07:15:40 +01:00
|
|
|
@Override
|
|
|
|
protected void requestChildOnScreen(View child, View focused) {
|
|
|
|
if (!(child instanceof PollEditTextCell)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
super.requestChildOnScreen(child, focused);
|
|
|
|
}
|
|
|
|
|
2019-01-23 18:03:33 +01:00
|
|
|
@Override
|
|
|
|
public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
|
|
|
|
rectangle.bottom += AndroidUtilities.dp(60);
|
|
|
|
return super.requestChildRectangleOnScreen(child, rectangle, immediate);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
listView.setVerticalScrollBarEnabled(false);
|
|
|
|
((DefaultItemAnimator) listView.getItemAnimator()).setDelayAnimations(false);
|
|
|
|
listView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
|
|
|
|
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new TouchHelperCallback());
|
|
|
|
itemTouchHelper.attachToRecyclerView(listView);
|
|
|
|
frameLayout.addView(listView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, Gravity.TOP | Gravity.LEFT));
|
|
|
|
listView.setAdapter(listAdapter);
|
|
|
|
listView.setOnItemClickListener((view, position) -> {
|
|
|
|
if (position == addAnswerRow) {
|
|
|
|
addNewField();
|
2020-01-23 07:15:40 +01:00
|
|
|
} else if (view instanceof TextCheckCell) {
|
|
|
|
TextCheckCell cell = (TextCheckCell) view;
|
|
|
|
boolean checked;
|
|
|
|
boolean wasChecksBefore = quizPoll;
|
|
|
|
if (position == anonymousRow) {
|
|
|
|
checked = anonymousPoll = !anonymousPoll;
|
|
|
|
} else if (position == multipleRow) {
|
|
|
|
checked = multipleChoise = !multipleChoise;
|
|
|
|
if (multipleChoise && quizPoll) {
|
2020-04-24 11:21:58 +02:00
|
|
|
int prevSolutionRow = solutionRow;
|
2020-01-23 07:15:40 +01:00
|
|
|
quizPoll = false;
|
2020-04-24 11:21:58 +02:00
|
|
|
updateRows();
|
2020-01-23 07:15:40 +01:00
|
|
|
RecyclerView.ViewHolder holder = listView.findViewHolderForAdapterPosition(quizRow);
|
|
|
|
if (holder != null) {
|
|
|
|
((TextCheckCell) holder.itemView).setChecked(false);
|
|
|
|
} else {
|
|
|
|
listAdapter.notifyItemChanged(quizRow);
|
|
|
|
}
|
2020-04-24 11:21:58 +02:00
|
|
|
listAdapter.notifyItemRangeRemoved(prevSolutionRow, 2);
|
2020-01-23 07:15:40 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (quizOnly != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
checked = quizPoll = !quizPoll;
|
2020-04-24 11:21:58 +02:00
|
|
|
int prevSolutionRow = solutionRow;
|
|
|
|
updateRows();
|
|
|
|
if (quizPoll) {
|
|
|
|
listAdapter.notifyItemRangeInserted(solutionRow, 2);
|
|
|
|
} else {
|
|
|
|
listAdapter.notifyItemRangeRemoved(prevSolutionRow, 2);
|
|
|
|
}
|
2020-01-23 07:15:40 +01:00
|
|
|
if (quizPoll && multipleChoise) {
|
|
|
|
multipleChoise = false;
|
|
|
|
RecyclerView.ViewHolder holder = listView.findViewHolderForAdapterPosition(multipleRow);
|
|
|
|
if (holder != null) {
|
|
|
|
((TextCheckCell) holder.itemView).setChecked(false);
|
|
|
|
} else {
|
|
|
|
listAdapter.notifyItemChanged(multipleRow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (quizPoll) {
|
|
|
|
boolean was = false;
|
|
|
|
for (int a = 0; a < answersChecks.length; a++) {
|
|
|
|
if (was) {
|
|
|
|
answersChecks[a] = false;
|
|
|
|
} else if (answersChecks[a]) {
|
|
|
|
was = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hintShowed && !quizPoll) {
|
|
|
|
hintView.hide();
|
|
|
|
}
|
|
|
|
int count = listView.getChildCount();
|
|
|
|
for (int a = answerStartRow; a < answerStartRow + answersCount; a++) {
|
|
|
|
RecyclerView.ViewHolder holder = listView.findViewHolderForAdapterPosition(a);
|
|
|
|
if (holder != null && holder.itemView instanceof PollEditTextCell) {
|
|
|
|
PollEditTextCell pollEditTextCell = (PollEditTextCell) holder.itemView;
|
|
|
|
pollEditTextCell.setShowCheckBox(quizPoll, true);
|
|
|
|
pollEditTextCell.setChecked(answersChecks[a - answerStartRow], wasChecksBefore);
|
|
|
|
if (pollEditTextCell.getTop() > AndroidUtilities.dp(40) && position == quizRow && !hintShowed) {
|
|
|
|
hintView.showForView(pollEditTextCell.getCheckBox(), true);
|
|
|
|
hintShowed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cell.setChecked(checked);
|
|
|
|
checkDoneButton();
|
2019-01-23 18:03:33 +01:00
|
|
|
}
|
|
|
|
});
|
2020-04-24 11:21:58 +02:00
|
|
|
listView.setOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
|
|
@Override
|
2020-05-01 04:08:26 +02:00
|
|
|
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
|
|
|
|
2020-04-24 11:21:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-05-01 04:08:26 +02:00
|
|
|
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
|
|
|
|
if (dy != 0 && hintView != null) {
|
|
|
|
hintView.hide();
|
|
|
|
}
|
2020-04-24 11:21:58 +02:00
|
|
|
}
|
|
|
|
});
|
2019-01-23 18:03:33 +01:00
|
|
|
|
2020-01-23 07:15:40 +01:00
|
|
|
hintView = new HintView(context, 4);
|
|
|
|
hintView.setText(LocaleController.getString("PollTapToSelect", R.string.PollTapToSelect));
|
|
|
|
hintView.setAlpha(0.0f);
|
|
|
|
hintView.setVisibility(View.INVISIBLE);
|
|
|
|
frameLayout.addView(hintView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.LEFT | Gravity.TOP, 19, 0, 19, 0));
|
|
|
|
|
2019-01-23 18:03:33 +01:00
|
|
|
checkDoneButton();
|
|
|
|
|
|
|
|
return fragmentView;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
if (listAdapter != null) {
|
|
|
|
listAdapter.notifyDataSetChanged();
|
|
|
|
}
|
2020-09-30 15:48:47 +02:00
|
|
|
AndroidUtilities.requestAdjustResize(getParentActivity(), classGuid);
|
2019-01-23 18:03:33 +01:00
|
|
|
}
|
|
|
|
|
2020-01-23 07:15:40 +01:00
|
|
|
private void showQuizHint() {
|
|
|
|
int count = listView.getChildCount();
|
|
|
|
for (int a = answerStartRow; a < answerStartRow + answersCount; a++) {
|
|
|
|
RecyclerView.ViewHolder holder = listView.findViewHolderForAdapterPosition(a);
|
|
|
|
if (holder != null && holder.itemView instanceof PollEditTextCell) {
|
|
|
|
PollEditTextCell pollEditTextCell = (PollEditTextCell) holder.itemView;
|
|
|
|
if (pollEditTextCell.getTop() > AndroidUtilities.dp(40)) {
|
|
|
|
hintView.showForView(pollEditTextCell.getCheckBox(), true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 18:03:33 +01:00
|
|
|
private void checkDoneButton() {
|
|
|
|
boolean enabled = true;
|
2020-01-23 07:15:40 +01:00
|
|
|
int checksCount = 0;
|
|
|
|
if (quizPoll) {
|
|
|
|
for (int a = 0; a < answersChecks.length; a++) {
|
2020-05-01 04:08:26 +02:00
|
|
|
if (!TextUtils.isEmpty(ChatAttachAlertPollLayout.getFixedString(answers[a])) && answersChecks[a]) {
|
2020-01-23 07:15:40 +01:00
|
|
|
checksCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 04:08:26 +02:00
|
|
|
if (!TextUtils.isEmpty(ChatAttachAlertPollLayout.getFixedString(solutionString)) && solutionString.length() > ChatAttachAlertPollLayout.MAX_SOLUTION_LENGTH) {
|
2020-04-24 11:21:58 +02:00
|
|
|
enabled = false;
|
2020-05-01 04:08:26 +02:00
|
|
|
} else if (TextUtils.isEmpty(ChatAttachAlertPollLayout.getFixedString(questionString)) || questionString.length() > ChatAttachAlertPollLayout.MAX_QUESTION_LENGTH) {
|
2019-01-23 18:03:33 +01:00
|
|
|
enabled = false;
|
|
|
|
} else {
|
|
|
|
int count = 0;
|
|
|
|
for (int a = 0; a < answers.length; a++) {
|
2020-05-01 04:08:26 +02:00
|
|
|
if (!TextUtils.isEmpty(ChatAttachAlertPollLayout.getFixedString(answers[a]))) {
|
2020-04-24 12:30:21 +02:00
|
|
|
if (answers[a].length() > ChatAttachAlertPollLayout.MAX_ANSWER_LENGTH) {
|
2019-01-23 18:03:33 +01:00
|
|
|
count = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
2020-01-23 07:15:40 +01:00
|
|
|
if (count < 2 || quizPoll && checksCount < 1) {
|
2019-01-23 18:03:33 +01:00
|
|
|
enabled = false;
|
|
|
|
}
|
|
|
|
}
|
2020-01-23 07:15:40 +01:00
|
|
|
doneItem.setEnabled(quizPoll && checksCount == 0 || enabled);
|
2019-01-23 18:03:33 +01:00
|
|
|
doneItem.setAlpha(enabled ? 1.0f : 0.5f);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateRows() {
|
|
|
|
rowCount = 0;
|
|
|
|
questionHeaderRow = rowCount++;
|
|
|
|
questionRow = rowCount++;
|
|
|
|
questionSectionRow = rowCount++;
|
|
|
|
answerHeaderRow = rowCount++;
|
|
|
|
if (answersCount != 0) {
|
|
|
|
answerStartRow = rowCount;
|
|
|
|
rowCount += answersCount;
|
|
|
|
} else {
|
|
|
|
answerStartRow = -1;
|
|
|
|
}
|
|
|
|
if (answersCount != answers.length) {
|
|
|
|
addAnswerRow = rowCount++;
|
|
|
|
} else {
|
|
|
|
addAnswerRow = -1;
|
|
|
|
}
|
|
|
|
answerSectionRow = rowCount++;
|
2020-01-23 07:15:40 +01:00
|
|
|
settingsHeaderRow = rowCount++;
|
|
|
|
TLRPC.Chat chat = parentFragment.getCurrentChat();
|
|
|
|
if (!ChatObject.isChannel(chat) || chat.megagroup) {
|
|
|
|
anonymousRow = rowCount++;
|
|
|
|
} else {
|
|
|
|
anonymousRow = -1;
|
|
|
|
}
|
|
|
|
if (quizOnly != 1) {
|
|
|
|
multipleRow = rowCount++;
|
|
|
|
} else {
|
|
|
|
multipleRow = -1;
|
|
|
|
}
|
|
|
|
if (quizOnly == 0) {
|
|
|
|
quizRow = rowCount++;
|
|
|
|
} else {
|
|
|
|
quizRow = -1;
|
|
|
|
}
|
|
|
|
settingsSectionRow = rowCount++;
|
2020-04-24 11:21:58 +02:00
|
|
|
if (quizPoll) {
|
|
|
|
solutionRow = rowCount++;
|
|
|
|
solutionInfoRow = rowCount++;
|
|
|
|
} else {
|
|
|
|
solutionRow = -1;
|
|
|
|
solutionInfoRow = -1;
|
|
|
|
}
|
2019-01-23 18:03:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onBackPressed() {
|
|
|
|
return checkDiscard();
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean checkDiscard() {
|
2020-05-01 04:08:26 +02:00
|
|
|
boolean allowDiscard = TextUtils.isEmpty(ChatAttachAlertPollLayout.getFixedString(questionString));
|
2019-01-23 18:03:33 +01:00
|
|
|
if (allowDiscard) {
|
|
|
|
for (int a = 0; a < answersCount; a++) {
|
2020-05-01 04:08:26 +02:00
|
|
|
allowDiscard = TextUtils.isEmpty(ChatAttachAlertPollLayout.getFixedString(answers[a]));
|
2019-01-23 18:03:33 +01:00
|
|
|
if (!allowDiscard) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!allowDiscard) {
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
|
|
|
|
builder.setTitle(LocaleController.getString("CancelPollAlertTitle", R.string.CancelPollAlertTitle));
|
|
|
|
builder.setMessage(LocaleController.getString("CancelPollAlertText", R.string.CancelPollAlertText));
|
2020-01-23 07:15:40 +01:00
|
|
|
builder.setPositiveButton(LocaleController.getString("PassportDiscard", R.string.PassportDiscard), (dialogInterface, i) -> finishFragment());
|
2019-01-23 18:03:33 +01:00
|
|
|
builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
|
|
|
|
showDialog(builder.create());
|
|
|
|
}
|
|
|
|
return allowDiscard;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setDelegate(PollCreateActivityDelegate pollCreateActivityDelegate) {
|
|
|
|
delegate = pollCreateActivityDelegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setTextLeft(View cell, int index) {
|
2020-04-24 12:30:21 +02:00
|
|
|
if (!(cell instanceof PollEditTextCell)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PollEditTextCell textCell = (PollEditTextCell) cell;
|
|
|
|
int max;
|
|
|
|
int left;
|
|
|
|
if (index == questionRow) {
|
|
|
|
max = ChatAttachAlertPollLayout.MAX_QUESTION_LENGTH;
|
|
|
|
left = ChatAttachAlertPollLayout.MAX_QUESTION_LENGTH - (questionString != null ? questionString.length() : 0);
|
|
|
|
} else if (index == solutionRow) {
|
|
|
|
max = ChatAttachAlertPollLayout.MAX_SOLUTION_LENGTH;
|
|
|
|
left = ChatAttachAlertPollLayout.MAX_SOLUTION_LENGTH - (solutionString != null ? solutionString.length() : 0);
|
|
|
|
} else if (index >= answerStartRow && index < answerStartRow + answersCount) {
|
|
|
|
index -= answerStartRow;
|
|
|
|
max = ChatAttachAlertPollLayout.MAX_ANSWER_LENGTH;
|
|
|
|
left = ChatAttachAlertPollLayout.MAX_ANSWER_LENGTH - (answers[index] != null ? answers[index].length() : 0);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (left <= max - max * 0.7f) {
|
|
|
|
textCell.setText2(String.format("%d", left));
|
|
|
|
SimpleTextView textView = textCell.getTextView2();
|
|
|
|
String key = left < 0 ? Theme.key_windowBackgroundWhiteRedText5 : Theme.key_windowBackgroundWhiteGrayText3;
|
|
|
|
textView.setTextColor(Theme.getColor(key));
|
|
|
|
textView.setTag(key);
|
|
|
|
} else {
|
|
|
|
textCell.setText2("");
|
2019-01-23 18:03:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addNewField() {
|
2020-01-23 07:15:40 +01:00
|
|
|
answersChecks[answersCount] = false;
|
2019-01-23 18:03:33 +01:00
|
|
|
answersCount++;
|
|
|
|
if (answersCount == answers.length) {
|
|
|
|
listAdapter.notifyItemRemoved(addAnswerRow);
|
|
|
|
}
|
|
|
|
listAdapter.notifyItemInserted(addAnswerRow);
|
|
|
|
updateRows();
|
|
|
|
requestFieldFocusAtPosition = answerStartRow + answersCount - 1;
|
|
|
|
listAdapter.notifyItemChanged(answerSectionRow);
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ListAdapter extends RecyclerListView.SelectionAdapter {
|
|
|
|
|
|
|
|
private Context mContext;
|
|
|
|
|
|
|
|
public ListAdapter(Context context) {
|
|
|
|
mContext = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return rowCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
|
|
|
|
switch (holder.getItemViewType()) {
|
|
|
|
case 0: {
|
|
|
|
HeaderCell cell = (HeaderCell) holder.itemView;
|
|
|
|
if (position == questionHeaderRow) {
|
2020-04-24 11:21:58 +02:00
|
|
|
cell.setText(LocaleController.getString("PollQuestion", R.string.PollQuestion));
|
2019-01-23 18:03:33 +01:00
|
|
|
} else if (position == answerHeaderRow) {
|
2020-01-23 07:15:40 +01:00
|
|
|
if (quizOnly == 1) {
|
|
|
|
cell.setText(LocaleController.getString("QuizAnswers", R.string.QuizAnswers));
|
|
|
|
} else {
|
2020-04-24 11:21:58 +02:00
|
|
|
cell.setText(LocaleController.getString("AnswerOptions", R.string.AnswerOptions));
|
2020-01-23 07:15:40 +01:00
|
|
|
}
|
|
|
|
} else if (position == settingsHeaderRow) {
|
|
|
|
cell.setText(LocaleController.getString("Settings", R.string.Settings));
|
2019-01-23 18:03:33 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2: {
|
|
|
|
TextInfoPrivacyCell cell = (TextInfoPrivacyCell) holder.itemView;
|
2020-04-24 12:30:21 +02:00
|
|
|
cell.setFixedSize(0);
|
2019-01-23 18:03:33 +01:00
|
|
|
cell.setBackgroundDrawable(Theme.getThemedDrawable(mContext, R.drawable.greydivider_bottom, Theme.key_windowBackgroundGrayShadow));
|
2020-04-24 11:21:58 +02:00
|
|
|
if (position == solutionInfoRow) {
|
|
|
|
cell.setText(LocaleController.getString("AddAnExplanationInfo", R.string.AddAnExplanationInfo));
|
|
|
|
} else if (position == settingsSectionRow) {
|
2020-01-23 07:15:40 +01:00
|
|
|
if (quizOnly != 0) {
|
2020-04-24 12:30:21 +02:00
|
|
|
cell.setFixedSize(12);
|
2020-01-23 07:15:40 +01:00
|
|
|
cell.setText(null);
|
|
|
|
} else {
|
|
|
|
cell.setText(LocaleController.getString("QuizInfo", R.string.QuizInfo));
|
|
|
|
}
|
|
|
|
} else if (10 - answersCount <= 0) {
|
2019-01-23 18:03:33 +01:00
|
|
|
cell.setText(LocaleController.getString("AddAnOptionInfoMax", R.string.AddAnOptionInfoMax));
|
|
|
|
} else {
|
|
|
|
cell.setText(LocaleController.formatString("AddAnOptionInfo", R.string.AddAnOptionInfo, LocaleController.formatPluralString("Option", 10 - answersCount)));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 3: {
|
2020-01-23 07:15:40 +01:00
|
|
|
TextCell textCell = (TextCell) holder.itemView;
|
|
|
|
textCell.setColors(null, Theme.key_windowBackgroundWhiteBlueText4);
|
|
|
|
Drawable drawable1 = mContext.getResources().getDrawable(R.drawable.poll_add_circle);
|
|
|
|
Drawable drawable2 = mContext.getResources().getDrawable(R.drawable.poll_add_plus);
|
2020-06-25 17:12:58 +02:00
|
|
|
drawable1.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_switchTrackChecked), PorterDuff.Mode.SRC_IN));
|
|
|
|
drawable2.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_checkboxCheck), PorterDuff.Mode.SRC_IN));
|
2020-01-23 07:15:40 +01:00
|
|
|
CombinedDrawable combinedDrawable = new CombinedDrawable(drawable1, drawable2);
|
|
|
|
textCell.setTextAndIcon(LocaleController.getString("AddAnOption", R.string.AddAnOption), combinedDrawable, false);
|
2019-01-23 18:03:33 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-01-23 07:15:40 +01:00
|
|
|
case 6: {
|
|
|
|
TextCheckCell checkCell = (TextCheckCell) holder.itemView;
|
|
|
|
if (position == anonymousRow) {
|
|
|
|
checkCell.setTextAndCheck(LocaleController.getString("PollAnonymous", R.string.PollAnonymous), anonymousPoll, multipleRow != -1 || quizRow != -1);
|
|
|
|
checkCell.setEnabled(true, null);
|
|
|
|
} else if (position == multipleRow) {
|
|
|
|
checkCell.setTextAndCheck(LocaleController.getString("PollMultiple", R.string.PollMultiple), multipleChoise, quizRow != -1);
|
|
|
|
checkCell.setEnabled(true, null);
|
|
|
|
} else if (position == quizRow) {
|
|
|
|
checkCell.setTextAndCheck(LocaleController.getString("PollQuiz", R.string.PollQuiz), quizPoll, false);
|
|
|
|
checkCell.setEnabled(quizOnly == 0, null);
|
2019-01-23 18:03:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
|
2019-08-22 01:53:26 +02:00
|
|
|
int viewType = holder.getItemViewType();
|
2020-01-23 07:15:40 +01:00
|
|
|
if (viewType == 4) {
|
|
|
|
PollEditTextCell textCell = (PollEditTextCell) holder.itemView;
|
|
|
|
textCell.setTag(1);
|
|
|
|
textCell.setTextAndHint(questionString != null ? questionString : "", LocaleController.getString("QuestionHint", R.string.QuestionHint), false);
|
|
|
|
textCell.setTag(null);
|
2020-04-24 12:30:21 +02:00
|
|
|
setTextLeft(holder.itemView, holder.getAdapterPosition());
|
2020-01-23 07:15:40 +01:00
|
|
|
} else if (viewType == 5) {
|
|
|
|
int position = holder.getAdapterPosition();
|
|
|
|
PollEditTextCell textCell = (PollEditTextCell) holder.itemView;
|
|
|
|
textCell.setTag(1);
|
|
|
|
int index = position - answerStartRow;
|
|
|
|
textCell.setTextAndHint(answers[index], LocaleController.getString("OptionHint", R.string.OptionHint), true);
|
|
|
|
textCell.setTag(null);
|
|
|
|
if (requestFieldFocusAtPosition == position) {
|
|
|
|
EditTextBoldCursor editText = textCell.getTextView();
|
|
|
|
editText.requestFocus();
|
|
|
|
AndroidUtilities.showKeyboard(editText);
|
|
|
|
requestFieldFocusAtPosition = -1;
|
|
|
|
}
|
2020-04-24 12:30:21 +02:00
|
|
|
setTextLeft(holder.itemView, position);
|
2020-04-24 11:21:58 +02:00
|
|
|
} else if (viewType == 7) {
|
|
|
|
PollEditTextCell textCell = (PollEditTextCell) holder.itemView;
|
|
|
|
textCell.setTag(1);
|
|
|
|
textCell.setTextAndHint(solutionString != null ? solutionString : "", LocaleController.getString("AddAnExplanation", R.string.AddAnExplanation), false);
|
|
|
|
textCell.setTag(null);
|
2020-04-24 12:30:21 +02:00
|
|
|
setTextLeft(holder.itemView, holder.getAdapterPosition());
|
2020-01-23 07:15:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
|
|
|
|
if (holder.getItemViewType() == 4) {
|
|
|
|
PollEditTextCell editTextCell = (PollEditTextCell) holder.itemView;
|
|
|
|
EditTextBoldCursor editText = editTextCell.getTextView();
|
|
|
|
if (editText.isFocused()) {
|
|
|
|
editText.clearFocus();
|
|
|
|
AndroidUtilities.hideKeyboard(editText);
|
|
|
|
}
|
|
|
|
}
|
2019-01-23 18:03:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isEnabled(RecyclerView.ViewHolder holder) {
|
2020-01-23 07:15:40 +01:00
|
|
|
int position = holder.getAdapterPosition();
|
|
|
|
return position == addAnswerRow || position == anonymousRow || position == multipleRow || quizOnly == 0 && position == quizRow;
|
2019-01-23 18:03:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
|
|
|
View view;
|
|
|
|
switch (viewType) {
|
|
|
|
case 0:
|
2020-04-24 12:30:21 +02:00
|
|
|
view = new HeaderCell(mContext, Theme.key_windowBackgroundWhiteBlueHeader, 21, 15, false);
|
2019-01-23 18:03:33 +01:00
|
|
|
view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite));
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
view = new ShadowSectionCell(mContext);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
view = new TextInfoPrivacyCell(mContext);
|
|
|
|
break;
|
|
|
|
case 3:
|
2020-01-23 07:15:40 +01:00
|
|
|
view = new TextCell(mContext);
|
2019-01-23 18:03:33 +01:00
|
|
|
view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite));
|
|
|
|
break;
|
|
|
|
case 4: {
|
|
|
|
PollEditTextCell cell = new PollEditTextCell(mContext, null);
|
2020-04-24 12:30:21 +02:00
|
|
|
cell.createErrorTextView();
|
2019-01-23 18:03:33 +01:00
|
|
|
cell.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite));
|
|
|
|
cell.addTextWatcher(new TextWatcher() {
|
|
|
|
@Override
|
|
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
if (cell.getTag() != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
questionString = s.toString();
|
2020-04-24 12:30:21 +02:00
|
|
|
RecyclerView.ViewHolder holder = listView.findViewHolderForAdapterPosition(questionRow);
|
2019-01-23 18:03:33 +01:00
|
|
|
if (holder != null) {
|
2020-04-24 12:30:21 +02:00
|
|
|
setTextLeft(holder.itemView, questionRow);
|
2019-01-23 18:03:33 +01:00
|
|
|
}
|
|
|
|
checkDoneButton();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
view = cell;
|
|
|
|
break;
|
|
|
|
}
|
2020-01-23 07:15:40 +01:00
|
|
|
case 6:
|
|
|
|
view = new TextCheckCell(mContext);
|
|
|
|
view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite));
|
|
|
|
break;
|
2020-04-24 11:21:58 +02:00
|
|
|
case 7: {
|
2020-05-01 04:08:26 +02:00
|
|
|
PollEditTextCell cell = new PollEditTextCell(mContext, true, null) {
|
|
|
|
@Override
|
|
|
|
protected void onActionModeStart(EditTextBoldCursor editText, ActionMode actionMode) {
|
|
|
|
if (editText.isFocused() && editText.hasSelection()) {
|
|
|
|
Menu menu = actionMode.getMenu();
|
|
|
|
if (menu.findItem(android.R.id.copy) == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
parentFragment.fillActionModeMenu(menu);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-04-24 12:30:21 +02:00
|
|
|
cell.createErrorTextView();
|
2020-04-24 11:21:58 +02:00
|
|
|
cell.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite));
|
|
|
|
cell.addTextWatcher(new TextWatcher() {
|
|
|
|
@Override
|
|
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
if (cell.getTag() != null) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-01 04:08:26 +02:00
|
|
|
solutionString = s;
|
2020-04-24 12:30:21 +02:00
|
|
|
RecyclerView.ViewHolder holder = listView.findViewHolderForAdapterPosition(solutionRow);
|
2020-04-24 11:21:58 +02:00
|
|
|
if (holder != null) {
|
2020-04-24 12:30:21 +02:00
|
|
|
setTextLeft(holder.itemView, solutionRow);
|
|
|
|
}
|
2020-04-24 11:21:58 +02:00
|
|
|
checkDoneButton();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
view = cell;
|
|
|
|
break;
|
|
|
|
}
|
2019-01-23 18:03:33 +01:00
|
|
|
default: {
|
|
|
|
PollEditTextCell cell = new PollEditTextCell(mContext, v -> {
|
|
|
|
if (v.getTag() != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
v.setTag(1);
|
2020-01-23 07:15:40 +01:00
|
|
|
PollEditTextCell p = (PollEditTextCell) v.getParent();
|
|
|
|
RecyclerView.ViewHolder holder = listView.findContainingViewHolder(p);
|
2019-01-23 18:03:33 +01:00
|
|
|
if (holder != null) {
|
|
|
|
int position = holder.getAdapterPosition();
|
2020-01-23 13:58:50 +01:00
|
|
|
if (position != RecyclerView.NO_POSITION) {
|
|
|
|
int index = position - answerStartRow;
|
|
|
|
listAdapter.notifyItemRemoved(position);
|
|
|
|
System.arraycopy(answers, index + 1, answers, index, answers.length - 1 - index);
|
|
|
|
System.arraycopy(answersChecks, index + 1, answersChecks, index, answersChecks.length - 1 - index);
|
|
|
|
answers[answers.length - 1] = null;
|
|
|
|
answersChecks[answersChecks.length - 1] = false;
|
|
|
|
answersCount--;
|
|
|
|
if (answersCount == answers.length - 1) {
|
|
|
|
listAdapter.notifyItemInserted(answerStartRow + answers.length - 1);
|
|
|
|
}
|
|
|
|
holder = listView.findViewHolderForAdapterPosition(position - 1);
|
|
|
|
EditTextBoldCursor editText = p.getTextView();
|
|
|
|
if (holder != null && holder.itemView instanceof PollEditTextCell) {
|
|
|
|
PollEditTextCell editTextCell = (PollEditTextCell) holder.itemView;
|
|
|
|
editTextCell.getTextView().requestFocus();
|
|
|
|
} else if (editText.isFocused()) {
|
|
|
|
AndroidUtilities.hideKeyboard(editText);
|
|
|
|
}
|
|
|
|
editText.clearFocus();
|
|
|
|
checkDoneButton();
|
|
|
|
updateRows();
|
|
|
|
listAdapter.notifyItemChanged(answerSectionRow);
|
2019-01-23 18:03:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}) {
|
|
|
|
@Override
|
|
|
|
protected boolean drawDivider() {
|
|
|
|
RecyclerView.ViewHolder holder = listView.findContainingViewHolder(this);
|
|
|
|
if (holder != null) {
|
|
|
|
int position = holder.getAdapterPosition();
|
|
|
|
if (answersCount == 10 && position == answerStartRow + answersCount - 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-01-23 07:15:40 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean shouldShowCheckBox() {
|
|
|
|
return quizPoll;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCheckBoxClick(PollEditTextCell editText, boolean checked) {
|
|
|
|
if (checked && quizPoll) {
|
2020-04-24 11:21:58 +02:00
|
|
|
Arrays.fill(answersChecks, false);
|
2020-01-23 07:15:40 +01:00
|
|
|
int count = listView.getChildCount();
|
|
|
|
for (int a = answerStartRow; a < answerStartRow + answersCount; a++) {
|
|
|
|
RecyclerView.ViewHolder holder = listView.findViewHolderForAdapterPosition(a);
|
|
|
|
if (holder != null && holder.itemView instanceof PollEditTextCell) {
|
|
|
|
PollEditTextCell pollEditTextCell = (PollEditTextCell) holder.itemView;
|
|
|
|
pollEditTextCell.setChecked(false, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
super.onCheckBoxClick(editText, checked);
|
|
|
|
RecyclerView.ViewHolder holder = listView.findContainingViewHolder(editText);
|
|
|
|
if (holder != null) {
|
|
|
|
int position = holder.getAdapterPosition();
|
2020-01-23 13:58:50 +01:00
|
|
|
if (position != RecyclerView.NO_POSITION) {
|
|
|
|
int index = position - answerStartRow;
|
|
|
|
answersChecks[index] = checked;
|
|
|
|
}
|
2020-01-23 07:15:40 +01:00
|
|
|
}
|
|
|
|
checkDoneButton();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean isChecked(PollEditTextCell editText) {
|
|
|
|
RecyclerView.ViewHolder holder = listView.findContainingViewHolder(editText);
|
|
|
|
if (holder != null) {
|
|
|
|
int position = holder.getAdapterPosition();
|
2020-01-23 13:58:50 +01:00
|
|
|
if (position != RecyclerView.NO_POSITION) {
|
|
|
|
int index = position - answerStartRow;
|
|
|
|
return answersChecks[index];
|
|
|
|
}
|
2020-01-23 07:15:40 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-23 18:03:33 +01:00
|
|
|
};
|
|
|
|
cell.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite));
|
|
|
|
cell.addTextWatcher(new TextWatcher() {
|
|
|
|
@Override
|
|
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
RecyclerView.ViewHolder holder = listView.findContainingViewHolder(cell);
|
|
|
|
if (holder != null) {
|
|
|
|
int position = holder.getAdapterPosition();
|
|
|
|
int index = position - answerStartRow;
|
|
|
|
if (index < 0 || index >= answers.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
answers[index] = s.toString();
|
|
|
|
setTextLeft(cell, index);
|
|
|
|
checkDoneButton();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
cell.setShowNextButton(true);
|
|
|
|
EditTextBoldCursor editText = cell.getTextView();
|
|
|
|
editText.setImeOptions(editText.getImeOptions() | EditorInfo.IME_ACTION_NEXT);
|
|
|
|
editText.setOnEditorActionListener((v, actionId, event) -> {
|
|
|
|
if (actionId == EditorInfo.IME_ACTION_NEXT) {
|
|
|
|
RecyclerView.ViewHolder holder = listView.findContainingViewHolder(cell);
|
|
|
|
if (holder != null) {
|
|
|
|
int position = holder.getAdapterPosition();
|
2020-01-23 13:58:50 +01:00
|
|
|
if (position != RecyclerView.NO_POSITION) {
|
|
|
|
int index = position - answerStartRow;
|
|
|
|
if (index == answersCount - 1 && answersCount < 10) {
|
|
|
|
addNewField();
|
2019-01-23 18:03:33 +01:00
|
|
|
} else {
|
2020-01-23 13:58:50 +01:00
|
|
|
if (index == answersCount - 1) {
|
|
|
|
AndroidUtilities.hideKeyboard(cell.getTextView());
|
|
|
|
} else {
|
|
|
|
holder = listView.findViewHolderForAdapterPosition(position + 1);
|
|
|
|
if (holder != null && holder.itemView instanceof PollEditTextCell) {
|
|
|
|
PollEditTextCell editTextCell = (PollEditTextCell) holder.itemView;
|
|
|
|
editTextCell.getTextView().requestFocus();
|
|
|
|
}
|
2019-01-23 18:03:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
editText.setOnKeyListener((v, keyCode, event) -> {
|
|
|
|
EditTextBoldCursor field = (EditTextBoldCursor) v;
|
|
|
|
if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN && field.length() == 0) {
|
|
|
|
cell.callOnDelete();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
view = cell;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
|
|
|
|
return new RecyclerListView.Holder(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemViewType(int position) {
|
2020-01-23 07:15:40 +01:00
|
|
|
if (position == questionHeaderRow || position == answerHeaderRow || position == settingsHeaderRow) {
|
2019-01-23 18:03:33 +01:00
|
|
|
return 0;
|
|
|
|
} else if (position == questionSectionRow) {
|
|
|
|
return 1;
|
2020-04-24 11:21:58 +02:00
|
|
|
} else if (position == answerSectionRow || position == settingsSectionRow || position == solutionInfoRow) {
|
2019-01-23 18:03:33 +01:00
|
|
|
return 2;
|
|
|
|
} else if (position == addAnswerRow) {
|
|
|
|
return 3;
|
|
|
|
} else if (position == questionRow) {
|
|
|
|
return 4;
|
2020-04-24 11:21:58 +02:00
|
|
|
} else if (position == solutionRow) {
|
|
|
|
return 7;
|
2020-01-23 07:15:40 +01:00
|
|
|
} else if (position == anonymousRow || position == multipleRow || position == quizRow) {
|
|
|
|
return 6;
|
2019-01-23 18:03:33 +01:00
|
|
|
} else {
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void swapElements(int fromIndex, int toIndex) {
|
|
|
|
int idx1 = fromIndex - answerStartRow;
|
|
|
|
int idx2 = toIndex - answerStartRow;
|
|
|
|
if (idx1 < 0 || idx2 < 0 || idx1 >= answersCount || idx2 >= answersCount) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
String from = answers[idx1];
|
|
|
|
answers[idx1] = answers[idx2];
|
|
|
|
answers[idx2] = from;
|
2020-02-14 03:26:25 +01:00
|
|
|
boolean temp = answersChecks[idx1];
|
|
|
|
answersChecks[idx1] = answersChecks[idx2];
|
|
|
|
answersChecks[idx2] = temp;
|
2019-01-23 18:03:33 +01:00
|
|
|
notifyItemMoved(fromIndex, toIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-04-24 11:21:58 +02:00
|
|
|
public ArrayList<ThemeDescription> getThemeDescriptions() {
|
|
|
|
ArrayList<ThemeDescription> themeDescriptions = new ArrayList<>();
|
|
|
|
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_CELLBACKGROUNDCOLOR, new Class[]{HeaderCell.class, TextCell.class, PollEditTextCell.class, TextCheckCell.class}, null, null, null, Theme.key_windowBackgroundWhite));
|
|
|
|
themeDescriptions.add(new ThemeDescription(fragmentView, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_windowBackgroundGray));
|
|
|
|
|
|
|
|
themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_actionBarDefault));
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_LISTGLOWCOLOR, null, null, null, null, Theme.key_actionBarDefault));
|
|
|
|
themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_ITEMSCOLOR, null, null, null, null, Theme.key_actionBarDefaultIcon));
|
|
|
|
themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_TITLECOLOR, null, null, null, null, Theme.key_actionBarDefaultTitle));
|
|
|
|
themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SELECTORCOLOR, null, null, null, null, Theme.key_actionBarDefaultSelector));
|
|
|
|
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{HeaderCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlueHeader));
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_CHECKTAG, new Class[]{HeaderCell.class}, new String[]{"textView2"}, null, null, null, Theme.key_windowBackgroundWhiteRedText5));
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_CHECKTAG, new Class[]{HeaderCell.class}, new String[]{"textView2"}, null, null, null, Theme.key_windowBackgroundWhiteGrayText3));
|
|
|
|
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_TEXTCOLOR, new Class[]{PollEditTextCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText));
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_HINTTEXTCOLOR, new Class[]{PollEditTextCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteHintText));
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_HINTTEXTCOLOR, new Class[]{PollEditTextCell.class}, new String[]{"deleteImageView"}, null, null, null, Theme.key_windowBackgroundWhiteGrayIcon));
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_HINTTEXTCOLOR, new Class[]{PollEditTextCell.class}, new String[]{"moveImageView"}, null, null, null, Theme.key_windowBackgroundWhiteGrayIcon));
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_USEBACKGROUNDDRAWABLE | ThemeDescription.FLAG_DRAWABLESELECTEDSTATE, new Class[]{PollEditTextCell.class}, new String[]{"deleteImageView"}, null, null, null, Theme.key_stickers_menuSelector));
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_CHECKTAG, new Class[]{PollEditTextCell.class}, new String[]{"textView2"}, null, null, null, Theme.key_windowBackgroundWhiteRedText5));
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{PollEditTextCell.class}, new String[]{"checkBox"}, null, null, null, Theme.key_windowBackgroundWhiteGrayIcon));
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{PollEditTextCell.class}, new String[]{"checkBox"}, null, null, null, Theme.key_checkboxCheck));
|
|
|
|
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextCheckCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText));
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextCheckCell.class}, new String[]{"valueTextView"}, null, null, null, Theme.key_windowBackgroundWhiteGrayText2));
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextCheckCell.class}, new String[]{"checkBox"}, null, null, null, Theme.key_switchTrack));
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextCheckCell.class}, new String[]{"checkBox"}, null, null, null, Theme.key_switchTrackChecked));
|
|
|
|
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_SELECTOR, null, null, null, null, Theme.key_listSelector));
|
|
|
|
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{View.class}, Theme.dividerPaint, null, null, Theme.key_divider));
|
|
|
|
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlueText4));
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_BACKGROUNDFILTER, new Class[]{TextCell.class}, new String[]{"imageView"}, null, null, null, Theme.key_switchTrackChecked));
|
|
|
|
themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextCell.class}, new String[]{"imageView"}, null, null, null, Theme.key_checkboxCheck));
|
|
|
|
|
|
|
|
return themeDescriptions;
|
2019-01-23 18:03:33 +01:00
|
|
|
}
|
|
|
|
}
|