Skip to content

Commit a63b04f

Browse files
committed
Unified start URL calls
1 parent f6c6d47 commit a63b04f

4 files changed

Lines changed: 50 additions & 54 deletions

File tree

app/src/main/java/com/example/deeplviewer/activity/FloatingTextSelection.kt

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import android.webkit.WebView
1010
import androidx.appcompat.app.AppCompatActivity
1111
import com.example.deeplviewer.R
1212
import com.example.deeplviewer.config.WebViewConfig
13-
import com.example.deeplviewer.helper.WebViewUrlHelper
13+
import com.example.deeplviewer.helper.UrlHelper
1414
import com.example.deeplviewer.webview.MyWebViewClient
1515
import com.example.deeplviewer.webview.NestedScrollWebView
1616
import com.example.deeplviewer.webview.WebAppInterface
@@ -22,27 +22,13 @@ class FloatingTextSelection : AppCompatActivity() {
2222
private lateinit var webView: WebView
2323
private lateinit var webViewClient: MyWebViewClient
2424
private lateinit var layout: View
25-
26-
private val startUrl by lazy {
27-
val configPrefs = getSharedPreferences("config", Context.MODE_PRIVATE)
28-
val urlParam = configPrefs.getString(
29-
"urlParam",
30-
DEFAULT_PARAM
31-
) ?: DEFAULT_PARAM
32-
val pageType = configPrefs.getString("pageType", DEFAULT_PAGE_TYPE) ?: DEFAULT_PAGE_TYPE
33-
34-
ORIGIN_URL + pageType + urlParam
35-
}
36-
37-
companion object {
38-
private const val ORIGIN_URL = "https://www.deepl.com/"
39-
private const val DEFAULT_PARAM = "#en/en/"
40-
private const val DEFAULT_PAGE_TYPE = "translator"
41-
}
25+
private lateinit var startUrl: String
4226

4327
override fun onCreate(savedInstanceState: Bundle?) {
4428
super.onCreate(savedInstanceState)
4529

30+
startUrl = UrlHelper.buildStartUrl(this)
31+
4632
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
4733
val androidTranslateFloatingText =
4834
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
@@ -104,7 +90,7 @@ class FloatingTextSelection : AppCompatActivity() {
10490
showBottomSheetDialog()
10591

10692
// Load the initial URL
107-
val targetUrl = WebViewUrlHelper.buildUrl(startUrl, initialText)
93+
val targetUrl = UrlHelper.buildWebViewUrl(startUrl, initialText)
10894
webView.loadUrl(targetUrl)
10995
}
11096

app/src/main/java/com/example/deeplviewer/activity/MainActivity.kt

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,28 @@ import android.util.Log
88
import android.webkit.WebView
99
import android.widget.ImageButton
1010
import androidx.appcompat.app.AppCompatActivity
11-
import com.example.deeplviewer.helper.CookieManagerHelper
12-
import com.example.deeplviewer.webview.MyWebViewClient
11+
import androidx.core.content.edit
1312
import com.example.deeplviewer.R
1413
import com.example.deeplviewer.config.WebViewConfig
15-
import com.example.deeplviewer.helper.WebViewUrlHelper
14+
import com.example.deeplviewer.helper.CookieManagerHelper
15+
import com.example.deeplviewer.helper.UrlHelper
16+
import com.example.deeplviewer.webview.MyWebViewClient
1617
import com.example.deeplviewer.webview.WebAppInterface
17-
import androidx.core.content.edit
1818

1919
class MainActivity : AppCompatActivity() {
2020
private lateinit var webView: WebView
2121
private lateinit var webViewClient: MyWebViewClient
22-
23-
private val startUrl by lazy {
24-
val configPrefs = getSharedPreferences("config", Context.MODE_PRIVATE)
25-
val urlParam = configPrefs.getString("urlParam", DEFAULT_PARAM) ?: DEFAULT_PARAM
26-
val pageType = configPrefs.getString("pageType", DEFAULT_PAGE_TYPE) ?: DEFAULT_PAGE_TYPE
27-
28-
ORIGIN_URL + pageType + urlParam
29-
}
22+
private lateinit var startUrl: String
3023

3124
val testWebViewClient: MyWebViewClient?
3225
get() = if (::webViewClient.isInitialized) webViewClient else null
3326

34-
companion object {
35-
private const val ORIGIN_URL = "https://www.deepl.com/"
36-
private const val DEFAULT_PARAM = "#en/en/"
37-
private const val DEFAULT_PAGE_TYPE = "translator"
38-
}
39-
4027
override fun onCreate(savedInstanceState: Bundle?) {
4128
super.onCreate(savedInstanceState)
4229
setContentView(R.layout.activity_main)
4330

31+
startUrl = UrlHelper.buildStartUrl(this)
32+
4433
initializeWebView()
4534
createWebView(intent, savedInstanceState)
4635
setupSettingsButton()
@@ -78,7 +67,7 @@ class MainActivity : AppCompatActivity() {
7867
webView.addJavascriptInterface(WebAppInterface(this), "Android")
7968

8069
// Load the initial URL
81-
val targetUrl = WebViewUrlHelper.buildUrl(startUrl, receivedText)
70+
val targetUrl = UrlHelper.buildWebViewUrl(startUrl, receivedText)
8271
webView.loadUrl(targetUrl)
8372
}
8473

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.example.deeplviewer.helper
2+
3+
import android.content.Context
4+
import android.net.Uri
5+
6+
object UrlHelper {
7+
/**
8+
* Builds the start URL for the DeepL translator based on the provided parameters
9+
*/
10+
fun buildStartUrl(
11+
context: Context,
12+
originUrl: String = "https://www.deepl.com/",
13+
pageType: String = "translator",
14+
urlParam: String = "#en/en/"
15+
): String {
16+
val configPrefs = context.getSharedPreferences("config", Context.MODE_PRIVATE)
17+
val buildPageType = configPrefs.getString("pageType", pageType)
18+
?: pageType
19+
val buildUrlParam = configPrefs.getString(
20+
"urlParam",
21+
urlParam
22+
) ?: urlParam
23+
24+
return "$originUrl$buildPageType$buildUrlParam"
25+
}
26+
27+
/**
28+
* Builds a URL for the WebView based on the provided start URL and text
29+
*/
30+
fun buildWebViewUrl(startUrl: String, text: String): String {
31+
if (text.isEmpty()) return startUrl
32+
33+
val processedText = text.replace("/", "\\/")
34+
val encodedText = Uri.encode(processedText)
35+
return "$startUrl$encodedText"
36+
}
37+
}

app/src/main/java/com/example/deeplviewer/helper/WebViewUrlHelper.kt

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)