22using ReactiveUI ;
33using SmartCommander . Models ;
44using System . Collections . ObjectModel ;
5+ using System . Linq ;
56using System . Reactive ;
7+ using System . Reactive . Linq ;
68
79namespace SmartCommander . ViewModels
810{
911 public class FtpViewModel : ViewModelBase
1012 {
1113 public ObservableCollection < Ftp > Ftps { get ; set ; } = [ ] ;
12- public string ? FtpName { get ; private set ; }
13- public bool IsAnonymous { get ; private set ; }
14- public string ? UserName { get ; private set ; }
15- public string ? Password { get ; private set ; }
14+ private string ? _ftpName ;
15+ public string ? FtpName
16+ {
17+ get => _ftpName ;
18+ set => this . RaiseAndSetIfChanged ( ref _ftpName , value ) ;
19+ }
20+
21+ private bool _isAnonymous ;
22+ public bool IsAnonymous
23+ {
24+ get => _isAnonymous ;
25+ set => this . RaiseAndSetIfChanged ( ref _isAnonymous , value ) ;
26+ }
27+
28+ private string ? _userName ;
29+ public string ? UserName
30+ {
31+ get => _userName ;
32+ set => this . RaiseAndSetIfChanged ( ref _userName , value ) ;
33+ }
34+
35+ private string ? _password ;
36+ public string ? Password
37+ {
38+ get => _password ;
39+ set => this . RaiseAndSetIfChanged ( ref _password , value ) ;
40+ }
41+
42+ private Ftp ? _selectedFtp ;
43+ public Ftp ? SelectedFtp
44+ {
45+ get => _selectedFtp ;
46+ set
47+ {
48+ this . RaiseAndSetIfChanged ( ref _selectedFtp , value ) ;
49+
50+ if ( value != null )
51+ {
52+ FtpName = value . FtpName ;
53+ IsAnonymous = value . IsAnonymous ;
54+ UserName = value . UserName ;
55+ Password = value . Password ;
56+ }
57+ else
58+ {
59+ FtpName = null ;
60+ IsAnonymous = false ;
61+ UserName = null ;
62+ Password = null ;
63+ }
64+ }
65+ }
1666
1767 public ReactiveCommand < Window , Unit > OKCommand { get ; }
1868 public ReactiveCommand < Window , Unit > CancelCommand { get ; }
@@ -22,13 +72,43 @@ public FtpViewModel()
2272 OKCommand = ReactiveCommand . Create < Window > ( SaveClose ) ;
2373 CancelCommand = ReactiveCommand . Create < Window > ( Close ) ;
2474
25- // TODO: load data from model
75+ Ftps . Clear ( ) ;
76+ foreach ( var ftp in FtpModel . Instance . Ftps )
77+ {
78+ Ftps . Add ( ftp ) ;
79+ }
2680 }
2781
2882 public void SaveClose ( Window window )
2983 {
30- // TODO: save data to model
31-
84+ if ( string . IsNullOrWhiteSpace ( FtpName ) )
85+ {
86+ return ;
87+ }
88+
89+ var existing = FtpModel . Instance . Ftps . FirstOrDefault ( f => f . FtpName == FtpName ) ;
90+
91+ if ( existing != null )
92+ {
93+ existing . IsAnonymous = IsAnonymous ;
94+ existing . UserName = UserName ;
95+ existing . Password = Password ;
96+ }
97+ else
98+ {
99+ var newFtp = new Ftp ( FtpName )
100+ {
101+ IsAnonymous = IsAnonymous ,
102+ UserName = UserName ,
103+ Password = Password
104+ } ;
105+
106+ FtpModel . Instance . Ftps . Add ( newFtp ) ;
107+ Ftps . Add ( newFtp ) ;
108+ }
109+
110+ FtpModel . Instance . Save ( ) ;
111+
32112 window ? . Close ( this ) ;
33113
34114 }
0 commit comments