Skip to content

Commit c5fe38b

Browse files
committed
feat: add share button to the DTC dialog
1 parent 10d7ce0 commit c5fe38b

7 files changed

Lines changed: 79 additions & 7 deletions

File tree

app/src/main/java/org/obd/graphs/preferences/CoreDialogFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract class CoreDialogFragment : DialogFragment() {
3636
root: View,
3737
func: () -> Unit = {},
3838
) {
39-
root.findViewById<Button>(R.id.trip_action_close_window).apply {
39+
root.findViewById<Button>(R.id.action_close_window).apply {
4040
setOnClickListener {
4141
func()
4242
dialog?.dismiss()

app/src/main/java/org/obd/graphs/preferences/dtc/DiagnosticTroubleCodePreferenceDialogFragment.kt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
*/
1717
package org.obd.graphs.preferences.dtc
1818

19+
import android.content.Intent
1920
import android.os.Bundle
2021
import android.view.LayoutInflater
2122
import android.view.View
2223
import android.view.ViewGroup
24+
import android.widget.Button
2325
import androidx.recyclerview.widget.GridLayoutManager
2426
import androidx.recyclerview.widget.RecyclerView
2527
import org.obd.graphs.R
@@ -44,10 +46,68 @@ internal class DiagnosticTroubleCodePreferenceDialogFragment : CoreDialogFragmen
4446
recyclerView.layoutManager = GridLayoutManager(context, 1)
4547
recyclerView.adapter = adapter
4648

49+
attachShareButton(root, sortedDtcList)
4750
attachCloseButton(root)
51+
4852
return root
4953
}
5054

55+
private fun attachShareButton(
56+
root: View,
57+
sortedDtcList: List<DiagnosticTroubleCode>,
58+
) {
59+
val shareButton: Button = root.findViewById(R.id.action_share)
60+
if (sortedDtcList.size == 1 && sortedDtcList.first().standardCode.isEmpty()) {
61+
shareButton.visibility = View.GONE
62+
} else {
63+
shareButton.visibility = View.VISIBLE
64+
shareButton.setOnClickListener {
65+
shareDtcReport(sortedDtcList)
66+
}
67+
}
68+
}
69+
70+
private fun shareDtcReport(dtcList: List<DiagnosticTroubleCode>) {
71+
val reportBuilder = StringBuilder()
72+
reportBuilder.append("Vehicle Diagnostic Report\n")
73+
reportBuilder.append("-------------------------\n\n")
74+
75+
for (code in dtcList) {
76+
if (code.standardCode.isEmpty()) continue
77+
78+
val formattedCode =
79+
if (!code.failureType?.code.isNullOrEmpty()) {
80+
"${code.standardCode}-${code.failureType.code}"
81+
} else {
82+
code.standardCode
83+
}
84+
85+
reportBuilder.append("DTC: $formattedCode\n")
86+
reportBuilder.append("Description: ${code.description ?: "Unknown"}\n")
87+
88+
val systemTxt = code.system?.description
89+
val categoryTxt = code.category?.description
90+
if (!systemTxt.isNullOrBlank() || !categoryTxt.isNullOrBlank()) {
91+
reportBuilder.append("System: ${systemTxt ?: "N/A"} | Category: ${categoryTxt ?: "N/A"}\n")
92+
}
93+
94+
val hex = code.rawHex ?: "N/A"
95+
val activeStatuses = code.activeStatuses?.joinToString(", ") ?: "None"
96+
reportBuilder.append("Status: $activeStatuses (Hex: $hex)\n")
97+
reportBuilder.append("\n") // Blank line between codes
98+
}
99+
100+
val sendIntent: Intent =
101+
Intent().apply {
102+
action = Intent.ACTION_SEND
103+
putExtra(Intent.EXTRA_TEXT, reportBuilder.toString())
104+
type = "text/plain"
105+
}
106+
107+
val shareIntent = Intent.createChooser(sendIntent, "Share Diagnostic Report")
108+
startActivity(shareIntent)
109+
}
110+
51111
private fun diagnosticTroubleCodes(): List<DiagnosticTroubleCode> {
52112
val diagnosticTroubleCodes = VehicleCapabilitiesManager.getDiagnosticTroubleCodes()
53113

app/src/main/java/org/obd/graphs/preferences/pid/PidDefinitionPreferenceDialogFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ open class PidDefinitionPreferenceDialogFragment(
185185
}
186186
}
187187

188-
root.findViewById<Button>(R.id.trip_action_close_window).apply {
188+
root.findViewById<Button>(R.id.action_close_window).apply {
189189
setOnClickListener {
190190
dialog?.dismiss()
191191
onDialogCloseListener.invoke()

app/src/main/res/layout/dialog_dtc.xml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,25 @@
4848
android:layout_height="wrap_content"
4949
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
5050
app:layout_constrainedHeight="true"
51-
app:layout_constraintBottom_toTopOf="@+id/trip_action_close_window"
51+
app:layout_constraintBottom_toTopOf="@+id/action_close_window"
5252
app:layout_constraintEnd_toEndOf="parent"
5353
app:layout_constraintStart_toStartOf="parent"
5454
app:layout_constraintTop_toBottomOf="@+id/header_divider" />
5555

5656
<Button
57-
android:id="@+id/trip_action_close_window"
57+
android:id="@+id/action_share"
58+
android:layout_width="wrap_content"
59+
android:layout_height="wrap_content"
60+
android:layout_margin="16dp"
61+
android:backgroundTint="@color/rainbow_indigo"
62+
android:text="Share"
63+
android:textSize="12sp"
64+
app:layout_constraintBottom_toBottomOf="parent"
65+
app:layout_constraintStart_toStartOf="parent"
66+
app:layout_constraintTop_toBottomOf="@+id/recycler_view" />
67+
68+
<Button
69+
android:id="@+id/action_close_window"
5870
android:layout_width="wrap_content"
5971
android:layout_height="wrap_content"
6072
android:layout_margin="16dp"

app/src/main/res/layout/dialog_pid.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@
274274
tools:ignore="SmallSp" />
275275

276276
<Button
277-
android:id="@+id/trip_action_close_window"
277+
android:id="@+id/action_close_window"
278278
style="?android:attr/buttonBarButtonStyle"
279279
android:layout_width="0dp"
280280
android:layout_height="wrap_content"

app/src/main/res/layout/dialog_trip.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
android:padding="4dp">
105105

106106
<Button
107-
android:id="@+id/trip_action_close_window"
107+
android:id="@+id/action_close_window"
108108
android:layout_width="wrap_content"
109109
android:layout_height="wrap_content"
110110
android:layout_marginEnd="4dp"

app/src/main/res/layout/dialog_vehicle_metadata.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
android:orientation="horizontal">
6868

6969
<Button
70-
android:id="@+id/trip_action_close_window"
70+
android:id="@+id/action_close_window"
7171
android:layout_width="wrap_content"
7272
android:layout_height="wrap_content"
7373
android:layout_marginStart="6dp"

0 commit comments

Comments
 (0)