Author: energetically intelligent client team speaker song way
background
Burying point is an important but tedious part of the daily development of our intelligent client team. Every time to the burial point, there is an old yard farmers in the diligent moving bricks of both visual sense. Well, this button is CTRL, this button is C, this button is CTRL, and then a V. Oh yeah, it worked! Summon the dragon!! The dragon, however, did not appear. What the F ** K, it is already 2021 why I still need to move the name of buried Event from the document to the project one by one? I have to pick the exact same variable name in all uppercase. And the Key and the Value. Every time I bury something, can’t you take the initiative to tell me that you belong to this Event? Do you want to be single all your life? To preserve the dignity of an old coder. I decided to fight them to the last ditch.
The final result
Code first:
One-click import of Event, Key, and Value
Before the import
public interface Track {
String KEY_FROM_PAGE = "key_from_page";
String PAGE_STAY = "page_stay";
String PAGE_ENTER = "page_enter";
interface Page {
}
interface Event {
}
interface Enter {
String PROFILE_DETAIL_ENTER = "profile_detail_enter";
}
interface KEY {
}
interface Value {
}
}
Copy the code
After import
public interface Track { String KEY_FROM_PAGE = "key_from_page"; String PAGE_STAY = "page_stay"; String PAGE_ENTER = "page_enter"; interface Page { String POINT_READING_RECORD = "point_reading_record"; } interface Event { String POINT_READING_RECORD_BOOK_ENTER = "point_reading_record_book_enter"; String POINT_READING_SETTING = "point_reading_setting"; String BIND_PUSH_REQUIRE = "bind_push_require"; String HARDWARE_BIND_FINISH = "hardware_bind_finish"; String POINT_READING_CHECK_RESULT = "point_reading_check_result"; String POINT_READING_CHECK_AGAIN = "point_reading_check_again"; String POINT_READING_GUIDE_CLICK = "point_reading_guide_click"; String POINT_READING_SUPPORT_BOOK_ENTER = "point_reading_support_book_enter"; String POINT_READING_DETAIL_ENTER = "point_reading_detail_enter"; String POINT_READING_FEEDBACK = "point_reading_feedback"; String POINT_READING_RECORD_ENTER = "point_reading_record_enter"; }}Copy the code
One key generation buried point method
before
class TapReadTrackCenter(val handler: ITrackHandler) {
}
Copy the code
Generated after
class TapReadTrackCEnter(val handler: ITrackHandler) {
fun logPOintReadingScaleEvent() {
//Track.Event.POINT_READING_SCALE.log (handler,
//Track.Key.STATUS to Track.Value ,
//Track.Key.ACTION to Track.Value
//)
}
fun logPointReadingSettingEvent() {
//Track. Event. POINT_READING_SETTING.log (handler,
//Track.Key.PAGE_ID to Track.Value ,
//Track.Key.IMAGE_URI to Track.Value ,
//Track.Key.B00K_ID to Track.Value ,
//Track.Key.PAGE_NO to Track.Value T ,
//Track.Key.RESULT to Track.Value
//)
}
fun logPointReadingFeedbackEvent() {
//Track. Event. POINT_READING_FEEDBACK.log(handler,
//Track.Key.PAGE_ID to Track.Value ,
//Track.Key.IMAGE_URI to Track.Value ,
//Track.Key.BOOK_ID to Track.Value ,
//Track.Key.PAGE_NO to Track.Value ,
//Track.Key.CONTENT_to Track.Value
//)
}
}
Copy the code
I swear on my honor that the code above was not typed by my hand.
Implementation approach
Live Templates
Live Templates is a handy tool provided by Android Studio. But for a long time, its use was limited to:
- Type psfi to generate public static final Int
- Input logd, Generate HLogger. Tag (TAGTAGTAG). D ({” fileNamefileNamefileName classNameclassNameclassName functionNamefunctionNamefunctionName InfoInfoInfo”})
It reminded me of something a fitness instructor used to say: Why isn’t the lower trapezius working on its own weight? Because these two muscles are so powerful, you’re insulting them by training them under your own weight. I have a strong feeling that Live Templates has been severely insulted in the long run. Em…
Groovy
Live Templates actually supports Groovy scripts. With Groovy scripting support, there’s practically no limit to what you can do in theory. It’s hidden in our lovely Live Templates setup.
The question then becomes how to use Groovy scripts to generate buried code. Obviously, we need to have some information about the burial site. For example, what events and pages are there, and what events are corresponding to each Page. Looking at a buried doc that obviously contained this information but didn’t have any structure, I had a plan in my head to hook up with some guy on the front end to help me with it. Fortunately, what you want is already hiding somewhere. You just haven’t seen it yet.
Buried point management platform
Each company has its own buried point management platform, after which we can easily extract structured data, similar to database.
The resulting byte buried platform exports an Excel file containing jSON-formatted information about buried events. Yes, you read that right. It is in JSON format. Full of happiness ~
Of course, this is not limited to any format, but we can export structured data based on the basic capabilities provided by each company’s existing buried platform. The point is to map the data source to relevant buried code.
everything
OK. Now we have everything we need. Then all I need is CTRL + C, CTRL + V, oh no, I pooh. All we have to do is parse the Excel file and create a Live Template.
Parse Excel and generate auxiliary files
1. By analyzing Excel files, we have already known all the pages, keys, events and values of the buried sites. This information can be populated directly into the required classes at parsing time. I did this in Python.
2. Generate an Event and Key mapping file
3. Generate a Page and Event mapping file
Of course, you can also put 2,3 in a file. I just split it up to make it easier to parse.
Create a Live Template
The value of info is
groovyScript(“/Applications/Android\ Studio.app/Contents/plugins/android/lib/templates/pageEvent.groovy”, clipboard())
The clipboard is passed in to accept which Page the event we currently want to bury belongs to. So, when we enter the page keyword in the code, we will execute the pageEvent.groovy script. I put the script there in advance (of course I didn’t copy it myself). After all, the rest of the team uses it too).
The contents of the pageEvent.groovy script will not be posted. It is mainly to know how many events there are and what keys each Event corresponds to according to the page name and the auxiliary files generated before. And then according to certain rules spliced into a code segment return.
The overall effect is the effect posted at the beginning of the article. It all seemed so good that the prince finally didn’t have to work overtime, and the prince and princess lived happily ever after. The End~
Wait, I have to go manually export Excel?
There is no end to laziness. I understand you. Because SO am I. So I’m not going to manually export Excel to a buried platform. I wouldn’t do it in my life. But the buried platform didn’t provide an API for me to call. What to do? B: well… It doesn’t bother me. I also have Selenium. We solved this problem by simulating browser operations. It’s not quite as elegant, but it’s nothing compared to the fear of being buried and dominated.
Tips:
In order to simplify the code of buried points, we made an extension method. In the end, the embedded code is cleaner and only needs to focus on the Key and Value itself, without the need for various tedious method calls.
Track.Event.POINT_READING_SCALE.log(handler,
Track.Key.STATUS to status,
Track.Key.ACTION to action
)
Copy the code
The extension method code is as follows
fun String.log(vararg pairs: Pair<String, Any>): Event { return log(null, *pairs) } fun String.log(handler: ITrackHandler? , vararg pairs: Pair<String, Any>): Event { val event = Event.create(this).apply { pairs.forEach { val (key, value) = it params.put(key, value) } } event.log(handler) return event }Copy the code
defects
This process has been practiced for several versions in our project. There’s not much of a problem overall. The only drawback… Basically, push DA to get a burial-point entry done on the burial-point platform before you do. Well, my advice is, if your DA is a man, send a woman. The same thing applies.
Afterword.
This article explores how Live Templates can play a greater role in project engineering, based on our experience with buried site processes. I also hope that you can share more ideas with your own experience to improve the development efficiency. T = t ^ t leave work early