Skip to content

Commit

Permalink
Try to connect to headphones under the hood so that user won't see sp…
Browse files Browse the repository at this point in the history
…inning wheel and thus have *even* smoother experience

This was SO easy!! Thanks @jonbhanson !!!
  • Loading branch information
TheLastGimbus committed Sep 20, 2023
1 parent ea81774 commit d469335
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
14 changes: 13 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:provider/provider.dart';
import 'package:streaming_shared_preferences/streaming_shared_preferences.dart';

Expand All @@ -21,7 +22,10 @@ import 'ui/pages/settings/settings_page.dart';
import 'ui/theme/themes.dart';

void main() {
WidgetsFlutterBinding.ensureInitialized();
final bind = WidgetsFlutterBinding.ensureInitialized();
// This is so that we try to connect to headphones under splash screen
// This will make it more smooth to the user
FlutterNativeSplash.preserve(widgetsBinding: bind);
if (Platform.isAndroid) {
// this is async, so it won't block runApp
android_periodic.init();
Expand All @@ -45,6 +49,14 @@ class _MyAppWrapperState extends State<MyAppWrapper>
@override
void initState() {
WidgetsBinding.instance.addObserver(this);
// ...and this removes splash if we either connected or 1 second passed
_btBlock.stream
.firstWhere((e) => e is HeadphonesConnectedOpen)
.timeout(
const Duration(seconds: 1),
onTimeout: () => HeadphonesNotPaired(), // just placeholder
)
.then((_) => FlutterNativeSplash.remove());
super.initState();
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ packages:
source: sdk
version: "0.0.0"
flutter_native_splash:
dependency: "direct dev"
dependency: "direct main"
description:
name: flutter_native_splash
sha256: ecff62b3b893f2f665de7e4ad3de89f738941fcfcaaba8ee601e749efafa4698
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dependencies:
dynamic_color: ^1.6.7
home_widget: ^0.3.0
workmanager: ^0.5.1
flutter_native_splash: ^2.3.2

# Logic libs
crclib: ^3.0.0
Expand Down Expand Up @@ -74,7 +75,6 @@ dev_dependencies:
sdk: flutter

flutter_launcher_icons: ^0.13.1
flutter_native_splash: ^2.3.2

flutter_lints: ^2.0.3

Expand Down

1 comment on commit d469335

@jonbhanson
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are welcome. Glad it worked so well for you!

Please sign in to comment.