diff --git a/src/Apps/Corathing.Organizer/Services/AppStateService.cs b/src/Apps/Corathing.Organizer/Services/AppStateService.cs index ff4aa14..5f9086c 100644 --- a/src/Apps/Corathing.Organizer/Services/AppStateService.cs +++ b/src/Apps/Corathing.Organizer/Services/AppStateService.cs @@ -166,13 +166,9 @@ public async void UpdateProject(ProjectState project) return; } - if (_cachedAppDashboardState.Projects == null) - _cachedAppDashboardState.Projects = new Dictionary(); + _cachedAppDashboardState.UpdateProject(project); - _cachedAppDashboardState.Projects.ContainsKey(project.Id); - _cachedAppDashboardState.Projects[project.Id] = project; - - await WrtieAppState(); + await PendingWriteAppState(); } public async void UpdateWorkflow(WorkflowState workflow) @@ -186,13 +182,9 @@ public async void UpdateWorkflow(WorkflowState workflow) return; } - if (_cachedAppDashboardState.Workflows == null) - _cachedAppDashboardState.Workflows = new Dictionary(); - - _cachedAppDashboardState.Workflows.ContainsKey(workflow.Id); - _cachedAppDashboardState.Workflows[workflow.Id] = workflow; + _cachedAppDashboardState.UpdateWorkflow(workflow); - await WrtieAppState(); + await PendingWriteAppState(); } public async void UpdateWidget(WidgetState widget) @@ -206,21 +198,9 @@ public async void UpdateWidget(WidgetState widget) return; } - if (_cachedAppDashboardState.Widgets == null) - _cachedAppDashboardState.Widgets = new Dictionary(); - - _cachedAppDashboardState.Widgets.ContainsKey(widget.Id); - _cachedAppDashboardState.Widgets[widget.Id] = widget; - - await WrtieAppState(); - } - - public void UpdateOrAdd(Guid id, object value) - { - } + _cachedAppDashboardState.UpdateWidget(widget); - public void UpdateOverwrite(Guid id, object value) - { + await PendingWriteAppState(); } #region Private Methods diff --git a/src/Apps/Corathing.Organizer/ViewModels/DashboardViewModel.cs b/src/Apps/Corathing.Organizer/ViewModels/DashboardViewModel.cs index 146cf5b..e6684aa 100644 --- a/src/Apps/Corathing.Organizer/ViewModels/DashboardViewModel.cs +++ b/src/Apps/Corathing.Organizer/ViewModels/DashboardViewModel.cs @@ -15,9 +15,10 @@ using CommunityToolkit.Mvvm.Input; using Corathing.Contracts.Bases; +using Corathing.Contracts.Bases.Interfaces; using Corathing.Contracts.Entries; using Corathing.Contracts.Services; - +using Corathing.Dashboards.Bases; using Corathing.Dashboards.WPF.Controls; using Corathing.Organizer.Controls; using Corathing.Organizer.Extensions; @@ -270,7 +271,9 @@ public Task Start(IServiceProvider services) Header = splitedMenuHeaders[i], Command = new RelayCommand(() => { - SelectedProject.SelectedWorkflow.Widgets.Add(widget.CreateWidget()); + var context = widget.CreateWidget(); + context.Layout = WidgetLayoutUtils.Create(context); + SelectedProject.SelectedWorkflow.Widgets.Add(context); }, () => true), }); } diff --git a/src/Shared/Corathing.Contracts/Bases/AppDashboardState.cs b/src/Shared/Corathing.Contracts/Bases/AppDashboardState.cs index 0d34bd8..d30cd0b 100644 --- a/src/Shared/Corathing.Contracts/Bases/AppDashboardState.cs +++ b/src/Shared/Corathing.Contracts/Bases/AppDashboardState.cs @@ -12,8 +12,33 @@ public record StateRecord(string key, string value); public class AppDashboardState { + public Guid Id { get; set; } public Dictionary Projects { get; set; } public Dictionary Workflows { get; set; } public Dictionary Widgets { get; set; } + + public void UpdateProject(ProjectState project) + { + if (Projects == null) + Projects = new Dictionary(); + + Projects[project.Id] = project; + } + + public void UpdateWorkflow(WorkflowState workflow) + { + if (Workflows == null) + Workflows = new Dictionary(); + + Workflows[workflow.Id] = workflow; + } + + public void UpdateWidget(WidgetState widget) + { + if (Widgets == null) + Widgets = new Dictionary(); + + Widgets[widget.Id] = widget; + } } diff --git a/src/Shared/Corathing.Contracts/Bases/Interfaces/IWidgetCoreState.cs b/src/Shared/Corathing.Contracts/Bases/Interfaces/IWidgetCoreState.cs index aaa9308..c775c1d 100644 --- a/src/Shared/Corathing.Contracts/Bases/Interfaces/IWidgetCoreState.cs +++ b/src/Shared/Corathing.Contracts/Bases/Interfaces/IWidgetCoreState.cs @@ -6,6 +6,3 @@ namespace Corathing.Contracts.Bases.Interfaces; -public interface IWidgetCoreState -{ -} diff --git a/src/Shared/Corathing.Contracts/Bases/WidgetState.cs b/src/Shared/Corathing.Contracts/Bases/WidgetState.cs index 6226cf5..babe7f0 100644 --- a/src/Shared/Corathing.Contracts/Bases/WidgetState.cs +++ b/src/Shared/Corathing.Contracts/Bases/WidgetState.cs @@ -9,17 +9,31 @@ namespace Corathing.Contracts.Bases; +public interface IWidgetCoreState +{ + string Name { get; } + string Title { get; } + bool VisibleTitle { get; } +} + public interface IWidgetState : IEntity { IWidgetCoreState CoreSettings { get; } - List CustomSettings { get; } + object CustomSettings { get; } WidgetContext Context { get; } } +public class WidgetCoreState : IWidgetCoreState +{ + public string Name { get; set; } + public string Title { get; set; } + public bool VisibleTitle { get; set; } +} + public class WidgetState : IWidgetState { public Guid Id { get; set; } public IWidgetCoreState CoreSettings { get; set; } - public List CustomSettings { get; set; } + public object CustomSettings { get; set; } public WidgetContext Context { get; set; } } diff --git a/src/Shared/Corathing.Contracts/DataContexts/WidgetContext.cs b/src/Shared/Corathing.Contracts/DataContexts/WidgetContext.cs index 000ed59..7741de8 100644 --- a/src/Shared/Corathing.Contracts/DataContexts/WidgetContext.cs +++ b/src/Shared/Corathing.Contracts/DataContexts/WidgetContext.cs @@ -20,6 +20,13 @@ public partial class WidgetContext : ObservableRecipient private Guid _widgetId; #endregion + #region 확정된 프로퍼티 건드리지 말기 + [ObservableProperty] + private WidgetState? _state; + [ObservableProperty] + private WidgetLayout? _layout; + #endregion + #region 확정된 프로퍼티 [ObservableProperty] private string _widgetTitle; @@ -41,13 +48,9 @@ public partial class WidgetContext : ObservableRecipient private bool? _isResizing; [ObservableProperty] private bool? _isEditing; - [ObservableProperty] - private WidgetLayout? _layout; public WidgetContext() { - WidgetTitle = "Widget"; - VisibleTitle = true; EditMode = true; MinColumns = 2; @@ -59,9 +62,14 @@ public WidgetContext() IsEditing = false; } - public WidgetContext(IServiceProvider services) : this() + public WidgetContext(IServiceProvider services, WidgetState state) : this() { _services = services; + + WidgetId = state.Id; + State = state; + WidgetTitle = state.CoreSettings.Title; + VisibleTitle = state.CoreSettings.VisibleTitle; } public virtual void OnDestroy() diff --git a/src/Shared/Corathing.Contracts/Entries/CoraWidgetGenerator.cs b/src/Shared/Corathing.Contracts/Entries/CoraWidgetGenerator.cs index c9dd3e2..f345a4a 100644 --- a/src/Shared/Corathing.Contracts/Entries/CoraWidgetGenerator.cs +++ b/src/Shared/Corathing.Contracts/Entries/CoraWidgetGenerator.cs @@ -79,9 +79,23 @@ Type optionType /// Creates the widget. /// /// WidgetBase. - public WidgetContext CreateWidget() + public WidgetContext CreateWidget(WidgetState? state = null) { - return (WidgetContext)Activator.CreateInstance(ContextType, Services); + if (state == null) + { + state = CreateEmptyState(); + } + var context = (WidgetContext)Activator.CreateInstance(ContextType, Services, state); + return context; + } + + public WidgetState CreateEmptyState() + { + WidgetState state = new WidgetState(); + state.Id = new Guid(); + state.CoreSettings = new WidgetCoreState(); + state.CustomSettings = Activator.CreateInstance(OptionType); + return state; } #endregion Public Methods diff --git a/src/Shared/Corathing.Contracts/Factories/AppDashboardStateFactory.cs b/src/Shared/Corathing.Contracts/Factories/AppDashboardStateFactory.cs index 4263cd0..0c7a563 100644 --- a/src/Shared/Corathing.Contracts/Factories/AppDashboardStateFactory.cs +++ b/src/Shared/Corathing.Contracts/Factories/AppDashboardStateFactory.cs @@ -13,5 +13,6 @@ public static class AppDashboardStateFactory public static AppDashboardState Create() => new AppDashboardState() { + Id = new Guid(), }; } diff --git a/src/Shared/Corathing.Dashboards.WPF/Controls/DashboardHost.xaml.cs b/src/Shared/Corathing.Dashboards.WPF/Controls/DashboardHost.xaml.cs index eaac4af..ba229d2 100644 --- a/src/Shared/Corathing.Dashboards.WPF/Controls/DashboardHost.xaml.cs +++ b/src/Shared/Corathing.Dashboards.WPF/Controls/DashboardHost.xaml.cs @@ -302,27 +302,17 @@ protected override void PrepareContainerForItemOverride(DependencyObject element // 여기서 WidgetHost의 Layout을 설정하고, WidgetHost의 위치를 설정해야 함 // 설정을 읽게 된다면 여기서 WidgetContext 의 정보는 설정으로부터 읽어져옴 var widgetLayout = widgetContext.Layout; - if (widgetLayout == null) - { - // 위젯이 처음 생성 됨 - widgetContext.Layout = widgetLayout = WidgetLayoutUtils.Create(widgetHost.Id, widgetContext); - - // 초기 위치 및 는 - // Set min/max dimensions of host so it isn't allowed to grow any larger or smaller - widgetHost.MinHeight = (WidgetMinimumHeight * widgetLayout.H) - widgetHost.Margin.Top - widgetHost.Margin.Bottom; - widgetHost.MinWidth = (WidgetMinimumWidth * widgetLayout.W) - widgetHost.Margin.Left - widgetHost.Margin.Right; - widgetHost.Height = (_widgetSize.Height * widgetLayout.H) - widgetHost.Margin.Top - widgetHost.Margin.Bottom; - widgetHost.Width = (_widgetSize.Width * widgetLayout.W) - widgetHost.Margin.Left - widgetHost.Margin.Right; - } - else - { - // 위젯이 탭컨트롤 변경 혹은 설정에 의해서 읽어짐 - widgetHost.Id = widgetLayout.WidgetStateId; - widgetHost.MinHeight = (WidgetMinimumHeight * widgetLayout.H) - widgetHost.Margin.Top - widgetHost.Margin.Bottom; - widgetHost.MinWidth = (WidgetMinimumWidth * widgetLayout.W) - widgetHost.Margin.Left - widgetHost.Margin.Right; - widgetHost.Height = (_widgetSize.Height * widgetLayout.H) - widgetHost.Margin.Top - widgetHost.Margin.Bottom; - widgetHost.Width = (_widgetSize.Width * widgetLayout.W) - widgetHost.Margin.Left - widgetHost.Margin.Right; - } + + Debug.Assert(widgetLayout != null, nameof(widgetLayout) + " != null"); + + // 초기 위치 + // 위젯이 탭컨트롤 변경 혹은 설정에 의해서 읽어짐 + // Set min/max dimensions of host so it isn't allowed to grow any larger or smaller + widgetHost.Id = widgetLayout.WidgetStateId; + widgetHost.MinHeight = (WidgetMinimumHeight * widgetLayout.H) - widgetHost.Margin.Top - widgetHost.Margin.Bottom; + widgetHost.MinWidth = (WidgetMinimumWidth * widgetLayout.W) - widgetHost.Margin.Left - widgetHost.Margin.Right; + widgetHost.Height = (_widgetSize.Height * widgetLayout.H) - widgetHost.Margin.Top - widgetHost.Margin.Bottom; + widgetHost.Width = (_widgetSize.Width * widgetLayout.W) - widgetHost.Margin.Left - widgetHost.Margin.Right; // Subscribe to the widgets drag started and add the widget // to the _widgetHosts to keep tabs on it diff --git a/src/Shared/Corathing.Dashboards/Bases/WidgetLayoutUtils.cs b/src/Shared/Corathing.Dashboards/Bases/WidgetLayoutUtils.cs index c5e9ff6..8222b7c 100644 --- a/src/Shared/Corathing.Dashboards/Bases/WidgetLayoutUtils.cs +++ b/src/Shared/Corathing.Dashboards/Bases/WidgetLayoutUtils.cs @@ -25,6 +25,21 @@ public static WidgetLayout Create(Guid hostId, WidgetContext context) => H = context.MinRows ?? 2, } }; + + public static WidgetLayout Create(WidgetContext context) => + new WidgetLayout() + { + Id = Guid.NewGuid(), + WidgetStateId = context.WidgetId, + Rect = new WidgetLayoutRect() + { + X = 0, + Y = 0, + W = context.MinColumns ?? 2, + H = context.MinRows ?? 2, + } + }; + #endregion #region 02. Update diff --git a/src/Widgets/Corathing.Widgets.Basics/Widgets/FileOpeners/FileOpenerWidget.xaml.cs b/src/Widgets/Corathing.Widgets.Basics/Widgets/FileOpeners/FileOpenerWidget.xaml.cs index ebb4faa..25a050b 100644 --- a/src/Widgets/Corathing.Widgets.Basics/Widgets/FileOpeners/FileOpenerWidget.xaml.cs +++ b/src/Widgets/Corathing.Widgets.Basics/Widgets/FileOpeners/FileOpenerWidget.xaml.cs @@ -50,12 +50,11 @@ public partial class FileOpenerWidgetViewModel : WidgetContext /// Initializes a new instance of the class. /// public FileOpenerWidgetViewModel( - IServiceProvider services) - : base(services) + IServiceProvider services, WidgetState state) + : base(services, state) { ILocalizationService localizationService = services.GetService(); localizationService.Provide("Corathing.Widgets.Basics.FileOpenerName", value => WidgetTitle = value); - VisibleTitle = true; } }