Skip to content

Commit

Permalink
Use Intent extension function to make code more concise. (corona-warn…
Browse files Browse the repository at this point in the history
  • Loading branch information
johnjohndoe committed Sep 20, 2020
1 parent c6cde25 commit 4d80db6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import de.rki.coronawarnapp.exception.ExceptionCategory
import de.rki.coronawarnapp.exception.reporting.ReportingConstants.STATUS_CODE_GOOGLE_API_FAIL
import de.rki.coronawarnapp.exception.reporting.ReportingConstants.STATUS_CODE_GOOGLE_UPDATE_NEEDED
import de.rki.coronawarnapp.exception.reporting.ReportingConstants.STATUS_CODE_REACHED_REQUEST_LIMIT
import de.rki.coronawarnapp.util.withExtras
import java.io.PrintWriter
import java.io.StringWriter

Expand All @@ -20,12 +21,12 @@ fun Throwable.report(
prefix: String?,
suffix: String?
) {
val intent = Intent(ReportingConstants.ERROR_REPORT_LOCAL_BROADCAST_CHANNEL)
intent.putExtra(ReportingConstants.ERROR_REPORT_CATEGORY_EXTRA, exceptionCategory.name)
intent.putExtra(ReportingConstants.ERROR_REPORT_PREFIX_EXTRA, prefix)
intent.putExtra(ReportingConstants.ERROR_REPORT_SUFFIX_EXTRA, suffix)
intent.putExtra(ReportingConstants.ERROR_REPORT_MESSAGE_EXTRA, this.message)

val intent = Intent(ReportingConstants.ERROR_REPORT_LOCAL_BROADCAST_CHANNEL).withExtras(
ReportingConstants.ERROR_REPORT_CATEGORY_EXTRA to exceptionCategory.name,
ReportingConstants.ERROR_REPORT_PREFIX_EXTRA to prefix,
ReportingConstants.ERROR_REPORT_SUFFIX_EXTRA to suffix,
ReportingConstants.ERROR_REPORT_MESSAGE_EXTRA to this.message
)
if (this is ReportedExceptionInterface) {
intent.putExtra(ReportingConstants.ERROR_REPORT_CODE_EXTRA, this.code)
this.resId?.let { intent.putExtra(ReportingConstants.ERROR_REPORT_RES_ID, it) }
Expand Down Expand Up @@ -68,8 +69,9 @@ fun Throwable.report(
fun reportGeneric(
stackString: String
) {
val intent = Intent(ReportingConstants.ERROR_REPORT_LOCAL_BROADCAST_CHANNEL)
intent.putExtra("category", ExceptionCategory.INTERNAL.name)
intent.putExtra("stack", stackString)
val intent = Intent(ReportingConstants.ERROR_REPORT_LOCAL_BROADCAST_CHANNEL).withExtras(
"category" to ExceptionCategory.INTERNAL.name,
"stack" to stackString
)
LocalBroadcastManager.getInstance(CoronaWarnApplication.getAppContext()).sendBroadcast(intent)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package de.rki.coronawarnapp.util

import android.content.Intent
import android.os.Bundle
import androidx.core.os.bundleOf

/**
* Puts the given [pairs] as a [Bundle] into this [Intent].
*/
fun Intent.withExtras(vararg pairs: Pair<String, Any?>): Intent = putExtras(bundleOf(*pairs))

0 comments on commit 4d80db6

Please sign in to comment.