Skip to content

Commit

Permalink
保存配置文件优化
Browse files Browse the repository at this point in the history
  • Loading branch information
mehaifeng committed May 5, 2023
1 parent 4939adb commit 6bf13f0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 88 deletions.
8 changes: 8 additions & 0 deletions ViewModels/VisualChatViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public VisualChatViewModel()
UserConfig = new UserConfig();
messageList = new List<Message>();
request = new Tools.WebRequest();
MenuHistorySources = new ObservableCollection<string>();
InitLoad();
}

Expand Down Expand Up @@ -364,6 +365,13 @@ async void ShowGenerateText(ReceivedViewModel receivedViewModel)
}
}

[RelayCommand]
void SaveConfig()
{
string configStr = JsonConvert.SerializeObject(userConfig, Formatting.Indented);
File.WriteAllText(configPath, configStr);
}

[RelayCommand]
void ClearAll(StackPanel o)
{
Expand Down
48 changes: 42 additions & 6 deletions Views/VisualChat.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@
Name="OptionalModelsComboBox"
BorderThickness="0"
VerticalAlignment="Center"
SelectionChanged="OptionalModelsComboBox_SelectionChanged"
SelectedValue="{Binding UserConfig.Model, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Margin="0,0,0,0"
Height="28"
Expand All @@ -321,7 +320,6 @@
<ComboBox
x:Name="ObjectDegreeCombobox"
BorderThickness="0"
SelectionChanged="ObjectDegreeCombobox_SelectionChanged"
SelectedValue="{Binding UserConfig.ObjectDegree, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
VerticalAlignment="Center"
Margin="0,0,0,0"
Expand All @@ -336,8 +334,6 @@
Margin="5,0,5,0"/>
<TextBox
x:Name="systemMessageTextbox"
Text="{Binding ., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
TextChanged="MaxTokensTextbox_TextChanged"
BorderThickness="1"
VerticalAlignment="Center"
Margin="0,0,0,0"
Expand All @@ -353,7 +349,6 @@
<TextBox
x:Name="MaxTokensTextbox"
Text="{Binding UserConfig.MaxTokens, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
TextChanged="MaxTokensTextbox_TextChanged"
BorderThickness="1"
VerticalAlignment="Center"
Margin="0,0,0,0"
Expand All @@ -375,8 +370,49 @@
Width="300"
Height="28"
Template="{StaticResource TextBoxStyle}"
Text="{Binding UserConfig.Apikey, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
Text="{Binding UserConfig.Apikey, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Button
Command="{Binding SaveConfigCommand}"
Margin="188,0,0,0"
Height="30"
Width="60">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border
x:Name="border"
Cursor="Hand"
CornerRadius= "7"
BorderBrush="{x:Null}">
<TextBlock
FontSize="12"
Foreground="White"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="保存"/>
<Border.Style>
<Style TargetType="Border">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Background" Value="#8fbc8f"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#03c03c"/>
</Trigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
</Button>
</StackPanel>

</Grid>
</Border>
<TextBlock
Expand Down
99 changes: 17 additions & 82 deletions Views/VisualChat.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ public VisualChat()
};
ObjectDegreeCombobox.ItemsSource = new[]
{
0,
0.1,
0.2,
0.3,
0.4,
0.5,
0.6,
0.7,
0.8,
0.9
"0",
"0.1",
"0.2",
"0.3",
"0.4",
"0.5",
"0.6",
"0.7",
"0.8",
"0.9"
};
ModeSwitch.Content = "\xe687";
}
Expand Down Expand Up @@ -114,76 +114,20 @@ private void OptionalModelsComboBox_SelectionChanged(object sender, SelectionCha
{
Dictionary<string, string> UserConfig = new()
{
{ "model", _visualChatViewModel.UserConfig.Model },
{ "objectDegree", _visualChatViewModel.UserConfig.ObjectDegree.ToString() },
{ "maxTokens", _visualChatViewModel.UserConfig.MaxTokens.ToString() },
{ "APIKey", _visualChatViewModel.UserConfig.Apikey },
{ "model", _visualChatViewModel.UserConfig?.Model },
{ "objectDegree", _visualChatViewModel.UserConfig.ObjectDegree?.ToString() },
{ "maxTokens", _visualChatViewModel.UserConfig.MaxTokens?.ToString() },
{ "APIKey", _visualChatViewModel.UserConfig?.Apikey },
{ "EnableDarkMode", _visualChatViewModel.UserConfig.EnableDarkMode.ToString() }
};
var jsonStr = JsonConvert.SerializeObject(UserConfig);
File.WriteAllText(UserConfigPath, jsonStr);
}
}

