Last active
July 23, 2019 10:09
-
-
Save alorma/777afc8a42edeb0ac88eb9903a465f24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.crashlytics.android.answers.* | |
import io.fabric.sdk.android.Fabric | |
import java.math.BigDecimal | |
import java.util.* | |
@DslMarker | |
annotation class FabricEventDsl | |
@FabricEventDsl | |
class CustomAttribute { | |
lateinit var key: String | |
var value: Any? = null | |
fun build(event: AnswersEvent<*>) { | |
value?.let { | |
when (value) { | |
is Number -> event.putCustomAttribute(key, it as Number) | |
else -> event.putCustomAttribute(key, it.toString()) | |
} | |
} | |
} | |
} | |
@Suppress("FINITE_BOUNDS_VIOLATION_IN_JAVA") | |
abstract class EventBuilder<T : AnswersEvent<T>> { | |
private val customAttributes: MutableList<CustomAttribute> = mutableListOf() | |
abstract fun createEvent(): T | |
fun build(): T = createEvent().apply { | |
configure() | |
customAttributes.forEach { | |
it.build(this) | |
} | |
} | |
fun attr(block: CustomAttribute.() -> Unit) { | |
customAttributes.add(CustomAttribute().apply(block)) | |
} | |
abstract fun T.configure() | |
} | |
@FabricEventDsl | |
class AddToCartEventBuilder : EventBuilder<AddToCartEvent>() { | |
private lateinit var priceValue: BigDecimal | |
var price: Any? = null | |
set(value) { | |
when (value) { | |
is String -> value.toDoubleOrNull() | |
is Number -> value.toDouble() | |
else -> value.toString().toDoubleOrNull() | |
}?.let { | |
priceValue = BigDecimal.valueOf(it) | |
} | |
} | |
private var currencyValue: Currency? = null | |
var currency: String? = null | |
set(value) { | |
try { | |
currencyValue = Currency.getInstance(value) | |
} catch (_: IllegalArgumentException) { | |
} | |
} | |
lateinit var itemId: Any | |
var name: String? = null | |
var categoryId: Any? = null | |
override fun createEvent(): AddToCartEvent = AddToCartEvent() | |
override fun AddToCartEvent.configure() { | |
putItemId(itemId.toString()) | |
categoryId?.let { | |
putCustomAttribute("category_id", it.toString()) | |
} | |
currencyValue?.let { | |
putCurrency(it) | |
} | |
putItemName(name) | |
putItemPrice(priceValue) | |
} | |
} | |
@FabricEventDsl | |
class StartCheckoutEventBuilder : EventBuilder<StartCheckoutEvent>() { | |
private lateinit var totalPriceValue: BigDecimal | |
var totalPrice: Any = 0 | |
set(value) { | |
when (value) { | |
is String -> value.toDoubleOrNull() | |
is Number -> value.toDouble() | |
else -> value.toString().toDoubleOrNull() | |
}?.let { | |
totalPriceValue = BigDecimal.valueOf(it) | |
} | |
} | |
private var currencyValue: Currency? = null | |
var currency: String? = null | |
set(value) { | |
try { | |
currencyValue = Currency.getInstance(value) | |
} catch (_: IllegalArgumentException) { | |
} | |
} | |
override fun createEvent(): StartCheckoutEvent = StartCheckoutEvent() | |
private var itemCount: Int = 0 | |
var items: Any? = null | |
set(value) { | |
itemCount = when (value) { | |
is Collection<*> -> value.size | |
is Number -> value.toInt() | |
else -> 0 | |
} | |
} | |
override fun StartCheckoutEvent.configure() { | |
putTotalPrice(totalPriceValue) | |
currencyValue?.let { | |
putCurrency(it) | |
} | |
putItemCount(itemCount) | |
} | |
} | |
@FabricEventDsl | |
class ContentViewEventBuilder : EventBuilder<ContentViewEvent>() { | |
override fun createEvent(): ContentViewEvent = ContentViewEvent() | |
lateinit var contentId: String | |
var contentName: String? = null | |
lateinit var contentType: String | |
override fun ContentViewEvent.configure() { | |
putContentId(contentId) | |
contentName?.let { putContentName(contentName) } | |
putContentName(contentType) | |
} | |
} | |
@FabricEventDsl | |
class SignUpEventBuilder : EventBuilder<SignUpEvent>() { | |
lateinit var signMethod: String | |
var isSuccess: Boolean = false | |
override fun createEvent(): SignUpEvent = SignUpEvent() | |
override fun SignUpEvent.configure() { | |
putMethod(signMethod) | |
putSuccess(isSuccess) | |
} | |
} | |
@FabricEventDsl | |
class LoginEventBuilder : EventBuilder<LoginEvent>() { | |
lateinit var signMethod: String | |
var isSuccess: Boolean = false | |
override fun createEvent(): LoginEvent = LoginEvent() | |
override fun LoginEvent.configure() { | |
putMethod(signMethod) | |
putSuccess(isSuccess) | |
} | |
} | |
@FabricEventDsl | |
fun sendAddCartEvent(block: AddToCartEventBuilder.() -> Unit) = | |
AddToCartEventBuilder().apply(block).build().also { | |
answers()?.logAddToCart(it) | |
} | |
@FabricEventDsl | |
fun sendStartCheckoutEvent(block: StartCheckoutEventBuilder.() -> Unit) = | |
StartCheckoutEventBuilder().apply(block).build().also { | |
answers()?.logStartCheckout(it) | |
} | |
@FabricEventDsl | |
fun sendCustomEvent(name: String, block: EventBuilder<CustomEvent>.() -> Unit) { | |
object : EventBuilder<CustomEvent>() { | |
override fun createEvent(): CustomEvent = CustomEvent(name) | |
override fun CustomEvent.configure() { | |
} | |
}.apply(block).build().also { | |
answers()?.logCustom(it) | |
} | |
} | |
@FabricEventDsl | |
fun sendContentView(block: ContentViewEventBuilder.() -> Unit) { | |
ContentViewEventBuilder().apply(block).build().also { | |
answers()?.logContentView(it) | |
} | |
} | |
@FabricEventDsl | |
fun sendSignUp(block: SignUpEventBuilder.() -> Unit) { | |
SignUpEventBuilder().apply(block).build().also { | |
answers()?.logSignUp(it) | |
} | |
} | |
@FabricEventDsl | |
fun sendLogin(block: LoginEventBuilder.() -> Unit) { | |
LoginEventBuilder().apply(block).build().also { | |
answers()?.logLogin(it) | |
} | |
} | |
private fun answers(): Answers? = if (Fabric.isInitialized()) Answers.getInstance() else null |
how to create block to pass ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, How do I use this in any of Activity or Fragment. Can you please add a bit of source to call your sendAddCartEvent fun.