Compare commits

..

1 commit

Author SHA1 Message Date
Vadim
228bcddc66
Merge 7979bbda9c into d938be61c4 2024-10-30 19:01:18 +00:00
7 changed files with 86 additions and 118 deletions

View file

@ -70,9 +70,8 @@ class FilmsProviderState extends State<FilmsProvider> {
/* Custom films **/ /* Custom films **/
Future<void> addCustomFilm(FilmExponential film) async { Future<void> addCustomFilm(FilmExponential film) async {
// ignore: avoid_redundant_argument_values await widget.filmsStorageService.addFilm(film);
await widget.filmsStorageService.addFilm(film, isUsed: true); customFilms[film.id] = (film: film, isUsed: false);
customFilms[film.id] = (film: film, isUsed: true);
setState(() {}); setState(() {});
} }

View file

@ -45,18 +45,18 @@ class FilmEditBloc extends Bloc<FilmEditEvent, FilmEditState> {
), ),
) { ) {
on<FilmEditEvent>( on<FilmEditEvent>(
(event, emit) async { (event, emit) {
switch (event) { switch (event) {
case final FilmEditNameChangedEvent e: case final FilmEditNameChangedEvent e:
await _onNameChanged(e, emit); _onNameChanged(e, emit);
case final FilmEditIsoChangedEvent e: case final FilmEditIsoChangedEvent e:
await _onIsoChanged(e, emit); _onIsoChanged(e, emit);
case final FilmEditExpChangedEvent e: case final FilmEditExpChangedEvent e:
await _onExpChanged(e, emit); _onExpChanged(e, emit);
case FilmEditSaveEvent(): case FilmEditSaveEvent():
await _onSave(event, emit); _onSave(event, emit);
case FilmEditDeleteEvent(): case FilmEditDeleteEvent():
await _onDelete(event, emit); _onDelete(event, emit);
} }
}, },
); );
@ -65,8 +65,10 @@ class FilmEditBloc extends Bloc<FilmEditEvent, FilmEditState> {
Future<void> _onNameChanged(FilmEditNameChangedEvent event, Emitter emit) async { Future<void> _onNameChanged(FilmEditNameChangedEvent event, Emitter emit) async {
_newFilm = _newFilm.copyWith(name: event.name); _newFilm = _newFilm.copyWith(name: event.name);
emit( emit(
state.copyWith( FilmEditState(
name: event.name, name: event.name,
isoValue: state.isoValue,
exponent: state.exponent,
canSave: _canSave(event.name, state.exponent), canSave: _canSave(event.name, state.exponent),
), ),
); );
@ -75,8 +77,10 @@ class FilmEditBloc extends Bloc<FilmEditEvent, FilmEditState> {
Future<void> _onIsoChanged(FilmEditIsoChangedEvent event, Emitter emit) async { Future<void> _onIsoChanged(FilmEditIsoChangedEvent event, Emitter emit) async {
_newFilm = _newFilm.copyWith(iso: event.iso.value); _newFilm = _newFilm.copyWith(iso: event.iso.value);
emit( emit(
state.copyWith( FilmEditState(
name: state.name,
isoValue: event.iso, isoValue: event.iso,
exponent: state.exponent,
canSave: _canSave(state.name, state.exponent), canSave: _canSave(state.name, state.exponent),
), ),
); );
@ -97,9 +101,8 @@ class FilmEditBloc extends Bloc<FilmEditEvent, FilmEditState> {
} }
Future<void> _onSave(FilmEditSaveEvent _, Emitter emit) async { Future<void> _onSave(FilmEditSaveEvent _, Emitter emit) async {
emit(state.copyWith(isLoading: true));
if (_isEdit) { if (_isEdit) {
await filmsProvider.updateCustomFilm( filmsProvider.updateCustomFilm(
FilmExponential( FilmExponential(
id: _originalFilm.id, id: _originalFilm.id,
name: state.name, name: state.name,
@ -108,7 +111,7 @@ class FilmEditBloc extends Bloc<FilmEditEvent, FilmEditState> {
), ),
); );
} else { } else {
await filmsProvider.addCustomFilm( filmsProvider.addCustomFilm(
FilmExponential( FilmExponential(
id: const Uuid().v1(), id: const Uuid().v1(),
name: state.name, name: state.name,
@ -117,13 +120,10 @@ class FilmEditBloc extends Bloc<FilmEditEvent, FilmEditState> {
), ),
); );
} }
emit(state.copyWith(isLoading: false));
} }
Future<void> _onDelete(FilmEditDeleteEvent _, Emitter emit) async { Future<void> _onDelete(FilmEditDeleteEvent _, Emitter emit) async {
emit(state.copyWith(isLoading: true)); filmsProvider.deleteCustomFilm(_originalFilm);
await filmsProvider.deleteCustomFilm(_originalFilm);
emit(state.copyWith(isLoading: false));
} }
bool _canSave(String name, double? exponent) { bool _canSave(String name, double? exponent) {

View file

@ -24,7 +24,6 @@ class FilmEditNameField extends StatelessWidget {
), ),
child: LightmeterTextField( child: LightmeterTextField(
initialValue: name, initialValue: name,
autofocus: true,
maxLength: 48, maxLength: 48,
hintText: S.of(context).name, hintText: S.of(context).name,
onChanged: onChanged, onChanged: onChanged,

View file

@ -25,94 +25,79 @@ class FilmEditScreen extends StatefulWidget {
class _FilmEditScreenState extends State<FilmEditScreen> { class _FilmEditScreenState extends State<FilmEditScreen> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocConsumer<FilmEditBloc, FilmEditState>( return SliverScreen(
listenWhen: (previous, current) => previous.isLoading != current.isLoading, title: Text(widget.isEdit ? S.of(context).editFilmTitle : S.of(context).addFilmTitle),
listener: (context, state) { appBarActions: [
if (state.isLoading) { BlocBuilder<FilmEditBloc, FilmEditState>(
FocusScope.of(context).unfocus(); buildWhen: (previous, current) => previous.canSave != current.canSave,
} else { builder: (context, state) => IconButton(
Navigator.of(context).pop(); onPressed: state.canSave
} ? () {
}, context.read<FilmEditBloc>().add(const FilmEditSaveEvent());
buildWhen: (previous, current) => previous.isLoading != current.isLoading, Navigator.of(context).pop();
builder: (context, state) => IgnorePointer( }
ignoring: state.isLoading, : null,
child: SliverScreen( icon: const Icon(Icons.save),
title: Text(widget.isEdit ? S.of(context).editFilmTitle : S.of(context).addFilmTitle), ),
appBarActions: [ ),
BlocBuilder<FilmEditBloc, FilmEditState>( if (widget.isEdit)
buildWhen: (previous, current) => previous.canSave != current.canSave, IconButton(
builder: (context, state) => IconButton( onPressed: () {
onPressed: state.canSave context.read<FilmEditBloc>().add(const FilmEditDeleteEvent());
? () { Navigator.of(context).pop();
context.read<FilmEditBloc>().add(const FilmEditSaveEvent()); },
} icon: const Icon(Icons.delete),
: null, ),
icon: const Icon(Icons.save), ],
), slivers: [
SliverToBoxAdapter(
child: Padding(
padding: const EdgeInsets.fromLTRB(
Dimens.paddingM,
0,
Dimens.paddingM,
Dimens.paddingM,
), ),
if (widget.isEdit) child: Card(
IconButton(
onPressed: () {
context.read<FilmEditBloc>().add(const FilmEditDeleteEvent());
},
icon: const Icon(Icons.delete),
),
],
slivers: [
SliverToBoxAdapter(
child: Padding( child: Padding(
padding: const EdgeInsets.fromLTRB( padding: const EdgeInsets.symmetric(vertical: Dimens.paddingM),
Dimens.paddingM, child: Column(
0, children: [
Dimens.paddingM, BlocBuilder<FilmEditBloc, FilmEditState>(
Dimens.paddingM, buildWhen: (previous, current) => previous.name != current.name,
), builder: (context, state) => FilmEditNameField(
child: Card( name: state.name,
child: Padding( onChanged: (value) {
padding: const EdgeInsets.symmetric(vertical: Dimens.paddingM), context.read<FilmEditBloc>().add(FilmEditNameChangedEvent(value));
child: Opacity( },
opacity: state.isLoading ? Dimens.disabledOpacity : Dimens.enabledOpacity,
child: Column(
children: [
BlocBuilder<FilmEditBloc, FilmEditState>(
buildWhen: (previous, current) => previous.name != current.name,
builder: (context, state) => FilmEditNameField(
name: state.name,
onChanged: (value) {
context.read<FilmEditBloc>().add(FilmEditNameChangedEvent(value));
},
),
),
BlocBuilder<FilmEditBloc, FilmEditState>(
buildWhen: (previous, current) => previous.isoValue != current.isoValue,
builder: (context, state) => FilmEditIsoPicker(
selected: state.isoValue,
onChanged: (value) {
context.read<FilmEditBloc>().add(FilmEditIsoChangedEvent(value));
},
),
),
BlocBuilder<FilmEditBloc, FilmEditState>(
buildWhen: (previous, current) => previous.exponent != current.exponent,
builder: (context, state) => FilmEditExponentialFormulaInput(
value: state.exponent,
onChanged: (value) {
context.read<FilmEditBloc>().add(FilmEditExpChangedEvent(value));
},
),
),
],
), ),
), ),
), BlocBuilder<FilmEditBloc, FilmEditState>(
buildWhen: (previous, current) => previous.isoValue != current.isoValue,
builder: (context, state) => FilmEditIsoPicker(
selected: state.isoValue,
onChanged: (value) {
context.read<FilmEditBloc>().add(FilmEditIsoChangedEvent(value));
},
),
),
BlocBuilder<FilmEditBloc, FilmEditState>(
buildWhen: (previous, current) => previous.exponent != current.exponent,
builder: (context, state) => FilmEditExponentialFormulaInput(
value: state.exponent,
onChanged: (value) {
context.read<FilmEditBloc>().add(FilmEditExpChangedEvent(value));
},
),
),
],
), ),
), ),
), ),
SliverToBoxAdapter(child: SizedBox(height: MediaQuery.paddingOf(context).bottom)), ),
],
), ),
), SliverToBoxAdapter(child: SizedBox(height: MediaQuery.paddingOf(context).bottom)),
],
); );
} }
} }

View file

@ -5,28 +5,13 @@ class FilmEditState {
final IsoValue isoValue; final IsoValue isoValue;
final double? exponent; final double? exponent;
final bool canSave; final bool canSave;
final bool isLoading; final FilmExponential? filmToSave;
const FilmEditState({ const FilmEditState({
required this.name, required this.name,
required this.isoValue, required this.isoValue,
required this.exponent, required this.exponent,
required this.canSave, required this.canSave,
this.isLoading = false, this.filmToSave,
}); });
FilmEditState copyWith({
String? name,
IsoValue? isoValue,
double? exponent,
bool? canSave,
bool? isLoading,
}) =>
FilmEditState(
name: name ?? this.name,
isoValue: isoValue ?? this.isoValue,
exponent: exponent ?? this.exponent,
canSave: canSave ?? this.canSave,
isLoading: isLoading ?? this.isLoading,
);
} }

View file

@ -3,7 +3,7 @@ import 'package:flutter/material.dart';
class LightmeterTextField extends TextFormField { class LightmeterTextField extends TextFormField {
LightmeterTextField({ LightmeterTextField({
super.controller, super.controller,
super.autofocus, super.focusNode,
super.initialValue, super.initialValue,
super.inputFormatters, super.inputFormatters,
super.maxLength, super.maxLength,

View file

@ -30,7 +30,7 @@ dependencies:
m3_lightmeter_iap: m3_lightmeter_iap:
git: git:
url: "https://github.com/vodemn/m3_lightmeter_iap" url: "https://github.com/vodemn/m3_lightmeter_iap"
ref: v1.1.0 ref: v1.0.0
m3_lightmeter_resources: m3_lightmeter_resources:
git: git:
url: "https://github.com/vodemn/m3_lightmeter_resources" url: "https://github.com/vodemn/m3_lightmeter_resources"