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 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.events.eventbus.EventBus;
  27. import com.dmdirc.interfaces.WindowModel;
  28. import com.dmdirc.util.colours.Colour;
  29. import java.util.Optional;
  30. import java.util.concurrent.TimeUnit;
  31. import io.reactivex.Emitter;
  32. import io.reactivex.Observable;
  33. import io.reactivex.ObservableEmitter;
  34. import io.reactivex.Observer;
  35. import io.reactivex.functions.Consumer;
  36. import io.reactivex.subjects.PublishSubject;
  37. import io.reactivex.subjects.ReplaySubject;
  38. import io.reactivex.subjects.Subject;
  39. import net.engio.mbassy.listener.Handler;
  40. import org.reactivestreams.Publisher;
  41. /**
  42. * Tracks unread messages and other notifications.
  43. */
  44. public class UnreadStatusManagerImpl implements UnreadStatusManager {
  45. private final WindowModel container;
  46. private final ColourManager colourManager;
  47. private final Subject<UnreadStatusChangedEvent> eventSubject;
  48. private int unreadLines;
  49. private Optional<Colour> notificationColour = Optional.empty();
  50. private Optional<Colour> miscellaneousColour = Optional.of(Colour.GREEN);
  51. private Optional<Colour> messageColour = Optional.of(Colour.BLUE);
  52. private Optional<Colour> highlightColour = Optional.of(Colour.RED);
  53. public UnreadStatusManagerImpl(final WindowModel container) {
  54. this.container = container;
  55. this.colourManager = new ColourManagerImpl(container.getConfigManager());
  56. this.eventSubject = PublishSubject.create();
  57. eventSubject
  58. .throttleLast(200, TimeUnit.MILLISECONDS)
  59. .subscribe(unreadStatusChangedEvent -> container.getEventBus().publish(unreadStatusChangedEvent));
  60. }
  61. @Handler
  62. public void handleDisplayableEvent(final DisplayableEvent event) {
  63. if (includeEvent(event)) {
  64. updateStatus(miscellaneousColour, unreadLines + 1);
  65. }
  66. }
  67. @Handler
  68. public void handleChannelTextEvent(final BaseChannelTextEvent event) {
  69. if (includeEvent(event)) {
  70. updateStatus(messageColour);
  71. }
  72. }
  73. @Handler
  74. public void handleQueryTextEvent(final BaseQueryTextEvent event) {
  75. if (includeEvent(event)) {
  76. updateStatus(messageColour);
  77. }
  78. }
  79. @Handler
  80. public void handleChannelHighlightEvent(final ChannelHighlightEvent event) {
  81. if (includeEvent(event)) {
  82. updateStatus(highlightColour);
  83. }
  84. }
  85. @Handler
  86. public void handleQueryHighlightEvent(final QueryHighlightEvent event) {
  87. if (includeEvent(event)) {
  88. updateStatus(highlightColour);
  89. }
  90. }
  91. private boolean includeEvent(final DisplayableEvent event) {
  92. return event.getSource().equals(container)
  93. && !event.getDisplayProperty(DisplayProperty.DO_NOT_DISPLAY).orElse(false);
  94. }
  95. @Override
  96. public int getUnreadLines() {
  97. return unreadLines;
  98. }
  99. @Override
  100. public Optional<Colour> getNotificationColour() {
  101. return notificationColour;
  102. }
  103. @Override
  104. public void clearStatus() {
  105. updateStatus(Optional.empty(), 0);
  106. }
  107. private void updateStatus(final Optional<Colour> desiredColour) {
  108. updateStatus(desiredColour, unreadLines);
  109. }
  110. private void updateStatus(final Optional<Colour> desiredColour, final int newUnreadCount) {
  111. final Optional<Colour> newColour = getBestColour(desiredColour, notificationColour);
  112. final boolean updated = !newColour.equals(notificationColour)
  113. || newUnreadCount != unreadLines;
  114. notificationColour = newColour;
  115. unreadLines = newUnreadCount;
  116. if (updated) {
  117. publishChangedEvent();
  118. }
  119. }
  120. private Optional<Colour> getBestColour(
  121. final Optional<Colour> desiredColour,
  122. final Optional<Colour> existingColour) {
  123. if (!desiredColour.isPresent()) {
  124. // If we're trying to explicitly reset, go with the empty one.
  125. return desiredColour;
  126. }
  127. if (desiredColour.equals(highlightColour)
  128. || !existingColour.isPresent()
  129. || existingColour.equals(miscellaneousColour)) {
  130. return desiredColour;
  131. } else {
  132. return existingColour;
  133. }
  134. }
  135. @ConfigBinding(domain = "ui", key = "miscellaneousNotificationColour")
  136. void handleMiscellaneousColour(final String colour) {
  137. final Optional<Colour> newColour = Optional.ofNullable(
  138. colourManager.getColourFromString(colour, Colour.GREEN));
  139. if (notificationColour.equals(miscellaneousColour)) {
  140. notificationColour = newColour;
  141. publishChangedEvent();
  142. }
  143. miscellaneousColour = newColour;
  144. }
  145. @ConfigBinding(domain = "ui", key = "messageNotificationColour")
  146. void handleMessageColour(final String colour) {
  147. final Optional<Colour> newColour = Optional.ofNullable(
  148. colourManager.getColourFromString(colour, Colour.BLUE));
  149. if (notificationColour.equals(messageColour)) {
  150. notificationColour = newColour;
  151. publishChangedEvent();
  152. }
  153. messageColour = newColour;
  154. }
  155. @ConfigBinding(domain = "ui", key = "highlightNotificationColour")
  156. void handleHighlightColour(final String colour) {
  157. final Optional<Colour> newColour = Optional.ofNullable(
  158. colourManager.getColourFromString(colour, Colour.RED));
  159. if (notificationColour.equals(highlightColour)) {
  160. notificationColour = newColour;
  161. publishChangedEvent();
  162. }
  163. highlightColour = newColour;
  164. }
  165. private void publishChangedEvent() {
  166. eventSubject.onNext(new UnreadStatusChangedEvent(container, this, notificationColour,
  167. unreadLines));
  168. }
  169. }