Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
Issues #17, 13, 8
  • Loading branch information
matthaigh27 committed Apr 25, 2023
1 parent 20b0e59 commit 1b44056
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
67 changes: 66 additions & 1 deletion app/src/main/java/com/matthaigh27/chatgptwrapper/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,37 @@ 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 =
"Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.5615.135 Mobile Safari/537.36"
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?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
webView = binding.webView
swipeLayout = binding.swipeRefreshLayout

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
onBackInvokedDispatcher.registerOnBackInvokedCallback(
Expand All @@ -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)
}
Expand All @@ -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)
}
}
}
16 changes: 10 additions & 6 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<WebView
android:id="@+id/webView"
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true" />
android:layout_height="match_parent">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 1b44056

Please sign in to comment.