Line data Source code
1 : import 'package:cross_file/cross_file.dart';
2 : import 'package:flutter/foundation.dart';
3 : import 'platform_utils/platform_utils_io.dart' if (dart.library.js_interop) 'platform_utils_web.dart';
4 :
5 : /// Platform-specific utilities used throughout the package.
6 : ///
7 : /// Exposes a single `instance` accessor which delegates to a platform
8 : /// implementation selected via conditional imports (IO vs web).
9 : ///
10 : /// Use `PlatformUtils.instance` to perform file and directory operations
11 : /// in a platform-agnostic way.
12 : class PlatformUtils {
13 3 : static final _instance = PlatformUtilsFacadeImpl();
14 2 : static PlatformUtilsFacade get instance => _instance;
15 : }
16 :
17 : /// Facade interface implemented by platform-specific utilities.
18 : ///
19 : /// Implementations must provide safe, asynchronous file and directory
20 : /// operations suitable for the runtime platform (native IO or web).
21 : abstract class PlatformUtilsFacade {
22 : /// Deletes the given [XFile] from the underlying platform storage.
23 : ///
24 : /// Returns `true` when deletion succeeds, `false` otherwise.
25 : Future<bool> deleteFile(XFile file);
26 :
27 : /// Ensures the directory at [directoryPath] exists, creating it if needed.
28 : Future<void> createDirectoryIfNotExists(String directoryPath);
29 :
30 : /// Returns `true` if the directory exists.
31 : ///
32 : /// Marked `@visibleForTesting` because consumers should prefer the
33 : /// higher-level operations; exposed mainly to support unit tests.
34 : @visibleForTesting
35 : Future<bool> directoryExists(String directoryPath);
36 :
37 : /// Deletes the directory at [directoryPath] recursively.
38 : ///
39 : /// Marked `@visibleForTesting` for test helpers that need to clean up.
40 : @visibleForTesting
41 : Future<bool> deleteDirectory(String directoryPath);
42 :
43 : /// Returns `true` if the given [XFile] refers to an existing resource on
44 : /// the underlying platform (file system or web blob etc.).
45 : Future<bool> fileExists(XFile xFile);
46 : }
|