Anki-Android icon indicating copy to clipboard operation
Anki-Android copied to clipboard

[Bug] Unexpected line breaks in one-way sync message

Open user1823 opened this issue 3 years ago • 17 comments

Reproduction Steps
  1. Click on Manage Note Types option.
  2. Delete a note type.

The message warning the user that the action would require one-way sync has unexpected line breaks.

IMG_20221227_140611

Debug info

AnkiDroid Version = 2.16alpha91

Android Version = 7.0

Manufacturer = motorola

Model = Moto C Plus

Hardware = mt6735

Webview User Agent = Mozilla/5.0 (Linux; Android 7.0; Moto C Plus Build/NRD90M.03.045; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/108.0.5359.128 Mobile Safari/537.36

ACRA UUID = f2e4f224-7a41-48c9-9812-264b40f7e5ba

New schema = true

Scheduler = std3

Crash Reports Enabled = true

DatabaseV2 Enabled = true

Research

Enter an [x] character to confirm the points below:

  • [x] I have read the support page and am reporting a bug or enhancement request specific to AnkiDroid
  • [x] I have checked the manual and the FAQ and could not find a solution to my issue
  • [x] I have searched for similar existing issues here and on the user forum
  • [ ] (Optional) I have confirmed the issue is not resolved in the latest alpha release (instructions)

user1823 avatar Dec 27 '22 08:12 user1823

To devs: Most longer strings are used in a HTML context, where whitespace is automatically collapsed. There are one or two places in the desktop code that convert newlines in a translation to spaces before using it, using something like:

def with_collapsed_whitespace(string: str) -> str:
    return re.sub(r"\s+", " ", string)

You could do something similar here.

dae avatar Dec 27 '22 08:12 dae

I will like to take up this issue

parthn2 avatar Dec 27 '22 20:12 parthn2

Go for it, @parthn2. Have a nice job there

BrayanDSO avatar Dec 27 '22 21:12 BrayanDSO

Hi sir , i want to work on this project

ChetanaKuruba avatar Dec 29 '22 07:12 ChetanaKuruba

Hi, I am having some trouble with reproducing this error. When deleting a note type, I am getting the message "The requested change will require a full upload of the database when you next synchronize your collection. If you have reviews or other changes waiting on another device that haven't been synchronized here yet, they will be lost. Continue?"

mayankr1307 avatar Jan 01 '23 05:01 mayankr1307

Do you have the new backend enabled?

user1823 avatar Jan 01 '23 05:01 user1823

No I don't think so, how can I do that?

mayankr1307 avatar Jan 01 '23 06:01 mayankr1307

  • Install an alpha release newer than 2.16alpha82 from https://github.com/ankidroid/Anki-Android/releases
  • Go to "Settings" --> "Advanced" and enable "Use the new backend" in "Experimental Settings"

user1823 avatar Jan 01 '23 06:01 user1823

Thank you for your help, I will do my best to solve this issue

mayankr1307 avatar Jan 01 '23 06:01 mayankr1307

HI, I am able to reproduce the issue but I am unable to find out how the text is being rendered in the Pop Up. I believe it is being produced due to something in the below code from ManageNotetypes.kt file but can not find out what it is. I also tried logging some message in console but nothing shows up. Can someone help me out?

private fun deleteNotetype(noteTypeUiModel: NoteTypeUiModel) {
        launchCatchingTask {
            val messageResourceId: Int? = if (userAcceptsSchemaChange()) {
                withProgress {
                    withCol {
                        if (newBackend.getNotetypeNames().size <= 1) {
                            return@withCol null
                        }
                        R.string.model_delete_warning
                    }
                }
            } else {
                return@launchCatchingTask
            }
            if (messageResourceId == null) {
                showSnackbar(getString(R.string.toast_last_model))
                return@launchCatchingTask
            }
            MaterialDialog(this@ManageNotetypes).show {
                title(R.string.model_browser_delete)
                message(res = messageResourceId)
                positiveButton(R.string.dialog_ok) {
                    launchCatchingTask {
                        runAndRefreshAfter { newBackend.removeNotetype(noteTypeUiModel.id) }
                    }
                }
                negativeButton(R.string.dialog_cancel)
            }
        }
  }

parthn2 avatar Jan 10 '23 20:01 parthn2

@Parthn2 The message is store in the string resource file with id R.string.model_delete_warning and from the code above it is conditionally assigned to the variable val messageResourceId: Int?.

The text is then rendered in the Pop Up by passing messageResourceId to the message method of the MaterialDialog. Check the code below.

            MaterialDialog(this@ManageNotetypes).show {
                title(R.string.model_browser_delete)

                //**! This is the line you're looking for
                message(res = messageResourceId)

                positiveButton(R.string.dialog_ok) {
                    launchCatchingTask {
                        runAndRefreshAfter { newBackend.removeNotetype(noteTypeUiModel.id) }
                    }
                }
                negativeButton(R.string.dialog_cancel)
            }

To actually solve this issue, what you might want to do first is use getString(messageResourceId) to retrieve the text and apply any necessary formatting to get rid of the line breaks before passing it to the message method of the MaterialDialog.

The MaterialDialog used in Anki is -> https://github.com/afollestad/material-dialogs and it's message method construct looks like this:

fun message(
    @StringRes res: Int? = null,
    text: CharSequence? = null,
    applySettings: (DialogMessageSettings.() -> Unit)? = null
  )

So instead of passing in a resource id, you can pass your processed text as a CharSequence to it.

            MaterialDialog(this@ManageNotetypes).show {
                title(R.string.model_browser_delete)

                //**! It should look something like this
                val rawMessageText = getString(messageResourceId)
                val processedMessageText = removeWhiteSpace(awMessageTex)
                message(text = processedMessageText)

                positiveButton(R.string.dialog_ok) {
                    launchCatchingTask {
                        runAndRefreshAfter { newBackend.removeNotetype(noteTypeUiModel.id) }
                    }
                }
                negativeButton(R.string.dialog_cancel)
            }

I know my explanation is unnecessarily verbose but I hope it helps :)

