Skip to content

Instantly share code, notes, and snippets.

// Dummy analytics event tracker that logs the events to Logcat
object EventTracker {
private const val TAG = "EventTracker"
fun logEvent(name: String, params: Bundle) {
Log.d(TAG, "Logging event name: $name; params: $params")
}
}
// Set Target as class since we want this annotation to be added to a class element
@Target(AnnotationTarget.CLASS)
// Set retention as Source since we only need this annotation
// during annotation processing process and
// we don't need this class at runtime.
@Retention(AnnotationRetention.SOURCE)
annotation class AnalyticsEvent
@malvinstn
malvinstn / processor_module_build.gradle
Last active July 9, 2019 05:19
build.gradle for your processor module
apply plugin: 'kotlin'
apply plugin: 'kotlin-kapt'
dependencies {
// Add dependency to your module that has the annotation class.
compileOnly project(':annotation')
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
// To easily register your custom annotation processor to the annotation process flow.
implementation "com.google.auto.service:auto-service:1.0-rc4"
@AnalyticsEvent
sealed class MyEvent {
data class ShareImage(val imageName: String, val fullString: String) : MyEvent()
object ButtonTapped : MyEvent()
}
@AutoService(Processor::class)
class AnalyticsEventProcessor : KotlinAbstractProcessor(), KotlinMetadataUtils {
companion object {
private val ANNOTATION = AnalyticsEvent::class.java
}
override fun getSupportedAnnotationTypes() = setOf(ANNOTATION.canonicalName)
override fun getSupportedSourceVersion(): SourceVersion = SourceVersion.latestSupported()
@AutoService(Processor::class)
class AnalyticsEventProcessor : KotlinAbstractProcessor(), KotlinMetadataUtils {
companion object {
private val ANNOTATION = AnalyticsEvent::class.java
}
...
override fun process(
annotations: Set<TypeElement>,
@AutoService(Processor::class)
class AnalyticsEventProcessor : KotlinAbstractProcessor(), KotlinMetadataUtils {
companion object {
private val ANNOTATION = AnalyticsEvent::class.java
private const val EVENT_PARAMETER_NAME = "event"
private const val EVENT_NAME_PARAMETER_NAME = "name"
private const val EVENT_PARAM_PARAMETER_NAME = "params"
private const val LOG_EVENT_FUNCTION_NAME = "logEvent"
...
apply plugin: 'kotlin-kapt'
android {
...
}
dependencies {
implementation project(':tracker')
@malvinstn
malvinstn / Flow.kt
Last active June 9, 2019 09:10
Kotlin merge 2 different Flow
fun <T> Flow<T>.mergeWith(other: Flow<T>): Flow<T> = flow {
coroutineScope {
launch {
collect { emit(it) }
}
launch {
other.collect { emit(it) }
}
}
}
@malvinstn
malvinstn / Roboscript.json
Created July 7, 2019 07:06
Sample robo script
[
{
"eventType": "VIEW_CLICKED",
"timestamp": 1561810695346,
"replacementText": "",
"actionCode": -1,
"delayTime": 0,
"canScrollTo": false,
"elementDescriptors": [
{