From 2afbe58722215494e5eea8de148cd42d8970715e Mon Sep 17 00:00:00 2001 From: Stypox Date: Wed, 11 Jan 2023 19:45:55 +0100 Subject: [PATCH] UX improvements: keep user edits & do not reset cursor --- .../newpipe/download/DownloadDialog.java | 42 +++++++++++++------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java index 77bb48a81..208d27bb2 100644 --- a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java +++ b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java @@ -75,6 +75,7 @@ import java.io.IOException; import java.util.List; import java.util.Locale; import java.util.Objects; +import java.util.Optional; import icepick.Icepick; import icepick.State; @@ -566,19 +567,34 @@ public class DownloadDialog extends DialogFragment } private void onItemSelectedSetFileName() { - final String fileName = FilenameUtils.createFilename(getContext(), - currentInfo.getName()); - switch (dialogBinding.videoAudioGroup.getCheckedRadioButtonId()) { - case R.id.audio_button: - case R.id.video_button: - dialogBinding.fileName.setText(fileName); - break; - case R.id.subtitle_button: - final String setSubtitleLanguageCode = subtitleStreamsAdapter - .getItem(selectedSubtitleIndex).getLanguageTag(); - dialogBinding.fileName.setText(getString( - R.string.caption_file_name, fileName, setSubtitleLanguageCode)); - break; + final String fileName = FilenameUtils.createFilename(getContext(), currentInfo.getName()); + final String prevFileName = Optional.ofNullable(dialogBinding.fileName.getText()) + .map(Object::toString) + .orElse(""); + + if (prevFileName.isEmpty() + || prevFileName.equals(fileName) + || prevFileName.startsWith(getString(R.string.caption_file_name, fileName, ""))) { + // only update the file name field if it was not edited by the user + + switch (dialogBinding.videoAudioGroup.getCheckedRadioButtonId()) { + case R.id.audio_button: + case R.id.video_button: + if (!prevFileName.equals(fileName)) { + // since the user might have switched between audio and video, the correct + // text might already be in place, so avoid resetting the cursor position + dialogBinding.fileName.setText(fileName); + } + break; + + case R.id.subtitle_button: + final String setSubtitleLanguageCode = subtitleStreamsAdapter + .getItem(selectedSubtitleIndex).getLanguageTag(); + // this will reset the cursor position, which is bad UX, but it can't be avoided + dialogBinding.fileName.setText(getString( + R.string.caption_file_name, fileName, setSubtitleLanguageCode)); + break; + } } }