ismailnurudeen avatar Jan 11 '23 09:01 ismailnurudeen

@ismailnurudeen Thank You for the detailed explanation, it really helped me to understand the code better. I have found out that this pop-up message is not generated using the line which you marked out. I verified this by commenting on the line, and it did not affect the popup, which is expected, as far as I understand message(res = messageResourceId) If you haven't reproduced the issue yet, there are two popups which come during this process.

  1. This is the error pop-up in which the unexpected line break problem is.
  2. This comes after clicking 'OK' on the previous message and generally comes under normal conditions which is the "Delete note type" pop-up. This is affected by commenting the code and the error message is gone away.

One more thing is that there is no message shown in the pop-up present in the string resource file or even in the whole codebase of this repo. I tried it using the project search but can't find anything near it.

Please tell me if I am missing anything.

parthn2 avatar Jan 11 '23 10:01 parthn2

It is getting the string from TR.deckConfigWillRequireFullSync() in AnkiActivity.userAcceptsSchemaChange() defined in CoroutineHelpers.kt

To solve this issue either you can process the function call directly inside AnkiActivity.userAcceptsSchemaChange() like this:

MaterialDialog(this).show {
            message(text = TR.deckConfigWillRequireFullSync().replace('\n',' '))
            positiveButton(R.string.dialog_ok) { coroutine.resume(true) }
            negativeButton(R.string.dialog_cancel) { coroutine.resume(false) }
            onCancel { coroutine.resume(false) }
        }

But this is more of an ad-hoc approach you will have to change it everywhere you call a function from TR which is part of ankidroid-backend'

So instead, change it in the source where it is all generated. It is I think the issue from 'ankidroid-backend'.

2002suraj456 avatar Jan 11 '23 14:01 2002suraj456

It is getting the string from TR.deckConfigWillRequireFullSync() in AnkiActivity.userAcceptsSchemaChange() defined in CoroutineHelpers.kt

To solve this issue either you can process the function call directly inside AnkiActivity.userAcceptsSchemaChange() like this:

MaterialDialog(this).show {
            message(text = TR.deckConfigWillRequireFullSync().replace('\n',' '))
            positiveButton(R.string.dialog_ok) { coroutine.resume(true) }
            negativeButton(R.string.dialog_cancel) { coroutine.resume(false) }
            onCancel { coroutine.resume(false) }
        }

But this is more of an ad-hoc approach you will have to change it everywhere you call a function from TR which is part of ankidroid-backend'

So instead, change it in the source where it is all generated. It is I think the issue from 'ankidroid-backend'.

Thank you so much.

parthn2 avatar Jan 11 '23 18:01 parthn2

Hey!! I want to work on this issue. Kindly assign me this issue..

preetishsrivastav avatar Jan 14 '23 18:01 preetishsrivastav

I want to work on this issue.

pro-utkarshM avatar Jan 23 '23 07:01 pro-utkarshM

hey please assign this task to me

Anshika91 avatar Jan 24 '23 13:01 Anshika91