You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnreadStatusManagerImpl.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.ui.messages;
  18. import com.dmdirc.config.binding.ConfigBinding;
  19. import com.dmdirc.events.BaseChannelTextEvent;
  20. import com.dmdirc.events.BaseQueryTextEvent;
  21. import com.dmdirc.events.ChannelHighlightEvent;
  22. import com.dmdirc.events.DisplayProperty;
  23. import com.dmdirc.events.DisplayableEvent;
  24. import com.dmdirc.events.QueryHighlightEvent;
  25. import com.dmdirc.events.UnreadStatusChangedEvent;
  26. import com.dmdirc.interfaces.WindowModel;
  27. import com.dmdirc.util.colours.Colour;
  28. import io.reactivex.subjects.PublishSubject;
  29. import io.reactivex.subjects.Subject;
  30. import net.engio.mbassy.listener.Handler;
  31. import java.util.Optional;
  32. import java.util.concurrent.TimeUnit;
  33. /**
  34. * Tracks unread messages and other notifications.
  35. */
  36. public class UnreadStatusManagerImpl implements UnreadStatusManager {
  37. private final WindowModel container;
  38. private final ColourManager colourManager;
  39. private final Subject<UnreadStatusChangedEvent> eventSubject;
  40. private int unreadLines;
  41. private Optional<Colour> notificationColour = Optional.empty();
  42. private Optional<Colour> miscellaneousColour = Optional.of(Colour.GREEN);
  43. private Optional<Colour> messageColour = Optional.of(Colour.BLUE);
  44. private Optional<Colour> highlightColour = Optional.of(Colour.RED);
  45. public UnreadStatusManagerImpl(final WindowModel container) {
  46. this.container = container;
  47. this.colourManager = new ColourManagerImpl(container.getConfigManager());
  48. this.eventSubject = PublishSubject.create();
  49. eventSubject
  50. .throttleLast(200, TimeUnit.MILLISECONDS)
  51. .subscribe(unreadStatusChangedEvent -> container.getEventBus().publish(unreadStatusChangedEvent));
  52. }
  53. @Handler
  54. public void handleDisplayableEvent(final DisplayableEvent event) {
  55. if (includeEvent(event)) {
  56. updateStatus(miscellaneousColour, unreadLines + 1);
  57. }
  58. }
  59. @Handler
  60. public void handleChannelTextEvent(final BaseChannelTextEvent event) {
  61. if (includeEvent(event)) {
  62. updateStatus(messageColour);
  63. }
  64. }
  65. @Handler
  66. public void handleQueryTextEvent(final BaseQueryTextEvent event) {
  67. if (includeEvent(event)) {
  68. updateStatus(messageColour);
  69. }
  70. }
  71. @Handler
  72. public void handleChannelHighlightEvent(final ChannelHighlightEvent event) {
  73. if (includeEvent(event)) {
  74. updateStatus(highlightColour);
  75. }
  76. }
  77. @Handler
  78. public void handleQueryHighlightEvent(final QueryHighlightEvent event) {
  79. if (includeEvent(event)) {
  80. updateStatus(highlightColour);
  81. }
  82. }
  83. private boolean includeEvent(final DisplayableEvent event) {
  84. return event.getSource().equals(container)
  85. && !event.getDisplayProperty(DisplayProperty.DO_NOT_DISPLAY).orElse(false);
  86. }
  87. @Override
  88. public int getUnreadLines() {
  89. return unreadLines;
  90. }
  91. @Override
  92. public Optional<Colour> getNotificationColour() {
  93. return notificationColour;
  94. }
  95. @Override
  96. public void clearStatus() {
  97. updateStatus(Optional.empty(), 0);
  98. }
  99. private void updateStatus(final Optional<Colour> desiredColour) {
  100. updateStatus(desiredColour, unreadLines);
  101. }
  102. private void updateStatus(final Optional<Colour> desiredColour, final int newUnreadCount) {
  103. final Optional<Colour> newColour = getBestColour(desiredColour, notificationColour);
  104. final boolean updated = !newColour.equals(notificationColour)
  105. || newUnreadCount != unreadLines;
  106. notificationColour = newColour;
  107. unreadLines = newUnreadCount;
  108. if (updated) {
  109. publishChangedEvent();
  110. }
  111. }
  112. private Optional<Colour> getBestColour(
  113. final Optional<Colour> desiredColour,
  114. final Optional<Colour> existingColour) {
  115. if (!desiredColour.isPresent()) {
  116. // If we're trying to explicitly reset, go with the empty one.
  117. return desiredColour;
  118. }
  119. if (desiredColour.equals(highlightColour)
  120. || !existingColour.isPresent()
  121. || existingColour.equals(miscellaneousColour)) {
  122. return desiredColour;
  123. } else {
  124. return existingColour;
  125. }
  126. }
  127. @ConfigBinding(domain = "ui", key = "miscellaneousNotificationColour")
  128. void handleMiscellaneousColour(final String colour) {
  129. final Optional<Colour> newColour = Optional.ofNullable(
  130. colourManager.getColourFromString(colour, Colour.GREEN));
  131. if (notificationColour.equals(miscellaneousColour)) {
  132. notificationColour = newColour;
  133. publishChangedEvent();
  134. }
  135. miscellaneousColour = newColour;
  136. }
  137. @ConfigBinding(domain = "ui", key = "messageNotificationColour")
  138. void handleMessageColour(final String colour) {
  139. final Optional<Colour> newColour = Optional.ofNullable(
  140. colourManager.getColourFromString(colour, Colour.BLUE));
  141. if (notificationColour.equals(messageColour)) {
  142. notificationColour = newColour;
  143. publishChangedEvent();
  144. }
  145. messageColour = newColour;
  146. }
  147. @ConfigBinding(domain = "ui", key = "highlightNotificationColour")
  148. void handleHighlightColour(final String colour) {
  149. final Optional<Colour> newColour = Optional.ofNullable(
  150. colourManager.getColourFromString(colour, Colour.RED));
  151. if (notificationColour.equals(highlightColour)) {
  152. notificationColour = newColour;
  153. publishChangedEvent();
  154. }
  155. highlightColour = newColour;
  156. }
  157. private void publishChangedEvent() {
  158. eventSubject.onNext(new UnreadStatusChangedEvent(container, this, notificationColour,
  159. unreadLines));
  160. }
  161. }