Skip to content

Commit

Permalink
Make Desktop Less Jank
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Dec 17, 2023
1 parent 54c546b commit b3fb50a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ public CL_MainChanges GetAllChanges(DateTime date, int userID)
var changes = ChangeTracker<int>.GetChainedChanges(
new List<ChangeTracker<int>>
{
RepoFactory.FilterPreset.GetChangeTracker(),
RepoFactory.AnimeGroup.GetChangeTracker(),
RepoFactory.AnimeGroup_User.GetChangeTracker(userID),
RepoFactory.AnimeSeries.GetChangeTracker(),
Expand All @@ -186,46 +185,45 @@ public CL_MainChanges GetAllChanges(DateTime date, int userID)
var legacyConverter = HttpContext.RequestServices.GetRequiredService<LegacyFilterConverter>();
c.Filters = new CL_Changes<CL_GroupFilter>
{
ChangedItems = legacyConverter.ToClient(changes[0].ChangedItems.Select(a => RepoFactory.FilterPreset.GetByID(a)).ToList(), userID)
.Select(a => a.Value)
ChangedItems = legacyConverter.ToClient(RepoFactory.FilterPreset.GetAll(), userID)
.Where(a => a != null)
.ToList(),
RemovedItems = changes[0].RemovedItems.ToList(),
LastChange = changes[0].LastChange
RemovedItems = new List<int>(),
LastChange = DateTime.Now
};

c.Groups = new CL_Changes<CL_AnimeGroup_User>();
changes[1].ChangedItems.UnionWith(changes[2].ChangedItems);
changes[1].ChangedItems.UnionWith(changes[2].RemovedItems);
if (changes[2].LastChange > changes[1].LastChange)
changes[0].ChangedItems.UnionWith(changes[1].ChangedItems);
changes[0].ChangedItems.UnionWith(changes[1].RemovedItems);
if (changes[1].LastChange > changes[0].LastChange)
{
changes[1].LastChange = changes[2].LastChange;
changes[0].LastChange = changes[1].LastChange;
}

c.Groups.ChangedItems = changes[1]
c.Groups.ChangedItems = changes[0]
.ChangedItems.Select(a => RepoFactory.AnimeGroup.GetByID(a))
.Where(a => a != null)
.Select(a => a.GetUserContract(userID))
.ToList();


c.Groups.RemovedItems = changes[1].RemovedItems.ToList();
c.Groups.LastChange = changes[1].LastChange;
c.Groups.RemovedItems = changes[0].RemovedItems.ToList();
c.Groups.LastChange = changes[0].LastChange;
c.Series = new CL_Changes<CL_AnimeSeries_User>();
changes[3].ChangedItems.UnionWith(changes[4].ChangedItems);
changes[3].ChangedItems.UnionWith(changes[4].RemovedItems);
if (changes[4].LastChange > changes[3].LastChange)
changes[2].ChangedItems.UnionWith(changes[3].ChangedItems);
changes[2].ChangedItems.UnionWith(changes[3].RemovedItems);
if (changes[3].LastChange > changes[2].LastChange)
{
changes[3].LastChange = changes[4].LastChange;
changes[2].LastChange = changes[3].LastChange;
}

c.Series.ChangedItems = changes[3]
c.Series.ChangedItems = changes[2]
.ChangedItems.Select(a => RepoFactory.AnimeSeries.GetByID(a))
.Where(a => a != null)
.Select(a => a.GetUserContract(userID))
.ToList();
c.Series.RemovedItems = changes[3].RemovedItems.ToList();
c.Series.LastChange = changes[3].LastChange;
c.Series.RemovedItems = changes[2].RemovedItems.ToList();
c.Series.LastChange = changes[2].LastChange;
c.LastChange = c.Filters.LastChange;
if (c.Groups.LastChange > c.LastChange)
{
Expand All @@ -252,13 +250,11 @@ public CL_Changes<CL_GroupFilter> GetGroupFilterChanges(DateTime date)
try
{
var legacyConverter = HttpContext.RequestServices.GetRequiredService<LegacyFilterConverter>();
var changes = RepoFactory.FilterPreset.GetChangeTracker().GetChanges(date);
c.ChangedItems = legacyConverter.ToClient(changes.ChangedItems.Select(a => RepoFactory.FilterPreset.GetByID(a)).ToList())
.Select(a => a.Value)
c.ChangedItems = legacyConverter.ToClient(RepoFactory.FilterPreset.GetAll())
.Where(a => a != null)
.ToList();
c.RemovedItems = changes.RemovedItems.ToList();
c.LastChange = changes.LastChange;
c.RemovedItems = new List<int>();
c.LastChange = DateTime.Now;
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3118,7 +3118,7 @@ public List<CL_GroupFilterExtended> GetAllGroupFiltersExtended(int userID)
var legacyConverter = HttpContext.RequestServices.GetRequiredService<LegacyFilterConverter>();
gfs = legacyConverter.ToClient(allGfs).Select(a => new CL_GroupFilterExtended
{
GroupFilter = a.Value, GroupCount = a.Value.Groups[userID].Count
GroupFilter = a, GroupCount = a.Groups[userID].Count
}).ToList();
}
catch (Exception ex)
Expand Down Expand Up @@ -3147,7 +3147,7 @@ public List<CL_GroupFilterExtended> GetGroupFiltersExtended(int userID, int gfpa
var legacyConverter = HttpContext.RequestServices.GetRequiredService<LegacyFilterConverter>();
gfs = legacyConverter.ToClient(allGfs).Select(a => new CL_GroupFilterExtended
{
GroupFilter = a.Value, GroupCount = a.Value.Groups.FirstOrDefault().Value.Count
GroupFilter = a, GroupCount = a.Groups.FirstOrDefault().Value.Count
}).ToList();
}
catch (Exception ex)
Expand All @@ -3172,7 +3172,6 @@ public List<CL_GroupFilter> GetAllGroupFilters()

var legacyConverter = HttpContext.RequestServices.GetRequiredService<LegacyFilterConverter>();
gfs = legacyConverter.ToClient(allGfs)
.Select(a => a.Value)
.Where(a => a != null)
.ToList();
}
Expand All @@ -3197,7 +3196,6 @@ public List<CL_GroupFilter> GetGroupFilters(int gfparentid = 0)
logger.Info("GetAllGroupFilters (Database) in {0} ms", ts.TotalMilliseconds);
var legacyConverter = HttpContext.RequestServices.GetRequiredService<LegacyFilterConverter>();
gfs = legacyConverter.ToClient(allGfs)
.Select(a => a.Value)
.Where(a => a != null)
.ToList();
}
Expand Down
34 changes: 14 additions & 20 deletions Shoko.Server/Filters/Legacy/LegacyFilterConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,20 @@ public CL_GroupFilter ToClient(FilterPreset filter)
return contract;
}

