Skip to content

Commit

Permalink
Update websession
Browse files Browse the repository at this point in the history
  • Loading branch information
dogzz9445 committed Jul 15, 2024
1 parent c8950a7 commit 56af28d
Show file tree
Hide file tree
Showing 20 changed files with 383 additions and 104 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

Corathing is Customizable Widget Organizer. It is a WPF application that allows you to organize anything with customizable widget dashboards. You can create your own widgets or use the default widgets provided by Corathing.

| Wiki |Readme | Readme |
| --- | --- | --- |
| [Wiki](https://github.com/dogzz9445/Corathing/wiki/Home) | [English](README.md) | [한국어](README_KR.md) |
| Homepage | Wiki |Readme | Readme |
| --- | --- | --- | --- |
| [Homepage](https://corathing.com) | [Wiki](https://github.com/dogzz9445/Corathing/wiki/Home) | [English](README.md) | [한국어](README_KR.md) |

![sample](docs/images/version0.0.9.gif)

Expand Down
18 changes: 18 additions & 0 deletions src/Apps/Corathing.Organizer.AvaloniaUI.Desktop/app.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<!-- This manifest is used on Windows only.
Don't remove it as it might cause problems with window transparency and embeded controls.
For more details visit https://learn.microsoft.com/en-us/windows/win32/sbscs/application-manifests -->
<assemblyIdentity version="1.0.0.0" name="AvaloniaTest.Desktop"/>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- A list of the Windows versions that this application has been tested on
and is designed to work with. Uncomment the appropriate elements
and Windows will automatically select the most compatible environment. -->

<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
</assembly>
1 change: 1 addition & 0 deletions src/Apps/Corathing.Organizer.WPF/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ private static IServiceProvider ConfigureServices(string[] args)
serviceCollection.AddScoped<OrganizerSettingsViewModel>();
serviceCollection.AddScoped<ProjectSettingsViewModel>();
serviceCollection.AddScoped<WorkflowSettingsViewModel>();
serviceCollection.AddScoped<WidgetManagementView>();
serviceCollection.AddTransient<DataSourceSettingsViewModel>();
serviceCollection.AddTransient<WidgetSettingsViewModel>();
serviceCollection.AddTransient<ProjectSettingsContext>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,8 @@ public void AddDataSource()
if (_dataSourceType == null)
return;

var packageService = _services.GetRequiredService<IPackageService>();
var dataContext = packageService.CreateDataSourceContext(_dataSourceType.FullName);

var appStateService = _services.GetRequiredService<IAppStateService>();
appStateService.UpdateDataSource(dataContext.State);

var dataSourceService = _services.GetRequiredService<IDataSourceService>();
dataSourceService.AddDataSourceContext(dataContext);

WeakReferenceMessenger.Default.Send(
new DataSourceStateChangedMessage(EntityStateChangedType.Added, dataContext),
dataContext.GetType().FullName
);
var dataContext = dataSourceService.CreateDataSourceContext(_dataSourceType);

DataSourceContexts.Add(dataContext);
Select(dataContext);
Expand All @@ -111,19 +100,8 @@ public void AddDataSource()
[RelayCommand]
public void RemoveDataSource(DataSourceContext context)
{
// TODO:
// 모든 위젯에서 해당 연결 끊기
//WeakReferenceMessenger.Default.Send(new DataSourceRemovedMessage(context.State.Id));

var appStateService = _services.GetRequiredService<IAppStateService>();
var dataSourceService = _services.GetRequiredService<IDataSourceService>();
appStateService.RemovePackage(context.State.Id);
dataSourceService.RemoveDataSourceContext(context);

WeakReferenceMessenger.Default.Send(
new DataSourceStateChangedMessage(EntityStateChangedType.Removed, context),
context.GetType().FullName
);
dataSourceService.DestroyDataSourceContext(context);
}

public void OnSelectedContext(DataSourceContext? selectedContext)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using CommunityToolkit.Mvvm.ComponentModel;

namespace Corathing.Organizer.WPF.ViewModels;

public class WidgetManagementViewModel : ObservableRecipient
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void Initialize(WidgetContext originalContext, WidgetHost settingsWidgetH
var packageService = _services.GetService<IPackageService>();

TempWidgetContext = packageService.CreateWidgetContext(_originalContext.GetType().FullName);
TempWidgetContext.IsTemporal = true;
_optionType = packageService.GetWidgetCustomSettingsType(_originalContext.GetType().FullName);
_originalContext.CopyTo(TempWidgetContext, _optionType);
_settingsWidgetHost.Content = TempWidgetContext;
Expand Down
100 changes: 87 additions & 13 deletions src/Apps/Corathing.Organizer/Services/DataSourceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
using System.Text;
using System.Threading.Tasks;

using CommunityToolkit.Mvvm.Messaging;

using Corathing.Contracts.DataContexts;
using Corathing.Contracts.Messages;
using Corathing.Contracts.Services;

using Microsoft.Extensions.DependencyInjection;

using NuGet.Packaging;

namespace Corathing.Organizer.Services;
Expand All @@ -15,6 +20,88 @@ public class DataSourceService(IServiceProvider services) : IDataSourceService
{
public Dictionary<Guid, DataSourceContext> DataSourceContexts { get; } = new();

public T? GetOrFirstOrCreateDataSourceContext<T>(Guid? guid) where T : DataSourceContext
{
var dataContext = GetDataSourceContext<T>(guid);
if (dataContext != null)
return dataContext;

return FirstOrCreateDataSourceContext<T>();
}

public T? FirstOrCreateDataSourceContext<T>() where T : DataSourceContext
{
var pair = DataSourceContexts.FirstOrDefault();
if (pair.Value != null)
return pair.Value as T;

return CreateDataSourceContext<T>();
}

public T? CreateDataSourceContext<T>() where T : DataSourceContext
{
return CreateDataSourceContext(typeof(T).FullName) as T;
}

public DataSourceContext? CreateDataSourceContext(Type type)
{
return CreateDataSourceContext(type.FullName);
}

public DataSourceContext? CreateDataSourceContext(string? contextFullName)
{
var packageService = services.GetRequiredService<IPackageService>();
var dataContext = packageService.CreateDataSourceContext(contextFullName);

var appStateService = services.GetRequiredService<IAppStateService>();
appStateService.UpdateDataSource(dataContext.State);

AddDataSourceContext(dataContext);

WeakReferenceMessenger.Default?.Send(
new DataSourceStateChangedMessage(EntityStateChangedType.Added, dataContext),
dataContext.GetType().FullName
);

return dataContext;
}

public T? GetDataSourceContext<T>(Guid? dataSourceStateId) where T : DataSourceContext
{
if (dataSourceStateId == null)
{
return null;
}
if (DataSourceContexts.TryGetValue(dataSourceStateId.Value, out var dataSourceContext))
{
return dataSourceContext as T;
}
return null;
}

public void DestroyDataSourceContext(DataSourceContext? dataSourceContext)
{
// TODO:
// 모든 위젯에서 해당 연결 끊기
//WeakReferenceMessenger.Default.Send(new DataSourceRemovedMessage(context.State.Id));

var appStateService = services.GetRequiredService<IAppStateService>();
appStateService.RemoveDataSource(dataSourceContext.State.Id);
RemoveDataSourceContext(dataSourceContext);

WeakReferenceMessenger.Default?.Send(
new DataSourceStateChangedMessage(EntityStateChangedType.Removed, dataSourceContext),
dataSourceContext.GetType().FullName
);
}

public void DestroyDataSourceContext(Guid? guid)
{
var dataContext = GetDataSourceContext<DataSourceContext>(guid);

DestroyDataSourceContext(dataContext);
}

public void AddDataSourceContext(Guid dataSourceStateId, DataSourceContext dataSourceContext)
{
DataSourceContexts.Add(dataSourceStateId, dataSourceContext);
Expand Down Expand Up @@ -59,17 +146,4 @@ public IEnumerable<T> GetDataSourceContexts<T>()
{
return DataSourceContexts.Values.OfType<T>();
}

public T? GetDataSourceContext<T>(Guid? dataSourceStateId) where T : DataSourceContext
{
if (dataSourceStateId == null)
{
return null;
}
if (DataSourceContexts.TryGetValue(dataSourceStateId.Value, out var dataSourceContext))
{
return dataSourceContext as T;
}
return null;
}
}
26 changes: 20 additions & 6 deletions src/Apps/Corathing.Organizer/Services/PackageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ public List<ICoraDataSourceInfo> GetAvailableDataSources()
/// <summary>
/// <inheritdoc/>
/// </summary>
public DataSourceContext? CreateDataSourceContext(string contextTypeFullName)
public DataSourceContext? CreateDataSourceContext(string? contextTypeFullName)
{
if (string.IsNullOrEmpty(contextTypeFullName))
return null;
if (!_dataSourceGenerator.TryGetValue(contextTypeFullName, out var dataSourceGenerator))
return null;
return dataSourceGenerator.CreateDataSource();
Expand All @@ -156,8 +158,10 @@ public List<ICoraDataSourceInfo> GetAvailableDataSources()
/// <summary>
/// <inheritdoc/>
/// </summary>
public Type? GetDataSourceCustomSettingsType(string contextTypeFullName)
public Type? GetDataSourceCustomSettingsType(string? contextTypeFullName)
{
if (string.IsNullOrEmpty(contextTypeFullName))
return null;
if (!_dataSourceGenerator.TryGetValue(contextTypeFullName, out var dataSourceGenerator))
return null;
return dataSourceGenerator.Info.OptionType;
Expand All @@ -166,15 +170,19 @@ public List<ICoraDataSourceInfo> GetAvailableDataSources()
/// <summary>
/// <inheritdoc/>
/// </summary>
public CustomSettingsContext? CreateDataSourceSettingsContext(string contextTypeFullName)
public CustomSettingsContext? CreateDataSourceSettingsContext(string? contextTypeFullName)
{
if (string.IsNullOrEmpty(contextTypeFullName))
return null;
if (!_dataSourceGenerator.TryGetValue(contextTypeFullName, out var dataSourceGenerator))
return null;
return dataSourceGenerator.CreateSettingsContext();
}

public Type? GetDataSourceSettingsContextType(string? contextTypeFullName)
{
if (string.IsNullOrEmpty(contextTypeFullName))
return null;
if (!_dataSourceGenerator.TryGetValue(contextTypeFullName, out var dataSourceGenerator))
return null;
return dataSourceGenerator.Info.SettingsContextType;
Expand All @@ -200,8 +208,10 @@ public List<CoraWidgetGenerator> GetWidgetGenerators()
/// <summary>
/// <inheritdoc/>
/// </summary>
public WidgetContext? CreateWidgetContext(string contextTypeFullName, WidgetState? state = null)
public WidgetContext? CreateWidgetContext(string? contextTypeFullName, WidgetState? state = null)
{
if (string.IsNullOrEmpty(contextTypeFullName))
return null;
if (!_widgetGenerators.TryGetValue(contextTypeFullName, out var widgetContext))
return null;
return widgetContext.CreateWidget(state);
Expand All @@ -210,8 +220,10 @@ public List<CoraWidgetGenerator> GetWidgetGenerators()
/// <summary>
/// <inheritdoc/>
/// </summary>
public Type? GetWidgetCustomSettingsType(string contextTypeFullName)
public Type? GetWidgetCustomSettingsType(string? contextTypeFullName)
{
if (string.IsNullOrEmpty(contextTypeFullName))
return null;
if (!_widgetGenerators.TryGetValue(contextTypeFullName, out var widgetContext))
return null;
return widgetContext.Info.WidgetCustomSettingsType;
Expand All @@ -220,8 +232,10 @@ public List<CoraWidgetGenerator> GetWidgetGenerators()
/// <summary>
/// <inheritdoc/>
/// </summary>
public CustomSettingsContext? CreateWidgetSettingsContext(string contextTypeFullName)
public CustomSettingsContext? CreateWidgetSettingsContext(string? contextTypeFullName)
{
if (string.IsNullOrEmpty(contextTypeFullName))
return null;
if (!_widgetGenerators.TryGetValue(contextTypeFullName, out var widgetContext))
return null;
return widgetContext.CreateSettingsContext();
Expand Down
31 changes: 24 additions & 7 deletions src/Shared/Corathing.Contracts/DataContexts/DataSourceSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,27 @@ public partial class DataSourceSelector<T> :
[ObservableProperty]
private string? _hintSelectionText;

private bool _selectDefaultCreateIfEmpty;

public DataSourceSelector(
IServiceProvider services,
Guid? guid = null)
Guid? guid = null,
bool selectDefaultCreateIfEmpty = false)
{
_services = services;
DataSourceContexts = new ObservableCollection<T>();
HintSelectionText = "Default DataSource";
_selectDefaultCreateIfEmpty = selectDefaultCreateIfEmpty;

var dataSourceService = _services.GetService<IDataSourceService>();

DataSourceContexts = new ObservableCollection<T>(
dataSourceService.GetAllDataSourceContexts<T>()
);

if (guid != null)
{
SelectedDataSourceContext = dataSourceService.GetDataSourceContext<T>(guid);
}
Select(guid);

WeakReferenceMessenger.Default.Register<DataSourceStateChangedMessage, string>(
WeakReferenceMessenger.Default?.Register<DataSourceStateChangedMessage, string>(
this,
typeof(T).FullName,
OnDataSourceStateChanged);
Expand Down Expand Up @@ -85,6 +86,22 @@ private void OnDataSourceStateChanged(object recipient, DataSourceStateChangedMe

public void Select(Guid? guid)
{
SelectedDataSourceContext = DataSourceContexts.FirstOrDefault(c => c.DataSourceId == guid);
if (guid != null)
{
SelectedDataSourceContext = DataSourceContexts.FirstOrDefault(c => c.DataSourceId == guid);
}

if (SelectedDataSourceContext == null && _selectDefaultCreateIfEmpty)
{
var dataSourceService = _services.GetRequiredService<IDataSourceService>();

if (DataSourceContexts.Count == 0)
{
var dataContext = dataSourceService.CreateDataSourceContext<T>();

DataSourceContexts.Add(dataContext);
}
SelectedDataSourceContext = DataSourceContexts.FirstOrDefault();
}
}
}
Loading

0 comments on commit 56af28d

Please sign in to comment.