Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,12 @@ android {
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}

kotlinOptions {
jvmTarget = '17'
}

sourceSets {
Expand All @@ -143,4 +147,3 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation project(":react-native-nitro-modules")
}

89 changes: 89 additions & 0 deletions android/src/main/java/com/margelo/nitro/toadly/LoggingService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.margelo.nitro.toadly

import android.util.Log
import android.R
import java.text.SimpleDateFormat
import java.util.*

/**
* LoggingService manages log collection and provides logging utilities
*/
object LoggingService {
private const val TAG = "Toadly"
private const val MAX_LOGS = 50
private val logs = mutableListOf<String>()
private val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US).apply {
timeZone = TimeZone.getTimeZone("UTC")
}

/**
* Log a message with the specified level
*/
private fun log(message: String, level: String = "INFO") {
val timestamp = dateFormat.format(Date())
val logEntry = "[$timestamp] [$level] $message"

synchronized(logs) {
logs.add(logEntry)

if (logs.size > MAX_LOGS) {
logs.removeAt(0)
}
}

when (level) {
"INFO" -> Log.i(TAG, message)
"WARN" -> Log.w(TAG, message)
"ERROR" -> Log.e(TAG, message)
else -> Log.d(TAG, message)
}
}

/**
* Log an info message
*/
fun info(message: String) {
log(message, "INFO")
}

/**
* Log a warning message
*/
fun warn(message: String) {
log(message, "WARN")
}

/**
* Log an error message
*/
fun error(message: String) {
log(message, "ERROR")
}

/**
* Get all recent logs as a single string
*/
fun getRecentLogs(): String {
synchronized(logs) {
return logs.joinToString("\n")
}
}

/**
* Get all collected logs as a single string
*/
fun getLogs(): String {
synchronized(logs) {
return logs.joinToString("\n")
}
}

/**
* Clear all stored logs
*/
fun clearLogs() {
synchronized(logs) {
logs.clear()
}
}
}
177 changes: 172 additions & 5 deletions android/src/main/java/com/margelo/nitro/toadly/Toadly.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,177 @@
package com.margelo.nitro.toadly


import android.app.Activity
import android.content.Context
import com.facebook.react.bridge.UiThreadUtil
import com.facebook.proguard.annotations.DoNotStrip
import com.margelo.nitro.toadly.dialog.BugReportDialog
import com.margelo.nitro.toadly.github.GitHubService
import kotlin.collections.Map

