The background,
Due to business needs, pages often have a lot of popovers. And because of the bad user experience of overlay display, these pop-ups are usually required to appear mutually exclusive.
In the previous design, these popovers were cluttered and unmanaged. Each time a new popover service is added, mutual exclusion with existing popovers should be considered. This unmanaged popover implementation has the following disadvantages:
1. State differentiation of the logic appearing in each popover is needed to achieve mutual exclusion. Every time a new popover is added, the corresponding state needs to be added for management judgment. Pop-up services are mixed and mutually exclusive relationships are easily omitted.
2. Enter the page according to the display logic of each popup business, judge whether the display of this popup will be triggered. Therefore, no matter whether the triggered popup business is successful, other pop-ups will not be displayed. Popovers have one fewer possible exposure opportunities.
3. Currently, the sequential presentation of popovers is not supported. If it is supported, the logic of each popover will be coupled with each other.
In view of the above pain points, service pop-ups need to be abstracted and uniformly managed to achieve the purpose of self-sorting according to the weight. Finally achieve the following functions:
1. Each popover is logically isolated, and whether it pops up is completely determined by the popover management class. If the high priority popover fails, the low priority popover will pop up according to the high priority.
2. The priority of pop-ups is dynamically delivered by the interface. The priority of pop-ups can be dynamically adjusted according to product needs.
Second, the implementation
1. Popover abstraction
1) Popover type definition: PopType
Implementation using enumeration to distinguish between different popovers.
2) Popover entity object: PopData
Aiming at the abstraction of popover data, popover data entity defines popover type, unique id and popover content.
3) Popover Task: Task
The specific execution body of popover business logic, including but not limited to network requests and other tasks, used to generate popover data and prioritization. Unique task ids are defined, priorities are defined, and the Comparable interface is implemented for sorting.
2, pop-up task management class TaskManager
It is used to manage pop-ups. It mainly maintains a priority queue of pop-ups and determines which pop-ups can pop up according to the priority order.
First, the popover Task implements the Comparable interface to give it the ability to compare sizes by priority
TaskManager uses PriorityQueue internally as a popover Task container, and PriorityQueue uses ** heapsort internally to sort internal elements. ** The queue is prioritized for each Task element added or removed.
Each time a Task returns data, if the Task is the top Task, the popup window corresponding to the Task is displayed. Otherwise, the popover data is queued.
If the pop-up Task does not return normal data, the Task is removed from the Task queue. Check whether the top Task in the current TaskQueue returns a value. Otherwise, wait for the result of the next task and repeat the process.
The architectural design is shown in the figure below: