From 3eccb0bd4aec57f35340c82d1aa8e0459b7c5141 Mon Sep 17 00:00:00 2001 From: Rico Berger Date: Tue, 6 Aug 2024 17:05:26 +0200 Subject: [PATCH] [core] Fix Web Build (#185) Fix web build be initializing `JustAudioMediaKit` only on Linux and Windows where it is really used. The corresponding issue for the broken web build can be found here https://github.com/Pato05/just_audio_media_kit/issues/15. To test the web build on every PR to avoid such issues, the CI/CD pipeline for the web build is now also run on every PR. On PRs we skip the upload to Cloudflare for the web build. --- .github/workflows/continuous-delivery.yaml | 9 ++++++--- app/lib/main.dart | 6 ++++-- .../item_audio_player_init.dart | 11 +++++++++++ .../item_audio_player_init_native.dart | 12 ++++++++++++ .../item_audio_player_init_stub.dart | 5 +++++ .../item_audio_player_init_web.dart | 8 ++++++++ 6 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 app/lib/widgets/item/details/utils/item_audio_palyer/item_audio_player_init/item_audio_player_init.dart create mode 100644 app/lib/widgets/item/details/utils/item_audio_palyer/item_audio_player_init/item_audio_player_init_native.dart create mode 100644 app/lib/widgets/item/details/utils/item_audio_palyer/item_audio_player_init/item_audio_player_init_stub.dart create mode 100644 app/lib/widgets/item/details/utils/item_audio_palyer/item_audio_player_init/item_audio_player_init_web.dart diff --git a/.github/workflows/continuous-delivery.yaml b/.github/workflows/continuous-delivery.yaml index a4fcf02..621eb1f 100644 --- a/.github/workflows/continuous-delivery.yaml +++ b/.github/workflows/continuous-delivery.yaml @@ -122,12 +122,14 @@ jobs: supabase functions deploy stripe-create-checkout-session-v1 --project-ref $PROJECT_ID --import-map supabase/functions/import_map.json supabase functions deploy stripe-webhooks-v1 --no-verify-jwt --project-ref $PROJECT_ID --import-map supabase/functions/import_map.json - # The "Web" job builds the Flutter web app and publishes it to Cloudflare Pages. The job only runs when a commit is - # pushed to the main branch or a new tag is created. + # The "Web" job builds the Flutter web app and publishes it to Cloudflare Pages. The job only runs on pull requests or + # when a commit is pushed to the main branch or a new tag is created. + # + # When the job runs on a pull request it only builds the app but doesn't upload the build to Cloudflare. web: name: Web runs-on: ubuntu-latest - if: github.ref == 'refs/heads/main' || (github.event_name == 'release' && github.event.action == 'published') + if: github.event_name == 'pull_request' || github.ref == 'refs/heads/main' || (github.event_name == 'release' && github.event.action == 'published') permissions: contents: read defaults: @@ -185,6 +187,7 @@ jobs: - name: Publish to Cloudflare Pages uses: cloudflare/pages-action@v1 + if: github.ref == 'refs/heads/main' || (github.event_name == 'release' && github.event.action == 'published') with: apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} diff --git a/app/lib/main.dart b/app/lib/main.dart index 22d37d3..5ade0ef 100644 --- a/app/lib/main.dart +++ b/app/lib/main.dart @@ -7,7 +7,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_native_splash/flutter_native_splash.dart'; import 'package:flutter_web_plugins/url_strategy.dart'; import 'package:just_audio_background/just_audio_background.dart'; -import 'package:just_audio_media_kit/just_audio_media_kit.dart'; import 'package:media_kit/media_kit.dart'; import 'package:provider/provider.dart'; import 'package:window_manager/window_manager.dart'; @@ -20,6 +19,7 @@ import 'package:feeddeck/utils/constants.dart'; import 'package:feeddeck/widgets/confirmation/confirmation.dart'; import 'package:feeddeck/widgets/home/home.dart'; import 'package:feeddeck/widgets/reset_password/reset_password.dart'; +import 'package:feeddeck/widgets/item/details/utils/item_audio_palyer/item_audio_player_init/item_audio_player_init.dart'; /// Before we are calling [runApp] we have to ensure that the widget bindings /// are initialized, so that we can preserve the splash screen until we are done @@ -51,7 +51,9 @@ void main() async { /// Initialize the [media_kit] packages, so that we can play audio and video /// files. MediaKit.ensureInitialized(); - JustAudioMediaKit.ensureInitialized(); + if (!kIsWeb && (Platform.isLinux || Platform.isWindows)) { + ItemAudioPlayerInit().init(); + } /// Initialize the [just_audio_background] package, so that we can play audio /// files in the background. diff --git a/app/lib/widgets/item/details/utils/item_audio_palyer/item_audio_player_init/item_audio_player_init.dart b/app/lib/widgets/item/details/utils/item_audio_palyer/item_audio_player_init/item_audio_player_init.dart new file mode 100644 index 0000000..a2f5f11 --- /dev/null +++ b/app/lib/widgets/item/details/utils/item_audio_palyer/item_audio_player_init/item_audio_player_init.dart @@ -0,0 +1,11 @@ +library item_audio_player_init; + +import 'item_audio_player_init_stub.dart' + if (dart.library.io) 'item_audio_player_init_native.dart' + if (dart.library.html) 'item_audio_player_init_web.dart'; + +abstract class ItemAudioPlayerInit { + void init(); + + factory ItemAudioPlayerInit() => getItemAudioPlayerInit(); +} diff --git a/app/lib/widgets/item/details/utils/item_audio_palyer/item_audio_player_init/item_audio_player_init_native.dart b/app/lib/widgets/item/details/utils/item_audio_palyer/item_audio_player_init/item_audio_player_init_native.dart new file mode 100644 index 0000000..9f4292f --- /dev/null +++ b/app/lib/widgets/item/details/utils/item_audio_palyer/item_audio_player_init/item_audio_player_init_native.dart @@ -0,0 +1,12 @@ +import 'package:just_audio_media_kit/just_audio_media_kit.dart'; + +import 'item_audio_player_init.dart'; + +class ItemAudioPlayerInitNative implements ItemAudioPlayerInit { + @override + void init() { + JustAudioMediaKit.ensureInitialized(); + } +} + +ItemAudioPlayerInit getItemAudioPlayerInit() => ItemAudioPlayerInitNative(); diff --git a/app/lib/widgets/item/details/utils/item_audio_palyer/item_audio_player_init/item_audio_player_init_stub.dart b/app/lib/widgets/item/details/utils/item_audio_palyer/item_audio_player_init/item_audio_player_init_stub.dart new file mode 100644 index 0000000..3aae555 --- /dev/null +++ b/app/lib/widgets/item/details/utils/item_audio_palyer/item_audio_player_init/item_audio_player_init_stub.dart @@ -0,0 +1,5 @@ +import 'item_audio_player_init.dart'; + +ItemAudioPlayerInit getItemAudioPlayerInit() => throw UnsupportedError( + 'Can not ItemAudioPlayerInit without the packages dart:html or dart:io', + ); diff --git a/app/lib/widgets/item/details/utils/item_audio_palyer/item_audio_player_init/item_audio_player_init_web.dart b/app/lib/widgets/item/details/utils/item_audio_palyer/item_audio_player_init/item_audio_player_init_web.dart new file mode 100644 index 0000000..dfea7f8 --- /dev/null +++ b/app/lib/widgets/item/details/utils/item_audio_palyer/item_audio_player_init/item_audio_player_init_web.dart @@ -0,0 +1,8 @@ +import 'item_audio_player_init.dart'; + +class ItemAudioPlayerInitWeb implements ItemAudioPlayerInit { + @override + void init() {} +} + +ItemAudioPlayerInit getItemAudioPlayerInit() => ItemAudioPlayerInitWeb();