Skip to content

Commit 07202c3

Browse files
committed
feat: added dynamic shortcuts
1 parent be426c1 commit 07202c3

File tree

3 files changed

+87
-16
lines changed

3 files changed

+87
-16
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,61 @@
11
package com.mrepol742.webappp
22

3+
import android.content.Intent
34
import android.os.Bundle
5+
import android.webkit.WebView
46
import androidx.activity.ComponentActivity
57
import androidx.activity.compose.setContent
68
import androidx.activity.enableEdgeToEdge
79
import androidx.compose.foundation.layout.fillMaxSize
810
import androidx.compose.foundation.layout.padding
911
import androidx.compose.material3.Scaffold
12+
import androidx.compose.runtime.mutableStateOf
1013
import androidx.compose.ui.Modifier
1114
import com.mrepol742.webappp.ui.theme.WebApppTheme
15+
import com.mrepol742.webappp.utils.DynamicShortcut
1216

1317

1418
class MainActivity : ComponentActivity() {
1519
private val allowedDomain = "www.melvinjonesrepol.com"
20+
private var currentUrl: String = "https://$allowedDomain"
21+
private val webViewState = mutableStateOf<WebView?>(null)
1622

1723
override fun onCreate(savedInstanceState: Bundle?) {
1824
super.onCreate(savedInstanceState)
1925
enableEdgeToEdge()
26+
2027
setContent {
2128
WebApppTheme {
2229
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
2330
WebViewScreen(
2431
allowedDomain = allowedDomain,
32+
initialUrl = currentUrl,
33+
webViewState = webViewState,
2534
modifier = Modifier.padding(innerPadding)
2635
)
2736
}
2837
}
2938
}
39+
40+
DynamicShortcut(this, allowedDomain).createDynamicShortcut()
41+
}
42+
43+
override fun onResume() {
44+
super.onResume()
45+
46+
val urlFromIntent = when (intent?.action) {
47+
Intent.ACTION_VIEW -> intent.getStringExtra("url")
48+
else -> null
49+
}
50+
51+
if (!urlFromIntent.isNullOrEmpty()) {
52+
currentUrl = urlFromIntent
53+
webViewState.value?.loadUrl(currentUrl)
54+
}
55+
}
56+
57+
override fun onNewIntent(intent: Intent) {
58+
super.onNewIntent(intent)
59+
setIntent(intent)
3060
}
3161
}

app/src/main/java/com/mrepol742/webappp/WebViewScreen.kt

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@ import android.annotation.SuppressLint
44
import android.app.Activity
55
import android.util.Log
66
import android.webkit.WebView
7-
import androidx.activity.compose.BackHandler
87
import androidx.compose.foundation.layout.fillMaxSize
98
import androidx.compose.runtime.Composable
10-
import androidx.compose.runtime.getValue
11-
import androidx.compose.runtime.mutableStateOf
12-
import androidx.compose.runtime.remember
13-
import androidx.compose.runtime.setValue
9+
import androidx.compose.runtime.MutableState
1410
import androidx.compose.ui.Modifier
1511
import androidx.compose.ui.viewinterop.AndroidView
1612
import com.mrepol742.webappp.client.SecureChromeClient
@@ -19,8 +15,7 @@ import com.mrepol742.webappp.utils.DownloadListener
1915

2016
@SuppressLint("SetJavaScriptEnabled")
2117
@Composable
22-
fun WebViewScreen(allowedDomain: String, modifier: Modifier = Modifier) {
23-
var webView by remember { mutableStateOf<WebView?>(null) }
18+
fun WebViewScreen(allowedDomain: String, initialUrl: String, webViewState: MutableState<WebView?>, modifier: Modifier = Modifier) {
2419

2520
AndroidView(
2621
modifier = Modifier.fillMaxSize(),
@@ -58,16 +53,9 @@ fun WebViewScreen(allowedDomain: String, modifier: Modifier = Modifier) {
5853
webChromeClient = SecureChromeClient(context as Activity)
5954
setDownloadListener(DownloadListener(context))
6055

61-
loadUrl("https://$allowedDomain")
62-
63-
// keep a reference
64-
webView = this
56+
loadUrl(initialUrl)
57+
webViewState.value = this
6558
}
6659
}
6760
)
68-
69-
BackHandler(enabled = webView?.canGoBack() == true) {
70-
Log.d("WebView", "test")
71-
webView?.goBack()
72-
}
7361
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.mrepol742.webappp.utils
2+
3+
import android.content.Context
4+
import android.content.Intent
5+
import android.content.pm.ShortcutInfo
6+
import android.content.pm.ShortcutManager
7+
import android.graphics.drawable.Icon
8+
import com.mrepol742.webappp.MainActivity
9+
import com.mrepol742.webappp.R
10+
11+
class DynamicShortcut(private val context: Context, private val allowedDomain: String) {
12+
13+
fun createDynamicShortcut() {
14+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
15+
val shortcutManager = context.getSystemService(ShortcutManager::class.java)
16+
17+
val projectShortcut = ShortcutInfo.Builder(context, "shortcut_projects")
18+
.setShortLabel("Projects")
19+
.setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher_round))
20+
.setIntent(
21+
Intent(context, MainActivity::class.java).apply {
22+
action = Intent.ACTION_VIEW
23+
putExtra("url", "https://$allowedDomain/projects")
24+
}
25+
)
26+
.build()
27+
28+
val gamingShortcut = ShortcutInfo.Builder(context, "shortcut_gaming")
29+
.setShortLabel("Gaming")
30+
.setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher_round))
31+
.setIntent(
32+
Intent(context, MainActivity::class.java).apply {
33+
action = Intent.ACTION_VIEW
34+
putExtra("url", "https://$allowedDomain/gaming")
35+
}
36+
)
37+
.build()
38+
39+
val contactMe = ShortcutInfo.Builder(context, "shortcut_contact_me")
40+
.setShortLabel("Contact")
41+
.setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher_round))
42+
.setIntent(
43+
Intent(context, MainActivity::class.java).apply {
44+
action = Intent.ACTION_VIEW
45+
putExtra("url", "https://$allowedDomain/contact-me")
46+
}
47+
)
48+
.build()
49+
50+
shortcutManager?.dynamicShortcuts = listOf(projectShortcut, gamingShortcut, contactMe)
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)