Skip to content

Commit

Permalink
Feedback + Notification Module refactored - cleaner and more comprehe…
Browse files Browse the repository at this point in the history
…nsive interface
  • Loading branch information
Michael Flisar authored and Michael Flisar committed Nov 3, 2023
1 parent d8611e4 commit 4b22ae4
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ class MainActivity : DemoBaseActivity() {
onClick = {
mail.takeIf { it.isNotEmpty() }?.let {
L.sendFeedback(
context,
DemoLogging.FILE_LOGGING_SETUP.getLatestLogFiles(),
it
context = context,
receiver = it,
attachments = listOfNotNull(DemoLogging.FILE_LOGGING_SETUP.getLatestLogFiles())
)
} ?: ToastHelper.show(
context,
Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
kotlin.code.style=official

useLiveDependencies=false
2 changes: 1 addition & 1 deletion gradle/androidx.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ recyclerview = "1.3.2"

[libraries]
core = { module = "androidx.core:core-ktx", version.ref = "core" }
lifecylce = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "lifecycle" }
lifecycle = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "lifecycle" }
constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayout" }
recyclerview = { module = "androidx.recyclerview:recyclerview", version.ref = "recyclerview" }
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ fun LumberjackDialog(
},
onClick = {
L.sendFeedback(
context,
setup.getLatestLogFiles(),
mail
context = context,
receiver = mail,
attachments = listOfNotNull(setup.getLatestLogFiles())
)
showMenu2 = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,29 @@ import com.michaelflisar.lumberjack.core.L
import com.michaelflisar.lumberjack.core.classes.CoreUtil
import java.io.File

/*
/**
* convenient extension to simply send a feedback via email with an intent chooser
*
* - app version will be appended to the subject automatically
* - file should be local and accessible files - they will be exposed via a ContentProvider so that the email client can access the file
* - app version will be appended to the subject automatically
* - file should be local and accessible files - they will be exposed via a ContentProvider so that the email client can access the file
*
* @param context context for the email chooser and to retrieve default strings
* @param receiver the receiver email of the feedback
* @param subject the subject of the mail
* @param titleForChooser the title of the email app chooser
* @param attachments files that should be appended to the mail
*/
fun L.sendFeedback(
context: Context,
logFile: File?,
receiver: String,
subject: String = "Feedback for ${context.packageName}",
titleForChooser: String = "Send feedback with",
filesToAppend: List<File> = emptyList()
attachments: List<File> = emptyList()
) {
val allFiles = filesToAppend.toMutableList()
logFile?.let { allFiles.add(0, it) }
val feedback = Feedback(
listOf(receiver),
CoreUtil.getRealSubject(context, subject),
attachments = allFiles.map { FeedbackFile.DefaultName(it) }
attachments = attachments.map { FeedbackFile.DefaultName(it) }
)
feedback.startEmailChooser(context, titleForChooser)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,73 +12,105 @@ import com.michaelflisar.lumberjack.core.L
import com.michaelflisar.lumberjack.core.classes.CoreUtil
import java.io.File

/*
sealed class NotificationClickHandler {

/**
* convenient class to simply send a feedback via clicking the notification via an intent chooser
*
* - app version will be appended to the subject automatically
* - file should be local and accessible files - they will be exposed via a ContentProvider so that the email client can access the file
*
* @param context context for the email chooser and to retrieve default strings
* @param receiver the receiver email of the feedback
* @param subject the subject of the mail
* @param titleForChooser the title of the email app chooser
* @param attachments files that should be appended to the mail
*/
class SendFeedback(
context: Context,
val receiver: String,
val subject: String = "Exception found in ${context.packageName}",
val titleForChooser: String = "Send report with",
val attachments: List<File> = emptyList()
) : NotificationClickHandler()

/**
* apply your custom notification click handler
*
* @param intent the intent to use when notification is clicked
* @param apply optional adjustment function for the notification builder
*/
class ClickIntent(
val intent: Intent,
val apply: ((builder: NotificationCompat.Builder) -> Unit)? = null
): NotificationClickHandler()

/**
* this will disable clicks on the notification => the notification will be there for information purposes only
*/
data object None: NotificationClickHandler()
}


/**
* convenient extension to simply show a notification for exceptions/infos/whatever that the user should report if possible
*
* - app version will be appended to the subject automatically
* - file should be local and accessible files - they will be exposed via a ContentProvider so that the email client can access the file
* @param context context start email chooser and retrieve default strings
* @param notificationIcon the notification icon
* @param notificationChannelId the notification channel id
* @param notificationId the notification id
* @param notificationTitle the notification title
* @param notificationText the notification title
* @param clickHandler the click handler for the notification
*/
fun L.showCrashNotification(
fun L.showNotification(
context: Context,
logFile: File?,
receiver: String,
appIcon: Int,
notificationIcon: Int,
notificationChannelId: String,
notificationId: Int,
notificationTitle: String = "Rare exception found",
notificationText: String = "Please report this error by clicking this notification, thanks",
subject: String = "Exception found in ${context.packageName}",
titleForChooser: String = "Send report with",
filesToAppend: List<File> = emptyList()
clickHandler: NotificationClickHandler
) {
val allFiles = filesToAppend.toMutableList()
logFile?.let { allFiles.add(0, it) }
val feedback = Feedback(
listOf(receiver),
CoreUtil.getRealSubject(context, subject),
attachments = allFiles.map { FeedbackFile.DefaultName(it) }
)
when (clickHandler) {
is NotificationClickHandler.SendFeedback -> {
val feedback = Feedback(
listOf(clickHandler.receiver),
CoreUtil.getRealSubject(context, clickHandler.subject),
attachments = clickHandler.attachments.map { FeedbackFile.DefaultName(it) }
)
feedback
.startNotification(
context,
clickHandler.titleForChooser,
notificationTitle,
notificationText,
notificationIcon,
notificationChannelId,
notificationId
)
}
is NotificationClickHandler.ClickIntent,
is NotificationClickHandler.None -> {

feedback
.startNotification(
context,
titleForChooser,
notificationTitle,
notificationText,
appIcon,
notificationChannelId,
notificationId
)
}
val pendingIntent = (clickHandler as? NotificationClickHandler.ClickIntent)?.let {
val flag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PendingIntent.FLAG_IMMUTABLE
} else 0
PendingIntent.getActivity(context, 0, it.intent, flag)
}

/*
* convenient extension to simply show a notification to the user or for debugging infos
*/
fun L.showInfoNotification(
context: Context,
notificationChannelId: String,
notificationId: Int,
notificationTitle: String,
notificationText: String,
notificationIcon: Int,
clickIntent: Intent? = null,
apply: ((builder: NotificationCompat.Builder) -> Unit)? = null
) {
val pendingIntent = clickIntent?.let {
val flag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PendingIntent.FLAG_IMMUTABLE
} else 0
PendingIntent.getActivity(context, 0, it, flag)
}
val builder: NotificationCompat.Builder =
NotificationCompat.Builder(context, notificationChannelId)
.setSmallIcon(notificationIcon)
.setContentTitle(notificationTitle)
.setContentText(notificationText)
pendingIntent?.let { builder.setContentIntent(it) }
(clickHandler as? NotificationClickHandler.ClickIntent)?.apply?.let { it(builder) }

val builder: NotificationCompat.Builder = NotificationCompat.Builder(context, notificationChannelId)
.setSmallIcon(notificationIcon)
.setContentTitle(notificationTitle)
.setContentText(notificationText)
pendingIntent?.let { builder.setContentIntent(it) }
apply?.let { it(builder) }

val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(notificationId, builder.build())
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(notificationId, builder.build())
}
}
}
2 changes: 1 addition & 1 deletion library/extensions/viewer/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ dependencies {

implementation(androidx.core)
implementation(androidx.recyclerview)
implementation(androidx.lifecylce)
implementation(androidx.lifecycle)

implementation(deps.material)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ internal class LumberjackViewerActivity : AppCompatActivity() {

R.id.menu_send_log_file -> {
// receiver must be valid if this menu entry is visible
L.sendFeedback(this, selectedFile, receiver!!)
L.sendFeedback(this, receiver!!, attachments = listOfNotNull(selectedFile))
true
}

Expand Down

0 comments on commit 4b22ae4

Please sign in to comment.