/// <summary>
/// 主客观改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ObjectDegreeCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (File.Exists(UserConfigPath))
{
string json = File.ReadAllText(UserConfigPath);
JObject configObject = JObject.Parse(json);
configObject["objectDegree"] = _visualChatViewModel.UserConfig.ObjectDegree;
json = JsonConvert.SerializeObject(configObject);
File.WriteAllText(UserConfigPath, json);
}
else
{
Dictionary<string, string> UserConfig = new()
{
{ "model", _visualChatViewModel.UserConfig.Model },
{ "objectDegree", _visualChatViewModel.UserConfig.ObjectDegree.ToString() },
{ "maxTokens", _visualChatViewModel.UserConfig.MaxTokens.ToString() },
{ "APIKey", _visualChatViewModel.UserConfig.Apikey },
{ "EnableDarkMode", _visualChatViewModel.UserConfig.EnableDarkMode.ToString() }
};
var jsonStr = JsonConvert.SerializeObject(UserConfig);
File.WriteAllText(UserConfigPath, jsonStr);
}
}

/// <summary>
/// maxTokenss改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MaxTokensTextbox_TextChanged(object sender, TextChangedEventArgs e)
{
if (File.Exists(UserConfigPath))
{
string json = File.ReadAllText(UserConfigPath);
JObject configObject = JObject.Parse(json);
configObject["maxTokens"] = _visualChatViewModel.UserConfig.MaxTokens;
json = JsonConvert.SerializeObject(configObject);
File.WriteAllText(UserConfigPath, json);
}
else
{
Dictionary<string, string> UserConfig = new()
{
{ "model", _visualChatViewModel.UserConfig.Model },
{ "objectDegree", _visualChatViewModel.UserConfig.ObjectDegree.ToString() },
{ "maxTokens", _visualChatViewModel.UserConfig.MaxTokens.ToString() },
{ "APIKey", _visualChatViewModel.UserConfig.Apikey },
{ "EnableDarkMode", _visualChatViewModel.UserConfig.EnableDarkMode.ToString() }
};
var jsonStr = JsonConvert.SerializeObject(UserConfig);
File.WriteAllText(UserConfigPath, jsonStr);
}
}




/// <summary>
Expand Down Expand Up @@ -275,16 +219,7 @@ private void ModeSwitch_Click(object sender, RoutedEventArgs e)
_visualChatViewModel.UserConfig.EnableDarkMode = true;

}
Dictionary<string, string> UserConfig = new()
{
{ "model", _visualChatViewModel.UserConfig.Model },
{ "objectDegree", _visualChatViewModel.UserConfig.ObjectDegree.ToString() },
{ "maxTokens", _visualChatViewModel.UserConfig.MaxTokens.ToString() },
{ "APIKey", _visualChatViewModel.UserConfig.Apikey },
{ "EnableDarkMode", _visualChatViewModel.UserConfig.EnableDarkMode.ToString() }
};
var jsonStr = JsonConvert.SerializeObject(UserConfig);
File.WriteAllText(UserConfigPath, jsonStr);
_visualChatViewModel.SaveConfigCommand.Execute(null);
}

/// <summary>
Expand Down

0 comments on commit 6bf13f0

Please sign in to comment.