1+ using System ;
2+ using System . Collections . ObjectModel ;
3+ using System . IO ;
4+ using System . Threading . Tasks ;
5+ using CommunityToolkit . Mvvm . ComponentModel ;
6+ using CommunityToolkit . Mvvm . Input ;
7+ using GeneralUpdate . Tool . Avalonia . Models ;
8+ using Newtonsoft . Json ;
9+ using Nlnet . Avalonia . Controls ;
10+
11+ namespace GeneralUpdate . Tool . Avalonia . ViewModels ;
12+
13+ public class OSSPacketViewModel : ObservableObject
14+ {
15+ #region Private Members
16+
17+ private OSSConfigModel ? _currnetConfig ;
18+
19+ private AsyncRelayCommand ? _copyCommand ;
20+ private AsyncRelayCommand ? _buildCommand ;
21+ private RelayCommand ? _appendCommand ;
22+ private RelayCommand ? _clearCommand ;
23+ private RelayCommand ? _loadedCommand ;
24+
25+ #endregion
26+
27+ #region Public Properties
28+
29+ public ObservableCollection < OSSConfigModel > Configs { get ; set ; } = new ( ) ;
30+
31+ public OSSConfigModel CurrnetConfig
32+ {
33+ get => _currnetConfig ;
34+ set => SetProperty ( ref _currnetConfig , value ) ;
35+ }
36+
37+ public AsyncRelayCommand BuildCommand { get => _buildCommand ??= new AsyncRelayCommand ( OSSBuildAction ) ; }
38+
39+ public RelayCommand AppendCommand { get => _appendCommand ??= new RelayCommand ( AppendAction ) ; }
40+
41+ public AsyncRelayCommand CopyCommand { get => _copyCommand ??= new AsyncRelayCommand ( CopyAction ) ; }
42+
43+ public RelayCommand ClearCommand { get => _clearCommand ??= new RelayCommand ( ClearAction ) ; }
44+
45+ public RelayCommand LoadedCommand
46+ {
47+ get { return _loadedCommand ??= new ( LoadedAction ) ; }
48+ }
49+
50+ #endregion
51+
52+ #region Private Methods
53+
54+ private async Task OSSBuildAction ( )
55+ {
56+ try
57+ {
58+ var file = await Storage . Instance . SaveFilePickerAsync ( ) ;
59+ if ( file != null )
60+ {
61+ var json = JsonConvert . SerializeObject ( Configs ) ;
62+ await File . WriteAllTextAsync ( file . Path . AbsolutePath , json , System . Text . Encoding . UTF8 ) ;
63+ var caption = string . Empty ;
64+ var message = string . Empty ;
65+ if ( File . Exists ( file . Path . AbsolutePath ) )
66+ {
67+ caption = "Success" ;
68+ message = "Build success" ;
69+ }
70+ else
71+ {
72+ caption = "Fail" ;
73+ message = "Build fail" ;
74+ }
75+
76+ await MessageBox . ShowAsync ( message , caption , Buttons . OK ) ;
77+ }
78+ }
79+ catch ( Exception e )
80+ {
81+ await MessageBox . ShowAsync ( "Build fail" , "Fail" , Buttons . OK ) ;
82+ }
83+ }
84+
85+ private void AppendAction ( )
86+ {
87+ try
88+ {
89+ Configs . Add ( new OSSConfigModel
90+ {
91+ Date = CurrnetConfig . Date ,
92+ Time = CurrnetConfig . Time ,
93+ Hash = CurrnetConfig . Hash ,
94+ PacketName = CurrnetConfig . PacketName ,
95+ Url = CurrnetConfig . Url ,
96+ Version = CurrnetConfig . Version
97+ } ) ;
98+ var settings = new JsonSerializerSettings
99+ {
100+ Formatting = Formatting . Indented , NullValueHandling = NullValueHandling . Ignore
101+ } ;
102+ CurrnetConfig . JsonContent = JsonConvert . SerializeObject ( Configs , settings ) ;
103+ }
104+ catch ( Exception e )
105+ {
106+ MessageBox . Show ( "Append fail" , "Fail" , Buttons . OK ) ;
107+ }
108+ }
109+
110+ private async Task CopyAction ( )
111+ {
112+ try
113+ {
114+ await ClipboardUtility . SetText ( CurrnetConfig . JsonContent ) ;
115+ await MessageBox . ShowAsync ( "Copy success" , "Success" , Buttons . OK ) ;
116+ }
117+ catch ( Exception e )
118+ {
119+ await MessageBox . ShowAsync ( "Copy fail" , "Fail" , Buttons . OK ) ;
120+ }
121+ }
122+
123+ private void ClearAction ( )
124+ {
125+ CurrnetConfig . JsonContent = "{}" ;
126+ Configs . Clear ( ) ;
127+ }
128+
129+ private void LoadedAction ( ) => Initialize ( ) ;
130+
131+ private void Initialize ( )
132+ {
133+ DateTime dateTime = DateTime . Now ;
134+ CurrnetConfig = new OSSConfigModel
135+ {
136+ JsonContent = "{}" ,
137+ PacketName = "Packet1" ,
138+ Hash = Guid . NewGuid ( ) . ToString ( ) ,
139+ Date = new DateTime ( dateTime . Year , dateTime . Month , dateTime . Day ) ,
140+ Time = new TimeSpan ( dateTime . Hour , dateTime . Minute , dateTime . Second ) ,
141+ Version = "1.0.0.0" ,
142+ Url = "http://127.0.0.1"
143+ } ;
144+ }
145+
146+ #endregion
147+ }
0 commit comments