Skip to content

Commit

Permalink
Avoid a big breaking change on ChuckerCollector and ChuckerInterceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierperez committed Nov 4, 2019
1 parent b8ba648 commit 0a5cefe
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import android.content.Context
/**
* No-op implementation.
*/
class ChuckerCollector @JvmOverloads constructor(
context: Context,
var showNotification: Boolean = true,
var retentionPeriod: RetentionManager.Period = RetentionManager.Period.ONE_WEEK
class ChuckerCollector(
context: Context
) {
@Deprecated("This constructor will disappear in a following version.")
constructor(
context: Context,
showNotification: Boolean,
retentionPeriod: RetentionManager.Period
) : this(context)

fun onError(obj: Any?, obj2: Any?) {
// Empty method for the library-no-op artifact
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ import okhttp3.Response
*/
class ChuckerInterceptor @JvmOverloads constructor(
context: Context,
collector: Any? = null,
maxContentLength: Any? = null,
headersToRedact: Any? = null
collector: Any? = null
) : Interceptor {

@Deprecated("This constructor will disappear in a following version.")
constructor(
context: Context,
collector: Any? = null,
maxContentLength: Any? = null,
headersToRedact: Any? = null
) : this(context, collector)

fun redactHeaders(vararg names: String): ChuckerInterceptor {
return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import android.content.Context
/**
* No-op implementation.
*/
class RetentionManager @JvmOverloads constructor(
context: Context,
retentionPeriod: Any? = null
class RetentionManager(
context: Context
) {

@Synchronized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import android.content.Context
import com.chuckerteam.chucker.api.internal.EmptyFragment

class ErrorsFeature(
override val enabled: Boolean,
val showNotification: Boolean
override var enabled: Boolean,
var showNotification: Boolean
) : TabFeature {
override val name: Int = 0

override val id: Int = 0

override fun newFragment() = EmptyFragment()

override fun dismissNotification(context: Context) {}
override fun dismissNotification(context: Context) {
// Empty method for the library-no-op artifact
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import com.chuckerteam.chucker.api.RetentionManager
import com.chuckerteam.chucker.api.internal.EmptyFragment

class HttpFeature(
override val enabled: Boolean,
val showNotification: Boolean,
val retentionPeriod: RetentionManager.Period
override var enabled: Boolean,
var showNotification: Boolean,
var retentionPeriod: RetentionManager.Period
) : TabFeature {
override val name: Int = 0

override val id: Int = 0

override fun newFragment() = EmptyFragment()

override fun dismissNotification(context: Context) {}
override fun dismissNotification(context: Context) {
// Empty method for the library-no-op artifact
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.fragment.app.Fragment
interface TabFeature {
val name: Int
val id: Int
val enabled: Boolean
var enabled: Boolean
fun newFragment(): Fragment
fun dismissNotification(context: Context)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,22 @@ import com.chuckerteam.chucker.internal.support.NotificationHelper
class ChuckerCollector(
context: Context
) {
@Deprecated("This constructor will disappear in a following version.")
constructor(
context: Context,
showNotification: Boolean,
retentionPeriod: RetentionManager.Period
) : this(context) {
// This 3 lines are here to avoid breaking changes in the constructor signature
// They will disappear when we will make the breaking changes.
httpFeature.showNotification = showNotification
httpFeature.retentionPeriod = retentionPeriod
errorsFeature.showNotification = showNotification
}

private val retentionManager: RetentionManager = RetentionManager(context)
private val notificationHelper: NotificationHelper = NotificationHelper(context)

private val httpFeature: HttpFeature = FeatureManager.find()
private val errorsFeature: ErrorsFeature = FeatureManager.find()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.util.Log
import com.chuckerteam.chucker.api.Chucker.LOG_TAG
import com.chuckerteam.chucker.api.config.HttpFeature
import com.chuckerteam.chucker.api.dsl.DEFAULT_MAX_CONTENT_LENGTH
import com.chuckerteam.chucker.internal.data.entity.HttpTransaction
import com.chuckerteam.chucker.internal.support.FeatureManager
import com.chuckerteam.chucker.internal.support.IOUtils
Expand Down Expand Up @@ -33,6 +34,19 @@ class ChuckerInterceptor @JvmOverloads constructor(
private val collector: ChuckerCollector = ChuckerCollector(context)
) : Interceptor {

@Deprecated("This constructor will disappear in a following version.")
constructor(
context: Context,
collector: ChuckerCollector = ChuckerCollector(context),
maxContentLength: Long = DEFAULT_MAX_CONTENT_LENGTH,
headersToRedact: MutableSet<String> = mutableSetOf()
) : this(context, collector) {
// This 2 lines are here to avoid breaking changes in the constructor signature
// They will disappear when we will make the breaking changes.
httpFeature.maxContentLength = maxContentLength
httpFeature.headersToRedact = headersToRedact
}

private val io: IOUtils = IOUtils(context)
private val httpFeature: HttpFeature = FeatureManager.find()

Expand All @@ -41,7 +55,6 @@ class ChuckerInterceptor @JvmOverloads constructor(
}

@Throws(IOException::class)
@Suppress("LongMethod", "ComplexMethod")
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import com.chuckerteam.chucker.api.Chucker
import com.chuckerteam.chucker.internal.ui.error.ErrorListFragment

class ErrorsFeature(
override val enabled: Boolean,
val showNotification: Boolean
override var enabled: Boolean,
var showNotification: Boolean
) : TabFeature {
override val name: Int = R.string.chucker_tab_errors

Expand All @@ -21,4 +21,12 @@ class ErrorsFeature(
override fun dismissNotification(context: Context) {
Chucker.dismissErrorsNotification(context)
}

companion object {
fun default(): ErrorsFeature =
ErrorsFeature(
enabled = true,
showNotification = true
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import androidx.fragment.app.Fragment
import com.chuckerteam.chucker.R
import com.chuckerteam.chucker.api.Chucker
import com.chuckerteam.chucker.api.RetentionManager
import com.chuckerteam.chucker.api.dsl.DEFAULT_MAX_CONTENT_LENGTH
import com.chuckerteam.chucker.internal.ui.transaction.TransactionListFragment

class HttpFeature(
override val enabled: Boolean,
val showNotification: Boolean,
val retentionPeriod: RetentionManager.Period,
override var enabled: Boolean,
var showNotification: Boolean,
var retentionPeriod: RetentionManager.Period,
var maxContentLength: Long,
var headersToRedact: MutableSet<String>
) : TabFeature {
Expand All @@ -25,4 +26,15 @@ class HttpFeature(
override fun dismissNotification(context: Context) {
Chucker.dismissTransactionsNotification(context)
}

companion object {
fun default(): HttpFeature =
HttpFeature(
enabled = true,
showNotification = true,
retentionPeriod = RetentionManager.Period.ONE_WEEK,
headersToRedact = mutableSetOf(),
maxContentLength = DEFAULT_MAX_CONTENT_LENGTH
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface TabFeature {
@get:StringRes
val name: Int
val id: Int
val enabled: Boolean
var enabled: Boolean
fun newFragment(): Fragment
fun dismissNotification(context: Context)
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package com.chuckerteam.chucker.internal.support

import com.chuckerteam.chucker.api.config.ErrorsFeature
import com.chuckerteam.chucker.api.config.HttpFeature
import com.chuckerteam.chucker.api.config.TabFeature

internal object FeatureManager {

private val features = mutableListOf<TabFeature>()
private val features: MutableList<TabFeature> = mutableListOf(
HttpFeature.default(),
ErrorsFeature.default()
)

fun configure(tabFeature: TabFeature) {
features.removeAll { it.javaClass == tabFeature.javaClass }
features.add(tabFeature)
}

inline fun <reified T : TabFeature> find(): T {
return features.first { it is T } as T
return features.firstOrNull { it is T } as T
}

fun countEnabledFeatures(): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import java.lang.ref.WeakReference

internal class HomePageAdapter(context: Context, fragmentManager: FragmentManager) :
FragmentStatePagerAdapter(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

private val context: WeakReference<Context> = WeakReference(context)

override fun getItem(position: Int): Fragment =
Expand All @@ -18,9 +19,4 @@ internal class HomePageAdapter(context: Context, fragmentManager: FragmentManage

override fun getPageTitle(position: Int): CharSequence? =
context.get()?.getString(FeatureManager.getAt(position).name)

companion object {
const val SCREEN_HTTP_INDEX = 0
const val SCREEN_ERROR_INDEX = 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ChuckerApplication : Application() {
}
error {
enabled = true
showNotification = true
}
}
}
Expand Down

0 comments on commit 0a5cefe

Please sign in to comment.