Removed redundant LightmeterProviders

This commit is contained in:
Vadim 2023-08-13 22:08:05 +02:00
parent 28c72d492f
commit ee3bf31856
2 changed files with 71 additions and 93 deletions

View file

@ -1,14 +1,23 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:lightmeter/data/caffeine_service.dart';
import 'package:lightmeter/data/haptics_service.dart';
import 'package:lightmeter/data/light_sensor_service.dart';
import 'package:lightmeter/data/models/supported_locale.dart'; import 'package:lightmeter/data/models/supported_locale.dart';
import 'package:lightmeter/data/permissions_service.dart';
import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:lightmeter/data/volume_events_service.dart';
import 'package:lightmeter/environment.dart'; import 'package:lightmeter/environment.dart';
import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/providers.dart'; import 'package:lightmeter/providers/equipment_profile_provider.dart';
import 'package:lightmeter/providers/services_provider.dart';
import 'package:lightmeter/providers/user_preferences_provider.dart'; import 'package:lightmeter/providers/user_preferences_provider.dart';
import 'package:lightmeter/res/theme.dart'; import 'package:lightmeter/res/theme.dart';
import 'package:lightmeter/screens/metering/flow_metering.dart'; import 'package:lightmeter/screens/metering/flow_metering.dart';
import 'package:lightmeter/screens/settings/flow_settings.dart'; import 'package:lightmeter/screens/settings/flow_settings.dart';
import 'package:platform/platform.dart';
import 'package:shared_preferences/shared_preferences.dart';
class Application extends StatelessWidget { class Application extends StatelessWidget {
final Environment env; final Environment env;
@ -17,49 +26,71 @@ class Application extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return LightmeterProviders( return FutureBuilder(
env: env, future: Future.wait([
builder: (context, ready) { SharedPreferences.getInstance(),
if (ready) { const LightSensorService(LocalPlatform()).hasSensor(),
final theme = themeFrom( ]),
UserPreferencesProvider.primaryColorOf(context), builder: (_, snapshot) {
UserPreferencesProvider.brightnessOf(context), if (snapshot.data != null) {
); return ServicesProvider(
final systemIconsBrightness = caffeineService: const CaffeineService(),
ThemeData.estimateBrightnessForColor(theme.colorScheme.onSurface); environment: env.copyWith(hasLightSensor: snapshot.data![1] as bool),
return AnnotatedRegion( hapticsService: const HapticsService(),
value: SystemUiOverlayStyle( lightSensorService: const LightSensorService(LocalPlatform()),
statusBarColor: Colors.transparent, permissionsService: const PermissionsService(),
statusBarBrightness: userPreferencesService: UserPreferencesService(snapshot.data![0] as SharedPreferences),
systemIconsBrightness == Brightness.light ? Brightness.dark : Brightness.light, volumeEventsService: const VolumeEventsService(LocalPlatform()),
statusBarIconBrightness: systemIconsBrightness, child: UserPreferencesProvider(
systemNavigationBarColor: Colors.transparent, child: EquipmentProfileProvider(
systemNavigationBarIconBrightness: systemIconsBrightness, child: Builder(
), builder: (context) {
child: MaterialApp( final theme = themeFrom(
theme: theme, UserPreferencesProvider.primaryColorOf(context),
locale: Locale(UserPreferencesProvider.localeOf(context).intlName), UserPreferencesProvider.brightnessOf(context),
localizationsDelegates: const [ );
S.delegate, final systemIconsBrightness =
GlobalMaterialLocalizations.delegate, ThemeData.estimateBrightnessForColor(theme.colorScheme.onSurface);
GlobalWidgetsLocalizations.delegate, return AnnotatedRegion(
GlobalCupertinoLocalizations.delegate, value: SystemUiOverlayStyle(
], statusBarColor: Colors.transparent,
supportedLocales: S.delegate.supportedLocales, statusBarBrightness: systemIconsBrightness == Brightness.light
builder: (context, child) => MediaQuery( ? Brightness.dark
data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0), : Brightness.light,
child: child!, statusBarIconBrightness: systemIconsBrightness,
systemNavigationBarColor: Colors.transparent,
systemNavigationBarIconBrightness: systemIconsBrightness,
),
child: MaterialApp(
theme: theme,
locale: Locale(UserPreferencesProvider.localeOf(context).intlName),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,
builder: (context, child) => MediaQuery(
data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
child: child!,
),
initialRoute: "metering",
routes: {
"metering": (context) => const MeteringFlow(),
"settings": (context) => const SettingsFlow(),
},
),
);
},
),
), ),
initialRoute: "metering",
routes: {
"metering": (context) => const MeteringFlow(),
"settings": (context) => const SettingsFlow(),
},
), ),
); );
} else { } else if (snapshot.error != null) {
return const SizedBox(); return Center(child: Text(snapshot.error!.toString()));
} }
return const SizedBox();
}, },
); );
} }

View file

@ -1,53 +0,0 @@
import 'package:flutter/material.dart';
import 'package:lightmeter/data/caffeine_service.dart';
import 'package:lightmeter/data/haptics_service.dart';
import 'package:lightmeter/data/light_sensor_service.dart';
import 'package:lightmeter/data/permissions_service.dart';
import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:lightmeter/data/volume_events_service.dart';
import 'package:lightmeter/environment.dart';
import 'package:lightmeter/providers/equipment_profile_provider.dart';
import 'package:lightmeter/providers/services_provider.dart';
import 'package:lightmeter/providers/user_preferences_provider.dart';
import 'package:platform/platform.dart';
import 'package:shared_preferences/shared_preferences.dart';
class LightmeterProviders extends StatelessWidget {
final Environment env;
final Widget Function(BuildContext context, bool ready) builder;
const LightmeterProviders({required this.env, required this.builder, super.key});
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: Future.wait([
SharedPreferences.getInstance(),
const LightSensorService(LocalPlatform()).hasSensor(),
]),
builder: (_, snapshot) {
if (snapshot.data != null) {
return ServicesProvider(
caffeineService: const CaffeineService(),
environment: env.copyWith(hasLightSensor: snapshot.data![1] as bool),
hapticsService: const HapticsService(),
lightSensorService: const LightSensorService(LocalPlatform()),
permissionsService: const PermissionsService(),
userPreferencesService: UserPreferencesService(snapshot.data![0] as SharedPreferences),
volumeEventsService: const VolumeEventsService(LocalPlatform()),
child: UserPreferencesProvider(
child: EquipmentProfileProvider(
child: Builder(
builder: (context) => builder(context, true),
),
),
),
);
} else if (snapshot.error != null) {
return Center(child: Text(snapshot.error!.toString()));
}
return builder(context, false);
},
);
}
}