Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/cross_file/cross_file_platform_interface/AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Below is a list of people and organizations that have contributed
# to the Flutter project. Names should be added to the list like so:
#
# Name/Organization <email address>

Google LLC
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.1.0

* Initial release.
25 changes: 25 additions & 0 deletions packages/cross_file/cross_file_platform_interface/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright 2013 The Flutter Authors

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 changes: 21 additions & 0 deletions packages/cross_file/cross_file_platform_interface/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# cross_file_platform_interface

A common platform interface for the [`cross_file`](https://pub.dev/packages/cross_file) plugin.

This interface allows platform implementations of the `cross_file` plugin, as well as the plugin
itself, to ensure they are supporting the same interface.

# Usage

To implement a new platform implementation of `cross_file`, extend
[`CrossFilePlatform`](lib/src/cross_file_platform.dart) with an implementation that performs the
platform-specific behavior, and when you register your plugin, set the default
`CrossFilePlatform` by calling `CrossFilePlatform.instance = CrossFileMyPlatform()`.

# Note on breaking changes

Strongly prefer non-breaking changes (such as adding a method to the interface)
over breaking changes for this package.

See https://flutter.dev/go/platform-interface-breaking-changes for a discussion on why a less-clean
interface is preferable to a breaking change.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

export 'src/cross_file_platform.dart';
export 'src/file_system/platform_file_system_cross_directory.dart';
export 'src/file_system/platform_file_system_cross_file.dart';
export 'src/platform_cross_directory.dart';
export 'src/platform_cross_entity.dart';
export 'src/platform_cross_file.dart';
export 'src/scoped_storage/platform_scoped_storage_cross_directory.dart';
export 'src/scoped_storage/platform_scoped_storage_cross_file.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:convert';
import 'dart:typed_data';

import 'file_system/platform_file_system_cross_directory.dart';
import 'file_system/platform_file_system_cross_file.dart';
import 'platform_cross_directory.dart';
import 'platform_cross_entity.dart';
import 'scoped_storage/platform_scoped_storage_cross_directory.dart';
import 'scoped_storage/platform_scoped_storage_cross_file.dart';

/// Interface for a platform implementation of `cross_file`.
abstract base class CrossFilePlatform {
/// The instance of [CrossFilePlatform] to be used.
///
/// Platform implementations packages should set this with their own
/// implementation of [CrossFilePlatform] when they register themselves.
static CrossFilePlatform? instance;

/// Creates a new [PlatformFileSystemXFile].
PlatformFileSystemXFile createPlatformFileSystemXFile(
PlatformFileSystemXFileCreationParams params,
) {
return _DefaultFileSystemXFile(params);
}

/// Creates a new [PlatformFileSystemXDirectory].
PlatformFileSystemXDirectory createPlatformFileSystemXDirectory(
PlatformFileSystemXDirectoryCreationParams params,
) {
return _DefaultFileSystemXDirectory(params);
}

/// Creates a new [PlatformScopedStorageXFile].
PlatformScopedStorageXFile createPlatformScopedStorageXFile(
PlatformScopedStorageXFileCreationParams params,
) {
return _DefaultScopedStorageXFile(params);
}

/// Creates a new [PlatformScopedStorageXDirectory].
PlatformScopedStorageXDirectory createPlatformScopedStorageXDirectory(
PlatformScopedStorageXDirectoryCreationParams params,
) {
return _DefaultScopedStorageXDirectory(params);
}
}

/// Implementation of [PlatformFileSystemXFile] that represents a resource that
/// does not exist.
final class _DefaultFileSystemXFile extends PlatformFileSystemXFile {
_DefaultFileSystemXFile(super.params) : super.implementation();

@override
Future<bool> exists() async => false;

@override
Future<DateTime?> lastModified() async => null;

@override
Future<int?> length() async => null;

@override
Future<String?> name() async => null;

@override
Stream<Uint8List> openRead([int? start, int? end]) async* {
throw UnsupportedError('This instance does not represent any resource.');
}

@override
Future<Uint8List> readAsBytes() {
throw UnsupportedError('This instance does not represent any resource.');
}

@override
Future<String> readAsString({Encoding encoding = utf8}) {
throw UnsupportedError('This instance does not represent any resource.');
}

@override
Future<PlatformFileSystemXFile> writeAsBytes(PlatformWriteAsBytesParams params) {
throw UnsupportedError('This instance does not represent any resource.');
}
}

/// Implementation of [PlatformFileSystemXDirectory] that represents a directory
/// that does not exist.
final class _DefaultFileSystemXDirectory extends PlatformFileSystemXDirectory {
_DefaultFileSystemXDirectory(super.params) : super.implementation();

@override
Future<bool> exists() async => false;

@override
Stream<PlatformXEntity> list(PlatformListParams params) async* {
throw UnsupportedError('This instance does not represent any directory.');
}
}

/// Implementation of [PlatformScopedStorageXFile] that represents a resource
/// that does not exist.
final class _DefaultScopedStorageXFile extends PlatformScopedStorageXFile {
_DefaultScopedStorageXFile(super.params) : super.implementation();

@override
Future<bool> canRead() async => false;

@override
Future<bool> exists() async => false;

@override
Future<DateTime?> lastModified() async => null;

@override
Future<int?> length() async => null;

@override
Future<String?> name() async => null;

@override
Stream<Uint8List> openRead([int? start, int? end]) async* {
throw UnsupportedError('This instance does not represent any resource.');
}

@override
Future<Uint8List> readAsBytes() {
throw UnsupportedError('This instance does not represent any resource.');
}

@override
Future<String> readAsString({Encoding encoding = utf8}) {
throw UnsupportedError('This instance does not represent any resource.');
}

@override
Future<void> dispose() async {}
}

/// Implementation of [PlatformScopedStorageXDirectory] that represents a
/// directory that does not exist.
final class _DefaultScopedStorageXDirectory extends PlatformScopedStorageXDirectory {
_DefaultScopedStorageXDirectory(super.params) : super.implementation();

@override
Future<bool> exists() async => false;

@override
Future<bool> canRead() async => false;

@override
Stream<PlatformXEntity> list(PlatformListParams params) async* {
throw UnsupportedError('This instance does not represent any directory.');
}

@override
Future<void> dispose() async {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/foundation.dart';

import '../cross_file_platform.dart';
import '../platform_cross_directory.dart';

/// Object specifying creation parameters for creating a [PlatformFileSystemXDirectory].
///
/// Platform-specific implementations can add additional fields by extending
/// this class.
///
/// This example demonstrates how to extend the [PlatformFileSystemXDirectoryCreationParams] to
/// provide additional platform-specific parameters.
///
/// When extending [PlatformFileSystemXDirectoryCreationParams] additional parameters
/// should always accept `null` or have a default value to prevent breaking
/// changes.
///
/// ```dart
/// base class AndroidFileSystemXDirectoryCreationParams
/// extends PlatformFileSystemXDirectoryCreationParams {
/// AndroidFileSystemXDirectoryCreationParams({required super.uri, this.platformValue});
///
/// factory AndroidFileSystemXDirectoryCreationParams.fromCreationParams(
/// PlatformFileSystemXDirectoryCreationParams params, {
/// Object? platformValue,
/// }) {
/// return AndroidFileSystemXDirectoryCreationParams(
/// uri: params.uri,
/// platformValue: platformValue,
/// );
/// }
///
/// final Object? platformValue;
/// }
/// ```
@immutable
base class PlatformFileSystemXDirectoryCreationParams extends PlatformXDirectoryCreationParams {
/// Constructs a [PlatformFileSystemXDirectoryCreationParams].
PlatformFileSystemXDirectoryCreationParams(this.path)
: super(
uri: Uri.directory(
path,
windows: defaultTargetPlatform == TargetPlatform.windows,
).toString(),
);

/// The path of the directory.
final String path;
}

/// Base mixin used to provide platform-specific features for implementations of
/// [PlatformFileSystemXDirectory].
///
/// When providing platform specific features, platform implementations are
/// expected to declare a mixin that implements this mixin and return an
/// instance with [PlatformFileSystemXDirectory.extension].
///
/// ```dart
/// base class AndroidFileSystemXDirectory extends PlatformFileSystemXDirectory with AndroidFileSystemXDirectoryExtension {
/// // ...
/// @override
/// PlatformFileSystemXDirectoryExtension? get extension => this;
///
/// Future<void> platformMethod() {
/// // ...
/// }
/// }
///
/// mixin AndroidFileSystemXDirectoryExtension implements PlatformFileSystemXDirectoryExtension {
/// Future<void> platformMethod();
/// }
/// ```
mixin PlatformFileSystemXDirectoryExtension implements PlatformXDirectoryExtension {}

/// Interface for a reference to a directory (or folder) on the file system.
abstract base class PlatformFileSystemXDirectory extends PlatformXDirectory {
/// Creates a new [PlatformFileSystemXDirectory]
factory PlatformFileSystemXDirectory(PlatformFileSystemXDirectoryCreationParams params) {
assert(
CrossFilePlatform.instance != null,
'A platform implementation for `cross_file` has not been set. Please '
'ensure that an implementation of `CrossFilePlatform` has been set to '
'`CrossFilePlatform.instance` before use. For unit testing, '
'`CrossFilePlatform.instance` can be set with your own test implementation.',
);
return CrossFilePlatform.instance!.createPlatformFileSystemXDirectory(params);
}

/// Used by the platform implementation to create a new
/// [PlatformFileSystemXDirectory].
///
/// Should only be used by platform implementations because they can't extend
/// a class that only contains a factory constructor.
@protected
PlatformFileSystemXDirectory.implementation(
PlatformFileSystemXDirectoryCreationParams super.params,
);
Comment thread
bparrishMines marked this conversation as resolved.

@override
PlatformFileSystemXDirectoryCreationParams get params =>
super.params as PlatformFileSystemXDirectoryCreationParams;

/// Extension for providing platform-specific features.
@override
PlatformFileSystemXDirectoryExtension? get extension => null;
}
Loading
Loading