refactor: Clean up WebView code and remove debug prints

- Remove all debug print statements for cleaner output
- Group related properties in WebViewCoordinator for better organization
- Remove redundant comments throughout the code
- Simplify JavaScript code by removing unnecessary comments
- Maintain all functionality while improving code readability
This commit is contained in:
Ilyas Hallak 2025-09-30 23:21:46 +02:00
parent f50ad505ae
commit f42d138f58

View File

@ -26,7 +26,6 @@ struct WebView: UIViewRepresentable {
webView.allowsBackForwardNavigationGestures = false webView.allowsBackForwardNavigationGestures = false
webView.allowsLinkPreview = true webView.allowsLinkPreview = true
// Message Handler hier einmalig hinzufügen
webView.configuration.userContentController.add(context.coordinator, name: "heightUpdate") webView.configuration.userContentController.add(context.coordinator, name: "heightUpdate")
webView.configuration.userContentController.add(context.coordinator, name: "scrollProgress") webView.configuration.userContentController.add(context.coordinator, name: "scrollProgress")
context.coordinator.onHeightChange = onHeightChange context.coordinator.onHeightChange = onHeightChange
@ -36,13 +35,10 @@ struct WebView: UIViewRepresentable {
} }
func updateUIView(_ webView: WKWebView, context: Context) { func updateUIView(_ webView: WKWebView, context: Context) {
// Nur den HTML-Inhalt laden, keine Handler-Konfiguration
context.coordinator.onHeightChange = onHeightChange context.coordinator.onHeightChange = onHeightChange
context.coordinator.onScroll = onScroll context.coordinator.onScroll = onScroll
let isDarkMode = colorScheme == .dark let isDarkMode = colorScheme == .dark
// Font Settings aus Settings-Objekt
let fontSize = getFontSize(from: settings.fontSize ?? .extraLarge) let fontSize = getFontSize(from: settings.fontSize ?? .extraLarge)
let fontFamily = getFontFamily(from: settings.fontFamily ?? .serif) let fontFamily = getFontFamily(from: settings.fontFamily ?? .serif)
@ -235,8 +231,6 @@ struct WebView: UIViewRepresentable {
function updateHeight() { function updateHeight() {
const height = document.body.scrollHeight; const height = document.body.scrollHeight;
// Only send height update if it changed significantly and we're not scrolling
if (Math.abs(height - lastHeight) > 5 && !isScrolling) { if (Math.abs(height - lastHeight) > 5 && !isScrolling) {
lastHeight = height; lastHeight = height;
window.webkit.messageHandlers.heightUpdate.postMessage(height); window.webkit.messageHandlers.heightUpdate.postMessage(height);
@ -251,31 +245,24 @@ struct WebView: UIViewRepresentable {
window.addEventListener('load', updateHeight); window.addEventListener('load', updateHeight);
setTimeout(updateHeight, 500); setTimeout(updateHeight, 500);
// Höhe bei Bild-Ladevorgängen aktualisieren
document.querySelectorAll('img').forEach(img => { document.querySelectorAll('img').forEach(img => {
img.addEventListener('load', debouncedHeightUpdate); img.addEventListener('load', debouncedHeightUpdate);
}); });
// Throttled scroll progress reporting
window.addEventListener('scroll', function() { window.addEventListener('scroll', function() {
isScrolling = true; isScrolling = true;
// Clear existing timeout
clearTimeout(scrollTimeout); clearTimeout(scrollTimeout);
// Throttle scroll events to reduce frequency
scrollTimeout = setTimeout(function() { scrollTimeout = setTimeout(function() {
var scrollTop = window.scrollY || document.documentElement.scrollTop; var scrollTop = window.scrollY || document.documentElement.scrollTop;
var docHeight = document.documentElement.scrollHeight - document.documentElement.clientHeight; var docHeight = document.documentElement.scrollHeight - document.documentElement.clientHeight;
var progress = docHeight > 0 ? scrollTop / docHeight : 0; var progress = docHeight > 0 ? scrollTop / docHeight : 0;
window.webkit.messageHandlers.scrollProgress.postMessage(progress); window.webkit.messageHandlers.scrollProgress.postMessage(progress);
// Reset scrolling state after a delay
setTimeout(function() { setTimeout(function() {
isScrolling = false; isScrolling = false;
debouncedHeightUpdate(); // Check for height changes after scrolling stops debouncedHeightUpdate();
}, 200); }, 200);
}, 16); // ~60fps throttling }, 16);
}); });
</script> </script>
</body> </body>
@ -284,27 +271,13 @@ struct WebView: UIViewRepresentable {
webView.loadHTMLString(styledHTML, baseURL: nil) webView.loadHTMLString(styledHTML, baseURL: nil)
} }
// CRITICAL: Proper cleanup when view is destroyed
func dismantleUIView(_ webView: WKWebView, coordinator: WebViewCoordinator) { func dismantleUIView(_ webView: WKWebView, coordinator: WebViewCoordinator) {
print("🔴 WebView - DISMANTLING: Starting cleanup")
// Stop all loading
webView.stopLoading() webView.stopLoading()
// Remove navigation delegate
webView.navigationDelegate = nil webView.navigationDelegate = nil
// Remove message handlers to prevent memory leaks
webView.configuration.userContentController.removeScriptMessageHandler(forName: "heightUpdate") webView.configuration.userContentController.removeScriptMessageHandler(forName: "heightUpdate")
webView.configuration.userContentController.removeScriptMessageHandler(forName: "scrollProgress") webView.configuration.userContentController.removeScriptMessageHandler(forName: "scrollProgress")
// Clear content
webView.loadHTMLString("", baseURL: nil) webView.loadHTMLString("", baseURL: nil)
// Cleanup coordinator
coordinator.cleanup() coordinator.cleanup()
print("🔴 WebView - DISMANTLING: Cleanup completed")
} }
func makeCoordinator() -> WebViewCoordinator { func makeCoordinator() -> WebViewCoordinator {
@ -335,19 +308,25 @@ struct WebView: UIViewRepresentable {
} }
class WebViewCoordinator: NSObject, WKNavigationDelegate, WKScriptMessageHandler { class WebViewCoordinator: NSObject, WKNavigationDelegate, WKScriptMessageHandler {
// Callbacks
var onHeightChange: ((CGFloat) -> Void)? var onHeightChange: ((CGFloat) -> Void)?
var onScroll: ((Double) -> Void)? var onScroll: ((Double) -> Void)?
var isScrolling: Bool = false
var scrollEndTimer: Timer? // Height management
var heightUpdateTimer: Timer?
var lastHeight: CGFloat = 0 var lastHeight: CGFloat = 0
var pendingHeight: CGFloat = 0 var pendingHeight: CGFloat = 0
var heightUpdateTimer: Timer?
// Scroll management
var isScrolling: Bool = false
var scrollVelocity: Double = 0 var scrollVelocity: Double = 0
var lastScrollTime: Date = Date() var lastScrollTime: Date = Date()
var scrollEndTimer: Timer?
// Lifecycle
private var isCleanedUp = false private var isCleanedUp = false
deinit { deinit {
print("🔴 WebViewCoordinator - deinit called")
cleanup() cleanup()
} }
@ -441,18 +420,12 @@ class WebViewCoordinator: NSObject, WKNavigationDelegate, WKScriptMessageHandler
guard !isCleanedUp else { return } guard !isCleanedUp else { return }
isCleanedUp = true isCleanedUp = true
print("🔴 WebViewCoordinator - cleanup: Invalidating timers")
// Invalidate all timers
scrollEndTimer?.invalidate() scrollEndTimer?.invalidate()
scrollEndTimer = nil scrollEndTimer = nil
heightUpdateTimer?.invalidate() heightUpdateTimer?.invalidate()
heightUpdateTimer = nil heightUpdateTimer = nil
// Clear callbacks to prevent memory leaks
onHeightChange = nil onHeightChange = nil
onScroll = nil onScroll = nil
print("🔴 WebViewCoordinator - cleanup: Completed")
} }
} }