fixed redundant vibrations

This commit is contained in:
Vadim 2023-06-04 12:50:07 +02:00
parent 0daa689c01
commit 67f8e6e236
3 changed files with 59 additions and 51 deletions

View file

@ -1,8 +1,8 @@
import 'package:permission_handler/permission_handler.dart';
class PermissionsService {
const PermissionsService();
Future<PermissionStatus> checkCameraPermission() async => Permission.camera.status;
Future<PermissionStatus> requestCameraPermission() async => Permission.camera.request();

View file

@ -3,35 +3,6 @@ import 'package:lightmeter/data/models/metering_screen_layout_config.dart';
import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:lightmeter/utils/inherited_generics.dart';
class MeteringScreenLayout extends InheritedModelBase<MeteringScreenLayoutFeature, bool> {
const MeteringScreenLayout({
required super.data,
required super.child,
super.key,
});
static MeteringScreenLayoutConfig of(BuildContext context, {bool listen = true}) {
if (listen) {
return context
.dependOnInheritedWidgetOfExactType<
InheritedModelBase<MeteringScreenLayoutFeature, bool>>()!
.data;
} else {
return context
.findAncestorWidgetOfExactType<InheritedModelBase<MeteringScreenLayoutFeature, bool>>()!
.data;
}
}
static bool featureOf(BuildContext context, MeteringScreenLayoutFeature aspect) {
return InheritedModel.inheritFrom<InheritedModelBase<MeteringScreenLayoutFeature, bool>>(
context,
aspect: aspect,
)!
.data[aspect]!;
}
}
class MeteringScreenLayoutProvider extends StatefulWidget {
final Widget child;
@ -70,3 +41,20 @@ class MeteringScreenLayoutProviderState extends State<MeteringScreenLayoutProvid
context.get<UserPreferencesService>().meteringScreenLayout = _config;
}
}
typedef _MeteringScreenLayoutModel = InheritedModelBase<MeteringScreenLayoutFeature, bool>;
extension MeteringScreenLayout on InheritedModelBase<MeteringScreenLayoutFeature, bool> {
static MeteringScreenLayoutConfig of(BuildContext context, {bool listen = true}) {
if (listen) {
return context.dependOnInheritedWidgetOfExactType<_MeteringScreenLayoutModel>()!.data;
} else {
return context.findAncestorWidgetOfExactType<_MeteringScreenLayoutModel>()!.data;
}
}
static bool featureOf(BuildContext context, MeteringScreenLayoutFeature aspect) {
return InheritedModel.inheritFrom<_MeteringScreenLayoutModel>(context, aspect: aspect)!
.data[aspect]!;
}
}

View file

@ -40,7 +40,7 @@ class MeteringBloc extends Bloc<MeteringEvent, MeteringState> {
this.stopType,
) : super(
MeteringDataState(
ev: 0.0,
ev: null,
film: _meteringInteractor.film,
iso: _meteringInteractor.iso,
nd: _meteringInteractor.ndFilter,
@ -77,56 +77,76 @@ class MeteringBloc extends Bloc<MeteringEvent, MeteringState> {
}
void _onStopTypeChanged(StopTypeChangedEvent event, Emitter emit) {
if (stopType != event.stopType) {
stopType = event.stopType;
_updateMeasurements();
}
}
void _onEquipmentProfileChanged(EquipmentProfileChangedEvent event, Emitter emit) {
_equipmentProfileData = event.equipmentProfileData;
bool willUpdateMeasurements = false;
/// Update selected ISO value, if selected equipment profile
/// doesn't contain currently selected value
if (!event.equipmentProfileData.isoValues.any((v) => _iso.value == v.value)) {
_meteringInteractor.iso = event.equipmentProfileData.isoValues.first;
_iso = event.equipmentProfileData.isoValues.first;
willUpdateMeasurements &= true;
}
/// The same for ND filter
if (!event.equipmentProfileData.ndValues.any((v) => _nd.value == v.value)) {
_meteringInteractor.ndFilter = event.equipmentProfileData.ndValues.first;
_nd = event.equipmentProfileData.ndValues.first;
willUpdateMeasurements &= true;
}
if (willUpdateMeasurements) {
_updateMeasurements();
}
}
void _onFilmChanged(FilmChangedEvent event, Emitter emit) {
if (_iso.value != event.data.iso) {
if (_film.name != event.data.name) {
_film = event.data;
_meteringInteractor.film = event.data;
/// If user selects 'Other' film we preserve currently selected ISO
/// and therefore only discard reciprocity formula
if (_iso.value != event.data.iso && event.data != const Film.other()) {
final newIso = IsoValue.values.firstWhere(
(e) => e.value == event.data.iso,
orElse: () => _iso,
);
add(IsoChangedEvent(newIso));
_meteringInteractor.iso = newIso;
_iso = newIso;
}
_film = event.data;
_meteringInteractor.film = event.data;
_updateMeasurements();
}
}
void _onIsoChanged(IsoChangedEvent event, Emitter emit) {
if (event.isoValue.value != _film.iso) {
/// Discard currently selected film even if ISO is the same,
/// because, for example, Fomapan 400 and any Ilford 400
/// have different reciprocity formulas
_film = Film.values.first;
}
if (_iso != event.isoValue) {
_meteringInteractor.iso = event.isoValue;
_iso = event.isoValue;
_updateMeasurements();
}
}
void _onNdChanged(NdChangedEvent event, Emitter emit) {
if (_nd != event.ndValue) {
_meteringInteractor.ndFilter = event.ndValue;
_nd = event.ndValue;
_updateMeasurements();
}
}
void _onMeasure(MeasureEvent _, Emitter emit) {
_meteringInteractor.quickVibration();