mirror of
https://github.com/discourse/discourse.git
synced 2025-01-19 03:12:46 +08:00
FIX: Retry updating webview background color (#28912)
In some very rare cases, the header element doesn't yet have the bg when this code is run. This PR adds a simple retry mechanism. No tests because this relies on specific load timing from the browser.
This commit is contained in:
parent
7f55e8b2be
commit
501f07ab1f
|
@ -4,6 +4,7 @@ import discourseLater from "discourse-common/lib/later";
|
|||
// Send bg color to webview so iOS status bar matches site theme
|
||||
export default {
|
||||
after: "inject-objects",
|
||||
retryCount: 0,
|
||||
|
||||
initialize(owner) {
|
||||
const caps = owner.lookup("service:capabilities");
|
||||
|
@ -15,19 +16,27 @@ export default {
|
|||
}
|
||||
},
|
||||
|
||||
teardown() {
|
||||
window
|
||||
.matchMedia("(prefers-color-scheme: dark)")
|
||||
.removeEventListener("change", this.updateAppBackground);
|
||||
updateAppBackground(delay = 500) {
|
||||
discourseLater(() => {
|
||||
if (this.headerBgColor()) {
|
||||
postRNWebviewMessage("headerBg", this.headerBgColor());
|
||||
} else {
|
||||
this.retry();
|
||||
}
|
||||
}, delay);
|
||||
},
|
||||
|
||||
updateAppBackground() {
|
||||
discourseLater(() => {
|
||||
const header = document.querySelector(".d-header-wrap .d-header");
|
||||
if (header) {
|
||||
const styles = window.getComputedStyle(header);
|
||||
postRNWebviewMessage("headerBg", styles.backgroundColor);
|
||||
}
|
||||
}, 500);
|
||||
headerBgColor() {
|
||||
const header = document.querySelector(".d-header-wrap .d-header");
|
||||
if (header) {
|
||||
return window.getComputedStyle(header)?.backgroundColor;
|
||||
}
|
||||
},
|
||||
|
||||
retry() {
|
||||
if (this.retryCount < 2) {
|
||||
this.retryCount++;
|
||||
this.updateAppBackground(1000);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue
Block a user