Skip to content

Commit

Permalink
Merge pull request #276 from tusharlock10/236-add-option-to-remove-lo…
Browse files Browse the repository at this point in the history
…adouts

feat(loadout): add functionality to delete custom loadout
  • Loading branch information
tusharlock10 authored May 4, 2022
2 parents be7c0bb + 4768e5e commit 47d0555
Show file tree
Hide file tree
Showing 26 changed files with 488 additions and 175 deletions.
Binary file modified assets/icons/google-colored.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/icons/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/icons/paladins.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions lib/api/loadout/requests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,26 @@ abstract class LoadoutRequests {
return null;
}
}

static Future<responses.DeletePlayerLoadoutResponse?> deletePlayerLoadout({
required String loadoutHash,
bool? dryRun,
}) async {
try {
final response = await utilities.api.delete<Map<String, dynamic>>(
constants.Urls.deletePlayerLoadout,
queryParameters: {
'loadoutHash': loadoutHash,
'dryRun': dryRun ?? false,
},
);
if (response.data != null) {
return responses.DeletePlayerLoadoutResponse.fromJson(response.data!);
}

return null;
} catch (_) {
return null;
}
}
}
11 changes: 11 additions & 0 deletions lib/api/loadout/responses.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,14 @@ class SavePlayerLoadoutResponse {
_$SavePlayerLoadoutResponseFromJson(json);
Map<String, dynamic> toJson() => _$SavePlayerLoadoutResponseToJson(this);
}