public Dictionary<FilterPreset, CL_GroupFilter> ToClient(IReadOnlyList<FilterPreset> filters, int? userID = null)
public List<CL_GroupFilter> ToClient(IReadOnlyList<FilterPreset> filters, int? userID = null)
{
var result = new Dictionary<FilterPreset, CL_GroupFilter>();
var result = new List<CL_GroupFilter>();
var userFilters = filters.Where(a => a?.Expression?.UserDependent ?? false).ToList();
var otherFilters = filters.Except(userFilters).ToList();

// batch evaluate each list, then build the mappings
if (userFilters.Count > 0) SetUserFilters(userID, userFilters, result);
if (otherFilters.Count > 0) SetOtherFilters(otherFilters, result);
if (userFilters.Count > 0) result.AddRange(SetUserFilters(userID, userFilters));
if (otherFilters.Count > 0) result.AddRange(SetOtherFilters(otherFilters));

return result;
}

private void SetOtherFilters(List<FilterPreset> otherFilters, Dictionary<FilterPreset, CL_GroupFilter> result)
private List<CL_GroupFilter> SetOtherFilters(List<FilterPreset> otherFilters)
{

var results = _evaluator.BatchEvaluateFilters(otherFilters, null, true);
Expand All @@ -132,7 +132,7 @@ private void SetOtherFilters(List<FilterPreset> otherFilters, Dictionary<FilterP

LegacyConditionConverter.TryConvertToConditions(filter, out var conditions, out var baseCondition);
conditions?.ForEach(condition => condition.GroupFilterID = filter.FilterPresetID);
return (Filter: filter, new CL_GroupFilter
return new CL_GroupFilter
{
GroupFilterID = filter.FilterPresetID,
GroupFilterName = filter.Name,
Expand All @@ -149,13 +149,10 @@ private void SetOtherFilters(List<FilterPreset> otherFilters, Dictionary<FilterP
Childs = filter.FilterPresetID == 0
? new HashSet<int>()
: RepoFactory.FilterPreset.GetByParentID(filter.FilterPresetID).Select(a => a.FilterPresetID).ToHashSet()
});
});
};
}).ToList();

