Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix popup for reconnecting to the server #4358

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Roffild
Copy link
Contributor

@Roffild Roffild commented Apr 21, 2024

After receiving an ERROR, the page must be reloaded.

WebView cannot report an error from the server.

Closed #4272

Copy link
Member

@jpelgrom jpelgrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Demanding merges via Discord is very much not appreciated.

The relation to the issue (which in itself is hard to follow) is unclear. I'm seeing some attempts to handle errors in another callback here and trying to review those, but a clear issue description would be appreciated.

}
alert.setNeutralButton(commonR.string.wait) { _, _ ->
waitForConnection()
reloadPage()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert. The button says Wait - it is for when loading takes >10s (which triggers this pop-up) and you want to wait/see it's almost done. It should not reload, that is what the reload button is for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reloadPage reloads when there is an ERROR + run waitForConnection. Only waitForConnection does not work on error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wait button is not for errors. In case of an error, you are supposed to fix it and refresh :)


@RequiresApi(23)
override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
Log.e(TAG, "onReceivedError: errorCode: ${error?.errorCode} ${error?.description} url:${request?.url}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent flooding the logs, could this be moved inside the if() block below? We're not going to do anything with it otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a copy-paste as in line 351. There is no log flooding here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing code isn't perfect. I think my comment about this being an unnecessary log statement when nothing is done with it still stands.

@@ -470,7 +494,7 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi
startActivity(browserIntent)
return true
} else {
Log.d(TAG, "No unique cases found to override")
// Log.d(TAG, "No unique cases found to override url=${it}") // Unstoppable flood.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert. We don't leave commented code like this, and if you're seeing this statement a lot that is probably an issue with your setup (it is only called for main page navigation/redirects, so with a normal setup you don't see it unless trying to navigate to outside). Some of the log statements you added will generate more lines than this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without a URL, the information content of this log is poor.
This log shows the correct URL, which has no additional action.

For example, 3 logs per second: https://community.home-assistant.io/t/apps-cant-access-ha/534210/3

I was receiving more than 10 logs per second.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding an url could be useful but you're commenting it out so in the end adding no value. And as mentioned, we don't leave in commented code like this.

I was receiving more than 10 logs per second.

Did you manage to find out what caused it? If so, that could be discussed in an issue and/or changed/fixed in a different PR.

@@ -1511,6 +1544,7 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi
}

private fun waitForConnection() {
// Called inside an this.loadUrl() [not webView.loadUrl()]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is called from other functions as well? I don't really understand the comment and there seems to be broken Markdown syntax as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not Markdown. This is a pointer to line 1263.

Copy link
Member

@jpelgrom jpelgrom May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Syntax aside, this is still called from other functions as well and I don't really see what the comment adds.

@@ -371,7 +394,8 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi
errorResponse: WebResourceResponse?
) {
Log.e(TAG, "onReceivedHttpError: ${errorResponse?.statusCode} : ${errorResponse?.reasonPhrase} for: ${request?.url}")
if (request?.url.toString() == loadedUrl) {
if (request?.isForMainFrame == true && request.url.toString() == loadedUrl) {
pageError = errorResponse?.statusCode ?: 404
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why fallback to 404? HTTP errors can be for various codes; if unknown is a valid response the pageError variable should be nullable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pageError cannot be 0 (for an error) or null.
onReceivedHttpError always returns >=400.
This 404 is for Kotlin.

@@ -208,6 +209,8 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi
private var mFilePathCallback: ValueCallback<Array<Uri>>? = null
private var isConnected = false
private var isShowingError = false
private var isPageFinished = false
private var pageError = 0 // 0 - OK, <0 - Connection Error, >=400 - HTTP Error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What purpose does this have? It is not used in the UI? I can only see it in a log statement when reloading at which point it is no longer relevant because there is reloading 🙃

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used in reloadPage. It is possible to show more information about the error to the user.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you're not showing anything. For debugging with http status codes etc. you may as well use Chrome remote debugging, the app is never going to match that level of detail.

Comment on lines 930 to 933

if (::loadedUrl.isInitialized) {
reloadPage()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert. This will cause a reload with state loss anytime the app is resumed, eg. when opening it from the background.

Copy link
Contributor Author

@Roffild Roffild May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Fixed) It's better to use waitForConnection here.

This is a test after returning from Settings.

@home-assistant home-assistant bot marked this pull request as draft May 7, 2024 17:20
@home-assistant
Copy link

home-assistant bot commented May 7, 2024

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@Roffild
Copy link
Contributor Author

Roffild commented May 8, 2024

@Roffild Roffild marked this pull request as ready for review May 8, 2024 18:37
@home-assistant home-assistant bot requested a review from jpelgrom May 8, 2024 18:37
@jpelgrom
Copy link
Member

jpelgrom commented May 13, 2024

Full fix for: https://github.com/Roffild/roffild.github.io/assets/38705255/2e049aea-bc77-482c-8355-d03e22345464

This still doesn't describe the issue (when down an error is expected, and the refresh button refreshes) and also doesn't match the code changes which are all over the place. Trying hard to find something here but if we cannot agree on an issue and the value of changes, maybe this should be rejected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(Auto)Reconnect from unreachable server popup not working
2 participants