Skip to content

Commit

Permalink
Picking incompatible changes to web_adapter v1 (#2362)
Browse files Browse the repository at this point in the history
Further implementation of #2281
  • Loading branch information
AlexV525 authored Jan 24, 2025
2 parents a183db7 + 158046e commit ed93e58
Show file tree
Hide file tree
Showing 112 changed files with 3,458 additions and 247 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ on:
branches:
- main
- '6.0.0'
- 'feat/web/v1-adapter'
paths-ignore:
- "**.md"
pull_request:
branches:
- main
- '6.0.0'
- 'feat/web/v1-adapter'
paths-ignore:
- "**.md"

Expand Down Expand Up @@ -93,7 +95,13 @@ jobs:
run: melos run test:web:firefox
- name: '[Verify step] Test Flutter packages'
run: melos run test:flutter
- uses: actions/setup-java@v4
if: ${{ matrix.sdk == 'stable' }}
with:
distribution: 'adopt'
java-version: '17'
- name: '[Verify step] Build Flutter APK'
if: ${{ matrix.sdk == 'stable' }}
run: melos run build:example:apk
# Coverage
- name: '[Coverage] Format & print test coverage'
Expand Down
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ linter:
unnecessary_await_in_return: true
unnecessary_breaks: true
unnecessary_late: true
unnecessary_library_name: false
unnecessary_parenthesis: true
15 changes: 14 additions & 1 deletion dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,20 @@ See the [Migration Guide][] for the complete breaking changes list.**

## Unreleased

*None.*
- Update comments and strings with `MultipartFile`.
- Removes redundant warnings when composing request options on Web.
- Fixes boundary inconsistency in `FormData.clone()`.
- Support `FileAccessMode` in `Dio.download` and `Dio.downloadUri` to change download file opening mode

## 5.7.0

- Graceful handling of responses with nonzero `Content-Length`, `Content-Type` that is json, and empty payload.
- Empty responses are now transformed to `null`.

## 5.6.0

- Supports the WASM environment. Users should upgrade the adapter with
`dart pub upgrade` or `flutter pub upgrade` to use the WASM-supported version.

## 5.5.0+1

Expand Down
6 changes: 6 additions & 0 deletions dio/lib/src/dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ abstract class Dio {
/// [deleteOnError] whether delete the file when error occurs.
/// The default value is [true].
///
/// [fileAccessMode]
/// {@macro dio.options.FileAccessMode}
///
/// [lengthHeader] : The real size of original file (not compressed).
/// When file is compressed:
/// 1. If this value is 'content-length', the `total` argument of
Expand Down Expand Up @@ -241,6 +244,7 @@ abstract class Dio {
Map<String, dynamic>? queryParameters,
CancelToken? cancelToken,
bool deleteOnError = true,
FileAccessMode fileAccessMode = FileAccessMode.write,
String lengthHeader = Headers.contentLengthHeader,
Object? data,
Options? options,
Expand All @@ -253,6 +257,7 @@ abstract class Dio {
ProgressCallback? onReceiveProgress,
CancelToken? cancelToken,
bool deleteOnError = true,
FileAccessMode fileAccessMode = FileAccessMode.write,
String lengthHeader = Headers.contentLengthHeader,
Object? data,
Options? options,
Expand All @@ -265,6 +270,7 @@ abstract class Dio {
deleteOnError: deleteOnError,
cancelToken: cancelToken,
data: data,
fileAccessMode: fileAccessMode,
options: options,
);
}
Expand Down
7 changes: 6 additions & 1 deletion dio/lib/src/dio/dio_for_native.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class DioForNative with DioMixin implements Dio {
Map<String, dynamic>? queryParameters,
CancelToken? cancelToken,
bool deleteOnError = true,
FileAccessMode fileAccessMode = FileAccessMode.write,
String lengthHeader = Headers.contentLengthHeader,
Object? data,
Options? options,
Expand Down Expand Up @@ -95,7 +96,11 @@ class DioForNative with DioMixin implements Dio {
// Shouldn't call file.writeAsBytesSync(list, flush: flush),
// because it can write all bytes by once. Consider that the file is
// a very big size (up to 1 Gigabytes), it will be expensive in memory.
RandomAccessFile raf = file.openSync(mode: FileMode.write);
RandomAccessFile raf = file.openSync(
mode: fileAccessMode == FileAccessMode.write
? FileMode.write
: FileMode.append,
);

// Create a Completer to notify the success/error state.
final completer = Completer<Response>();
Expand Down
3 changes: 3 additions & 0 deletions dio/lib/src/dio_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ abstract class DioMixin implements Dio {
ProgressCallback? onReceiveProgress,
CancelToken? cancelToken,
bool deleteOnError = true,
FileAccessMode fileAccessMode = FileAccessMode.write,
String lengthHeader = Headers.contentLengthHeader,
Object? data,
Options? options,
Expand All @@ -297,6 +298,7 @@ abstract class DioMixin implements Dio {
deleteOnError: deleteOnError,
cancelToken: cancelToken,
data: data,
fileAccessMode: fileAccessMode,
options: options,
);
}
Expand All @@ -309,6 +311,7 @@ abstract class DioMixin implements Dio {
Map<String, dynamic>? queryParameters,
CancelToken? cancelToken,
bool deleteOnError = true,
FileAccessMode fileAccessMode = FileAccessMode.write,
String lengthHeader = Headers.contentLengthHeader,
Object? data,
Options? options,
Expand Down
14 changes: 14 additions & 0 deletions dio/lib/src/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -767,3 +767,17 @@ class _RequestConfig {
ResponseDecoder? responseDecoder;
late ListFormat listFormat;
}

/// {@template dio.options.FileAccessMode}
/// The file access mode when downloading a file, corresponds to a subset of
/// dart:io::[FileMode].
/// {@endtemplate}
enum FileAccessMode {
/// Mode for opening a file for reading and writing. The file is overwritten
/// if it already exists. The file is created if it does not already exist.
write,

/// Mode for opening a file for reading and writing to the end of it.
/// The file is created if it does not already exist.
append,
}
2 changes: 1 addition & 1 deletion dio_test/lib/src/test/basic_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void basicTests(
group('basic request', () {
test(
'works with non-TLS requests',
() => dio.get('http://neverssl.com/'),
() => dio.get('http://flutter-io.cn/'),
testOn: 'vm',
);

Expand Down
49 changes: 49 additions & 0 deletions dio_test/lib/src/test/download_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,55 @@ void downloadTests(
completes,
);
});

test('append bytes to previous download', () async {
final cancelToken = CancelToken();
final path = p.join(tmp.path, 'download_3.txt');
final requestedBytes = 1024 * 1024 * 10;
int recievedBytes1 = 0;
await expectLater(
dio.download(
'/bytes/$requestedBytes',
path,
cancelToken: cancelToken,
onReceiveProgress: (c, t) {
if (c > 5000) {
recievedBytes1 = c;
cancelToken.cancel();
}
},
deleteOnError: false,
),
throwsDioException(
DioExceptionType.cancel,
stackTraceContains: 'test/download_tests.dart',
),
);

final cancelToken2 = CancelToken();
int recievedBytes2 = 0;
expectLater(
dio.download(
'/bytes/$requestedBytes',
path,
cancelToken: cancelToken,
onReceiveProgress: (c, t) {
recievedBytes2 = c;
if (c > 5000) {
cancelToken2.cancel();
}
},
deleteOnError: false,
fileAccessMode: FileAccessMode.append,
),
throwsDioException(
DioExceptionType.cancel,
stackTraceContains: 'test/download_tests.dart',
),
);
await Future.delayed(const Duration(milliseconds: 100), () {});
expect(File(path).lengthSync(), recievedBytes1 + recievedBytes2);
});
},
testOn: 'vm',
);
Expand Down
39 changes: 37 additions & 2 deletions example_flutter_app/.metadata
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,42 @@
# This file should be version controlled and should not be manually edited.

version:
revision: 60bd88df915880d23877bfc1602e8ddcf4c4dd2a
channel: beta
revision: "8495dee1fd4aacbe9de707e7581203232f591b2f"
channel: "stable"

project_type: app

# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
base_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
- platform: android
create_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
base_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
- platform: ios
create_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
base_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
- platform: linux
create_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
base_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
- platform: macos
create_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
base_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
- platform: web
create_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
base_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
- platform: windows
create_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
base_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f

# User provided section

# List of Local paths (relative to this file) that should be
# ignored by the migrate tool.
#
# Files that are not part of the templates will be ignored by default.
unmanaged_files:
- 'lib/main.dart'
- 'ios/Runner.xcodeproj/project.pbxproj'
4 changes: 3 additions & 1 deletion example_flutter_app/android/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ gradle-wrapper.jar
GeneratedPluginRegistrant.java

# Remember to never publicly share your keystore.
# See https://flutter.dev/docs/deployment/android#reference-the-keystore-from-the-app
# See https://flutter.dev/to/reference-keystore
key.properties
**/*.keystore
**/*.jks
57 changes: 26 additions & 31 deletions example_flutter_app/android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,49 +1,44 @@
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
plugins {
id "com.android.application"
id "kotlin-android"
// The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
id "dev.flutter.flutter-gradle-plugin"
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'
}
android {
namespace = "cn.flutter.dio_flutter_example"
compileSdk = flutter.compileSdkVersion
ndkVersion = flutter.ndkVersion

apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

android {
compileSdkVersion 33
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
}

defaultConfig {
applicationId "com.example.flutterApp"
minSdkVersion 16
targetSdkVersion 33
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId = "cn.flutter.dio_flutter_example"
// You can update the following values to match your application needs.
// For more information, see: https://flutter.dev/to/review-gradle-config.
minSdk = flutter.minSdkVersion
targetSdk = flutter.targetSdkVersion
versionCode = flutter.versionCode
versionName = flutter.versionName
}

buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
signingConfig = signingConfigs.debug
}
}
}

flutter {
source '../..'
source = "../.."
}
Loading

0 comments on commit ed93e58

Please sign in to comment.