Skip to content

Commit

Permalink
Update data source context
Browse files Browse the repository at this point in the history
  • Loading branch information
dogzz9445 committed Jul 22, 2024
1 parent 00cb980 commit e863141
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 33 deletions.
8 changes: 4 additions & 4 deletions src/Apps/Corathing.Organizer.WPF/Services/AppStateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ private string GetOrganizerSettingsFilename()
else
{
using var fileStream = File.Open(AppSettingsFilename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using var streamReader = new StreamReader(fileStream);
using var streamReader = new StreamReader(fileStream, Encoding.UTF8);
var json = await streamReader.ReadToEndAsync();
using var document = JsonDocument.Parse(json, _documentOptions);
var appSettings = document.RootElement
Expand All @@ -454,14 +454,14 @@ private async Task WriteAppSettingsToAppPath(AppSettings appSettings)
if (appSettings == null)
return;

string jsonString = await File.ReadAllTextAsync(AppSettingsFilename);
string jsonString = await File.ReadAllTextAsync(AppSettingsFilename, Encoding.UTF8);

var rootNode = JsonNode.Parse(jsonString);

rootNode["Corathing"]["Organizer"].ReplaceWith(appSettings);
var json = rootNode.ToJsonString(_serializerOptions);

await File.WriteAllTextAsync(AppSettingsFilename, json);
await File.WriteAllTextAsync(AppSettingsFilename, json, Encoding.UTF8);
}

private async Task ReadOrCreateAppStateByAppSettings()
Expand Down Expand Up @@ -551,7 +551,7 @@ private async Task WrtieAppState()
{
_isWriting = true;
using var fileStream = File.Open(GetOrganizerSettingsFilename(), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
using var streamReader = new StreamReader(fileStream);
using var streamReader = new StreamReader(fileStream, Encoding.UTF8);
var json = await streamReader.ReadToEndAsync();
if (string.IsNullOrEmpty(json))
json = AppStateJsonDomBase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ public void Apply()
if (SelectedContext == null)
return;

// FIXME:
// 무언가 우아한 방법
SelectedContext.Name = TempName;
SelectedContext.State.CoreSettings.Title = TempName;
SelectedContext.State.CustomSettigns = JsonHelper.DeepCopy(TempSettingsContext.CustomSettings, _optionType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ private DataSourceState CreateState()
{
TypeName = GetDataSourceFullName(),
Title = GenerateDefaultTitle(),
IsDependentOnWidget = false,
DependencyWidget = null,
};

state.CustomSettigns = CreateCustomOption();
Expand Down
6 changes: 5 additions & 1 deletion src/Shared/Corathing.Contracts/Bases/DataSourceState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ public interface IDataSourceCoreState
{
string? TypeName { get; }
string? Title { get; }
bool IsDependentOnWidget { get; }
Guid? DependencyWidget { get; }
}

public class DataSourceCoreState
public class DataSourceCoreState : IDataSourceCoreState
{
public string? TypeName { get; set; }
public string? Title { get; set; }
public bool IsDependentOnWidget { get; set; }
public Guid? DependencyWidget { get; set; }
}

public interface IDataSourceState : IEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ namespace Corathing.Contracts.DataContexts;

public partial class DataSourceContext : ObservableRecipient
{
protected IServiceProvider _services;
protected IServiceProvider? _services;

#region 숨겨진 프로퍼티
public Guid DataSourceId;
public Guid? DataSourceId { get; set; }

public DataSourceState? State { get; set; }
#endregion

[ObservableProperty]
private string _name;
private string? _name;

public void Initialize(IServiceProvider services, DataSourceState state)
{
Expand Down Expand Up @@ -63,6 +63,9 @@ public void Destroy()

_services?.GetRequiredService<IStorageService>().DeleteEntityFolder(State);
_services?.GetRequiredService<IAppStateService>().RemoveDataSource(State);

// TODO:
//WeakReferenceMessenger.Default.Register
}

public virtual void OnCreate(IServiceProvider services, DataSourceState state)
Expand Down
21 changes: 14 additions & 7 deletions src/Shared/Corathing.Contracts/DataContexts/DataSourceSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public partial class DataSourceSelector<T> :
[ObservableProperty]
private string? _hintSelectionText;

private bool _selectDefaultCreateIfEmpty;
private readonly bool _selectDefaultCreateIfEmpty;

public DataSourceSelector(
IServiceProvider services,
Expand All @@ -50,10 +50,13 @@ public DataSourceSelector(

Select(guid);

WeakReferenceMessenger.Default?.Register<DataSourceStateChangedMessage, string>(
this,
typeof(T).FullName,
OnDataSourceStateChanged);
if (!string.IsNullOrEmpty(typeof(T).FullName))
{
WeakReferenceMessenger.Default?.Register<DataSourceStateChangedMessage, string>(
this,
typeof(T).FullName,
OnDataSourceStateChanged);
}
}

[RelayCommand]
Expand All @@ -69,14 +72,18 @@ private void OnDataSourceStateChanged(object recipient, DataSourceStateChangedMe
{
if (message.Value is T context)
{
DataSourceContexts.Add(message.Value as T);
DataSourceContexts.Add(context);
}
}
else if (message.ChangedType == EntityStateChangedType.Removed)
{
if (SelectedDataSourceContext == message.Value)
SelectedDataSourceContext = null;
DataSourceContexts.Remove(message.Value as T);

if (message.Value is T context)
{
DataSourceContexts.Remove(context);
}
}
else if (message.ChangedType == EntityStateChangedType.Selected)
{
Expand Down
1 change: 0 additions & 1 deletion src/Shared/Corathing.UI.WPF/Styles/ListBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
</Style>

<Style TargetType="{x:Type ListBoxItem}"
BasedOn="{StaticResource DefaultListBoxItemStyle}"
x:Key="DefaultCorathingListBoxItemStyle">
<Setter Property="Foreground"
Value="{DynamicResource ListBoxItemForeground}" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,39 @@ public override void OnStateChanged(DataSourceState state)

public void Add(ToDo toDo)
{
ArgumentNullException.ThrowIfNull(State);

if (State.CustomSettigns is not ToDoDataSourceOption option)
{
return;
}
option.ToDos ??= new List<ToDo>();
option.ToDos.Add(toDo);
SaveState();
}

public void Remove(ToDo toDo)
{
ArgumentNullException.ThrowIfNull(State);

if (State.CustomSettigns is not ToDoDataSourceOption option)
{
return;
}
option.ToDos ??= new List<ToDo>();
option.ToDos.Remove(toDo);
SaveState();
}

public void Update(ToDo toDo)
{
ArgumentNullException.ThrowIfNull(State);

if (State.CustomSettigns is not ToDoDataSourceOption option)
{
return;
}
option.ToDos ??= new List<ToDo>();
var index = option.ToDos.FindIndex(t => t.Id == toDo.Id);
if (index == -1)
{
Expand All @@ -85,10 +94,14 @@ public void Update(ToDo toDo)

public List<ToDo>? GetToDos()
{
ArgumentNullException.ThrowIfNull(State);

if (State.CustomSettigns is not ToDoDataSourceOption option)
{
return null;
}

option.ToDos ??= new List<ToDo>();
return option.ToDos;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
IsChecked="{Binding IsShowTask}" />
</settings:SettingsBlock>

<!-- Files ExecutableAppDataSourceSelector -->
<!-- ToDo DataSources -->
<settings:SettingsBlock CategoryText="ToDo:"
Header="Executable App for Open File or Folder..">
<ContentPresenter Grid.Row="1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,11 @@
Margin="0"
Padding="0"
BorderThickness="0"
Text="{Binding Job}" />
Text="{Binding Job}">
<i:Interaction.Behaviors>
<behaviors:TextBoxEnterKeyUpdateBehavior />
</i:Interaction.Behaviors>
</TextBox>
</DockPanel>
<Border IsHitTestVisible="False"
Visibility="{Binding IsDone, Converter={StaticResource BoolToVis}}"
Expand All @@ -260,33 +264,22 @@
</DataTemplate>

</UserControl.Resources>

<Grid>
<!--<ListView BorderThickness="0"
Background="Transparent"
Style="{StaticResource MahApps.Styles.ListView.Virtualized}"
ItemsSource="{Binding GroupedJobs}"
ItemTemplateSelector="{StaticResource JobTemplateSelector}" />-->

<ListView BorderThickness="0"
Background="Transparent"
Style="{DynamicResource CoraListViewStyle}"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True"
dd:DragDrop.ShowAlwaysDropTargetAdorner="True"
dd:DragDrop.UseDefaultDragAdorner="True"
dd:DragDrop.DropHandler="{Binding}"
ItemContainerStyle="{StaticResource ChangedVisibilityByDoneListViewItem}"
SelectionChanged="ListView_SelectionChanged"
ItemsSource="{Binding ToDos}">
<ListView.ItemTemplateSelector>
<behaviors:EmptyDataTemplateSelector EmptyTemplate="{StaticResource AddingJobDataTemplate}" />
</ListView.ItemTemplateSelector>
</ListView>

<!--<ListBox x:Name="ToDoListBox"
BorderThickness="0"
Background="Transparent"
ItemsSource="{Binding GroupedJobs}"
ItemTemplateSelector="{StaticResource JobTemplateSelector}" />-->
</Grid>

</UserControl>

0 comments on commit e863141

Please sign in to comment.