Skip to content

Commit 11c1728

Browse files
committed
Add JSON file template generation function
1 parent 79acd20 commit 11c1728

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed
102 Bytes
Binary file not shown.
552 KB
Binary file not shown.

src/c#/GeneralUpdate.PacketTool/MainPage.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<Entry WidthRequest="400" HeightRequest="30" Margin="9,0,3,0" Text="{Binding CurrentVersion}" />
7373
</StackLayout>
7474
<StackLayout HorizontalOptions="Center" Grid.Row="11" HeightRequest="35" Orientation="Horizontal">
75+
<Button HorizontalOptions="Center" Text="Build json" WidthRequest="110" HeightRequest="35" Command="{Binding BuildJsonCommand}" />
7576
<Button HorizontalOptions="Center" Text="Build" WidthRequest="110" HeightRequest="35" Command="{Binding BuildCommand}" />
7677
<CheckBox Margin="10,0,0,0" IsChecked="{Binding IsPublish}" />
7778
<Label VerticalOptions="Center" Text="publish" />

src/c#/GeneralUpdate.PacketTool/ViewModels/MainViewModel.cs

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
using GeneralUpdate.Differential;
66
using GeneralUpdate.Infrastructure.DataServices.Pick;
77
using GeneralUpdate.Infrastructure.MVVM;
8+
using GeneralUpdate.PacketTool.Models;
89
using GeneralUpdate.PacketTool.Services;
910
using GeneralUpdate.Zip.Factory;
11+
using Newtonsoft.Json;
12+
using System.Net.Http.Json;
1013
using System.Text;
1114

1215
namespace GeneralUpdate.PacketTool.ViewModels
@@ -21,8 +24,10 @@ public class MainViewModel : ViewModeBase
2124
private bool isPublish;
2225
private AsyncRelayCommand buildCommand;
2326
private AsyncRelayCommand<string> selectFolderCommand;
27+
private AsyncRelayCommand buildJsonCommand;
2428
private readonly IFolderPickerService _folderPickerService;
2529
private MainService _mainService;
30+
private const string _jsonTemplateFileName = "version.json";
2631

2732
#endregion
2833

@@ -60,6 +65,11 @@ public AsyncRelayCommand BuildCommand
6065
get => buildCommand ?? (buildCommand = new AsyncRelayCommand(BuildPacketCallback));
6166
}
6267

68+
public AsyncRelayCommand BuildJsonCommand
69+
{
70+
get => buildJsonCommand ?? (buildJsonCommand = new AsyncRelayCommand(BuildJsonCallback));
71+
}
72+
6373
public List<string> AppTypes
6474
{
6575
get
@@ -94,15 +104,17 @@ public List<string> Encodings
94104
{
95105
if (_currentEncoding == null)
96106
{
97-
_encodings = new List<string>();
98-
_encodings.Add("Default");
99-
_encodings.Add("UTF8");
100-
_encodings.Add("UTF7");
101-
_encodings.Add("Unicode");
102-
_encodings.Add("UTF32");
103-
_encodings.Add("BigEndianUnicode");
104-
_encodings.Add("Latin1");
105-
_encodings.Add("ASCII");
107+
_encodings = new List<string>
108+
{
109+
"Default",
110+
"UTF8",
111+
"UTF7",
112+
"Unicode",
113+
"UTF32",
114+
"BigEndianUnicode",
115+
"Latin1",
116+
"ASCII"
117+
};
106118
}
107119
return _encodings;
108120
}
@@ -127,7 +139,7 @@ public string CurrnetAppType
127139

128140
public string CurrentVersion { get => _currentVersion; set => SetProperty(ref _currentVersion, value); }
129141
public string CurrentClientAppKey { get => _currentClientAppKey; set => SetProperty(ref _currentClientAppKey, value); }
130-
142+
131143
#endregion
132144

133145
#region Private Methods
@@ -217,6 +229,31 @@ await _mainService.PostUpgradPakcet<UploadReapDTO>(Url,packetPath, String2AppTyp
217229
}
218230
}
219231

232+
/// <summary>
233+
/// Build version template file (json).
234+
/// </summary>
235+
/// <returns></returns>
236+
/// <exception cref="NotImplementedException"></exception>
237+
private async Task BuildJsonCallback()
238+
{
239+
var pickerResult = await _folderPickerService.PickFolderTaskAsync();
240+
if (pickerResult == null)
241+
{
242+
await Shell.Current.DisplayAlert("Pick options", "No results were selected !", "ok");
243+
return;
244+
}
245+
string path = Path.Combine(pickerResult, _jsonTemplateFileName);
246+
if (File.Exists(path)) await Shell.Current.DisplayAlert("Build options", "File already exists !", "check");
247+
var jsonModel = new VersionTmplateModel();
248+
await BuildVersionTemplate(path, jsonModel);
249+
}
250+
251+
private async Task BuildVersionTemplate<T>(string path, T content) where T : class
252+
{
253+
string json = JsonConvert.SerializeObject(content);
254+
await File.WriteAllTextAsync(path, json, System.Text.Encoding.UTF8);
255+
}
256+
220257
private bool ValidationParameters() => (string.IsNullOrEmpty(SourcePath) || string.IsNullOrEmpty(TargetPath) || string.IsNullOrEmpty(PatchPath) ||
221258
string.IsNullOrEmpty(PacketName) || string.IsNullOrEmpty(CurrentFormat) || string.IsNullOrEmpty(CurrentEncoding));
222259

0 commit comments

Comments
 (0)