foreach (var (filter, model) in models)
{
result[filter] = model;
}
return models;
}

/// <summary>
Expand All @@ -164,7 +161,7 @@ private void SetOtherFilters(List<FilterPreset> otherFilters, Dictionary<FilterP
/// <param name="userID">if this is specified, it only calculates one user</param>
/// <param name="userFilters"></param>
/// <param name="result"></param>
private void SetUserFilters(int? userID, List<FilterPreset> userFilters, Dictionary<FilterPreset, CL_GroupFilter> result)
private List<CL_GroupFilter> SetUserFilters(int? userID, List<FilterPreset> userFilters)
{

var userResults = userID.HasValue
Expand All @@ -185,7 +182,7 @@ private void SetUserFilters(int? userID, List<FilterPreset> userFilters, Diction
}
LegacyConditionConverter.TryConvertToConditions(filter, out var conditions, out var baseCondition);
conditions?.ForEach(condition => condition.GroupFilterID = filter.FilterPresetID);
return (Filter: filter, new CL_GroupFilter
return new CL_GroupFilter
{
GroupFilterID = filter.FilterPresetID,
GroupFilterName = filter.Name,
Expand All @@ -202,12 +199,9 @@ private void SetUserFilters(int? userID, List<FilterPreset> userFilters, Diction
Childs = filter.FilterPresetID == 0
? new HashSet<int>()
: RepoFactory.FilterPreset.GetByParentID(filter.FilterPresetID).Select(a => a.FilterPresetID).ToHashSet()
});
});
};
}).ToList();

foreach (var (filter, model) in userModels)
{
result[filter] = model;
}
return userModels;
}
}
19 changes: 0 additions & 19 deletions Shoko.Server/Repositories/Cached/FilterPresetRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,6 @@ namespace Shoko.Server.Repositories.Cached;
public class FilterPresetRepository : BaseCachedRepository<FilterPreset, int>
{
private PocoIndex<int, FilterPreset, int> Parents;
private readonly ChangeTracker<int> Changes = new();

public FilterPresetRepository()
{
EndSaveCallback = obj =>
{
Changes.AddOrUpdate(obj.FilterPresetID);
};
EndDeleteCallback = obj =>
{
Changes.Remove(obj.FilterPresetID);
};
}

protected override int SelectKey(FilterPreset entity)
{
Expand All @@ -48,7 +35,6 @@ protected override int SelectKey(FilterPreset entity)

public override void PopulateIndexes()
{
Changes.AddOrUpdateRange(Cache.Keys);
Parents = Cache.CreateIndex(a => a.ParentFilterPresetID ?? 0);
}

Expand Down Expand Up @@ -485,9 +471,4 @@ public List<FilterPreset> GetTimeDependentFilters()
{
return ReadLock(() => GetAll().Where(a => a.Expression.TimeDependent).ToList());
}

public ChangeTracker<int> GetChangeTracker()
{
return Changes;
}
}

0 comments on commit b3fb50a

Please sign in to comment.