Start introducing `ILive`

This commit is contained in:
Dean Herbert 2021-12-15 15:03:06 +09:00
parent 3152d2d8a0
commit 1d536fd0bc
6 changed files with 34 additions and 30 deletions

View File

@ -173,14 +173,14 @@ public virtual void Save(BeatmapInfo info, IBeatmap beatmapContent, ISkin? beatm
/// </summary>
/// <param name="query">The query.</param>
/// <returns>Results from the provided query.</returns>
public ILive<IEnumerable<BeatmapSetInfo>> QueryBeatmapSets(Expression<Func<BeatmapSetInfo, bool>> query) => beatmapModelManager.QueryBeatmapSets(query).ToLive(contextFactory);
public List<ILive<BeatmapSetInfo>> QueryBeatmapSets(Expression<Func<BeatmapSetInfo, bool>> query) => beatmapModelManager.QueryBeatmapSets(query).ToLive();
/// <summary>
/// Perform a lookup query on available <see cref="BeatmapSetInfo"/>s.
/// </summary>
/// <param name="query">The query.</param>
/// <returns>The first result for the provided query, or null if no results were found.</returns>
public BeatmapSetInfo? QueryBeatmapSet(Expression<Func<BeatmapSetInfo, bool>> query) => beatmapModelManager.QueryBeatmapSet(query);
public ILive<BeatmapSetInfo>? QueryBeatmapSet(Expression<Func<BeatmapSetInfo, bool>> query) => beatmapModelManager.QueryBeatmapSet(query);
/// <summary>
/// Perform a lookup query on available <see cref="BeatmapInfo"/>s.

View File

@ -104,10 +104,10 @@ public virtual void Save(BeatmapInfo beatmapInfo, IBeatmap beatmapContent, ISkin
/// </summary>
/// <param name="query">The query.</param>
/// <returns>The first result for the provided query, or null if no results were found.</returns>
public BeatmapSetInfo? QueryBeatmapSet(Expression<Func<BeatmapSetInfo, bool>> query)
public ILive<BeatmapSetInfo>? QueryBeatmapSet(Expression<Func<BeatmapSetInfo, bool>> query)
{
using (var context = ContextFactory.CreateContext())
return context.All<BeatmapSetInfo>().FirstOrDefault(query); // TODO: ?.ToLive();
return context.All<BeatmapSetInfo>().FirstOrDefault(query)?.ToLive();
}
/// <summary>

View File

@ -437,7 +437,7 @@ public void ShowChannel(string channel) => waitForReady(() => channelManager, _
/// </remarks>
public void PresentBeatmap(IBeatmapSetInfo beatmap, Predicate<BeatmapInfo> difficultyCriteria = null)
{
BeatmapSetInfo databasedSet = null;
ILive<BeatmapSetInfo> databasedSet = null;
if (beatmap.OnlineID > 0)
databasedSet = BeatmapManager.QueryBeatmapSet(s => s.OnlineID == beatmap.OnlineID);
@ -453,27 +453,30 @@ public void PresentBeatmap(IBeatmapSetInfo beatmap, Predicate<BeatmapInfo> diffi
PerformFromScreen(screen =>
{
// Find beatmaps that match our predicate.
var beatmaps = databasedSet.Beatmaps.Where(b => difficultyCriteria?.Invoke(b) ?? true).ToList();
// Use all beatmaps if predicate matched nothing
if (beatmaps.Count == 0)
beatmaps = databasedSet.Beatmaps.ToList();
// Prefer recommended beatmap if recommendations are available, else fallback to a sane selection.
var selection = difficultyRecommender.GetRecommendedBeatmap(beatmaps)
?? beatmaps.FirstOrDefault(b => b.Ruleset.Equals(Ruleset.Value))
?? beatmaps.First();
if (screen is IHandlePresentBeatmap presentableScreen)
databasedSet.PerformRead(set =>
{
presentableScreen.PresentBeatmap(BeatmapManager.GetWorkingBeatmap(selection), selection.Ruleset);
}
else
{
Ruleset.Value = selection.Ruleset;
Beatmap.Value = BeatmapManager.GetWorkingBeatmap(selection);
}
// Find beatmaps that match our predicate.
var beatmaps = set.Beatmaps.Where(b => difficultyCriteria?.Invoke(b) ?? true).ToList();
// Use all beatmaps if predicate matched nothing
if (beatmaps.Count == 0)
beatmaps = set.Beatmaps.ToList();
// Prefer recommended beatmap if recommendations are available, else fallback to a sane selection.
var selection = difficultyRecommender.GetRecommendedBeatmap(beatmaps)
?? beatmaps.FirstOrDefault(b => b.Ruleset.Equals(Ruleset.Value))
?? beatmaps.First();
if (screen is IHandlePresentBeatmap presentableScreen)
{
presentableScreen.PresentBeatmap(BeatmapManager.GetWorkingBeatmap(selection), selection.Ruleset);
}
else
{
Ruleset.Value = selection.Ruleset;
Beatmap.Value = BeatmapManager.GetWorkingBeatmap(selection);
}
});
}, validScreens: new[] { typeof(SongSelect), typeof(IHandlePresentBeatmap) });
}

View File

@ -88,7 +88,6 @@ private void beatmapsChanged(IRealmCollection<BeatmapSetInfo> sender, ChangeSet
if (changes == null)
{
beatmapSets.AddRange(sender);
return;
}
// beatmaps.ItemUpdated += set => Schedule(() =>

View File

@ -160,7 +160,8 @@ private void load(BeatmapManager beatmaps, ScoreManager scores, SkinManager skin
Action = () =>
{
undeleteButton.Enabled.Value = false;
Task.Run(() => beatmaps.Undelete(beatmaps.QueryBeatmapSets(b => b.DeletePending).ToList())).ContinueWith(t => Schedule(() => undeleteButton.Enabled.Value = true));
// TODO: reimplement similar to SkinManager?
// Task.Run(() => beatmaps.Undelete(beatmaps.QueryBeatmapSets(b => b.DeletePending).ToList())).ContinueWith(t => Schedule(() => undeleteButton.Enabled.Value = true));
}
},
});

View File

@ -15,6 +15,7 @@
using osu.Framework.Utils;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
using osu.Game.Database;
using osu.Game.IO.Archives;
using osu.Game.Overlays;
using osu.Game.Screens.Backgrounds;
@ -90,7 +91,7 @@ private void load(OsuConfigManager config, SkinManager skinManager, BeatmapManag
MenuMusic = config.GetBindable<bool>(OsuSetting.MenuMusic);
seeya = audio.Samples.Get(SeeyaSampleName);
BeatmapSetInfo setInfo = null;
ILive<BeatmapSetInfo> setInfo = null;
// if the user has requested not to play theme music, we should attempt to find a random beatmap from their collection.
if (!MenuMusic.Value)
@ -100,7 +101,7 @@ private void load(OsuConfigManager config, SkinManager skinManager, BeatmapManag
if (sets.Count > 0)
{
setInfo = beatmaps.QueryBeatmapSet(s => s.ID == sets[RNG.Next(0, sets.Count - 1)].ID);
initialBeatmap = beatmaps.GetWorkingBeatmap(setInfo?.Beatmaps[0]);
initialBeatmap = beatmaps.GetWorkingBeatmap(setInfo?.PerformRead(s => s.Beatmaps[0].ToLive()));
}
}
@ -130,7 +131,7 @@ bool loadThemedIntro()
if (setInfo == null)
return false;
initialBeatmap = beatmaps.GetWorkingBeatmap(setInfo.Beatmaps[0]);
initialBeatmap = beatmaps.GetWorkingBeatmap(setInfo.PerformRead(s => s.Beatmaps[0].ToLive()));
return UsingThemedIntro = initialBeatmap != null;
}