@@ -27,6 +27,18 @@ void touchFile(File file, CCSolution solution) {
2727void loadSolution (CCSolution solution, BuildContext context) {
2828 Navigator .pushNamed (context, EditorPage .routeName, arguments: solution);
2929}
30+ enum HistoryItemType {
31+ solution,
32+ singleFile
33+ }
34+ class HistoryItem {
35+ HistoryItemType type;
36+ CCSolution ? solution;
37+ String ? filePath;
38+ DateTime dateModified;
39+ String name;
40+ HistoryItem (this .type, {this .solution, this .filePath, required this .dateModified, required this .name});
41+ }
3042
3143class RecentProjectsManager {
3244 static RecentProjectsManager ? _instance;
@@ -36,13 +48,16 @@ class RecentProjectsManager {
3648 return _instance! ;
3749 }
3850
39- List <CCSolution > projects = List .empty (growable: true );
51+ List <HistoryItem > projects = List .empty (growable: true );
4052
4153 /// Commit the recent projects to the pref
4254 Future <void > commit (Future <SharedPreferences > _pref) async {
4355 List <String > list = List .empty (growable: true );
44- for (CCSolution p in projects) {
45- list.add (p.slnPath);
56+ for (HistoryItem p in projects) {
57+ if (p.type == HistoryItemType .solution) {
58+ list.add (p.solution! .slnPath);
59+ }
60+ //TODO: support single file
4661 }
4762 (await _pref).setStringList ("recentProjectsSln" , list).then ((bool success) {
4863 debugPrint ("Success: $success " );
@@ -57,12 +72,14 @@ class RecentProjectsManager {
5772 Future <CCSolution ?> addSolution (String slnPath) async {
5873 // Prevent project with same solution to be loaded
5974 for (var p in projects) {
60- if (p.slnPath == slnPath) return null ;
75+ if (p.solution ? . slnPath == slnPath) return null ;
6176 }
6277
6378 var sln = await CCSolution .loadFromFile (slnPath);
79+
6480 if (sln != null ) {
65- projects.add (sln);
81+ var item = HistoryItem (HistoryItemType .solution, solution: sln, dateModified: sln.dateModified, name: sln.name);
82+ projects.add (item);
6683 }
6784 return sln;
6885 }
@@ -283,7 +300,7 @@ class _HomePageState extends State<HomePage> {
283300 await loadPrefs ();
284301 setState (() {
285302 projectsWidgets.clear ();
286- RecentProjectsManager .instance.projects.sort ((CCSolution a, CCSolution b) {
303+ RecentProjectsManager .instance.projects.sort ((HistoryItem a, HistoryItem b) {
287304 return b.dateModified.compareTo (a.dateModified);
288305 });
289306 if (RecentProjectsManager .instance.projects.isEmpty) {
@@ -294,75 +311,88 @@ class _HomePageState extends State<HomePage> {
294311 );
295312 }
296313 else {
297- for (CCSolution p in RecentProjectsManager .instance.projects) {
298- if (p.name == "" ) {
299- continue ;
300- } // TODO: add better way to check if project is corrupt
314+ for (HistoryItem p in RecentProjectsManager .instance.projects) {
315+ // if (p.name == "") {
316+ // continue;
317+ // } // TODO: add better way to check if project is corrupt
301318 //debugPrint(p.name);
302319 projectsWidgets.add (Card ( //TODO: refactor this as a widget elsewhere, then reference that widget from here
303320 child: ListTile (
304321 onTap: () {
305- touchFile (File (p.slnPath), p);
306- refreshRecentProjects ();
307- loadSolution (p, context);
322+ if (p.type == HistoryItemType .solution) {
323+ touchFile (File (p.solution! .slnPath), p.solution! );
324+ refreshRecentProjects ();
325+ loadSolution (p.solution! , context);
326+ }
327+ //TODO: Handle single file
308328 },
309- leading: p.image ??
329+ leading: p.type == HistoryItemType .solution ? p.solution ! . image ??
310330 const Icon (
311331 Icons .insert_drive_file,
312332 size: 48 ,
313- ),
333+ ):
334+ const Icon (
335+ Icons .insert_drive_file,
336+ size: 48 ,
337+ ),
314338 title: Text (p.name),
315339 subtitle: Text (
316- p.desc + " Last Modified: " + p.dateModified.toString ()),
340+ (p.type == HistoryItemType .solution? p.solution! .desc : "" )
341+ + " Last Modified: " + p.dateModified.toString ()),
317342 trailing: PopupMenuButton <String >(
318343 onSelected: (String result) {
319344 switch (result) {
320345 case "delete" :
321- showDialog (
322- context: context,
323- builder: (BuildContext context) {
324- return AlertDialog (
325- title: Text ("Delete ${p .name }?" ),
326- content: Text (
327- "This action cannot be undone!\n folders will be deleted: ${() {
328- String result = "" ;
329- for (var folder in p .folders .keys ) {
330- result +=
331- (p .folders [folder ] as String ) + ", \n " ;
332- }
333- return result ;
334- }()}" ),
335- actions: [
336- TextButton (
337- onPressed: () {
338- Navigator .pop (context);
339- },
340- child: const Text ("No" )),
341- TextButton (
342- onPressed: () {
343- var folders = < String > [];
344- for (var folder in p.folders.keys) {
345- folders.add (p.slnFolderPath +
346- Platform .pathSeparator +
347- p.folders[folder]! );
346+ if (p.type == HistoryItemType .solution) {
347+ showDialog (
348+ context: context,
349+ builder: (BuildContext context) {
350+ return AlertDialog (
351+ title: Text ("Delete ${p .name }?" ),
352+ content: Text (
353+ "This action cannot be undone!\n folders will be deleted: ${() {
354+ String result = "" ;
355+ for (var folder in p .solution !.folders .keys ) {
356+ result +=
357+ (p .solution !.folders [folder ] as String ) +
358+ ", \n " ;
348359 }
349- deleteFolderWithIndicator (
350- context, folders);
351- // Delete the solution file too
352- File (p.slnPath).deleteSync ();
353-
354- // Quit and refresh
355- Navigator .pop (context);
356- refreshRecentProjects ();
357- },
358- child: const Text (
359- "Delete" ,
360- style:
361- TextStyle (color: Colors .redAccent),
362- )),
363- ],
364- );
365- });
360+ return result ;
361+ }()}" ),
362+ actions: [
363+ TextButton (
364+ onPressed: () {
365+ Navigator .pop (context);
366+ },
367+ child: const Text ("No" )),
368+ TextButton (
369+ onPressed: () {
370+ var folders = < String > [];
371+ for (var folder in p.solution! .folders.keys) {
372+ folders.add (p.solution! .slnFolderPath +
373+ Platform .pathSeparator +
374+ p.solution! .folders[folder]! );
375+ }
376+ deleteFolderWithIndicator (
377+ context, folders);
378+ // Delete the solution file too
379+ File (p.solution! .slnPath).deleteSync ();
380+
381+ // Quit and refresh
382+ Navigator .pop (context);
383+ refreshRecentProjects ();
384+ },
385+ child: const Text (
386+ "Delete" ,
387+ style:
388+ TextStyle (color: Colors .redAccent),
389+ )),
390+ ],
391+ );
392+ });
393+ }else {
394+ //TODO: Handle single file delete
395+ }
366396 break ;
367397 }
368398 },
0 commit comments