-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/reverse_action'
- Loading branch information
Showing
8 changed files
with
209 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,55 @@ | ||
import 'dart:collection'; | ||
import 'dart:convert'; | ||
|
||
import 'package:my_sudoku_table/models/cell_model.dart'; | ||
import 'package:my_sudoku_table/view_modal/sudoku_view_model.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
class SaveManager { | ||
static final SaveManager _singleton = SaveManager._internal(); | ||
factory SaveManager() { | ||
return _singleton; | ||
} | ||
SaveManager._internal(); | ||
final SudokuNotifier _sudokuNotifier; | ||
|
||
SaveManager(this._sudokuNotifier); | ||
|
||
Future<bool> save(SudokuNotifier sudokuNotifier) async { | ||
Future<bool> save() async { | ||
//save cells | ||
final sp = await SharedPreferences.getInstance(); | ||
final cellsJson = jsonEncode(sudokuNotifier.cells); | ||
|
||
return await sp.setString("cells", cellsJson); | ||
final cellsJson = jsonEncode(_sudokuNotifier.cells); | ||
final actionsJson = jsonEncode(_sudokuNotifier.lastActions.toList()); | ||
|
||
final jobs = await Future.wait([ | ||
sp.setString("cells", cellsJson), | ||
sp.setString("actions", actionsJson), | ||
]); | ||
|
||
return jobs.first && jobs.last; | ||
} | ||
|
||
Future<List<Cell>?> load() async { | ||
Future<(List<Cell>?, Queue<Cell>?)> load() async { | ||
final sp = await SharedPreferences.getInstance(); | ||
|
||
List<Cell>? cells; | ||
Queue<Cell>? actions; | ||
|
||
final cellsJson = sp.getString("cells"); | ||
if (cellsJson == null) { | ||
return null; | ||
if (cellsJson != null) { | ||
final List<dynamic> cellsList = jsonDecode(cellsJson); | ||
cells = cellsList.map((e) => Cell.fromJson(e)).toList(); | ||
} | ||
//load history | ||
final actionsJson = sp.getString("actions"); | ||
if (actionsJson != null) { | ||
final List<dynamic> cellsList = jsonDecode(actionsJson); | ||
final actionList = cellsList.map<Cell>((e) => Cell.fromJson(e)); | ||
actions = Queue.from(actionList); | ||
} | ||
|
||
return (cells, actions); | ||
} | ||
|
||
final List<dynamic> cellsList = jsonDecode(cellsJson); | ||
return cellsList.map((e) => Cell.fromJson(e)).toList(); | ||
|
||
void clear() async { | ||
final sp = await SharedPreferences.getInstance(); | ||
await sp.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.