~ruther/NosTale-PacketLogger

ref: c61f45970d133d6973bfa51d03c9ff4ff5b4e19b NosTale-PacketLogger/src/PacketLogger/ViewModels/Log/PacketLogViewModel.cs -rw-r--r-- 5.9 KiB
c61f4597 — Rutherther fix: make sender title change correctly 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
//  PacketLogViewModel.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using Avalonia;
using DynamicData;
using DynamicData.Binding;
using PacketLogger.Models;
using PacketLogger.Models.Filters;
using PacketLogger.Models.Packets;
using PacketLogger.ViewModels.Filters;
using ReactiveUI;

namespace PacketLogger.ViewModels.Log;

/// <inheritdoc />
public class PacketLogViewModel : ViewModelBase, IDisposable
{
    private readonly FilterProfiles _filterProfiles;
    private readonly ReadOnlyObservableCollection<PacketInfo> _packets;
    private readonly IDisposable _cleanUp;
    private bool _logReceived = true;
    private bool _logSent = true;

    /// <summary>
    /// Initializes a new instance of the <see cref="PacketLogViewModel"/> class.
    /// </summary>
    /// <param name="packetProvider">The packet provider.</param>
    /// <param name="filterProfiles">The filter profiles.</param>
    public PacketLogViewModel(IPacketProvider packetProvider, FilterProfiles filterProfiles)
    {
        _filterProfiles = filterProfiles;
        FilterChoose = new FilterChooseViewModel
        (
            filterProfiles.DefaultFilterEnabled
                ? filterProfiles.DefaultProfile
                : filterProfiles.NoProfile,
            filterProfiles.SelectableProfiles.Last()
        );
        Provider = packetProvider;

        var dynamicFilter = FilterChoose.WhenValueChanged(x => x.CurrentFilter)
            .Select
            (
                filter =>
                {
                    return (Func<PacketInfo, bool>)((pi) =>
                    {
                        if (filter is null)
                        {
                            return true;
                        }

                        return filter.Match(pi);
                    });
                }
            );

        var packetsSubscription = Provider.Packets.Connect()
            .Filter(dynamicFilter)
            .Sort(new PacketComparer())
            .Bind(out _packets)
            .ObserveOn(RxApp.MainThreadScheduler)
            .DisposeMany()
            .Subscribe
            (
                _ =>
                {
                    if (Scroll)
                    {
                        RxApp.MainThreadScheduler.Schedule
                        (
                            DateTimeOffset.Now.AddMilliseconds(100),
                            () =>
                            {
                                if (FilteredPackets.Count > 0)
                                {
                                    SelectedPacket = FilteredPackets[^1];
                                }
                            }
                        );
                    }
                }
            );

        _cleanUp = packetsSubscription;
        CopyPackets = ReactiveCommand.CreateFromObservable<IList, Unit>
        (
            list => Observable.StartAsync
            (
                async () =>
                {
                    var clipboardString = string.Join
                        ('\n', list.OfType<PacketInfo>().Select(x => x.PacketString));
                    await Application.Current!.Clipboard!.SetTextAsync(clipboardString);
                }
            )
        );

        TogglePane = ReactiveCommand.Create<Unit, bool>
        (
            _ => PaneOpen = !PaneOpen
        );

        Clear = ReactiveCommand.Create
        (
            () => Provider.Clear()
        );
    }

    /// <summary>
    /// Gets filter profiles.
    /// </summary>
    public FilterProfiles Profiles => _filterProfiles;

    /// <summary>
    /// Gets the filtered packets.
    /// </summary>
    public ReadOnlyObservableCollection<PacketInfo> FilteredPackets => _packets;

    /// <summary>
    /// Gets packet provider.
    /// </summary>
    public IPacketProvider Provider { get; }

    /// <summary>
    /// Gets whether the pane is open.
    /// </summary>
    public bool PaneOpen { get; private set; } = true;

    /// <summary>
    /// Gets the toggle pane command.
    /// </summary>
    public ReactiveCommand<Unit, bool> TogglePane { get; }

    /// <summary>
    /// Gets command to copy packets.
    /// </summary>
    public ReactiveCommand<IList, Unit> CopyPackets { get; }

    /// <summary>
    /// Gets the command for clearing.
    /// </summary>
    public ReactiveCommand<Unit, Unit> Clear { get; }

    /// <summary>
    /// Gets or sets whether to log received packets.
    /// </summary>
    public bool LogReceived
    {
        get => _logReceived;
        set
        {
            Provider.LogReceived = value;
            _logReceived = value;
        }
    }

    /// <summary>
    /// Gets or sets whether to log sent packets.
    /// </summary>
    public bool LogSent
    {
        get => _logSent;
        set
        {
            Provider.LogSent = value;
            _logSent = value;
        }
    }

    /// <summary>
    /// Gets or sets whether to scroll to teh bottom of the grid.
    /// </summary>
    public bool Scroll { get; set; } = true;

    /// <summary>
    /// Gets or sets the currently selected packet.
    /// </summary>
    public object? SelectedPacket { get; set; }

    /// <summary>
    /// Gets empty string.
    /// </summary>
    public string Empty { get; } = string.Empty;

    /// <summary>
    /// Gets the filter choose view model.
    /// </summary>
    public FilterChooseViewModel FilterChoose { get; }

    /// <inheritdoc />
    public void Dispose()
    {
        TogglePane.Dispose();
        CopyPackets.Dispose();
        Clear.Dispose();
        Provider.Dispose();
        (Provider as CommsPacketProvider)?.CustomDispose();
        _cleanUp.Dispose();
    }
}
Do not follow this link