-
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.
feat: add methods for saving unfinished puzzle
- Loading branch information
1 parent
81b15e9
commit a23d280
Showing
10 changed files
with
256 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,36 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:flutter/widgets.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
import 'package:sudoku/api/api.dart'; | ||
import 'package:sudoku/app/app.dart'; | ||
import 'package:sudoku/bootstrap.dart'; | ||
import 'package:sudoku/cache/cache.dart'; | ||
import 'package:sudoku/env/env.dart'; | ||
import 'package:sudoku/repository/repository.dart'; | ||
import 'package:sudoku/storage/storage.dart'; | ||
|
||
void main() async { | ||
WidgetsFlutterBinding.ensureInitialized(); | ||
|
||
unawaited( | ||
bootstrap(() async { | ||
final apiClient = SudokuDioClient(baseUrl: Env.apiBaseUrl); | ||
final cacheClient = CacheClient(); | ||
|
||
void main() { | ||
bootstrap(() { | ||
final apiClient = SudokuDioClient(baseUrl: Env.apiBaseUrl); | ||
final storageClient = LocalStorageClient( | ||
plugin: await SharedPreferences.getInstance(), | ||
); | ||
|
||
final cacheClient = CacheClient(); | ||
final puzzleRepository = PuzzleRepository(cacheClient: cacheClient); | ||
final puzzleRepository = PuzzleRepository( | ||
cacheClient: cacheClient, | ||
storageClient: storageClient, | ||
); | ||
|
||
return App( | ||
apiClient: apiClient, | ||
puzzleRepository: puzzleRepository, | ||
); | ||
}); | ||
return App( | ||
apiClient: apiClient, | ||
puzzleRepository: puzzleRepository, | ||
); | ||
}), | ||
); | ||
} |
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,20 +1,36 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:flutter/widgets.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
import 'package:sudoku/api/api.dart'; | ||
import 'package:sudoku/app/app.dart'; | ||
import 'package:sudoku/bootstrap.dart'; | ||
import 'package:sudoku/cache/cache.dart'; | ||
import 'package:sudoku/env/env.dart'; | ||
import 'package:sudoku/repository/repository.dart'; | ||
import 'package:sudoku/storage/storage.dart'; | ||
|
||
void main() async { | ||
WidgetsFlutterBinding.ensureInitialized(); | ||
|
||
unawaited( | ||
bootstrap(() async { | ||
final apiClient = SudokuDioClient(baseUrl: Env.apiBaseUrl); | ||
final cacheClient = CacheClient(); | ||
|
||
void main() { | ||
bootstrap(() { | ||
final apiClient = SudokuDioClient(baseUrl: Env.apiBaseUrl); | ||
final storageClient = LocalStorageClient( | ||
plugin: await SharedPreferences.getInstance(), | ||
); | ||
|
||
final cacheClient = CacheClient(); | ||
final puzzleRepository = PuzzleRepository(cacheClient: cacheClient); | ||
final puzzleRepository = PuzzleRepository( | ||
cacheClient: cacheClient, | ||
storageClient: storageClient, | ||
); | ||
|
||
return App( | ||
apiClient: apiClient, | ||
puzzleRepository: puzzleRepository, | ||
); | ||
}); | ||
return App( | ||
apiClient: apiClient, | ||
puzzleRepository: puzzleRepository, | ||
); | ||
}), | ||
); | ||
} |
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,20 +1,36 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:flutter/widgets.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
import 'package:sudoku/api/api.dart'; | ||
import 'package:sudoku/app/app.dart'; | ||
import 'package:sudoku/bootstrap.dart'; | ||
import 'package:sudoku/cache/cache.dart'; | ||
import 'package:sudoku/env/env.dart'; | ||
import 'package:sudoku/repository/repository.dart'; | ||
import 'package:sudoku/storage/storage.dart'; | ||
|
||
void main() async { | ||
WidgetsFlutterBinding.ensureInitialized(); | ||
|
||
unawaited( | ||
bootstrap(() async { | ||
final apiClient = SudokuDioClient(baseUrl: Env.apiBaseUrl); | ||
final cacheClient = CacheClient(); | ||
|
||
void main() { | ||
bootstrap(() { | ||
final apiClient = SudokuDioClient(baseUrl: Env.apiBaseUrl); | ||
final storageClient = LocalStorageClient( | ||
plugin: await SharedPreferences.getInstance(), | ||
); | ||
|
||
final cacheClient = CacheClient(); | ||
final puzzleRepository = PuzzleRepository(cacheClient: cacheClient); | ||
final puzzleRepository = PuzzleRepository( | ||
cacheClient: cacheClient, | ||
storageClient: storageClient, | ||
); | ||
|
||
return App( | ||
apiClient: apiClient, | ||
puzzleRepository: puzzleRepository, | ||
); | ||
}); | ||
return App( | ||
apiClient: apiClient, | ||
puzzleRepository: puzzleRepository, | ||
); | ||
}), | ||
); | ||
} |
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,35 +1,55 @@ | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:sudoku/cache/cache.dart'; | ||
import 'package:sudoku/puzzle/puzzle.dart'; | ||
import 'package:sudoku/storage/storage.dart'; | ||
|
||
/// {@template puzzle_repository} | ||
/// A repository that handles `puzzle` related data. | ||
/// | ||
/// Used to pass data from home page to puzzle page. | ||
/// Used to pass data from home page to puzzle page, and to save and | ||
/// retrieve unfinished puzzle from local memory. | ||
/// {@endtemplate} | ||
class PuzzleRepository { | ||
/// {@macro puzzle_repository} | ||
const PuzzleRepository({ | ||
required CacheClient cacheClient, | ||
}) : _cacheClient = cacheClient; | ||
required StorageAPI storageClient, | ||
}) : _cacheClient = cacheClient, | ||
_storageClient = storageClient; | ||
|
||
final CacheClient _cacheClient; | ||
|
||
/// The key used for storing the puzzle in-memory. | ||
final StorageAPI _storageClient; | ||
|
||
/// The key used for storing the puzzle in cache. | ||
/// | ||
/// This is only exposed for testing and shouldn't be used by consumers of | ||
/// this library. | ||
@visibleForTesting | ||
static const kPuzzleKey = '__puzzle_key__'; | ||
|
||
/// Provides the puzzle stored in-memory. | ||
/// Provides the puzzle stored in cache. | ||
/// | ||
/// Returns null, if there is no puzzle. | ||
Puzzle? getPuzzle() => _cacheClient.read<Puzzle>(key: kPuzzleKey); | ||
Puzzle? fetchPuzzleFromCache() => _cacheClient.read<Puzzle>(key: kPuzzleKey); | ||
|
||
/// Saves a puzzle in-memory. | ||
/// Saves a puzzle in cache. | ||
/// | ||
/// If there's already a puzzle there, it will be replaced. | ||
void storePuzzle({required Puzzle puzzle}) => | ||
void savePuzzleToCache({required Puzzle puzzle}) => | ||
_cacheClient.write<Puzzle>(key: kPuzzleKey, value: puzzle); | ||
|
||
/// Emits the puzzle stored in local storage. If there's not puzzle, | ||
/// it simply emits null. | ||
Stream<Puzzle?> getPuzzleFromLocalMemory() => _storageClient.getPuzzle(); | ||
|
||
/// Stores the puzzle in local storage. The aim is to save unfinished puzzle. | ||
Future<void> storePuzzleInLocalMemory({required Puzzle puzzle}) => | ||
_storageClient.storePuzzle(puzzle: puzzle); | ||
|
||
/// Clears out the local storage, after an unfinished puzzle is completed. | ||
Future<void> clearPuzzleInLocalMemory() => _storageClient.clearPuzzleStore(); | ||
|
||
/// Closed any resources used during the above operations. | ||
Future<void> close() => _storageClient.close(); | ||
} |
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
Oops, something went wrong.