From 1b440564d60b528c6ed71b386862aecbc6e027de Mon Sep 17 00:00:00 2001 From: matt haigh Date: Tue, 25 Apr 2023 15:34:52 +1000 Subject: [PATCH] Bug fixes Issues #17, 13, 8 --- .../chatgptwrapper/MainActivity.kt | 67 ++++++++++++++++++- app/src/main/res/layout/activity_main.xml | 16 +++-- 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/matthaigh27/chatgptwrapper/MainActivity.kt b/app/src/main/java/com/matthaigh27/chatgptwrapper/MainActivity.kt index 5075853..1ab82cf 100644 --- a/app/src/main/java/com/matthaigh27/chatgptwrapper/MainActivity.kt +++ b/app/src/main/java/com/matthaigh27/chatgptwrapper/MainActivity.kt @@ -2,14 +2,21 @@ package com.matthaigh27.chatgptwrapper import android.annotation.SuppressLint import android.app.Activity +import android.content.ClipData +import android.content.ClipboardManager +import android.content.Context +import android.content.Intent import android.graphics.Color import android.os.Build import android.os.Bundle import android.view.WindowManager +import android.webkit.JavascriptInterface +import android.webkit.WebResourceRequest import android.webkit.WebView import android.webkit.WebViewClient import android.window.OnBackInvokedDispatcher import com.matthaigh27.chatgptwrapper.databinding.ActivityMainBinding +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout class MainActivity : Activity() { private val userAgent = @@ -17,6 +24,7 @@ class MainActivity : Activity() { private val chatUrl = "https://chat.openai.com/" private lateinit var binding: ActivityMainBinding private lateinit var webView: WebView + private lateinit var swipeLayout: SwipeRefreshLayout @SuppressLint("SetJavaScriptEnabled") override fun onCreate(savedInstanceState: Bundle?) { @@ -24,6 +32,7 @@ class MainActivity : Activity() { binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) webView = binding.webView + swipeLayout = binding.swipeRefreshLayout if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { onBackInvokedDispatcher.registerOnBackInvokedCallback( @@ -43,7 +52,53 @@ class MainActivity : Activity() { webView.settings.userAgentString = userAgent webView.settings.domStorageEnabled = true webView.settings.javaScriptEnabled = true - webView.webViewClient = WebViewClient() + webView.addJavascriptInterface(WebViewInterface(this), "Android") + + webView.webViewClient = object : WebViewClient() { + override fun shouldOverrideUrlLoading( + view: WebView?, + request: WebResourceRequest? + ): Boolean { + val url = request?.url ?: return false + + if (url.toString().contains(chatUrl)) { + return false + } + + if (webView.url.toString().contains(chatUrl) && + !webView.url.toString().contains("/auth") + ) { + val intent = Intent(Intent.ACTION_VIEW, url) + startActivity(intent) + return true + } + + return false + } + + override fun onPageFinished(view: WebView?, url: String?) { + super.onPageFinished(view, url) + swipeLayout.isRefreshing = false + swipeLayout.isEnabled = !(webView.url.toString().contains(chatUrl) && + !webView.url.toString().contains("/auth")) + + webView.evaluateJavascript( + """ + (() => { + navigator.clipboard.writeText = (text) => { + Android.copyToClipboard(text); + return Promise.resolve(); + } + })(); + """.trimIndent(), + null + ) + } + } + + swipeLayout.setOnRefreshListener { + webView.reload() + } webView.loadUrl(chatUrl) } @@ -56,4 +111,14 @@ class MainActivity : Activity() { else super.onBackPressed() } + + private class WebViewInterface(private val context: Context) { + @JavascriptInterface + fun copyToClipboard(text: String) { + val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager + val clip = ClipData.newPlainText("Copied!", text) + + clipboard.setPrimaryClip(clip) + } + } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index cb39fa2..5e3426b 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -4,11 +4,15 @@ android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> - - + android:layout_height="match_parent"> + +