Skip to content

INPC003 warns incorrectly about calculated property when ++ #224

Open
@JohanLarsson

Description

@JohanLarsson
    public sealed class SymbolLoadStatus : INotifyPropertyChanged
    {
        private static readonly int TotalCount = Database.GetAllSymbolKeys().Count();

        private string? displayText;
        private int processed;
        private DateTimeOffset? startTime;
        private DateTimeOffset? endTime;

        public event PropertyChangedEventHandler? PropertyChanged;

        public int Total => TotalCount;

        public TimeSpan? Elapsed => (this.EndTime ?? DateTimeOffset.Now) - this.startTime;

        public TimeSpan? Average => this.Elapsed / this.Processed;

        public string? DisplayText
        {
            get => this.displayText;
            private set
            {
                if (value == this.displayText)
                {
                    return;
                }

                this.displayText = value;
                this.OnPropertyChanged();
            }
        }

        public int Processed
        {
            get => this.processed;
            private set
            {
                if (value == this.processed)
                {
                    return;
                }

                this.processed = value;
                this.OnPropertyChanged();
                this.OnPropertyChanged(nameof(this.Average));
            }
        }

        public DateTimeOffset? StartTime
        {
            get => this.startTime;
            private set
            {
                if (value == this.startTime)
                {
                    return;
                }

                this.startTime = value;
                this.OnPropertyChanged();
                this.OnPropertyChanged(nameof(this.Elapsed));
                this.OnPropertyChanged(nameof(this.Average));
            }
        }

        public DateTimeOffset? EndTime
        {
            get => this.endTime;
            private set
            {
                if (value == this.endTime)
                {
                    return;
                }

                this.endTime = value;
                this.OnPropertyChanged();
                this.OnPropertyChanged(nameof(this.Elapsed));
                this.OnPropertyChanged(nameof(this.Average));
            }
        }

        public void Load(SymbolKey key)
        {
            if (this.startTime is null)
            {
                this.StartTime = DateTimeOffset.Now;
            }

            this.Processed++;
            this.DisplayText = $"Loading {this.processed} of {this.Total} in {this.Elapsed:mm\\:ss} average {this.Average?.TotalMilliseconds:F0} ms {key}";
        }

        private void OnPropertyChanged([CallerMemberName] string? propertyName = null)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions