Added UserPreferencesService

This commit is contained in:
Vadim 2022-12-16 11:08:12 +03:00
parent 07dc5d8f57
commit 52d8578d77
22 changed files with 99 additions and 59 deletions

View file

@ -5,7 +5,9 @@ import 'package:lightmeter/data/ev_source/ev_source_type.dart';
import 'package:lightmeter/data/permissions_service.dart'; import 'package:lightmeter/data/permissions_service.dart';
import 'package:lightmeter/screens/settings/settings_screen.dart'; import 'package:lightmeter/screens/settings/settings_screen.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'data/shared_prefs_service.dart';
import 'generated/l10n.dart'; import 'generated/l10n.dart';
import 'res/theme.dart'; import 'res/theme.dart';
import 'screens/metering/flow_metering.dart'; import 'screens/metering/flow_metering.dart';
@ -38,8 +40,15 @@ class _ApplicationState extends State<Application> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Provider.value( return FutureBuilder<SharedPreferences>(
value: widget.evSource, future: SharedPreferences.getInstance(),
builder: (context, snapshot) {
if (snapshot.data != null) {
return MultiProvider(
providers: [
Provider(create: (_) => UserPreferencesService(snapshot.data!)),
Provider.value(value: widget.evSource),
],
child: Provider( child: Provider(
create: (context) => PermissionsService(), create: (context) => PermissionsService(),
child: StopTypeProvider( child: StopTypeProvider(
@ -69,4 +78,8 @@ class _ApplicationState extends State<Application> {
), ),
); );
} }
return const SizedBox();
},
);
}
} }

View file

@ -0,0 +1,19 @@
import 'package:lightmeter/data/models/nd_value.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'models/iso_value.dart';
class UserPreferencesService {
static const _isoKey = "ISO";
static const _ndFilterKey = "ND";
final SharedPreferences _sharedPreferences;
UserPreferencesService(this._sharedPreferences);
IsoValue get iso => isoValues.firstWhere((v) => v.value == (_sharedPreferences.getInt(_isoKey) ?? 100));
set iso(IsoValue value) => _sharedPreferences.setInt(_isoKey, value.value);
NdValue get ndFilter => ndValues.firstWhere((v) => v.value == (_sharedPreferences.getInt(_ndFilterKey) ?? 0));
set ndFilter(NdValue value) => _sharedPreferences.setInt(_ndFilterKey, value.value);
}

View file

