Remove dependency on load state for DrawableComment.AddReplies

This commit is contained in:
Andrei Zavatski
2020-02-13 00:19:21 +03:00
parent ec9c01a75f
commit e2b3494352
2 changed files with 30 additions and 8 deletions

View File

@ -78,7 +78,7 @@ namespace osu.Game.Overlays.Comments
if (replies.Any()) if (replies.Any())
{ {
replies.ForEach(c => c.ParentComment = comment); replies.ForEach(c => c.ParentComment = comment);
drawableComment.OnLoadComplete += _ => drawableComment.AddReplies(replies.Select(reply => createCommentWithReplies(reply, commentBundle))); drawableComment.InitialReplies.AddRange(replies.Select(reply => createCommentWithReplies(reply, commentBundle)));
} }
return drawableComment; return drawableComment;

View File

@ -36,6 +36,11 @@ namespace osu.Game.Overlays.Comments
public readonly Bindable<CommentsSortCriteria> Sort = new Bindable<CommentsSortCriteria>(); public readonly Bindable<CommentsSortCriteria> Sort = new Bindable<CommentsSortCriteria>();
public readonly List<Comment> LoadedReplies = new List<Comment>(); public readonly List<Comment> LoadedReplies = new List<Comment>();
/// <summary>
/// <see cref="DrawableComment"/>s which will be added to this <see cref="DrawableComment"/> as replies when it will be loaded.
/// </summary>
public readonly List<DrawableComment> InitialReplies = new List<DrawableComment>();
private readonly BindableBool childrenExpanded = new BindableBool(true); private readonly BindableBool childrenExpanded = new BindableBool(true);
private int currentPage; private int currentPage;
@ -265,6 +270,9 @@ namespace osu.Game.Overlays.Comments
Colour = OsuColour.Gray(0.1f) Colour = OsuColour.Gray(0.1f)
}); });
} }
if (InitialReplies.Any())
AddReplies(InitialReplies);
} }
protected override void LoadComplete() protected override void LoadComplete()
@ -283,14 +291,28 @@ namespace osu.Game.Overlays.Comments
public void AddReplies(IEnumerable<DrawableComment> replies) public void AddReplies(IEnumerable<DrawableComment> replies)
{ {
LoadComponentAsync(createRepliesPage(replies), page => if (LoadState == LoadState.NotLoaded)
throw new NotSupportedException($@"Can't use {nameof(AddReplies)} when not loaded.");
var page = createRepliesPage(replies);
if (LoadState == LoadState.Loading)
{ {
addRepliesPage(page, replies);
return;
}
LoadComponentAsync(page, loaded => addRepliesPage(loaded, replies));
}
private void addRepliesPage(FillFlowContainer<DrawableComment> page, IEnumerable<DrawableComment> replies)
{
childCommentsContainer.Add(page);
var newReplies = replies.Select(reply => reply.Comment); var newReplies = replies.Select(reply => reply.Comment);
LoadedReplies.AddRange(newReplies); LoadedReplies.AddRange(newReplies);
deletedCommentsCounter.Count.Value += newReplies.Count(reply => reply.IsDeleted); deletedCommentsCounter.Count.Value += newReplies.Count(reply => reply.IsDeleted);
childCommentsContainer.Add(page);
updateButtonsState(); updateButtonsState();
});
} }
private FillFlowContainer<DrawableComment> createRepliesPage(IEnumerable<DrawableComment> replies) => new FillFlowContainer<DrawableComment> private FillFlowContainer<DrawableComment> createRepliesPage(IEnumerable<DrawableComment> replies) => new FillFlowContainer<DrawableComment>