-
Notifications
You must be signed in to change notification settings - Fork 57
Replaced getApplicationDocumentsDirectory() with getApplicationSupportDirectory in io.dart #181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
06d06da
bddb860
f78df86
b88cf8e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -10,19 +10,28 @@ import 'package:path_provider/path_provider.dart'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class StoreImpl with Store { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
final bool storageJson; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
StoreImpl({this.storageJson = true}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
late final Future<void> _migrationCompleted; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
StoreImpl({this.storageJson = true}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Start migration immediately but don't block construction | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_migrationCompleted = _migrateFilesFromDocumentsToSupport(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Future get ready => Future.value(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Future<Map<String, dynamic>?> getPersisted(String key) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Future<Map<String, dynamic>?> getPersisted(String key) async { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!storageJson) return Future.value(null); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Ensure migration is complete before reading files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
await _migrationCompleted; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return _readFile(key); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Future get ready => Future.value(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Future setPersisted(String key, Map<String, dynamic> value) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Future setPersisted(String key, Map<String, dynamic> value) async { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!storageJson) return Future.value(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Ensure migration is complete before writing files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
await _migrationCompleted; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return _writeFile(key, value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -70,7 +79,7 @@ class StoreImpl with Store { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Future<String> _fileName(String fileKey) async { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
final path = (await _getDocumentDir()).path; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
final path = (await _getNewDocumentDir()).path; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return "$path/analytics-flutter-$fileKey.json"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -88,14 +97,60 @@ class StoreImpl with Store { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Future<Directory> _getDocumentDir() async { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Future<Directory> _getNewDocumentDir() async { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return await getApplicationSupportDirectory(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
throw PlatformNotSupportedError(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Future<Directory> _getOldDocumentDir() async { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return await getApplicationDocumentsDirectory(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
throw PlatformNotSupportedError(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// Migrates existing analytics files from Documents directory to Application Support directory | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Future<void> _migrateFilesFromDocumentsToSupport() async { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
final oldDir = await _getOldDocumentDir(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
final newDir = await _getNewDocumentDir(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// List all analytics files in the old directory | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
final oldDirFiles = oldDir.listSync() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.whereType<File>() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.where((file) => file.path.contains('analytics-flutter-') && file.path.endsWith('.json')) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.toList(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for (final oldFile in oldDirFiles) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
final fileName = oldFile.path.split('/').last; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using string splitting with hardcoded '/' is fragile across platforms. Use Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
final newFilePath = '${newDir.path}/$fileName'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
final newFile = File(newFilePath); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Only migrate if the file doesn't already exist in the new location | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!await newFile.exists()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Ensure the new directory exists | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
await newDir.create(recursive: true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Copy the file to the new location | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
await oldFile.copy(newFilePath); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Delete the old file after successful copy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
await oldFile.delete(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// The app should continue to work even if migration fails | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Migration failure shouldn't break the app | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+144
to
+150
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The catch block silently ignores all exceptions during file operations. Consider logging the error for debugging purposes while still allowing the app to continue.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback
Comment on lines
+145
to
+150
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The catch block silently ignores all exceptions during migration. Consider logging the error for debugging purposes while still allowing the app to continue.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void dispose() {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change modifies where application data is stored, which could be a breaking change for existing users. Consider implementing a migration strategy to move existing data from the documents directory to the application support directory, or provide a configuration option to maintain backward compatibility.
Copilot uses AI. Check for mistakes.