@ -2,12 +2,11 @@ import 'dart:async';
import 'dart:math'; import 'dart:math';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lightmeter/models/aperture_value.dart'; import 'package:lightmeter/data/models/aperture_value.dart';
import 'package:lightmeter/models/exposure_pair.dart'; import 'package:lightmeter/data/models/exposure_pair.dart';
import 'package:lightmeter/models/iso_value.dart'; import 'package:lightmeter/data/models/photography_value.dart';
import 'package:lightmeter/models/nd_value.dart'; import 'package:lightmeter/data/models/shutter_speed_value.dart';
import 'package:lightmeter/models/photography_value.dart'; import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:lightmeter/models/shutter_speed_value.dart';
import 'package:lightmeter/screens/metering/communication/event_communication_metering.dart' as communication_events; import 'package:lightmeter/screens/metering/communication/event_communication_metering.dart' as communication_events;
import 'package:lightmeter/screens/metering/communication/state_communication_metering.dart' as communication_states; import 'package:lightmeter/screens/metering/communication/state_communication_metering.dart' as communication_states;
import 'package:lightmeter/utils/log_2.dart'; import 'package:lightmeter/utils/log_2.dart';
@ -18,6 +17,7 @@ import 'state_metering.dart';
class MeteringBloc extends Bloc<MeteringEvent, MeteringState> { class MeteringBloc extends Bloc<MeteringEvent, MeteringState> {
final MeteringCommunicationBloc _communicationBloc; final MeteringCommunicationBloc _communicationBloc;
final UserPreferencesService _userPreferencesService;
late final StreamSubscription<communication_states.ScreenState> _communicationSubscription; late final StreamSubscription<communication_states.ScreenState> _communicationSubscription;
List<ApertureValue> get _apertureValues => apertureValues.whereStopType(stopType); List<ApertureValue> get _apertureValues => apertureValues.whereStopType(stopType);
@ -25,13 +25,16 @@ class MeteringBloc extends Bloc<MeteringEvent, MeteringState> {
StopType stopType; StopType stopType;
MeteringBloc(this._communicationBloc, this.stopType) MeteringBloc(
: super( this._communicationBloc,
this._userPreferencesService,
this.stopType,
) : super(
MeteringState( MeteringState(
iso: isoValues.where((element) => element.value == 100).first, iso: _userPreferencesService.iso,
ev: 0.0, ev: 0.0,
evCompensation: 0.0, evCompensation: 0.0,
nd: ndValues.first, nd: _userPreferencesService.ndFilter,
exposurePairs: [], exposurePairs: [],
), ),
) { ) {
@ -73,6 +76,7 @@ class MeteringBloc extends Bloc<MeteringEvent, MeteringState> {
} }
void _onIsoChanged(IsoChangedEvent event, Emitter emit) { void _onIsoChanged(IsoChangedEvent event, Emitter emit) {
_userPreferencesService.iso = event.isoValue;
final ev = state.ev + log2(event.isoValue.value / state.iso.value); final ev = state.ev + log2(event.isoValue.value / state.iso.value);
emit(MeteringState( emit(MeteringState(
iso: event.isoValue, iso: event.isoValue,
@ -84,6 +88,7 @@ class MeteringBloc extends Bloc<MeteringEvent, MeteringState> {
} }
void _onNdChanged(NdChangedEvent event, Emitter emit) { void _onNdChanged(NdChangedEvent event, Emitter emit) {
_userPreferencesService.ndFilter = event.ndValue;
final ev = state.ev - event.ndValue.stopReduction + state.nd.stopReduction; final ev = state.ev - event.ndValue.stopReduction + state.nd.stopReduction;
emit(MeteringState( emit(MeteringState(
iso: state.iso, iso: state.iso,

View file

@ -1,5 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:lightmeter/models/photography_value.dart'; import 'package:lightmeter/data/models/photography_value.dart';
import 'package:lightmeter/res/dimens.dart'; import 'package:lightmeter/res/dimens.dart';
class ExposurePaitListItem<T extends PhotographyStopValue> extends StatelessWidget { class ExposurePaitListItem<T extends PhotographyStopValue> extends StatelessWidget {

View file

@ -1,5 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:lightmeter/models/exposure_pair.dart'; import 'package:lightmeter/data/models/exposure_pair.dart';
import 'package:lightmeter/res/dimens.dart'; import 'package:lightmeter/res/dimens.dart';
import 'package:lightmeter/screens/metering/components/exposure_pairs_list/components/exposure_pair_item.dart'; import 'package:lightmeter/screens/metering/components/exposure_pairs_list/components/exposure_pair_item.dart';

View file

@ -1,6 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/models/photography_value.dart'; import 'package:lightmeter/data/models/photography_value.dart';
import 'package:lightmeter/res/dimens.dart'; import 'package:lightmeter/res/dimens.dart';
typedef DialogPickerItemBuilder<T extends PhotographyValue> = Widget Function(BuildContext, T); typedef DialogPickerItemBuilder<T extends PhotographyValue> = Widget Function(BuildContext, T);

View file

@ -2,10 +2,10 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lightmeter/data/ev_source/camera/bloc_camera.dart'; import 'package:lightmeter/data/ev_source/camera/bloc_camera.dart';
import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/models/exposure_pair.dart'; import 'package:lightmeter/data/models/exposure_pair.dart';
import 'package:lightmeter/models/iso_value.dart'; import 'package:lightmeter/data/models/iso_value.dart';
import 'package:lightmeter/models/nd_value.dart'; import 'package:lightmeter/data/models/nd_value.dart';
import 'package:lightmeter/models/photography_value.dart'; import 'package:lightmeter/data/models/photography_value.dart';
import 'package:lightmeter/res/dimens.dart'; import 'package:lightmeter/res/dimens.dart';
import 'components/camera_preview.dart'; import 'components/camera_preview.dart';

View file

@ -1,6 +1,6 @@
import 'package:lightmeter/models/iso_value.dart'; import 'package:lightmeter/data/models/iso_value.dart';
import 'package:lightmeter/models/nd_value.dart'; import 'package:lightmeter/data/models/nd_value.dart';
import 'package:lightmeter/models/photography_value.dart'; import 'package:lightmeter/data/models/photography_value.dart';
abstract class MeteringEvent { abstract class MeteringEvent {
const MeteringEvent(); const MeteringEvent();

View file

@ -3,7 +3,8 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lightmeter/data/ev_source/camera/bloc_camera.dart'; import 'package:lightmeter/data/ev_source/camera/bloc_camera.dart';
import 'package:lightmeter/data/ev_source/ev_source_type.dart'; import 'package:lightmeter/data/ev_source/ev_source_type.dart';
import 'package:lightmeter/data/ev_source/random_ev/bloc_random_ev.dart'; import 'package:lightmeter/data/ev_source/random_ev/bloc_random_ev.dart';
import 'package:lightmeter/models/photography_value.dart'; import 'package:lightmeter/data/models/photography_value.dart';
import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:lightmeter/screens/metering/bloc_metering.dart'; import 'package:lightmeter/screens/metering/bloc_metering.dart';
import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart'; import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart';
@ -20,6 +21,7 @@ class MeteringFlow extends StatelessWidget {
BlocProvider( BlocProvider(
create: (context) => MeteringBloc( create: (context) => MeteringBloc(
context.read<MeteringCommunicationBloc>(), context.read<MeteringCommunicationBloc>(),
context.read<UserPreferencesService>(),
context.read<StopType>(), context.read<StopType>(),
), ),
), ),

View file

@ -1,6 +1,6 @@
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/models/photography_value.dart'; import 'package:lightmeter/data/models/photography_value.dart';
import 'package:lightmeter/res/dimens.dart'; import 'package:lightmeter/res/dimens.dart';
import 'package:lightmeter/screens/settings/settings_screen.dart'; import 'package:lightmeter/screens/settings/settings_screen.dart';

View file

@ -1,6 +1,6 @@
import 'package:lightmeter/models/exposure_pair.dart'; import 'package:lightmeter/data/models/exposure_pair.dart';
import 'package:lightmeter/models/iso_value.dart'; import 'package:lightmeter/data/models/iso_value.dart';
import 'package:lightmeter/models/nd_value.dart'; import 'package:lightmeter/data/models/nd_value.dart';
class MeteringState { class MeteringState {
final double ev; final double ev;

View file

@ -1,6 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/models/photography_value.dart'; import 'package:lightmeter/data/models/photography_value.dart';
class FractionalStopsDialog extends StatefulWidget { class FractionalStopsDialog extends StatefulWidget {
final StopType selectedType; final StopType selectedType;

View file

@ -1,6 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/models/photography_value.dart'; import 'package:lightmeter/data/models/photography_value.dart';
import 'package:lightmeter/utils/stop_type_provider.dart'; import 'package:lightmeter/utils/stop_type_provider.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';

View file

@ -1,5 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:lightmeter/models/photography_value.dart'; import 'package:lightmeter/data/models/photography_value.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
class StopTypeProvider extends StatefulWidget { class StopTypeProvider extends StatefulWidget {

View file

@ -19,6 +19,7 @@ dependencies:
material_color_utilities: ^0.2.0 material_color_utilities: ^0.2.0
permission_handler: 10.2.0 permission_handler: 10.2.0
provider: ^6.0.4 provider: ^6.0.4
shared_preferences: 2.0.15
dev_dependencies: dev_dependencies:
google_fonts: ^3.0.1 google_fonts: ^3.0.1

View file

@ -1,7 +1,7 @@
import 'package:lightmeter/models/aperture_value.dart'; import 'package:lightmeter/data/models/aperture_value.dart';
import 'package:lightmeter/models/iso_value.dart'; import 'package:lightmeter/data/models/iso_value.dart';
import 'package:lightmeter/models/photography_value.dart'; import 'package:lightmeter/data/models/photography_value.dart';
import 'package:lightmeter/models/shutter_speed_value.dart'; import 'package:lightmeter/data/models/shutter_speed_value.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
void main() { void main() {