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/services.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/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/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/res/theme.dart';
import 'package:lightmeter/screens/metering/flow_metering.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 {
final Environment env;
@ -17,10 +26,25 @@ class Application extends StatelessWidget {
@override
Widget build(BuildContext context) {
return LightmeterProviders(
env: env,
builder: (context, ready) {
if (ready) {
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) {
final theme = themeFrom(
UserPreferencesProvider.primaryColorOf(context),
UserPreferencesProvider.brightnessOf(context),
@ -30,8 +54,9 @@ class Application extends StatelessWidget {
return AnnotatedRegion(
value: SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
statusBarBrightness:
systemIconsBrightness == Brightness.light ? Brightness.dark : Brightness.light,
statusBarBrightness: systemIconsBrightness == Brightness.light
? Brightness.dark
: Brightness.light,
statusBarIconBrightness: systemIconsBrightness,
systemNavigationBarColor: Colors.transparent,
systemNavigationBarIconBrightness: systemIconsBrightness,
@ -57,9 +82,15 @@ class Application extends StatelessWidget {
},
),
);
} else {
return const SizedBox();
},
),
),
),
);
} else if (snapshot.error != null) {
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);
},
);
}
}