Skip to content

Commit

Permalink
moved toggleFavoriteFunc to state mgmt, added remove func to fav screen
Browse files Browse the repository at this point in the history
  • Loading branch information
namelessto committed Feb 10, 2024
1 parent 9f9d7f1 commit c8e1d20
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 122 deletions.
4 changes: 2 additions & 2 deletions lib/presentation/screens/bloon/boss_bloon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ class _BossBloonState extends State<BossBloon> {
Consumer<FavoriteState>(
builder: (context, favoriteState, child) {
return IconButton(
onPressed: () =>
toggleFavoriteFunc(context, favoriteState, boss),
onPressed: () => favoriteState.toggleFavoriteFunc(
context, favoriteState, boss),
icon: favoriteState.isFavorite(boss.type, boss.id)
? const Icon(Icons.star)
: const Icon(Icons.star_border_outlined),
Expand Down
4 changes: 2 additions & 2 deletions lib/presentation/screens/bloon/single_bloon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class _SingleBloonState extends State<SingleBloon> {
Consumer<FavoriteState>(
builder: (context, favoriteState, child) {
return IconButton(
onPressed: () =>
toggleFavoriteFunc(context, favoriteState, bloon),
onPressed: () => favoriteState.toggleFavoriteFunc(
context, favoriteState, bloon),
icon: favoriteState.isFavorite(bloon.type, bloon.id)
? const Icon(Icons.star)
: const Icon(Icons.star_border_outlined),
Expand Down
7 changes: 4 additions & 3 deletions lib/presentation/screens/hero/heroes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ class _HeroesState extends State<Heroes> {

return InkWell(
borderRadius: BorderRadius.circular(20),
onLongPress: () =>
toggleFavoriteFunc(context, favoriteState, hero),
onLongPress: () => favoriteState.toggleFavoriteFunc(
context, favoriteState, hero),
onTap: () {
if (!favoriteState.multiSelect) {
widget.analyticsHelper.logEvent(
Expand All @@ -90,7 +90,8 @@ class _HeroesState extends State<Heroes> {
),
);
} else {
toggleFavoriteFunc(context, favoriteState, hero);
favoriteState.toggleFavoriteFunc(
context, favoriteState, hero);
}
},
child: Stack(
Expand Down
2 changes: 1 addition & 1 deletion lib/presentation/screens/hero/single_hero.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class _SingleHeroState extends State<SingleHero> {
Consumer<FavoriteState>(
builder: (context, favoriteState, child) {
return IconButton(
onPressed: () => toggleFavoriteFunc(
onPressed: () => favoriteState.toggleFavoriteFunc(
context, favoriteState, singleHero),
icon: favoriteState.isFavorite(
singleHero.type, singleHero.id)
Expand Down
7 changes: 4 additions & 3 deletions lib/presentation/screens/maps/maps.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ class _MapsState extends State<Maps> {
return Padding(
padding: const EdgeInsets.all(5.0),
child: GestureDetector(
onLongPress: () =>
toggleFavoriteFunc(context, favoriteState, map),
onLongPress: () => favoriteState.toggleFavoriteFunc(
context, favoriteState, map),
onTap: () {
if (!favoriteState.multiSelect) {
widget.analyticsHelper.logEvent(
Expand All @@ -97,7 +97,8 @@ class _MapsState extends State<Maps> {
),
);
} else {
toggleFavoriteFunc(context, favoriteState, map);
favoriteState.toggleFavoriteFunc(
context, favoriteState, map);
}
},
child: MapCard(singleMap: map),
Expand Down
4 changes: 2 additions & 2 deletions lib/presentation/screens/maps/single_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class _SingleMapState extends State<SingleMap> {
Consumer<FavoriteState>(
builder: (context, favoriteState, child) {
return IconButton(
onPressed: () =>
toggleFavoriteFunc(context, favoriteState, map),
onPressed: () => favoriteState.toggleFavoriteFunc(
context, favoriteState, map),
icon: favoriteState.isFavorite(map.type, map.id)
? const Icon(Icons.star)
: const Icon(Icons.star_border_outlined),
Expand Down
23 changes: 16 additions & 7 deletions lib/presentation/screens/misc/favorite_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,23 @@ class _FavoriteScreenState extends State<FavoriteScreen> {
return Scaffold(
appBar: AppBar(
title: const Text('Favorites'),
actions: [
Consumer<FavoriteState>(
builder: (context, favoriteState, child) {
return IconButton(
onPressed: () {
favoriteState.toggleMultiSelect();
},
icon: Icon(
!favoriteState.multiSelect ? Icons.delete : Icons.close),
);
},
),
],
),
body: Consumer<FavoriteState>(
builder: (context, favoriteState, child) {
return ListView(
shrinkWrap: true,
children: generatedChildren,
);
},
body: ListView(
shrinkWrap: true,
children: generatedChildren,
),
);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/presentation/screens/tower/single_tower.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ class _SingleTowerState extends State<SingleTower> {
Consumer<FavoriteState>(
builder: (context, favoriteState, child) {
return IconButton(
onPressed: () =>
toggleFavoriteFunc(context, favoriteState, tower),
onPressed: () => favoriteState.toggleFavoriteFunc(
context, favoriteState, tower),
icon: favoriteState.isFavorite(tower.type, tower.id)
? const Icon(Icons.star)
: const Icon(Icons.star_border_outlined),
Expand Down
7 changes: 4 additions & 3 deletions lib/presentation/screens/tower/towers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ class _TowersState extends State<Towers> {

return InkWell(
borderRadius: BorderRadius.circular(20),
onLongPress: () =>
toggleFavoriteFunc(context, favoriteState, tower),
onLongPress: () => favoriteState.toggleFavoriteFunc(
context, favoriteState, tower),
onTap: () {
if (!favoriteState.multiSelect) {
widget.analyticsHelper.logEvent(
Expand All @@ -91,7 +91,8 @@ class _TowersState extends State<Towers> {
),
);
} else {
toggleFavoriteFunc(context, favoriteState, tower);
favoriteState.toggleFavoriteFunc(
context, favoriteState, tower);
}
},
child: Stack(
Expand Down
7 changes: 4 additions & 3 deletions lib/presentation/widgets/bloons/bloons_grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class BloonsGrid extends StatelessWidget {
return Consumer<FavoriteState>(
builder: (context, favoriteState, child) {
return InkWell(
onLongPress: () =>
toggleFavoriteFunc(context, favoriteState, bloon),
onLongPress: () => favoriteState.toggleFavoriteFunc(
context, favoriteState, bloon),
onTap: () {
if (!favoriteState.multiSelect) {
analyticsHelper.logEvent(
Expand All @@ -60,7 +60,8 @@ class BloonsGrid extends StatelessWidget {
),
);
} else {
toggleFavoriteFunc(context, favoriteState, bloon);
favoriteState.toggleFavoriteFunc(
context, favoriteState, bloon);
}
},
child: Card(
Expand Down
7 changes: 4 additions & 3 deletions lib/presentation/widgets/bloons/bosses_grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class BossesGrid extends StatelessWidget {
return Consumer<FavoriteState>(
builder: (context, favoriteState, child) {
return InkWell(
onLongPress: () =>
toggleFavoriteFunc(context, favoriteState, boss),
onLongPress: () => favoriteState.toggleFavoriteFunc(
context, favoriteState, boss),
onTap: () {
if (!favoriteState.multiSelect) {
analyticsHelper.logEvent(
Expand All @@ -59,7 +59,8 @@ class BossesGrid extends StatelessWidget {
),
);
} else {
toggleFavoriteFunc(context, favoriteState, boss);
favoriteState.toggleFavoriteFunc(
context, favoriteState, boss);
}
},
child: Card(
Expand Down
127 changes: 48 additions & 79 deletions lib/presentation/widgets/misc/orderable_grid.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:btd6wiki/utilities/constants.dart';
import 'package:btd6wiki/utilities/strings.dart';
import 'package:flutter_reorderable_grid_view/entities/order_update_entity.dart';
import 'package:flutter_reorderable_grid_view/widgets/widgets.dart';
import 'package:provider/provider.dart';
Expand Down Expand Up @@ -30,87 +32,54 @@ class _OrderableGridState extends State<OrderableGrid> {
final _scrollController = ScrollController();
@override
Widget build(BuildContext context) {
final generatedChildren = List.generate(
widget.favoriteItems.length,
(index) {
FavoriteModel favItem = widget.favoriteItems.elementAt(index);
return InkWell(
key: Key(favItem.id),
onTap: () {
navigateToPage(
context,
favItem,
widget.analyticsHelper,
kFavoritesClass,
card,
);
},
child: Card(
child: Padding(
padding: const EdgeInsets.all(8),
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
flex: 3,
child: Image(
image: AssetImage(
assetImagePath(widget.typeName, favItem.image)),
),
),
Flexible(
flex: 1,
child: Center(
child: Text(
favItem.name,
textAlign: TextAlign.center,
)),
),
],
),
return Consumer<FavoriteState>(
builder: (context, favoriteState, child) {
List<Widget> generatedChildren = generateGridChildren(
widget.favoriteItems, widget.analyticsHelper, widget.typeName);
if (generatedChildren.isNotEmpty) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Text(
capitalizeEveryWord(widget.typeName),
style: bigTitleStyle,
),
ReorderableBuilder(
scrollController: _scrollController,
onReorder: (List<OrderUpdateEntity> orderUpdateEntities) {
for (final orderUpdateEntity in orderUpdateEntities) {
final favItem = widget.favoriteItems
.removeAt(orderUpdateEntity.oldIndex);
widget.favoriteItems
.insert(orderUpdateEntity.newIndex, favItem);
}
favoriteState.updateIndexes(
widget.typeName, widget.favoriteItems);
},
builder: (children) {
return GridView(
key: widget.gridKey,
controller: _scrollController,
shrinkWrap: true,
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
mainAxisSpacing: 15,
crossAxisSpacing: 10,
crossAxisCount: 3,
childAspectRatio: 0.70,
),
children: children,
);
},
children: generatedChildren,
),
],
),
),
);
);
}
return Container();
},
);

return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Text(widget.typeName),
ReorderableBuilder(
scrollController: _scrollController,
onReorder: (List<OrderUpdateEntity> orderUpdateEntities) {
for (final orderUpdateEntity in orderUpdateEntities) {
final favItem =
widget.favoriteItems.removeAt(orderUpdateEntity.oldIndex);
widget.favoriteItems
.insert(orderUpdateEntity.newIndex, favItem);
}
Provider.of<FavoriteState>(context, listen: false)
.updateIndexes(widget.typeName, widget.favoriteItems);
},
builder: (children) {
return GridView(
key: widget.gridKey,
controller: _scrollController,
shrinkWrap: true,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
mainAxisSpacing: 15,
crossAxisSpacing: 10,
crossAxisCount: 3,
childAspectRatio: 0.70,
),
children: children,
);
},
children: generatedChildren,
),
],
),
);
}
}
13 changes: 13 additions & 0 deletions lib/utilities/favorite_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ class FavoriteState extends ChangeNotifier {
notifyListeners();
}

void toggleFavoriteFunc(
BuildContext context, FavoriteState favoriteState, var item) {
String msg = favoriteState.toggleFavorite(item);
ScaffoldMessenger.of(context).removeCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Center(child: Text(msg)),
duration: snackBarDuration,
),
);
notifyListeners();
}

String toggleFavorite(var item) {
bool addedToFavorites = false;
FavoriteModel favItem = _createFavoriteItem(item);
Expand Down
Loading

0 comments on commit c8e1d20

Please sign in to comment.