fun WebView.loadChatWidget() {
val chatHtml = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>LiveCaller Chat</title>
<style>
html, body {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
overflow: hidden;
background: #ffffff;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif;
}
#live-caller-widget {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
}
.loading {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
flex-direction: column;
color: #666;
}
.spinner {
border: 3px solid #f3f3f3;
border-top: 3px solid #2196F3;
border-radius: 50%;
width: 40px;
height: 40px;
animation: spin 1s linear infinite;
margin-bottom: 16px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</head>
<body>
<div id="live-caller-widget">
<div class="loading">
<div class="spinner"></div>
<p>Initializing LiveCaller...</p>
</div>
</div>
<script>
console.log('Starting LiveCaller initialization...');
(function(w,t,c,p,s,l){
p = new Promise(function(resolve,reject){
w[c] = { client: () => p };
l = document.getElementById('live-caller-widget');
s = document.createElement(t);
s.async = true;
s.setAttribute('data-livecaller', 'script');
s.src='https://cdn.livecaller.io/js/app.js';
s.onload = function(){ resolve(w[c]); };
s.onerror = function(error){
l.innerHTML='<div class="loading"><p>Failed to load chat.</p></div>';
reject(error);
};
(document.head||document.body).appendChild(s);
});
return p;
})(window,'script','LiveCaller')
.then(function(){
LiveCaller.config.merge({
widget: {
id: 'YOUR_WIDGET_ID_HERE',
},
app: {
locale: 'en',
}
});
LiveCaller.liftOff();
setTimeout(function() { LiveCaller.${'$'}emit('ui.widget.open'); }, 500);
})
.catch(function(error){
console.error('LiveCaller initialization failed:', error);
document.getElementById('live-caller-widget').innerHTML =
'<div class="loading"><p>Chat initialization failed. Please try again.</p></div>';
});
</script>
</body>
</html>
"""
loadDataWithBaseURL(
"https://cdn.livecaller.io/",
chatHtml,
"text/html",
"UTF-8",
null
)
}