Skip to content

Commit 5bbfdf7

Browse files
committed
LocalPersist/LocalJsonPersist.findBaseDirectory
1 parent a2bd905 commit 5bbfdf7

File tree

5 files changed

+93
-18
lines changed

5 files changed

+93
-18
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ the <a href="https://github.com/marcglasberg/SameAppDifferentTech/blob/main/Mobi
33
Async Redux App Example GitHub Repo</a> for a full-fledged example app showcasing the fundamentals
44
and best practices._
55

6+
# 23.3.0-dev.1
7+
8+
* `LocalPersist` and `LocalJsonPersist` now allow you to define the base directory by setting
9+
the `findBaseDirectory` static field. The default is, as before, the application's documents
10+
directory. Other options are the cache directory, the downloads directory, or another custom
11+
directory.
12+
613
# 23.2.0
714

815
* You can now use the `UnlimitedRetryCheckInternet` to check if there is internet when you run some

lib/src/global_wrap_error.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'package:async_redux/async_redux.dart';
99
/// `UserException`). Then:
1010
/// * If it returns the same [error] unaltered, this original error will be used.
1111
/// * If it returns something else, that it will be used instead of the original [error].
12-
/// * It it returns `null`, the original error will be disabled (swallowed).
12+
/// * If it returns `null`, the original error will be disabled (swallowed).
1313
///
1414
/// IMPORTANT: If instead of RETURNING an error you THROW an error inside the `wrap` function,
1515
/// AsyncRedux will catch this error and use it instead the original error. In other

