feat(ui): Implement setting the volumes owned

This commit is contained in:
PapaTutuWawa 2023-02-06 16:01:33 +01:00
parent 64bc2821f9
commit e6c1e5e7fe

View File

@ -7,18 +7,37 @@ import 'package:anitrack/src/ui/widgets/dropdown.dart';
import 'package:anitrack/src/ui/widgets/image.dart'; import 'package:anitrack/src/ui/widgets/image.dart';
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:get_it/get_it.dart';
class DetailsPage extends StatelessWidget { class DetailsPage extends StatefulWidget {
const DetailsPage({ DetailsPage({
super.key, super.key,
}); });
static MaterialPageRoute<dynamic> get route => MaterialPageRoute<dynamic>( static MaterialPageRoute<dynamic> get route => MaterialPageRoute<dynamic>(
builder: (_) => const DetailsPage(), builder: (_) => DetailsPage(),
settings: const RouteSettings( settings: const RouteSettings(
name: detailsRoute, name: detailsRoute,
), ),
); );
@override
DetailsPageState createState() => DetailsPageState();
}
class DetailsPageState extends State<DetailsPage> {
final TextEditingController _volumesOwnedController = TextEditingController();
@override
void initState() {
super.initState();
final state = GetIt.I.get<DetailsBloc>().state;
if (state.trackingType == TrackingMediumType.manga) {
_volumesOwnedController.text = '${(state.data as MangaTrackingData).volumesOwned}';
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -67,51 +86,131 @@ class DetailsPage extends StatelessWidget {
], ],
), ),
DropdownSelector<MediumTrackingState>( Padding(
title: state.trackingType == TrackingMediumType.anime ? padding: const EdgeInsets.symmetric(
'Watch state' : vertical: 8,
'Read state', ),
onChanged: (MediumTrackingState newState) { child: DropdownSelector<MediumTrackingState>(
if (state.trackingType == TrackingMediumType.anime) { title: state.trackingType == TrackingMediumType.anime ?
context.read<DetailsBloc>().add( 'Watch state' :
DetailsUpdatedEvent( 'Read state',
(state.data as AnimeTrackingData).copyWith( onChanged: (MediumTrackingState newState) {
state: newState, if (state.trackingType == TrackingMediumType.anime) {
context.read<DetailsBloc>().add(
DetailsUpdatedEvent(
(state.data as AnimeTrackingData).copyWith(
state: newState,
),
), ),
), );
); } else if (state.trackingType == TrackingMediumType.manga) {
} else if (state.trackingType == TrackingMediumType.manga) { context.read<DetailsBloc>().add(
context.read<DetailsBloc>().add( DetailsUpdatedEvent(
DetailsUpdatedEvent( (state.data as MangaTrackingData).copyWith(
(state.data as MangaTrackingData).copyWith( state: newState,
state: newState, ),
), ),
), );
); }
} },
}, values: [
values: [ SelectorItem(
SelectorItem( MediumTrackingState.ongoing,
MediumTrackingState.ongoing, MediumTrackingState.ongoing.toNameString(state.trackingType),
MediumTrackingState.ongoing.toNameString(state.trackingType), ),
), SelectorItem(
SelectorItem( MediumTrackingState.completed,
MediumTrackingState.completed, MediumTrackingState.completed.toNameString(state.trackingType),
MediumTrackingState.completed.toNameString(state.trackingType), ),
), SelectorItem(
SelectorItem( MediumTrackingState.planned,
MediumTrackingState.planned, MediumTrackingState.planned.toNameString(state.trackingType),
MediumTrackingState.planned.toNameString(state.trackingType), ),
), SelectorItem(
SelectorItem( MediumTrackingState.dropped,
MediumTrackingState.dropped, MediumTrackingState.dropped.toNameString(state.trackingType),
MediumTrackingState.dropped.toNameString(state.trackingType), ),
), ],
], initialValue: state.trackingType == TrackingMediumType.anime ?
initialValue: state.trackingType == TrackingMediumType.anime ? (state.data as AnimeTrackingData).state :
(state.data as AnimeTrackingData).state : (state.data as MangaTrackingData).state,
(state.data as MangaTrackingData).state, ),
), ),
if (state.trackingType == TrackingMediumType.manga)
Padding(
padding: const EdgeInsets.symmetric(
vertical: 8,
),
child: Row(
children: [
ElevatedButton(
onPressed: () {
final data = (state.data as MangaTrackingData);
if (data.volumesOwned == 0) return;
_volumesOwnedController.text = '${data.volumesOwned - 1}';
context.read<DetailsBloc>().add(
DetailsUpdatedEvent(
data.copyWith(
volumesOwned: data.volumesOwned - 1,
),
),
);
},
child: Icon(Icons.remove),
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16,
),
child: TextField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Volumes owned',
),
keyboardType: TextInputType.numberWithOptions(
signed: false,
decimal: false,
),
textInputAction: TextInputAction.done,
controller: _volumesOwnedController,
onSubmitted: (value) {
final amount = int.parse(value);
if (amount < 0) return;
context.read<DetailsBloc>().add(
DetailsUpdatedEvent(
(state.data as MangaTrackingData).copyWith(
volumesOwned: amount,
),
),
);
},
),
),
),
ElevatedButton(
onPressed: () {
final data = (state.data as MangaTrackingData);
_volumesOwnedController.text = '${data.volumesOwned + 1}';
context.read<DetailsBloc>().add(
DetailsUpdatedEvent(
data.copyWith(
volumesOwned: data.volumesOwned + 1,
),
),
);
},
child: Icon(Icons.add),
),
],
),
),
], ],
), ),
); );