This commit is contained in:
Vadim 2023-01-26 23:03:03 +03:00
parent 179008ff77
commit 8c9f3d98f7
11 changed files with 98 additions and 45 deletions

View file

@ -6,6 +6,7 @@ import 'package:lightmeter/data/models/ev_source_type.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'data/light_sensor_service.dart';
import 'data/permissions_service.dart'; import 'data/permissions_service.dart';
import 'data/shared_prefs_service.dart'; import 'data/shared_prefs_service.dart';
import 'environment.dart'; import 'environment.dart';
@ -35,6 +36,7 @@ class Application extends StatelessWidget {
Provider(create: (_) => UserPreferencesService(snapshot.data!)), Provider(create: (_) => UserPreferencesService(snapshot.data!)),
Provider(create: (_) => const HapticsService()), Provider(create: (_) => const HapticsService()),
Provider(create: (_) => PermissionsService()), Provider(create: (_) => PermissionsService()),
Provider(create: (_) => const LightSensorService()),
], ],
child: StopTypeProvider( child: StopTypeProvider(
child: ThemeProvider( child: ThemeProvider(

View file

@ -0,0 +1,9 @@
import 'package:light_sensor/light_sensor.dart';
class LightSensorService {
const LightSensorService();
Future<bool> hasSensor() async => await LightSensor.hasSensor ?? false;
Stream<int> luxStream() => LightSensor.lightSensorStream;
}

View file

@ -1,13 +1,18 @@
import 'dart:io';
import 'package:lightmeter/data/haptics_service.dart'; import 'package:lightmeter/data/haptics_service.dart';
import 'package:lightmeter/data/light_sensor_service.dart';
import 'package:lightmeter/data/shared_prefs_service.dart'; import 'package:lightmeter/data/shared_prefs_service.dart';
class MeteringInteractor { class MeteringInteractor {
final UserPreferencesService _userPreferencesService; final UserPreferencesService _userPreferencesService;
final HapticsService _hapticsService; final HapticsService _hapticsService;
final LightSensorService _lightSensorService;
const MeteringInteractor( const MeteringInteractor(
this._userPreferencesService, this._userPreferencesService,
this._hapticsService, this._hapticsService,
this._lightSensorService,
); );
double get cameraEvCalibration => _userPreferencesService.cameraEvCalibration; double get cameraEvCalibration => _userPreferencesService.cameraEvCalibration;
@ -25,4 +30,14 @@ class MeteringInteractor {
} }
void enableHaptics(bool enable) => _userPreferencesService.haptics = enable; void enableHaptics(bool enable) => _userPreferencesService.haptics = enable;
Future<bool> hasAmbientLightSensor() async {
if (Platform.isAndroid) {
return _lightSensorService.hasSensor();
} else {
return false;
}
}
Stream<int> luxStream() => _lightSensorService.luxStream();
} }

View file

@ -0,0 +1,45 @@
import 'dart:async';
import 'package:lightmeter/interactors/metering_interactor.dart';
import 'package:lightmeter/screens/metering/ev_source/ev_source_bloc.dart';
import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart';
import 'package:lightmeter/screens/metering/communication/event_communication_metering.dart'
as communication_event;
import 'package:lightmeter/screens/metering/communication/state_communication_metering.dart'
as communication_states;
import 'package:lightmeter/utils/log_2.dart';
import 'event_light_sensor.dart';
import 'state_light_sensor.dart';
class LightSensorBloc extends EvSourceBloc<LightSensorEvent, LightSensorState> {
final MeteringInteractor _meteringInteractor;
StreamSubscription<int>? _luxSubscriptions;
LightSensorBloc(
MeteringCommunicationBloc communicationBloc,
this._meteringInteractor,
) : super(
communicationBloc,
const LightSensorInitState(),
);
@override
void onCommunicationState(communication_states.SourceState communicationState) {
if (communicationState is communication_states.MeasureState) {
if (_luxSubscriptions == null) {
_luxSubscriptions = _meteringInteractor.luxStream().listen((event) {
communicationBloc.add(communication_event.MeasuredEvent(log2(event.toDouble() / 2.5)));
});
} else {
_luxSubscriptions?.cancel().then((_) => _luxSubscriptions = null);
}
}
}
@override
Future<void> close() async {
_luxSubscriptions?.cancel().then((_) => _luxSubscriptions = null);
return super.close();
}
}

View file

@ -0,0 +1,3 @@
abstract class LightSensorEvent {
const LightSensorEvent();
}

View file

@ -0,0 +1,7 @@
abstract class LightSensorState {
const LightSensorState();
}
class LightSensorInitState extends LightSensorState {
const LightSensorInitState();
}

View file

@ -1,27 +0,0 @@
import 'dart:math';
import 'package:lightmeter/screens/metering/ev_source/ev_source_bloc.dart';
import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart';
import 'package:lightmeter/screens/metering/communication/event_communication_metering.dart'
as communication_event;
import 'package:lightmeter/screens/metering/communication/state_communication_metering.dart'
as communication_states;
import 'event_random_ev.dart';
import 'state_random_ev.dart';
class RandomEvBloc extends EvSourceBloc<RandomEvEvent, RandomEvState> {
final random = Random();
RandomEvBloc(MeteringCommunicationBloc communicationBloc)
: super(
communicationBloc,
RandomEvState(Random().nextDouble() * 15),
);
@override
void onCommunicationState(communication_states.SourceState communicationState) {
if (communicationState is communication_states.MeasureState) {
communicationBloc.add(communication_event.MeasuredEvent(random.nextDouble() * 15));
}
}
}

View file

@ -1,3 +0,0 @@
abstract class RandomEvEvent {
const RandomEvEvent();
}

View file

@ -1,5 +0,0 @@
class RandomEvState {
final double ev;
const RandomEvState(this.ev);
}

View file

@ -1,14 +1,15 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lightmeter/data/haptics_service.dart'; import 'package:lightmeter/data/haptics_service.dart';
import 'package:lightmeter/data/light_sensor_service.dart';
import 'package:lightmeter/data/models/ev_source_type.dart'; import 'package:lightmeter/data/models/ev_source_type.dart';
import 'package:lightmeter/data/models/photography_values/photography_value.dart'; import 'package:lightmeter/data/models/photography_values/photography_value.dart';
import 'package:lightmeter/data/shared_prefs_service.dart'; import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:lightmeter/interactors/metering_interactor.dart'; import 'package:lightmeter/interactors/metering_interactor.dart';
import 'package:lightmeter/screens/metering/ev_source/light_sensor/bloc_light_sensor.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'ev_source/camera/bloc_camera.dart'; import 'ev_source/camera/bloc_camera.dart';
import 'ev_source/random_ev/bloc_random_ev.dart';
import 'bloc_metering.dart'; import 'bloc_metering.dart';
import 'communication/bloc_communication_metering.dart'; import 'communication/bloc_communication_metering.dart';
import 'screen_metering.dart'; import 'screen_metering.dart';
@ -18,10 +19,12 @@ class MeteringFlow extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final sourceType = context.watch<EvSourceType>();
return Provider( return Provider(
create: (context) => MeteringInteractor( create: (context) => MeteringInteractor(
context.read<UserPreferencesService>(), context.read<UserPreferencesService>(),
context.read<HapticsService>(), context.read<HapticsService>(),
context.read<LightSensorService>(),
), ),
child: MultiBlocProvider( child: MultiBlocProvider(
providers: [ providers: [
@ -34,16 +37,19 @@ class MeteringFlow extends StatelessWidget {
context.read<StopType>(), context.read<StopType>(),
), ),
), ),
if (sourceType == EvSourceType.camera)
BlocProvider( BlocProvider(
create: (context) => CameraBloc( create: (context) => CameraBloc(
context.read<MeteringCommunicationBloc>(), context.read<MeteringCommunicationBloc>(),
context.read<MeteringInteractor>(), context.read<MeteringInteractor>(),
), ),
), ),
if (context.read<EvSourceType>() == EvSourceType.sensor) if (sourceType == EvSourceType.sensor)
BlocProvider( BlocProvider(
lazy: false, create: (context) => LightSensorBloc(
create: (context) => RandomEvBloc(context.read<MeteringCommunicationBloc>()), context.read<MeteringCommunicationBloc>(),
context.read<MeteringInteractor>(),
),
), ),
], ],
child: const MeteringScreen(), child: const MeteringScreen(),

View file

@ -17,6 +17,7 @@ dependencies:
sdk: flutter sdk: flutter
intl: 0.17.0 intl: 0.17.0
intl_utils: 2.8.1 intl_utils: 2.8.1
light_sensor: 2.0.2
material_color_utilities: 0.2.0 material_color_utilities: 0.2.0
package_info_plus: 3.0.2 package_info_plus: 3.0.2
permission_handler: 10.2.0 permission_handler: 10.2.0