1111using System . Threading . Tasks ;
1212using System . Windows . Forms ;
1313using Hypermc . Data ;
14+ using Hypermc . Services ;
15+ using Hypermc . Settings ;
1416using Hypermc . UI . Dialogs ;
1517using Hypermc . UI . UserControls ;
1618using Hypermc . UI . Views ;
@@ -25,20 +27,32 @@ public partial class HyperMcView : Form, IViewHost
2527
2628 private readonly IForgeClient _forgeClient ;
2729 private readonly IServiceProvider _provider ;
30+ private readonly IFileManager _fileManager ;
31+ private readonly IUserSettings _settings ;
2832
29- public HyperMcView ( IForgeClient forgeClient , IServiceProvider provider )
33+ public HyperMcView ( IForgeClient forgeClient , IServiceProvider provider , IFileManager fileManager , IUserSettings settings )
3034 {
3135 InitializeComponent ( ) ;
3236
3337 _forgeClient = forgeClient ;
3438 _provider = provider ;
39+ _fileManager = fileManager ;
40+ _settings = settings ;
3541 _modpacks = new ( ) ;
3642 _modpacks . CollectionChanged += ModpacksUpdated ;
3743 }
3844
39- private void HyperMcView_Load ( object sender , EventArgs e )
45+ private async void HyperMcView_Load ( object sender , EventArgs e )
4046 {
4147 SetView ( new ControlView ( pnl_MainArea ) ) ;
48+ var mods = await _fileManager . ReadFile < ModpackData [ ] > ( $@ "{ _settings . ModPacksPath } \packs.json") ;
49+ if ( mods != null )
50+ {
51+ foreach ( var mod in mods )
52+ {
53+ _modpacks . Add ( mod ) ;
54+ }
55+ }
4256 }
4357
4458 #region Default View
@@ -61,7 +75,7 @@ private void Hbtn_CreateModpack_Click(object sender, EventArgs e)
6175
6276 private ObservableCollection < ModpackData > _modpacks ;
6377
64- private void ModpacksUpdated ( object ? sender , NotifyCollectionChangedEventArgs e )
78+ private async void ModpacksUpdated ( object ? sender , NotifyCollectionChangedEventArgs e )
6579 {
6680 switch ( e . Action )
6781 {
@@ -112,13 +126,25 @@ private void ModpacksUpdated(object? sender, NotifyCollectionChangedEventArgs e)
112126 }
113127
114128 SortModpacks ( ) ;
129+ await _fileManager . WriteToFile ( _modpacks . ToArray ( ) , $@ "{ _settings . ModPacksPath } \packs.json") ;
115130 }
116131
117- private static ModpackBox CreateModpackBox ( ModpackData data )
132+ private ModpackBox CreateModpackBox ( ModpackData data )
118133 {
134+ Image thumbnail ;
135+ if ( string . IsNullOrWhiteSpace ( data . Thumbnail ) )
136+ {
137+ thumbnail = Properties . Resources . DefaultModpackImage ;
138+ }
139+ else
140+ {
141+ // May need to be changed depending on how the image will be set
142+ thumbnail = Image . FromStream ( _forgeClient . GetImageFromURL ( data . Thumbnail ) . GetAwaiter ( ) . GetResult ( ) ) ;
143+ }
144+
119145 return new ( )
120146 {
121- Thumbnail = data . Thumbnail ,
147+ Thumbnail = thumbnail ,
122148 SizeMode = PictureBoxSizeMode . StretchImage ,
123149 Name = data . Name ,
124150 Tag = data . Path
@@ -202,9 +228,12 @@ public void SetView(IView view, object? data = null)
202228 {
203229 if ( _view is not null )
204230 {
205- _view . HideView ( Utils . PopChildControls ( pnl_MainArea ) ) ;
206- _view . OnMessage -= View_OnMessage ;
207- _viewPrev = _view ;
231+ if ( _view . GetType ( ) != view . GetType ( ) )
232+ {
233+ _view . HideView ( Utils . PopChildControls ( pnl_MainArea ) ) ;
234+ _view . OnMessage -= View_OnMessage ;
235+ _viewPrev = _view ;
236+ }
208237 }
209238
210239 view . OnMessage += View_OnMessage ;
0 commit comments