Skip to content

Commit 4decc94

Browse files
committed
feat(app) added js app
1 parent 51a03cf commit 4decc94

File tree

10 files changed

+116
-65
lines changed

10 files changed

+116
-65
lines changed

example/BugsnagExample/composeApp/build.gradle.kts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
21
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
2+
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackConfig
33

44
plugins {
55
alias(libs.plugins.kotlinMultiplatform)
@@ -11,7 +11,6 @@ plugins {
1111

1212
kotlin {
1313
androidTarget {
14-
@OptIn(ExperimentalKotlinGradlePluginApi::class)
1514
compilerOptions {
1615
jvmTarget.set(JvmTarget.JVM_11)
1716
}
@@ -28,6 +27,20 @@ kotlin {
2827
}
2928
}
3029

30+
js(IR) {
31+
moduleName = "composeApp"
32+
useEsModules()
33+
browser {
34+
commonWebpackConfig {
35+
mode = KotlinWebpackConfig.Mode.PRODUCTION
36+
}
37+
}
38+
binaries.executable()
39+
}
40+
41+
applyDefaultHierarchyTemplate()
42+
43+
3144
sourceSets {
3245

3346
androidMain.dependencies {
@@ -45,8 +58,9 @@ kotlin {
4558
implementation(libs.androidx.lifecycle.runtimeCompose)
4659
implementation(libs.bugsnag.kmp)
4760
}
48-
commonTest.dependencies {
49-
implementation(libs.kotlin.test)
61+
jsMain.dependencies {
62+
implementation(libs.compose.html)
63+
implementation(npm("@bugsnag/browser", "=${libs.versions.bugsnag.js.get()}"))
5064
}
5165
}
5266
}
@@ -75,15 +89,15 @@ android {
7589
}
7690
}
7791
compileOptions {
78-
sourceCompatibility = JavaVersion.VERSION_1_8
79-
targetCompatibility = JavaVersion.VERSION_1_8
92+
sourceCompatibility = JavaVersion.VERSION_11
93+
targetCompatibility = JavaVersion.VERSION_11
8094
}
8195
}
8296

8397
dependencies {
84-
implementation(libs.androidx.appcompat)
8598
debugImplementation(compose.uiTooling)
8699

100+
implementation(libs.androidx.appcompat)
87101
implementation(libs.bugsnag.android)
88102
implementation(libs.bugsnag.kmp)
89103
}

example/BugsnagExample/composeApp/src/androidMain/kotlin/com/example/bugsnag/kmp/MainActivity.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ import com.bugsnag.kmp.Configuration
1212
class MainActivity : ComponentActivity() {
1313
override fun onCreate(savedInstanceState: Bundle?) {
1414
super.onCreate(savedInstanceState)
15-
startBugsnag(
16-
Configuration(applicationContext).apply {
17-
apiKey = BUGSNAG_API_KEY
18-
},
19-
)
15+
val config = Configuration(applicationContext, BUGSNAG_API_KEY)
16+
startBugsnag(config)
2017

2118
setContent {
2219
Surface(

example/BugsnagExample/composeApp/src/commonMain/composeResources/drawable/compose-multiplatform.xml

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

example/BugsnagExample/composeApp/src/commonMain/kotlin/com/example/bugsnag/kmp/App.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@ import androidx.compose.material3.Button
99
import androidx.compose.material3.MaterialTheme
1010
import androidx.compose.material3.Text
1111
import androidx.compose.runtime.Composable
12+
import androidx.compose.runtime.LaunchedEffect
13+
import androidx.compose.runtime.mutableStateOf
14+
import androidx.compose.runtime.remember
1215
import androidx.compose.ui.Modifier
16+
import androidx.compose.ui.graphics.Color
1317
import androidx.compose.ui.unit.dp
1418
import com.bugsnag.kmp.Bugsnag
19+
import kotlinx.coroutines.delay
1520

1621
@Composable
1722
fun App() {
23+
val message = remember { mutableStateOf("") }
24+
1825
MaterialTheme {
1926
Column(
2027
modifier = Modifier
@@ -27,6 +34,7 @@ fun App() {
2734
Button(
2835
onClick = {
2936
throw RuntimeException("Unhandled Kotlin Exception")
37+
message.value = "Unhandled Kotlin Exception triggered"
3038
},
3139
modifier = Modifier.fillMaxWidth(),
3240
) {
@@ -36,6 +44,7 @@ fun App() {
3644
Button(
3745
onClick = {
3846
Bugsnag.notify(RuntimeException("Handled Kotlin Exception"))
47+
message.value = "Handled Kotlin Exception reported"
3948
},
4049
modifier = Modifier.fillMaxWidth(),
4150
) {
@@ -49,11 +58,25 @@ fun App() {
4958
mapOf("reason" to "incorrect password"),
5059
)
5160
Bugsnag.notify(RuntimeException("Error Report with Breadcrumbs"))
61+
message.value = "Custom Breadcrumbs attached"
5262
},
5363
modifier = Modifier.fillMaxWidth(),
5464
) {
5565
Text("Attach Custom Breadcrumbs")
5666
}
67+
68+
if (message.value.isNotEmpty()) {
69+
Text(
70+
text = message.value,
71+
modifier = Modifier.padding(top = 16.dp),
72+
style = MaterialTheme.typography.bodyLarge,
73+
color = Color.Red
74+
)
75+
LaunchedEffect(message.value) {
76+
delay(1500)
77+
message.value = ""
78+
}
79+
}
5780
}
5881
}
5982
}

example/BugsnagExample/composeApp/src/jsMain/kotlin/com/example/bugsnag/kmp/JsApp.kt

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
package com.example.bugsnag.kmp
22

3+
import androidx.compose.runtime.Composable
34
import com.bugsnag.kmp.Bugsnag
45
import com.bugsnag.kmp.Configuration
6+
import kotlinx.browser.window
57
import org.jetbrains.compose.web.css.DisplayStyle
68
import org.jetbrains.compose.web.css.FlexDirection
79
import org.jetbrains.compose.web.css.display
810
import org.jetbrains.compose.web.css.flexDirection
911
import org.jetbrains.compose.web.css.percent
1012
import org.jetbrains.compose.web.css.width
13+
import org.jetbrains.compose.web.dom.Button
1114
import org.jetbrains.compose.web.dom.Div
1215
import org.jetbrains.compose.web.dom.H1
1316
import org.jetbrains.compose.web.dom.Text
1417
import org.jetbrains.compose.web.renderComposable
1518

16-
@Suppress("FunctionName")
1719
@JsExport
1820
@OptIn(ExperimentalJsExport::class)
19-
fun StartComposeAppWeb(rootId: String) {
21+
fun StartJsApp(rootId: String) {
2022
startBugsnag(Configuration(BUGSNAG_API_KEY))
2123
renderComposable(rootId) {
2224
Div {
@@ -31,17 +33,47 @@ fun StartComposeAppWeb(rootId: String) {
3133
},
3234
) {
3335
TextButton(
34-
text = "Trigger A Fatal Crash",
35-
onClick = { Bugsnag.notify(Exception("Fatal Crash")) },
36+
text = "Throw An Unhandled Exception",
37+
onClick = {
38+
throw Exception("Unhandled Kotlin Exception")
39+
window.alert("Unhandled Exception sent to Bugsnag")
40+
},
3641
)
42+
43+
TextButton(
44+
text = "A Handled Exception",
45+
onClick = {
46+
Bugsnag.notify(Exception("Handled Kotlin Exception"))
47+
window.alert("Handled Exception sent to Bugsnag")
48+
},
49+
)
50+
3751
TextButton(
3852
text = "Attach Custom Breadcrumbs",
3953
onClick = {
40-
Bugsnag.leaveBreadcrumb("WebAuthFailure")
54+
Bugsnag.leaveBreadcrumb(
55+
"WebAuthFailure",
56+
mapOf("reason" to "incorrect password"),
57+
)
4158
Bugsnag.notify(RuntimeException("Error Report with Breadcrumbs"))
59+
window.alert("Breadcrumbs sent to Bugsnag")
4260
},
4361
)
4462
}
4563
}
4664
}
4765
}
66+
67+
@Composable
68+
fun TextButton(
69+
text: String,
70+
onClick: () -> Unit,
71+
) {
72+
Button(
73+
attrs = {
74+
onClick { onClick() }
75+
},
76+
) {
77+
Text(text)
78+
}
79+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>BugSnag Example</title>
5+
</head>
6+
<body>
7+
<div id="compose-content">
8+
9+
</div>
10+
<script src="composeApp.js"></script>
11+
<script>
12+
window.onload = (event) => {
13+
composeApp.StartJsApp("compose-content");
14+
}
15+
</script>
16+
</body>
17+
</html>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
html, body {
2+
width: 100%;
3+
height: 100%;
4+
margin: 0;
5+
padding: 0;
6+
overflow: hidden;
7+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
config.output = config.output || {};
2+
config.output.library = "composeApp";

example/BugsnagExample/gradle.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ org.gradle.caching=true
99

1010
#Android
1111
android.nonTransitiveRClass=true
12-
android.useAndroidX=true
12+
android.useAndroidX=true
13+
14+
org.jetbrains.compose.experimental.jscanvas.enabled=true

example/BugsnagExample/gradle/libs.versions.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@ androidx-lifecycle = "2.9.1"
66
bugsnag-android = "6.13.0"
77
bugsnag-gradle = "0.4.0"
88
composeMultiplatform = "1.8.2"
9-
kotlin = "2.2.0"
9+
kotlin = "2.1.0"
1010
kmp = "+"
11+
bugsnag-js = "8.2.0"
1112

1213
[libraries]
13-
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
1414
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "androidx-appcompat" }
1515
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activity" }
1616
androidx-lifecycle-viewmodel = { module = "org.jetbrains.androidx.lifecycle:lifecycle-viewmodel", version.ref = "androidx-lifecycle" }
1717
androidx-lifecycle-runtimeCompose = { module = "org.jetbrains.androidx.lifecycle:lifecycle-runtime-compose", version.ref = "androidx-lifecycle" }
1818
bugsnag-android = { group = "com.bugsnag", name = "bugsnag-android", version.ref = "bugsnag-android"}
1919
bugsnag-kmp = { module = "com.bugsnag:bugsnag-kmp", version.ref = "kmp" }
20+
compose-html = { module = "org.jetbrains.compose.html:html-core-js", version = "1.7.3" }
2021

2122
[plugins]
2223
androidApplication = { id = "com.android.application", version.ref = "agp" }
2324
androidLibrary = { id = "com.android.library", version.ref = "agp" }
2425
bugsnag-gradle = { id = "com.bugsnag.gradle", version.ref = "bugsnag-gradle"}
2526
composeMultiplatform = { id = "org.jetbrains.compose", version.ref = "composeMultiplatform" }
2627
composeCompiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
27-
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
28+
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }

0 commit comments

Comments
 (0)