@JsonSerializable()
class DeletePlayerLoadoutResponse {
final Loadout loadout;

DeletePlayerLoadoutResponse({required this.loadout});

factory DeletePlayerLoadoutResponse.fromJson(Map<String, dynamic> json) =>
_$DeletePlayerLoadoutResponseFromJson(json);
Map<String, dynamic> toJson() => _$DeletePlayerLoadoutResponseToJson(this);
}
12 changes: 12 additions & 0 deletions lib/api/loadout/responses.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ abstract class Urls {
static const playerLoadouts = "/loadout/playerLoadouts"; // GET
static const savePlayerLoadout = "/loadout/savePlayerLoadout"; // POST
static const updatePlayerLoadout = '/loadout/updatePlayerLoadout'; // PUT
static const deletePlayerLoadout = '/loadout/deletePlayerLoadout'; // DELETE

// feedback
static const submitFeedback = "/feedback/submitFeedback"; // POST
Expand Down
22 changes: 22 additions & 0 deletions lib/providers/loadout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:paladinsedge/api/index.dart' as api;
import 'package:paladinsedge/constants.dart' as constants;
import 'package:paladinsedge/data_classes/index.dart' as data_classes;
import 'package:paladinsedge/models/index.dart' as models;
import 'package:paladinsedge/utilities/index.dart' as utilities;
Expand Down Expand Up @@ -162,6 +163,27 @@ class _LoadoutNotifier extends ChangeNotifier {
return result != null;
}

/// Deletes a loadout using its loadoutHash
Future<void> deleteLoadout(String loadoutHash) async {
if (loadouts == null) return;

final loadoutsClone = [...loadouts!];
// temporarily delete the loadout from loadouts
loadouts = loadouts!.where((_) => _.loadoutHash != loadoutHash).toList();
notifyListeners();

// make api call to delete loadout
final response = await api.LoadoutRequests.deletePlayerLoadout(
loadoutHash: loadoutHash,
dryRun: constants.isDebug,
);
if (response == null) {
// revert the changes in case of failure
loadouts = loadoutsClone;
notifyListeners();
}
}

/// Clears all user sensitive data upon logout
void clearData() {
isGettingLoadouts = true;
Expand Down
45 changes: 27 additions & 18 deletions lib/screens/create_loadout/create_loadout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:paladinsedge/data_classes/index.dart' as data_classes;
import 'package:paladinsedge/providers/index.dart' as providers;
import 'package:paladinsedge/screens/create_loadout/create_loadout_delete_button.dart';
import 'package:paladinsedge/screens/create_loadout/create_loadout_draggable_cards.dart';
import 'package:paladinsedge/screens/create_loadout/create_loadout_target.dart';
import 'package:paladinsedge/screens/create_loadout/create_loadout_text.dart';
import 'package:paladinsedge/widgets/index.dart' as widgets;

class CreateLoadout extends HookConsumerWidget {
Expand All @@ -23,14 +25,15 @@ class CreateLoadout extends HookConsumerWidget {
// Variables
final arguments = ModalRoute.of(context)?.settings.arguments
as data_classes.CreateLoadoutScreenArguments;
final loadout = arguments.loadout;

// Effects
useEffect(
() {
loadoutProvider.createDraftLoadout(
championId: arguments.champion.championId,
playerId: authProvider.player!.playerId,
loadout: arguments.loadout,
loadout: loadout,
);

return loadoutProvider.resetDraftLoadout;
Expand Down Expand Up @@ -64,10 +67,19 @@ class CreateLoadout extends HookConsumerWidget {
[],
);

final onDelete = useCallback(
() {
if (loadout?.loadoutHash != null) {
loadoutProvider.deleteLoadout(loadout!.loadoutHash!);
Navigator.of(context).pop();
}
},
[],
);

return Scaffold(
appBar: AppBar(
title:
Text(arguments.loadout != null ? "Edit Loadout" : "Create Loadout"),
title: Text(loadout != null ? "Edit Loadout" : "Create Loadout"),
actions: [
Padding(
padding: const EdgeInsets.only(right: 20),
Expand Down Expand Up @@ -108,21 +120,18 @@ class CreateLoadout extends HookConsumerWidget {
padding: const EdgeInsets.symmetric(vertical: 10),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: const [
SizedBox(height: 20),
CreateLoadoutTarget(),
SizedBox(height: 30),
Padding(
padding: EdgeInsets.symmetric(horizontal: 25),
child: Text('''
* Select a card from the list and drag it in the loadout
* Tap the card in the loadout to change its points
* Rename the loadout to your liking and save
'''),
),
SizedBox(height: 30),
CreateLoadoutDraggableCards(),
SizedBox(height: 30),
children: [
const SizedBox(height: 20),
const CreateLoadoutTarget(),
const SizedBox(height: 30),
const CreateLoadoutText(),
const SizedBox(height: 30),
const CreateLoadoutDraggableCards(),
const SizedBox(height: 30),
if (loadout?.loadoutHash != null)
CreateLoadoutDeleteButton(
onDelete: onDelete,
),
],
),
),
Expand Down
26 changes: 26 additions & 0 deletions lib/screens/create_loadout/create_loadout_delete_button.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:flutter/material.dart';
import 'package:flutter_feather_icons/flutter_feather_icons.dart';
import 'package:paladinsedge/screens/create_loadout/create_loadout_delete_confirmation.dart';
import 'package:paladinsedge/widgets/index.dart' as widgets;

class CreateLoadoutDeleteButton extends StatelessWidget {
final void Function() onDelete;
const CreateLoadoutDeleteButton({
required this.onDelete,
Key? key,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return widgets.Button(
label: 'Delete',
color: Colors.red,
disabled: false,
leading: FeatherIcons.trash,
onPressed: () => showCreateLoadoutDeleteConfirmation(
context: context,
onDelete: onDelete,
),
);
}
}
103 changes: 103 additions & 0 deletions lib/screens/create_loadout/create_loadout_delete_confirmation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:paladinsedge/widgets/index.dart' as widgets;

void showCreateLoadoutDeleteConfirmation({
required BuildContext context,
required void Function() onDelete,
}) {
final backgroundColor = Theme.of(context).scaffoldBackgroundColor;

showModalBottomSheet(
elevation: 10,
backgroundColor: backgroundColor,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15),
topRight: Radius.circular(15),
),
),
context: context,
builder: (_) => _CreateLoadoutDeleteConfirmation(onDelete: onDelete),
);
}

class _CreateLoadoutDeleteConfirmation extends HookWidget {
final void Function() onDelete;
const _CreateLoadoutDeleteConfirmation({
required this.onDelete,
Key? key,
}) : super(key: key);

@override
Widget build(BuildContext context) {
// Variables
final textTheme = Theme.of(context).textTheme;

// Methods
final onDeleteConfirm = useCallback(
() {
Navigator.of(context).pop();
onDelete();
},
[],
);

return Column(
mainAxisSize: MainAxisSize.min,
children: [
Card(
elevation: 0,
clipBehavior: Clip.hardEdge,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10)),
),
margin: const EdgeInsets.all(15),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 10),
child: Column(
children: [
Align(
alignment: Alignment.centerLeft,
child: Padding(
padding: const EdgeInsets.only(right: 10),
child: Text(
'DELETE LOADOUT',
style: textTheme.headline1?.copyWith(
fontSize: 20,
fontWeight: FontWeight.w900,
),
),
),
),
const SizedBox(height: 10),
Text(
'Are you sure, you want to delete this loadout?',
style: textTheme.bodyText1?.copyWith(fontSize: 16),
),
],
),
),
),
const SizedBox(height: 5),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
widgets.Button(
label: 'Cancel',
onPressed: Navigator.of(context).pop,
color: Colors.red,
style: widgets.ButtonStyle.outlined,
),
widgets.Button(
label: 'Confirm',
onPressed: onDeleteConfirm,
color: Colors.green,
),
],
),
const SizedBox(height: 15),
],
);
}
}
Loading

0 comments on commit 47d0555

Please sign in to comment.