<commit_before>// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

#nullable disable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using osu.Framework;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osuTK;
using osuTK.Graphics;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Allocation;
using osu.Framework.Layout;
using osu.Framework.Threading;

namespace osu.Game.Screens.Play
{
    public class SquareGraph : Container
    {
        private BufferedContainer<Column> columns;

        public SquareGraph()
        {
            AddLayout(layout);
        }

        public int ColumnCount => columns?.Children.Count ?? 0;

        private int progress;

        public int Progress
        {
            get => progress;
            set
            {
                if (value == progress) return;

                progress = value;
                redrawProgress();
            }
        }

        private float[] calculatedValues = Array.Empty<float>(); // values but adjusted to fit the amount of columns

        private int[] values;

        public int[] Values
        {
            get => values;
            set
            {
                if (value == values) return;

                values = value;
                layout.Invalidate();
            }
        }

        private Color4 fillColour;

        public Color4 FillColour
        {
            get => fillColour;
            set
            {
                if (value == fillColour) return;

                fillColour = value;
                redrawFilled();
            }
        }

        private readonly LayoutValue layout = new LayoutValue(Invalidation.DrawSize);
        private ScheduledDelegate scheduledCreate;

        private Vector2 previousDrawSize;

        protected override void Update()
        {
            base.Update();

            if (values != null && !layout.IsValid && DrawSize != previousDrawSize)
            {
                columns?.FadeOut(500, Easing.OutQuint).Expire();

                scheduledCreate?.Cancel();
                scheduledCreate = Scheduler.AddDelayed(RecreateGraph, 500);

                layout.Validate();
                previousDrawSize = DrawSize;
            }
        }

        private CancellationTokenSource cts;

        /// <summary>
        /// Recreates the entire graph.
        /// </summary>
        protected virtual void RecreateGraph()
        {
            var newColumns = new BufferedContainer<Column>(cachedFrameBuffer: true)
            {
                RedrawOnScale = false,
                RelativeSizeAxes = Axes.Both,
            };

            for (float x = 0; x < DrawWidth; x += Column.WIDTH)
            {
                newColumns.Add(new Column(DrawHeight)
                {
                    LitColour = fillColour,
                    Anchor = Anchor.BottomLeft,
                    Origin = Anchor.BottomLeft,
                    Position = new Vector2(x, 0),
                    State = ColumnState.Dimmed,
                });
            }

            cts?.Cancel();<commit_msg>Refactor update condition logic<commit_after>// Copyright (c) <NAME> <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

#nullable disable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using osu.Framework;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osuTK;
using osuTK.Graphics;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Allocation;
using osu.Framework.Threading;

namespace osu.Game.Screens.Play
{
    public class SquareGraph : Container
    {
        private BufferedContainer<Column> columns;

        public int ColumnCount => columns?.Children.Count ?? 0;

        private int progress;

        public int Progress
        {
            get => progress;
            set
            {
                if (value == progress) return;

                progress = value;
                redrawProgress();
            }
        }

        private float[] calculatedValues = Array.Empty<float>(); // values but adjusted to fit the amount of columns

        private int[] values;

        public int[] Values
        {
            get => values;
            set
            {
                if (value == values) return;

                values = value;
                graphNeedsUpdate = true;
            }
        }

        private Color4 fillColour;

        public Color4 FillColour
        {
            get => fillColour;
            set
            {
                if (value == fillColour) return;

                fillColour = value;
                redrawFilled();
            }
        }

        private ScheduledDelegate scheduledCreate;

        private bool graphNeedsUpdate = false;

        private Vector2 previousDrawSize;

        protected override void Update()
        {
            base.Update();

            if (graphNeedsUpdate || (values != null && DrawSize != previousDrawSize))
            {
                columns?.FadeOut(500, Easing.OutQuint).Expire();

                scheduledCreate?.Cancel();
                scheduledCreate = Scheduler.AddDelayed(RecreateGraph, 500);

                previousDrawSize = DrawSize;
                graphNeedsUpdate = false;
            }
        }

        private CancellationTokenSource cts;

        /// <summary>
        /// Recreates the entire graph.
        /// </summary>
        protected virtual void RecreateGraph()
        {
            var newColumns = new BufferedContainer<Column>(cachedFrameBuffer: true)
            {
                RedrawOnScale = false,
                RelativeSizeAxes = Axes.Both,
            };

            for (float x = 0; x < DrawWidth; x += Column.WIDTH)
            {
                newColumns.Add(new Column(DrawHeight)
                {
                    LitColour = fillColour,
                    Anchor = Anchor.BottomLeft,
                    Origin = Anchor.BottomLeft,
                    Position = new Vector2(x, 0),
                    State = ColumnState.Dimmed,
                });
            }

            cts?.Cancel();
{'fraction_non_alphanumeric': 0.08359181141439206, 'fraction_numerical': 0.004342431761786601, 'mean_word_length': 1.520125048847206, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6943091', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1547, 'n_words': 413}