Treda, or a chain of letters in mail — one of features, on which at geeks and mass audience polar views. Geeks actively use them; normal users, as show our polls, treat them rather watchfully. First, unusually, secondly, people are afraid that they will not be able to orient in chains. When we implemented treda in the web version of the Mail.Ru Mail, we remembered this chellendzh — and found how it seems to us, the most convenient and intuitively clear algorithm of grouping which will be convenient to both geeks, and less advanced users. In work on mobile treda we took the system developed by big Mail as we did not want to confuse users as a basis and to do different logic. Our task from the point of view of a product was in that both web tredy, and mobile treda worked for the user equally. But many things had to be remade taking into account offline - works. About how we made in a chain Mail.Ru Mail Android-application where letters are not lost even at failures in a network, I will tell in this article (how made the same in an iOS-application, we will tell in one of the following posts).
In mobile application we placed special emphasis on stable and smooth operation offline. Our purpose was to make so that the user could edit offline letters, move them, mark, work with mail and make actions with the whole treda — and when the network appears that information on all these actions was correctly transferred to the server.
The most widespread option of implementation of tred in email applications — grouping of letters on the client: letters are extorted and already then gather in chains. Such option covers the majority of standard situations except for difficult cases among which there is, for example, a correct grouping of letters in treda during the work in offline. However we needed a solution which will work guaranteed well for all users. We did not want to endow offline - work, just in difficult situations quality of a product is shown. Besides, when we carried out a usability testing, people noted that to use treda conveniently and abruptly — but at the same time many were afraid that they will cease to find in a letter box if chains are grouped not intuitively. Therefore we paid special attention to operation of application offline: it was necessary to make so that all changes remained separately, and then at an opportunity were imperceptibly synchronized. For the user it has to look so: it makes changes, and further the application itself understands how and when to send all this to the server.
Technical aspect of synchronization
So, we decided on approach, it was possible to start implementation. The logic of grouping of letters in chains was developed for the big web version of Mail. So we had not to invent the bicycle: in an Android-application we just implemented the same algorithm. On Habré there is a detailed post how it was developed and what differs in from algorithms which use other mail services therefore I will not describe in detail it in today's article.
So, it was important to be convinced that all operations with letters within our logic are executed correctly. For example, during the moving or removal of messages all counters in a web and the application have to display the correct (updated) number of letters in rub, all checkboxes and marks "Is read", "Have to be not read" are updated and synchronized, certain treda have to cease to be displayed in some folders etc. In addition to a correctness of synchronization it was important to make so that operations over letters were made quickly. We tested speed on an array from 200 letters in different treda and folders. In the first version on it left seconds 20 — and the user should wait for all this time. Naturally, we could not allow it.
We began to look for the reason of such sluggishness. Initially we assumed that most of all time was taken by SQL operations because they depended on the number of letters: at execution of operation with each of them it was necessary to update treda, counters, folders, the letter and so on. However actually copying of excessively difficult indexes on which we selected then data from memory was the bottleneck. It took 70-80% of time from these 20 seconds. We cleaned copying of these indexes, and also reorganization of indexes on each operation, made buffering and update after completion of operation at the end of transaction. As a result instead of 20 seconds at us only about 20-50 ms on operation of evolution of indexes turned out. Besides, we cleaned data which do not change according to our business logic from evolutions of indexes.
We have rather simple and fast indexes, for example, on some specific value: flagged, unflagged, read, unread, on value of the folder, according to the account. If value of options of statuses of course, indexes are rebuilt quickly. And the text indexes based on prefix trees are rebuilt long: they and on the volume of the occupied memory big, and in general this structure is rather bulky. It allows to accelerate search process, but at the same time very difficult and long changes. These are standard problems of different structures and algorithms of data handling. Where we want to win on search, we, naturally, lose on change of these data.
At first we tried to do different implementations of text indexes. The notable difference was not noticed and selected those which were quicker on our measurements. But all of us did not reach desirable indicators on time yet. Then we decided to come on the other hand. Such data as a letter subject, the sender, etc. during operations with the letter do not change. Means it is possible just to refuse evolution of difficult indexes and to be limited to those that are responsible for the parameters flagged, unflagged etc., that is fast. We refused all excess operations, optimized what it is impossible to refuse, and at once for 50-60% accelerated synchronization.
But also on it we did not stop — operations can be grouped. If not to try to do universal algorithm, and to sort on final options and to group input data in types of operations, then they can be executed already without any conditions and excess operations with a DB. We by the piece optimized operations with checkboxes, with movement of letters etc., and they ceased to depend on quantity of transmitted data.
As a result the user sees a fast response irrespective of the fact which operation he made sorts and over what data. Right at the beginning we put for ourselves a level in 100 ms, but rested already against restrictions of Android: the transaction mechanism is quite slow and if not to use it, then we receive an error of an intermediate status. It was necessary to correct requirements. We planned that from the point of view of the user operation occupied no more than 200-500 milliseconds. Most likely, in upcoming version of Android all of us will achieve that all of us for the user of operation were executed almost instantly.
As I already mentioned, the Android-application of Mail inherited logic of display of tred from the web version. However in implementation of the interface, naturally, there are also distinctions: the screen of the smartphone and even the tablet much less desktop, it was also necessary to solve, how exactly to display on them treda.
Initially we selected between two options. The first — display in two levels (the list of tred and viewing of a tred combined with reading the letter). At first it seemed to us logical and preferable. But having tested such implementation in other applications, we were disappointed in it: display of tred on two screens in all cases turned out rather inconvenient. Therefore we selected option with three levels (the list of tred, viewing of letters in this tred, opening and reading the letter). In this case the user has an opportunity to look at headings and to select what letters he wants to open.
Because in a tred at letters identical subjects, they are grouped, and on the screen there is a place for display of more detailed snippets. In general it turned out more convenient: when it is necessary to remember about what there was a speech in long correspondence, it is possible to refresh on snippets of letters it in memory, or to dolistat quickly to the necessary letter and to begin to read correspondence from that place where there is something important.
One more difference between a big web and the application concerns provision of new letters in rub. In the web version it is made in two screens because in the browser is where to place this information. The web always shows rubs and opens either the last unread letter, or the upper letter from this folder, and it is possible to turn and develop any letters on one screen further. In an Android-application the user always "fails" at present either in rubs, or in reading the letter (if the chain contains only one letter). Between letters in a tred it is possible to move on a svaypa to the left or to the right.
Separately It is necessary to tell about the toolbar relating to all tred. Here Android-mail has more discrepancies with the web version. In the last the toolbar is the big panel allowing to make operations with the upper letter of a tred in the folder. In a Mail.Ru Mail Android-application work is conducted or with the whole chain, or with the specific letters selected with the user. There is only one exception: when there is a work with tredy, there is an opportunity to respond to the last letter in this chain. It is one of rather frequent and important cases therefore we took out this button from reading the letter and turned on it in instruments of work with tredy. The Answer and Send buttons which it is possible to call the main action buttons for a tred are used to the last letter of a chain.
Interaction with Android Wear and Android Auto
We initially implemented support of Android Wear and Android Auto therefore for interaction of Android-Pochty with hours and the radio tape recorder nothing was necessary to do in addition. If the user included grouping of letters in settings of mail, notifications on hours will be grouped in treda too. If to turn off grouping of letters, then everything will work as before, and notifications will not be grouped. On the radio tape recorder under control of notification Android Auto are also grouped in treda.
In our opinion, we coped with objectives. But, as always, we very much wait from you for a fidbek. Test treda for Android and tell — whether it is convenient to you?
This article is a translation of the original post at habrahabr.ru/post/273841/
If you have any questions regarding the material covered in the article above, please, contact the original author of the post.
If you have any complaints about this article or you want this article to be deleted, please, drop an email here: firstname.lastname@example.org.
We believe that the knowledge, which is available at the most popular Russian IT blog habrahabr.ru, should be accessed by everyone, even though it is poorly translated.
Shared knowledge makes the world better.