@DoNotStrip
class Toadly : HybridToadlySpec() {
override fun multiply(a: Double, b: Double): Double {
return a * b
}
object Toadly : HybridToadlySpec() {
private var hasSetupBeenCalled = false
private val jsLogs = mutableListOf<String>()
private var githubToken: String = ""
private var repoOwner: String = ""
private var repoName: String = ""
private lateinit var githubService: GitHubService

override fun setup(githubToken: String, repoOwner: String, repoName: String) {
if (hasSetupBeenCalled) {
return
}
hasSetupBeenCalled = true

this.githubToken = githubToken
this.repoOwner = repoOwner
this.repoName = repoName
this.githubService = GitHubService(githubToken, repoOwner, repoName)

LoggingService.info("Setting up Toadly with GitHub integration")
}

override fun addJSLogs(logs: String) {
this.jsLogs.add(logs)
LoggingService.info("Received JavaScript logs")
}

override fun createIssueWithTitle(title: String, reportType: String?) {
LoggingService.info("Creating issue with title: $title, type: ${reportType ?: "bug"}")

val description = "User submitted bug report"
val type = reportType ?: "bug"
val email = "auto-generated@toadly.app"
val jsLogsContent = jsLogs.joinToString("\n")
val nativeLogs = LoggingService.getLogs()

val currentActivity = getCurrentActivity()

if (currentActivity == null) {
LoggingService.error("Cannot create GitHub issue: no activity context found")
return
}

Thread {
try {
val success = githubService.createIssue(
context = currentActivity,
title = title,
details = description,
email = email,
jsLogs = jsLogsContent,
nativeLogs = nativeLogs,
reportType = type
)

if (success) {
LoggingService.info("Successfully created GitHub issue")
} else {
LoggingService.info("Failed to create GitHub issue")
}
} catch (e: Exception) {
LoggingService.error("Error creating GitHub issue: ${e.message}")
}
}.start()
}

override fun show() {
LoggingService.info("Show bug report dialog requested")

val currentActivity = getCurrentActivity()

if (currentActivity == null) {
LoggingService.error("Cannot show bug report dialog: no activity found")
return
}

BugReportDialog(currentActivity) { title, reportType, email ->
createIssueWithEmailAndTitle(title, reportType, email)
}.show()
}

private fun createIssueWithEmailAndTitle(title: String, reportType: String, email: String) {
LoggingService.info("Creating issue with title: $title, type: $reportType, email: $email")

val description = "User submitted bug report"
val type = reportType
val jsLogsContent = jsLogs.joinToString("\n")
val nativeLogs = LoggingService.getLogs()

val currentActivity = getCurrentActivity()

if (currentActivity == null) {
LoggingService.error("Cannot create GitHub issue: no activity context found")
return
}

Thread {
try {
val success = githubService.createIssue(
context = currentActivity,
title = title,
details = description,
email = email,
jsLogs = jsLogsContent,
nativeLogs = nativeLogs,
reportType = type
)

if (success) {
LoggingService.info("Successfully created GitHub issue")
} else {
LoggingService.info("Failed to create GitHub issue")
}
} catch (e: Exception) {
LoggingService.error("Error creating GitHub issue: ${e.message}")
}
}.start()
}

override fun crashNative() {
LoggingService.warn("Crash native requested - this is for testing only")
throw RuntimeException("Manually triggered crash from Toadly")
}

// Helper method to get the current activity
private fun getCurrentActivity(): Context? {
try {
val activityThreadClass = Class.forName("android.app.ActivityThread")
val activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null)
val activityField = activityThreadClass.getDeclaredField("mActivities")

activityField.isAccessible = true

@Suppress("UNCHECKED_CAST")
val activities = activityField.get(activityThread) as? Map<Any, Any>

if (activities == null) {
return null
}

activities.values.forEach { activityRecord ->
activityRecord?.let { record ->
val activityRecordClass = record::class.java
val pausedField = activityRecordClass.getDeclaredField("paused")

pausedField.isAccessible = true

if (!pausedField.getBoolean(record)) {
val activityField = activityRecordClass.getDeclaredField("activity")
activityField.isAccessible = true
val activity = activityField.get(record)

if (activity is Context) {
return activity
}
}
}
}

return null
} catch (e: Exception) {
LoggingService.error("Error getting current activity: ${e.message}")
return null
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.margelo.nitro.toadly.dialog

import android.app.AlertDialog
import android.content.Context
import android.view.LayoutInflater
import android.widget.EditText
import android.widget.Spinner
import android.widget.ArrayAdapter
import android.widget.Toast
import android.widget.ImageButton
import android.os.Handler
import android.os.Looper
import androidx.appcompat.widget.AppCompatSpinner
import com.margelo.nitro.toadly.LoggingService
import com.margelo.nitro.toadly.R

class BugReportDialog(private val context: Context, private val onSubmit: (String, String, String) -> Unit) {

private val reportTypesMap = mapOf(
"🐞 Bug" to "bug",
"💡 Suggestion" to "enhancement",
"❓ Question" to "question"
)


private val reportTypeDisplays = reportTypesMap.keys.toTypedArray()

fun show() {
Handler(Looper.getMainLooper()).post {
try {
val dialog = AlertDialog.Builder(context, R.style.CustomDialog)
.create()

val layout = LayoutInflater.from(context).inflate(R.layout.dialog_bug_report, null)

val emailEditText = layout.findViewById<EditText>(R.id.emailEditText)
val reportTypeSpinner = layout.findViewById<AppCompatSpinner>(R.id.reportTypeSpinner)
val descriptionEditText = layout.findViewById<EditText>(R.id.descriptionEditText)
val closeButton = layout.findViewById<ImageButton>(R.id.closeButton)
val sendButton = layout.findViewById<ImageButton>(R.id.sendButton)

// Set up the spinner with report types
val adapter = ArrayAdapter(context, android.R.layout.simple_spinner_item, reportTypeDisplays)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
reportTypeSpinner.adapter = adapter

closeButton.setOnClickListener {
dialog.dismiss()
}

sendButton.setOnClickListener {
val email = emailEditText.text?.toString() ?: ""
val description = descriptionEditText.text?.toString() ?: ""
val selectedTypeDisplay = reportTypeSpinner.selectedItem.toString()
val typeLabel = reportTypesMap[selectedTypeDisplay] ?: "bug" // Get GitHub label without emoji

if (email.isEmpty() || description.isEmpty()) {
Toast.makeText(context, "Please fill all fields", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}

val title = if (description.length > 50) {
description.substring(0, 47) + "..."
} else {
description
}

onSubmit(title, typeLabel, email)
Toast.makeText(context, "Bug report submitted", Toast.LENGTH_SHORT).show()
dialog.dismiss()
}

dialog.setView(layout)
dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
dialog.show()
LoggingService.info("Bug report dialog shown")
} catch (e: Exception) {
LoggingService.error("Error showing bug report dialog: ${e.message}")
}
}
}
}
Loading
Loading