-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e490dd
commit d4204bb
Showing
75 changed files
with
6,128 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
Valour/Client/Components/PlanetsList/PlanetListItemComponents.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
@code { | ||
|
||
public static RenderFragment PlanetContent => | ||
@<div style="border: 1px solid black"> | ||
Planet | ||
</div>; | ||
|
||
public static RenderFragment CategoryContent => | ||
@<div style="border: 1px solid black"> | ||
Category | ||
</div>; | ||
|
||
public static RenderFragment ChatChannelContent => | ||
@<div style="border: 1px solid black"> | ||
Chat Channel | ||
</div>; | ||
|
||
public static RenderFragment VoiceChannelContent => | ||
@<div style="border: 1px solid black"> | ||
Voice Channel | ||
</div>; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Valour.Client.Components.Utility.DragList; | ||
using Valour.Sdk.Models; | ||
using Valour.Shared.Models; | ||
|
||
namespace Valour.Client.Components.PlanetsList; | ||
|
||
public class PlanetListItem : DragListItem | ||
{ | ||
public Planet Planet { get; set; } | ||
|
||
public override uint Depth => 0u; | ||
public override uint Position => Planet.Name.FirstOrDefault(); | ||
|
||
public PlanetListItem(Planet planet) | ||
{ | ||
Planet = planet; | ||
} | ||
} | ||
|
||
public class ChannelListItem : DragListItem | ||
{ | ||
public Channel Channel { get; set; } | ||
|
||
public override uint Depth => Channel.Position.Depth; | ||
public override uint Position => Channel.RawPosition; | ||
|
||
public ChannelListItem(Channel channel) | ||
{ | ||
Channel = channel; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
Valour/Client/Components/Utility/DragList/DragListComponent.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
@inherits ControlledRenderComponentBase | ||
|
||
@{ | ||
if (_items is null) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (var item in _items) | ||
{ | ||
<Virtualize Items="@_items" ItemSize="@ItemHeight"> | ||
<div style="height: @ItemHeight; max-height: @ItemHeight; margin-left: @(NestingMargin * @item.Depth)px"> | ||
@item.Content | ||
</div> | ||
</Virtualize> | ||
|
||
} | ||
} | ||
|
||
@code { | ||
|
||
[Parameter] | ||
public float NestingMargin { get; set; } = 8f; | ||
|
||
[Parameter] | ||
public float ItemHeight { get; set; } = 24f; | ||
|
||
// all items, sorted for render | ||
private List<DragListItem> _items = new(); | ||
|
||
/// <summary> | ||
/// Sets the items of this drag list. Note that changes to the list will | ||
/// pass down (stored by reference) but you must manually trigger rebuilds. | ||
/// </summary> | ||
public void SetTopLevelItems(List<DragListItem> items) | ||
{ | ||
_items = items; | ||
} | ||
|
||
/// <summary> | ||
/// Orders, | ||
/// </summary> | ||
public void Rebuild(bool render = true) | ||
{ | ||
OrderItems(); | ||
|
||
if (render) | ||
{ | ||
ReRender(); | ||
} | ||
} | ||
|
||
// Orders the items for display | ||
private void OrderItems() | ||
{ | ||
if (_items is null || _items.Count == 0) | ||
{ | ||
return; | ||
} | ||
|
||
_items.Sort(DragListItem.Compare); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Microsoft.AspNetCore.Components; | ||
|
||
namespace Valour.Client.Components.Utility.DragList; | ||
|
||
public abstract class DragListItem | ||
{ | ||
/// <summary> | ||
/// The drag list this item belongs to | ||
/// </summary> | ||
public DragListComponent DragList { get; set; } | ||
|
||
/// <summary> | ||
/// True if this item can contain other items | ||
/// </summary> | ||
public bool Container { get; set; } | ||
|
||
/// <summary> | ||
/// True if this item is opened. Only applies to containers. | ||
/// </summary> | ||
public bool Open { get; set; } | ||
|
||
/// <summary> | ||
/// The amount of margin to apply to children. Only applies to containers. | ||
/// </summary> | ||
public int ChildMargin { get; set; } | ||
|
||
public abstract uint Depth { get; } | ||
|
||
public abstract uint Position { get; } | ||
|
||
public virtual Task OnClick() | ||
{ | ||
if (Container) | ||
{ | ||
// Switch the open state | ||
Open = !Open; | ||
|
||
// Re-render drag list | ||
DragList.ReRender(); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
/// <summary> | ||
/// The content to render for this item | ||
/// </summary> | ||
public RenderFragment Content { get; set; } | ||
|
||
public static int Compare(DragListItem a, DragListItem b) | ||
{ | ||
// Compare position | ||
return a.Position.CompareTo(b.Position); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Microsoft.AspNetCore.Components.Web.Virtualization; | ||
using Valour.Sdk.ModelLogic; | ||
|
||
namespace Valour.Client.Utility; | ||
|
||
public static class ModelQueryEngineExtensions | ||
{ | ||
public static async ValueTask<ItemsProviderResult<TModel>> GetVirtualizedItemsAsync<TModel>(this ModelQueryEngine<TModel> engine, ItemsProviderRequest request) | ||
where TModel : ClientModel<TModel> | ||
{ | ||
var queryData = await engine.GetItemsAsync(request.StartIndex, request.Count); | ||
return new ItemsProviderResult<TModel>(queryData.Items, queryData.TotalCount); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System.Runtime.CompilerServices; | ||
using Valour.Shared.Models; | ||
|
||
namespace Valour.Database.Extensions; | ||
|
||
public static class ChannelExtensions | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static IQueryable<Channel> InPlanet(this IQueryable<Channel> channels, long? planetId) | ||
{ | ||
return channels.Where(x => x.PlanetId == planetId); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static IQueryable<Channel> InParent(this IQueryable<Channel> channels, long? parentId) | ||
{ | ||
return channels.Where(x => x.ParentId == parentId); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static IQueryable<Channel> DirectChildrenOf(this IQueryable<Channel> channels, ISharedChannel channel) | ||
{ | ||
return DirectChildrenOf(channels, channel.PlanetId, channel.RawPosition); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static IQueryable<Channel> DirectChildrenOf(this IQueryable<Channel> channels, long? planetId, uint position) | ||
{ | ||
// In this case we return nothing | ||
if (planetId is null) | ||
return channels.Where(x => false); | ||
|
||
var depth = ChannelPosition.GetDepth(position); | ||
var bounds = ChannelPosition.GetDescendentBounds(position, depth); | ||
var directChildMask = ChannelPosition.GetDirectChildMaskByDepth(depth); | ||
|
||
return channels.Where(x => | ||
x.PlanetId == planetId && | ||
x.RawPosition >= bounds.lower && | ||
x.RawPosition < bounds.upper && | ||
(x.RawPosition & directChildMask) == position); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static IQueryable<Channel> DescendantsOf(this IQueryable<Channel> channels, ISharedChannel channel) | ||
{ | ||
return DescendantsOf(channels, channel.PlanetId, channel.RawPosition); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static IQueryable<Channel> DescendantsOf(this IQueryable<Channel> channels, long? planetId, uint position) | ||
{ | ||
// In this case we return nothing | ||
if (planetId is null) | ||
return channels.Where(x => false); | ||
|
||
var bounds = ChannelPosition.GetDescendentBounds(position); | ||
return channels.Where(x => | ||
x.PlanetId == planetId && | ||
x.RawPosition >= bounds.lower && | ||
x.RawPosition < bounds.upper); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System.Collections.Concurrent; | ||
using Valour.Sdk.ModelLogic; | ||
using Valour.Sdk.Services; | ||
|
||
namespace Valour.Sdk.Client; | ||
|
||
/* Valour (TM) - A free and secure chat client | ||
* Copyright (C) 2024 Valour Software LLC | ||
* This program is subject to the GNU Affero General Public license | ||
* A copy of the license should be included - if not, see <http://www.gnu.org/licenses/> | ||
*/ | ||
|
||
public class ModelCache<TModel, TId> | ||
where TModel : ClientModel<TModel, TId> | ||
where TId : IEquatable<TId> | ||
{ | ||
private readonly ConcurrentDictionary<TId, TModel> _innerCache = new(); | ||
|
||
/// <summary> | ||
/// Places an item into the cache. Returns if the item already exists and should be updated. | ||
/// </summary> | ||
public TModel Put(TId id, TModel model) | ||
{ | ||
// Empty object is ignored | ||
if (model is null) | ||
return null; | ||
|
||
if (!_innerCache.TryAdd(id, model)) // Fails if already exists | ||
{ | ||
return _innerCache[id]; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/// <summary> | ||
/// Places an item into the cache, and replaces the item if it already exists | ||
/// </summary> | ||
public void PutReplace(TId id, TModel model) | ||
{ | ||
_innerCache[id] = model; | ||
} | ||
|
||
/// <summary> | ||
/// Returns true if the cache contains the item | ||
/// </summary> | ||
public bool Contains(TId id) | ||
{ | ||
return _innerCache.ContainsKey(id); | ||
} | ||
|
||
/// <summary> | ||
/// Returns all the items of the given type. You can use Linq functions like .Where on this function. | ||
/// </summary> | ||
public IEnumerable<TModel> GetAll() | ||
{ | ||
return _innerCache.Values; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Returns the item for the given id, or null if it does not exist | ||
/// </summary> | ||
public bool TryGet(TId id, out TModel model) | ||
{ | ||
return _innerCache.TryGetValue(id, out model); | ||
} | ||
|
||
/// <summary> | ||
/// Removes an item if present in the cache | ||
/// </summary> | ||
public void Remove(TId id) | ||
{ | ||
_innerCache.TryRemove(id, out var _); | ||
} | ||
|
||
public TModel TakeAndRemove(TId id) | ||
{ | ||
_innerCache.TryRemove(id, out var model); | ||
return model; | ||
} | ||
} | ||
|
Oops, something went wrong.