lib/src/local_json_persist.dart

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,27 @@ class LocalJsonPersist {
5252
/// Make it an empty string to remove it.
5353
static String defaultDbSubDir = "db";
5454

55+
/// If running from Flutter, the default base directory is the application's documents dir.
56+
/// If running from tests (detected by the `LocalFileSystem` not being present),
57+
/// it will use the system's temp directory.
58+
///
59+
/// You can change this variable to globally change the directory:
60+
/// ```
61+
/// // Will use the application's cache directory.
62+
/// findDirectoryToUse = LocalPersist.findAppCacheDir;
63+
///
64+
/// // Will use the application's downloads directory.
65+
/// findDirectoryToUse = LocalPersist.findAppDownloadsDir;
66+
/// ```
67+
///
68+
static Future<void> Function() findBaseDirectory = findAppDocDir;
69+
5570
/// The default is adding a ".json" termination to the file name.
5671
static const String jsonTermination = ".json";
5772

58-
static Directory? get appDocDir => _appDocDir;
73+
static Directory? get appDocDir => _baseDirectory;
5974

60-
static Directory? get _appDocDir => LocalPersist.appDocDir;
75+
static Directory? get _baseDirectory => LocalPersist.appDocDir;
6176

6277
static f.FileSystem get _fileSystem => LocalPersist.getFileSystem();
6378

@@ -106,6 +121,18 @@ class LocalJsonPersist {
106121
_file = file,
107122
_fileSystemRef = _fileSystem;
108123

124+
/// If running from Flutter, this will get the application's documents directory.
125+
/// If running from tests, it will use the system's temp directory.
126+
static Future<void> findAppDocDir() => LocalPersist.findAppDocDir();
127+
128+
/// If running from Flutter, this will get the application's cache directory.
129+
/// If running from tests, it will use the system's temp directory.
130+
static Future<void> findAppCacheDir() => LocalPersist.findAppCacheDir();
131+
132+
/// If running from Flutter, this will get the application's downloads directory.
133+
/// If running from tests, it will use the system's temp directory.
134+
static Future<void> findAppDownloadsDir() => LocalPersist.findAppDownloadsDir();
135+
109136
/// Saves the given simple object as JSON.
110137
/// If the file exists, it will be overwritten.
111138
Future<File> save(Object? simpleObj) async {
@@ -312,7 +339,7 @@ class LocalJsonPersist {
312339
if (_file != null)
313340
return _file!;
314341
else {
315-
if (_appDocDir == null) await _findAppDocDir();
342+
if (_baseDirectory == null) await findBaseDirectory();
316343
String pathNameStr = pathName(
317344
dbName,
318345
dbSubDir: dbSubDir,
@@ -334,7 +361,7 @@ class LocalJsonPersist {
334361
List<String>? subDirs,
335362
}) {
336363
return p.joinAll([
337-
LocalJsonPersist._appDocDir!.path,
364+
LocalJsonPersist._baseDirectory!.path,
338365
dbSubDir ?? LocalJsonPersist.defaultDbSubDir,
339366
if (subDirs != null) ...subDirs,
340367
"$dbName${LocalJsonPersist.jsonTermination}"
@@ -344,10 +371,6 @@ class LocalJsonPersist {
344371
static String _getStringFromEnum(Object dbName) =>
345372
(dbName is String) ? dbName : dbName.toString().split(".").last;
346373

347-
/// If running from Flutter, this will get the application's documents directory.
348-
/// If running from tests, it will use the system's temp directory.
349-
static Future<void> _findAppDocDir() async => LocalPersist.findAppDocDir();
350-
351374
/// Decodes a single JSON into a simple object, from the given [bytes].
352375
static Object? decodeJson(Uint8List bytes) {
353376
ByteBuffer buffer = bytes.buffer;

lib/src/local_persist.dart

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,27 @@ class LocalPersist {
8686
/// Make it an empty string to remove it.
8787
static String defaultDbSubDir = "db";
8888

89+
/// If running from Flutter, the default base directory is the application's documents dir.
90+
/// If running from tests (detected by the `LocalFileSystem` not being present),
91+
/// it will use the system's temp directory.
92+
///
93+
/// You can change this variable to globally change the directory:
94+
/// ```
95+
/// // Will use the application's cache directory.
96+
/// findDirectoryToUse = LocalPersist.findAppCacheDir;
97+
///
98+
/// // Will use the application's downloads directory.
99+
/// findDirectoryToUse = LocalPersist.findAppDownloadsDir;
100+
/// ```
101+
///
102+
static Future<void> Function() findBaseDirectory = LocalPersist.findAppDocDir;
103+
89104
/// The default is adding a ".db" termination to the file name.
90105
/// This is not final, so you can change it.
91106
static String defaultTermination = ".db";
92107

93-
static Directory? get appDocDir => _appDocDir;
94-
static Directory? _appDocDir;
108+
static Directory? get appDocDir => _baseDirectory;
109+
static Directory? _baseDirectory;
95110

96111
// In a json sequence, each object may have at most 65.536 bytes.
97112
// Note this refers to a single json object, not to the total json sequence file,
@@ -297,7 +312,7 @@ class LocalPersist {
297312
if (_file != null)
298313
return _file!;
299314
else {
300-
if (_appDocDir == null) await findAppDocDir();
315+
if (_baseDirectory == null) await findBaseDirectory();
301316
String pathNameStr = pathName(
302317
dbName,
303318
dbSubDir: dbSubDir,
@@ -319,7 +334,7 @@ class LocalPersist {
319334
List<String>? subDirs,
320335
}) {
321336
return p.joinAll([
322-
LocalPersist._appDocDir!.path,
337+
LocalPersist._baseDirectory!.path,
323338
dbSubDir ?? LocalPersist.defaultDbSubDir,
324339
if (subDirs != null) ...subDirs,
325340
"$dbName${LocalPersist.defaultTermination}"
@@ -332,16 +347,46 @@ class LocalPersist {
332347
/// If running from Flutter, this will get the application's documents directory.
333348
/// If running from tests, it will use the system's temp directory.
334349
static Future<void> findAppDocDir() async {
335-
if (_appDocDir != null) return;
350+
if (_baseDirectory != null) return;
351+
352+
if (_fileSystem == const LocalFileSystem()) {
353+
try {
354+
_baseDirectory = await getApplicationDocumentsDirectory();
355+
} on MissingPluginException catch (_) {
356+
_baseDirectory = const LocalFileSystem().systemTempDirectory;
357+
}
358+
} else
359+
_baseDirectory = _fileSystem.systemTempDirectory;
360+
}
361+
362+
/// If running from Flutter, this will get the application's cache directory.
363+
/// If running from tests, it will use the system's temp directory.
364+
static Future<void> findAppCacheDir() async {
365+
if (_baseDirectory != null) return;
366+
367+
if (_fileSystem == const LocalFileSystem()) {
368+
try {
369+
_baseDirectory = await getApplicationCacheDirectory();
370+
} on MissingPluginException catch (_) {
371+
_baseDirectory = const LocalFileSystem().systemTempDirectory;
372+
}
373+
} else
374+
_baseDirectory = _fileSystem.systemTempDirectory;
375+
}
376+
377+
/// If running from Flutter, this will get the application's downloads directory.
378+
/// If running from tests, it will use the system's temp directory.
379+
static Future<void> findAppDownloadsDir() async {
380+
if (_baseDirectory != null) return;
336381

337382
if (_fileSystem == const LocalFileSystem()) {
338383
try {
339-
_appDocDir = await getApplicationDocumentsDirectory();
384+
_baseDirectory = await getDownloadsDirectory();
340385
} on MissingPluginException catch (_) {
341-
_appDocDir = const LocalFileSystem().systemTempDirectory;
386+
_baseDirectory = const LocalFileSystem().systemTempDirectory;
342387
}
343388
} else
344-
_appDocDir = _fileSystem.systemTempDirectory;
389+
_baseDirectory = _fileSystem.systemTempDirectory;
345390
}
346391

347392
static Uint8List encode(List<Object> simpleObjs) {

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: async_redux
22
description: The modern version of Redux. State management that's simple to learn and easy to use; Powerful enough to handle complex applications with millions of users; Testable.
3-
version: 23.2.0
3+
version: 23.3.0-dev.1
44
# author: Marcelo Glasberg <marcglasberg@gmail.com>
55
homepage: https://github.com/marcglasberg/async_redux
66
topics:

0 commit comments

Comments
 (0)