-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refacted fileStore dart implementation
- Loading branch information
1 parent
6f426ee
commit 5085ed1
Showing
6 changed files
with
77 additions
and
44 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
13 changes: 13 additions & 0 deletions
13
packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/file_storage/file_storage.dart
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export 'file_storage.vm.dart' if (dart.library.html) 'file_storage.web.dart'; | ||
|
||
/// File storage interface for saving and loading constraint locally | ||
abstract interface class FileStorage { | ||
/// Save constraint locally to file | ||
Future<void> saveConstraintLocally(String filename, String data); | ||
|
||
/// Load constraint from file | ||
Future<String?> loadConstraint(String filename); | ||
} |
41 changes: 23 additions & 18 deletions
41
packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/file_storage/file_storage.vm.dart
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,29 +1,34 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:amplify_core/amplify_core.dart'; | ||
import 'package:aws_logging_cloudwatch/src/file_storage/file_storage.dart'; | ||
import 'package:path/path.dart' as p; | ||
import 'package:path_provider/path_provider.dart'; | ||
|
||
/// Save constraint locally to file | ||
Future<void> saveConstraintLocally(Map<String, dynamic> constraint) async { | ||
final directory = await getApplicationSupportDirectory(); | ||
final path = directory.path; | ||
final file = File(p.join(path, 'logging_constraint.json')); | ||
await file.writeAsString(jsonEncode(constraint)); | ||
} | ||
/// File storage implementation for saving and loading constraint locally | ||
final class FileStorageImpl implements FileStorage { | ||
/// File storage implementation for saving and loading constraint locally | ||
FileStorageImpl(this.pathProvider); | ||
|
||
/// Path provider to get the application support path | ||
final AppPathProvider pathProvider; | ||
|
||
/// Load constraint from file | ||
Future<Map<String, dynamic>?> loadConstraint() async { | ||
final directory = await getApplicationSupportDirectory(); | ||
final path = directory.path; | ||
final file = File('$path/logging_constraint.json'); | ||
@override | ||
Future<String?> loadConstraint(String fileName) async { | ||
final file = | ||
File(p.join(await pathProvider.getApplicationSupportPath(), fileName)); | ||
if (await file.exists()) { | ||
return file.readAsString(); | ||
} | ||
return null; | ||
} | ||
|
||
if (file.existsSync()) { | ||
final content = await file.readAsString(); | ||
return jsonDecode(content) as Map<String, dynamic>; | ||
@override | ||
Future<void> saveConstraintLocally(String fileName, String content) async { | ||
final file = | ||
File(p.join(await pathProvider.getApplicationSupportPath(), fileName)); | ||
await file.writeAsString(content); | ||
} | ||
return null; | ||
} |
30 changes: 17 additions & 13 deletions
30
...ages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/file_storage/file_storage.web.dart
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,22 +1,26 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import 'dart:convert'; | ||
import 'dart:html'; | ||
|
||
// Declare a constant global private variable for the key | ||
const String _localStorageKey = 'aws.cloudwatch.logging_constraint'; | ||
import 'package:amplify_core/amplify_core.dart'; | ||
import 'package:aws_logging_cloudwatch/src/file_storage/file_storage.dart'; | ||
|
||
/// Save constraint locally to web storage | ||
Future<void> saveConstraintLocally(Map<String, dynamic> constraint) async { | ||
window.localStorage[_localStorageKey] = jsonEncode(constraint); | ||
} | ||
/// File storage implementation for saving and loading constraint locally | ||
final class FileStorageImpl implements FileStorage { | ||
/// File storage implementation for saving and loading constraint locally | ||
// ignore: avoid_unused_constructor_parameters | ||
FileStorageImpl(AppPathProvider pathProvider); | ||
|
||
static const _prefix = 'aws.cloudwatch'; | ||
|
||
@override | ||
Future<String?> loadConstraint(String fileName) async { | ||
return window.localStorage['$_prefix.$fileName']; | ||
} | ||
|
||
/// Load constraint from web storage | ||
Map<String, dynamic>? loadConstraint() { | ||
final content = window.localStorage[_localStorageKey]; | ||
if (content != null) { | ||
return jsonDecode(content) as Map<String, dynamic>; | ||
@override | ||
Future<void> saveConstraintLocally(String fileName, String content) async { | ||
window.localStorage['$_prefix.$fileName'] = content; | ||
} | ||
return null; | ||
} |
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