m3_lightmeter/lib/screens/film_edit/bloc_film_edit.dart

133 lines
3.8 KiB
Dart
Raw Normal View History

2024-10-21 10:22:25 +00:00
import 'package:flutter_bloc/flutter_bloc.dart';
2024-10-25 11:01:37 +00:00
import 'package:lightmeter/providers/films_provider.dart';
2024-10-21 10:22:25 +00:00
import 'package:lightmeter/screens/film_edit/event_film_edit.dart';
import 'package:lightmeter/screens/film_edit/state_film_edit.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
2024-10-25 11:01:37 +00:00
import 'package:uuid/uuid.dart';
2024-10-21 10:22:25 +00:00
class FilmEditBloc extends Bloc<FilmEditEvent, FilmEditState> {
static const _defaultFilm = FilmExponential(name: '', iso: 100, exponent: 1.3);
2024-10-25 11:01:37 +00:00
final FilmsProviderState filmsProvider;
2024-10-22 16:34:23 +00:00
final FilmExponential _originalFilm;
FilmExponential _newFilm;
2024-10-25 11:01:37 +00:00
final bool _isEdit;
2024-10-22 16:34:23 +00:00
2024-10-25 11:01:37 +00:00
factory FilmEditBloc(
FilmsProviderState filmsProvider, {
required FilmExponential? film,
required bool isEdit,
}) =>
film != null
? FilmEditBloc._(
filmsProvider,
film,
isEdit,
)
: FilmEditBloc._(
filmsProvider,
_defaultFilm,
isEdit,
);
2024-10-25 11:01:37 +00:00
FilmEditBloc._(
this.filmsProvider,
FilmExponential film,
this._isEdit,
) : _originalFilm = film,
2024-10-22 16:34:23 +00:00
_newFilm = film,
super(
2024-10-21 10:22:25 +00:00
FilmEditState(
2024-10-22 16:34:23 +00:00
name: film.name,
isoValue: IsoValue.values.firstWhere((element) => element.value == film.iso),
exponent: film.exponent,
canSave: false,
2024-10-21 10:22:25 +00:00
),
) {
on<FilmEditEvent>(
2024-10-31 15:59:45 +00:00
(event, emit) async {
2024-10-21 10:22:25 +00:00
switch (event) {
case final FilmEditNameChangedEvent e:
2024-10-31 15:59:45 +00:00
await _onNameChanged(e, emit);
2024-10-21 10:22:25 +00:00
case final FilmEditIsoChangedEvent e:
2024-10-31 15:59:45 +00:00
await _onIsoChanged(e, emit);
2024-10-21 10:22:25 +00:00
case final FilmEditExpChangedEvent e:
2024-10-31 15:59:45 +00:00
await _onExpChanged(e, emit);
2024-10-21 10:22:25 +00:00
case FilmEditSaveEvent():
2024-10-31 15:59:45 +00:00
await _onSave(event, emit);
2024-10-25 11:01:37 +00:00
case FilmEditDeleteEvent():
2024-10-31 15:59:45 +00:00
await _onDelete(event, emit);
2024-10-21 10:22:25 +00:00
}
},
);
}
2024-10-22 16:34:23 +00:00
Future<void> _onNameChanged(FilmEditNameChangedEvent event, Emitter emit) async {
_newFilm = _newFilm.copyWith(name: event.name);
emit(
2024-10-31 15:59:45 +00:00
state.copyWith(
2024-10-22 16:34:23 +00:00
name: event.name,
canSave: _canSave(event.name, state.exponent),
),
);
}
2024-10-21 10:22:25 +00:00
Future<void> _onIsoChanged(FilmEditIsoChangedEvent event, Emitter emit) async {
2024-10-22 16:34:23 +00:00
_newFilm = _newFilm.copyWith(iso: event.iso.value);
2024-10-21 10:22:25 +00:00
emit(
2024-10-31 15:59:45 +00:00
state.copyWith(
2024-10-22 16:34:23 +00:00
isoValue: event.iso,
canSave: _canSave(state.name, state.exponent),
2024-10-21 10:22:25 +00:00
),
);
}
2024-10-22 16:34:23 +00:00
Future<void> _onExpChanged(FilmEditExpChangedEvent event, Emitter emit) async {
if (event.exponent != null) {
_newFilm = _newFilm.copyWith(exponent: event.exponent);
}
emit(
FilmEditState(
name: state.name,
isoValue: state.isoValue,
exponent: event.exponent,
canSave: _canSave(state.name, event.exponent),
),
);
}
2024-10-21 10:22:25 +00:00
2024-10-25 11:01:37 +00:00
Future<void> _onSave(FilmEditSaveEvent _, Emitter emit) async {
2024-10-31 15:59:45 +00:00
emit(state.copyWith(isLoading: true));
2024-10-25 11:01:37 +00:00
if (_isEdit) {
2024-10-31 15:59:45 +00:00
await filmsProvider.updateCustomFilm(
2024-10-25 11:01:37 +00:00
FilmExponential(
id: _originalFilm.id,
name: state.name,
iso: state.isoValue.value,
exponent: state.exponent!,
),
);
} else {
2024-10-31 15:59:45 +00:00
await filmsProvider.addCustomFilm(
2024-10-25 11:01:37 +00:00
FilmExponential(
id: const Uuid().v1(),
name: state.name,
iso: state.isoValue.value,
exponent: state.exponent!,
),
);
}
2024-10-31 15:59:45 +00:00
emit(state.copyWith(isLoading: false));
2024-10-25 11:01:37 +00:00
}
Future<void> _onDelete(FilmEditDeleteEvent _, Emitter emit) async {
2024-10-31 15:59:45 +00:00
emit(state.copyWith(isLoading: true));
await filmsProvider.deleteCustomFilm(_originalFilm);
emit(state.copyWith(isLoading: false));
2024-10-25 11:01:37 +00:00
}
2024-10-22 16:34:23 +00:00
bool _canSave(String name, double? exponent) {
return name.isNotEmpty && exponent != null && _newFilm != _originalFilm;
}
2024-10-21 10:22:25 +00:00
}