Skip to content

Commit

Permalink
Merge pull request #131 from Code52/tidy-up-message-merger
Browse files Browse the repository at this point in the history
tidy up message merger
  • Loading branch information
hnrkndrssn authored Apr 17, 2017
2 parents 6a38925 + a716d0c commit b741586
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 45 deletions.
2 changes: 0 additions & 2 deletions src/Carnac.Logic/Carnac.Logic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
<ItemGroup>
<Compile Include="Enums\NotificationPlacement.cs" />
<Compile Include="IConcurrencyService.cs" />
<Compile Include="IMessageMerger.cs" />
<Compile Include="IMessageProvider.cs" />
<Compile Include="Internal\FixedQueue.cs" />
<Compile Include="IKeyProvider.cs" />
Expand All @@ -92,7 +91,6 @@
<Compile Include="KeyMonitor\InterceptKeys.cs" />
<Compile Include="KeyMonitor\KeyDirection.cs" />
<Compile Include="KeysController.cs" />
<Compile Include="MessageMerger.cs" />
<Compile Include="Models\Processinfo.cs" />
<Compile Include="ShortcutAccumulator.cs" />
<Compile Include="KeyPressDefinition.cs" />
Expand Down
10 changes: 0 additions & 10 deletions src/Carnac.Logic/IMessageMerger.cs

This file was deleted.

25 changes: 0 additions & 25 deletions src/Carnac.Logic/MessageMerger.cs

This file was deleted.

6 changes: 2 additions & 4 deletions src/Carnac.Logic/MessageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ public class MessageProvider : IMessageProvider
readonly IShortcutProvider shortcutProvider;
readonly IKeyProvider keyProvider;
readonly PopupSettings settings;
readonly IMessageMerger messageMerger;

public MessageProvider(IShortcutProvider shortcutProvider, IKeyProvider keyProvider, PopupSettings settings, IMessageMerger messageMerger)
public MessageProvider(IShortcutProvider shortcutProvider, IKeyProvider keyProvider, PopupSettings settings)
{
this.shortcutProvider = shortcutProvider;
this.keyProvider = keyProvider;
this.messageMerger = messageMerger;
this.settings = settings;
}

Expand All @@ -38,7 +36,7 @@ public IObservable<Message> GetMessageStream()
.Scan(new ShortcutAccumulator(), (acc, key) => acc.ProcessKey(shortcutProvider, key))
.Where(c => c.HasCompletedValue)
.SelectMany(c => c.GetMessages())
.Scan(new Message(), (acc, key) => messageMerger.MergeIfNeeded(acc, key))
.Scan(new Message(), (acc, key) => Message.MergeIfNeeded(acc, key))
.Where(m => !settings.DetectShortcutsOnly || m.IsShortcut);
}
}
Expand Down
20 changes: 18 additions & 2 deletions src/Carnac.Logic/Models/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,29 @@ private Message(Message initial, bool isDeleting)
public DateTime LastMessage { get { return lastMessage; } }

public bool IsDeleting { get { return isDeleting; } }



public Message Merge(Message other)
{
return new Message(this, other);
}

static readonly TimeSpan OneSecond = TimeSpan.FromSeconds(1);

public static Message MergeIfNeeded(Message previousMessage, Message newMessage)
{
return ShouldCreateNewMessage(previousMessage, newMessage)
? newMessage
: previousMessage.Merge(newMessage);
}

static bool ShouldCreateNewMessage(Message previous, Message current)
{
return previous.ProcessName != current.ProcessName ||
current.LastMessage.Subtract(previous.LastMessage) > OneSecond ||
!previous.CanBeMerged ||
!current.CanBeMerged;
}

public Message FadeOut()
{
return new Message(this, true);
Expand Down
2 changes: 1 addition & 1 deletion src/Carnac.Tests/MessageProviderFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ MessageProvider CreateMessageProvider(IObservable<InterceptKeyEventArgs> keysStr
var desktopLockEventService = Substitute.For<IDesktopLockEventService>();
desktopLockEventService.GetSessionSwitchStream().Returns(Observable.Never<SessionSwitchEventArgs>());
var keyProvider = new KeyProvider(source, new PasswordModeService(), desktopLockEventService);
return new MessageProvider(shortcutProvider, keyProvider, new PopupSettings(), new MessageMerger());
return new MessageProvider(shortcutProvider, keyProvider, new PopupSettings());
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion src/Carnac/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public App()
var keyProvider = new KeyProvider(InterceptKeys.Current, new PasswordModeService(), new DesktopLockEventService());
settingsProvider = new SettingsProvider(new RoamingAppDataStorage("Carnac"));
settings = settingsProvider.GetSettings<PopupSettings>();
messageProvider = new MessageProvider(new ShortcutProvider(), keyProvider, settings, new MessageMerger());
messageProvider = new MessageProvider(new ShortcutProvider(), keyProvider, settings);
}

protected override void OnStartup(StartupEventArgs e)
Expand Down

0 comments on commit b741586

Please sign in to comment.