Created
August 19, 2024 21:40
-
-
Save Raf0707/87c8037700dbd21fe24bec27c165af26 to your computer and use it in GitHub Desktop.
goToPageAlert()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void goToPageAlert() { | |
MaterialAlertDialogBuilder alert = new MaterialAlertDialogBuilder(this); | |
View dialogView = getLayoutInflater().inflate(R.layout.go_to_page_dialog, null); | |
alert.setTitle("Перейти на страницу"); | |
alert.setMessage("Введите страницу"); | |
alert.setCancelable(true); | |
EditText pageNum = dialogView.findViewById(R.id.pageNum); | |
TextView suraTitle = dialogView.findViewById(R.id.suraTitleAyats); | |
AutoCompleteTextView suraClassic = dialogView.findViewById(R.id.suraClassic); | |
AutoCompleteTextView ayatClassic = dialogView.findViewById(R.id.ayatClassic); | |
MaterialButton back = dialogView.findViewById(R.id.backPage); | |
MaterialButton forward = dialogView.findViewById(R.id.forwardPage); | |
Slider pageSlider = dialogView.findViewById(R.id.pageSlider); | |
Slider sureSlider = dialogView.findViewById(R.id.sureSlider); | |
Slider ayatSlider = dialogView.findViewById(R.id.ayatSlider); | |
int currentPage = bookmarkAdapter.getCurrentPosition() + 1; | |
// OnClickListener для кнопки back | |
back.setOnClickListener(view -> { | |
if (pageNum.getText().toString().isEmpty()) { | |
// Поле pageNum пустое, получаем текущую страницу и вставляем ее в pageNum | |
int mcurrentPage = viewPager.getCurrentItem() + 1; | |
pageNum.setText(String.valueOf(mcurrentPage)); | |
} else { | |
// Поле pageNum не пустое, уменьшаем номер страницы на 1 | |
int page = Integer.parseInt(pageNum.getText().toString()); | |
if (page > 1) { | |
pageNum.setText(String.valueOf(page - 1)); | |
} | |
} | |
}); | |
// OnClickListener для кнопки forward | |
forward.setOnClickListener(view -> { | |
if (pageNum.getText().toString().isEmpty()) { | |
// Поле pageNum пустое, получаем текущую страницу и вставляем ее в pageNum | |
int mcurrentPage = viewPager.getCurrentItem() + 1; | |
pageNum.setText(String.valueOf(mcurrentPage)); | |
} else { | |
// Поле pageNum не пустое, увеличиваем номер страницы на 1 | |
int page = Integer.parseInt(pageNum.getText().toString()); | |
if (page < 604) { // Предположим, что 604 - максимальное количество страниц | |
pageNum.setText(String.valueOf(page + 1)); | |
} | |
} | |
}); | |
// Добавляем TextWatcher для обновления названия суры и аятов при вводе номера страницы | |
pageNum.addTextChangedListener(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) { | |
String input = s.toString().replaceAll("[\\.\\-,\\s]+", ""); | |
if (!input.isEmpty()) { | |
int page; | |
try { | |
page = Integer.parseInt(input); | |
} catch (NumberFormatException e) { | |
suraTitle.setText(""); | |
return; | |
} | |
if (page >= 1 && page <= 604) { | |
suraTitle.setText(bookmarkAdapter.getSuraTitle(page)+",\n"+bookmarkAdapter.getAyatsOnPage(page)); | |
pageSlider.setValue(page); | |
sureSlider.setValue(bookmarkAdapter.getSuraNum(page)); | |
ayatSlider.setValueTo(bookmarkAdapter.getNumAyatsOfSure(bookmarkAdapter.getSuraNum(page))); | |
ayatSlider.setValue(1); | |
suraClassic.setText(bookmarkAdapter.getSuraNum(page)); | |
ayatClassic.setText("1"); | |
} else { | |
suraTitle.setText(""); | |
} | |
} else { | |
suraTitle.setText(""); | |
} | |
} | |
}); | |
// Добавляем TextWatcher для обновления страницы при вводе суры и аята | |
TextWatcher suraAyatWatcher = 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) { | |
String suraInput = suraClassic.getText().toString().replaceAll("[\\.\\-,\\s]+", ""); | |
String ayatInput = ayatClassic.getText().toString().replaceAll("[\\.\\-,\\s]+", ""); | |
if (ayatClassic.getText().toString().isEmpty()) ayatInput = "1"; | |
int sura, ayat, page; | |
if (!suraInput.isEmpty() && !ayatInput.isEmpty()) { | |
try { | |
sura = Integer.parseInt(suraInput); | |
ayat = Integer.parseInt(ayatInput); | |
} catch (NumberFormatException e) { | |
pageNum.setText(""); | |
return; | |
} | |
page = bookmarkAdapter.goToAyat(sura, ayat); | |
if (page != -1) { | |
pageNum.setText(String.valueOf(page)); | |
suraTitle.setText(bookmarkAdapter.getSuraTitle(page) + ",\n" + bookmarkAdapter.getAyatsOnPage(page)); | |
pageSlider.setValue(page); | |
sureSlider.setValue(bookmarkAdapter.getSuraNum(page)); | |
ayatSlider.setValueTo(bookmarkAdapter.getNumAyatsOfSure(bookmarkAdapter.getSuraNum(page))); | |
ayatSlider.setValue(ayat); | |
} else if (!suraInput.isEmpty() && ayatInput.isEmpty()) { | |
try { | |
sura = Integer.parseInt(suraInput); | |
ayat = 1; | |
} catch (NumberFormatException e) { | |
pageNum.setText(""); | |
return; | |
} | |
page = bookmarkAdapter.goToAyat(sura, ayat); | |
if (page != -1) { | |
pageNum.setText(String.valueOf(page)); | |
suraTitle.setText(bookmarkAdapter.getSuraTitle(page) + ",\n" + bookmarkAdapter.getAyatsOnPage(page)); | |
} | |
} else { | |
pageNum.setText(""); | |
} | |
} else { | |
pageNum.setText(""); | |
} | |
} | |
}; | |
suraClassic.addTextChangedListener(suraAyatWatcher); | |
ayatClassic.addTextChangedListener(suraAyatWatcher); | |
pageSlider.addOnChangeListener(new Slider.OnChangeListener() { | |
@Override | |
public void onValueChange(Slider slider, float value, boolean fromUser) { | |
pageNum.setText(String.valueOf(value)); | |
suraTitle.setText(bookmarkAdapter.getSuraTitle((int) value) + ",\n" + bookmarkAdapter.getAyatsOnPage((int) value)); | |
sureSlider.setValue(bookmarkAdapter.getSuraNum((int) value)); | |
ayatSlider.setValueTo(bookmarkAdapter.getNumAyatsOfSure(bookmarkAdapter.getSuraNum((int) value))); | |
ayatSlider.setValue(1); | |
suraClassic.setText(bookmarkAdapter.getSuraNum((int) value)); | |
ayatClassic.setText("1"); | |
} | |
}); | |
sureSlider.addOnChangeListener(new Slider.OnChangeListener() { | |
@Override | |
public void onValueChange(Slider slider, float value, boolean fromUser) { | |
pageNum.setText(String.valueOf(value)); | |
suraTitle.setText(bookmarkAdapter.getSuraTitle(currentPage) + ",\n" + bookmarkAdapter.getAyatsOnPage(currentPage)); | |
pageSlider.setValue(currentPage); | |
ayatSlider.setValueTo(bookmarkAdapter.getNumAyatsOfSure(bookmarkAdapter.getSuraNum((int) value))); | |
ayatSlider.setValue(1); | |
suraClassic.setText((int) value); | |
ayatClassic.setText("1"); | |
} | |
}); | |
ayatSlider.addOnChangeListener(new Slider.OnChangeListener() { | |
@Override | |
public void onValueChange(Slider slider, float value, boolean fromUser) { | |
ayatClassic.setText((int) value); | |
} | |
}); | |
alert.setNegativeButton("Отмена", (dialogInterface, i) -> { | |
// Ничего не делаем | |
}); | |
alert.setPositiveButton("Перейти", (dialogInterface, i) -> { | |
if (pageNum.getText().toString().length() == 0) { | |
Snackbar.make(b.getRoot(), "Ничего не введено. Введите номер страницы", Snackbar.LENGTH_SHORT).show(); | |
} else { | |
int page = Integer.parseInt(pageNum.getText().toString().replaceAll("[\\.\\-,\\s]+", "")); | |
if ((page < 1) || (page > 604) || (pageNum.getText().toString().isEmpty())) { | |
Snackbar.make(b.getRoot(), "В Коране 604 страницы. Введите номер от 1 до 604", Snackbar.LENGTH_SHORT).show(); | |
} else { | |
viewPager.setCurrentItem(page - 1, true); | |
} | |
} | |
}); | |
alert.setView(dialogView); | |
alert.show(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment