Skip to content

Commit

Permalink
Small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
BhasherBEL committed Jun 6, 2023
1 parent 2039cfd commit 7d2f978
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 165 deletions.
154 changes: 2 additions & 152 deletions lib/components/pages/new_project_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import 'package:shared/model/participant.dart';

import '../../model/project.dart';
import '../../model/project_data.dart';
import '../../utils/tiles/header_tile.dart';
import '../../utils/tiles/participant_tile.dart';
import 'new_project/local.dart';
import 'new_project/pocketbase.dart';

Expand Down Expand Up @@ -127,155 +129,3 @@ class _ParticipantListWidgetState extends State<ParticipantListWidget> {
);
}
}

class HeaderTile extends StatelessWidget {
const HeaderTile(this.text, {super.key});

final String text;

@override
Widget build(BuildContext context) {
return ListTile(
title: Text(
text,
style: const TextStyle(
fontFeatures: [FontFeature.enable('smcp')],
),
),
tileColor: Theme.of(context).splashColor,
dense: false,
);
}
}

class ParticipantTile extends StatefulWidget {
ParticipantTile({
super.key,
required this.project,
required this.participant,
required this.onChange,
required this.setHasNew,
});

final Project project;
Participant? participant;
final Function() onChange;
final void Function(bool v) setHasNew;

@override
State<ParticipantTile> createState() => _ParticipantTileState();
}

class _ParticipantTileState extends State<ParticipantTile> {
late TextEditingController controller;

bool edit = false;

@override
Widget build(BuildContext context) {
bool hasParticipant = widget.participant != null;
if (!hasParticipant) edit = true;

bool isMe = widget.participant == widget.project.currentParticipant;

controller = TextEditingController(
text: hasParticipant
? widget.participant!.pseudo + (isMe && !edit ? ' (me)' : '')
: '',
);

return ListTile(
onTap: () {
widget.project.currentParticipant = widget.participant;
widget.onChange();
},
title: TextField(
autofocus: true,
enabled: edit,
maxLines: 1,
maxLength: 30,
maxLengthEnforcement: MaxLengthEnforcement.enforced,
decoration: InputDecoration(
counterText: "",
border: edit ? null : InputBorder.none,
),
controller: controller,
style: TextStyle(
fontWeight: isMe ? FontWeight.bold : FontWeight.normal,
),
),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
onPressed: () async {
edit = !edit;
if (!edit) {
if (hasParticipant) {
widget.participant!.pseudo = controller.text;
await widget.participant!.conn.save();
} else {
widget.participant = Participant(
pseudo: controller.text,
project: widget.project,
);
await widget.participant!.conn.save();
widget.project.participants.add(widget.participant!);
widget.setHasNew(false);
return;
}
}
setState(() {});
},
icon: Icon(edit ? Icons.done : Icons.edit),
),
IconButton(
icon: const Icon(Icons.close),
onPressed: () async {
if (hasParticipant) {
await confirmBox(
context: context,
title: "Remove ${widget.participant!.pseudo}",
content:
"Are you sure you want to remove ${widget.participant!.pseudo}? You will not be able to undo it.",
onValidate: () async {
await widget.project
.deleteParticipant(widget.participant!);
widget.onChange();
if (context.mounted) Navigator.of(context).pop();
},
);
} else {
widget.setHasNew(false);
}
}),
],
),
);
}

Future<dynamic> confirmBox({
required BuildContext context,
required String title,
required String content,
required Function()? onValidate,
}) {
return showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(title),
content: Text(content),
actions: [
TextButton(
onPressed: onValidate,
child: const Text("Yes"),
),
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text("No"),
),
],
),
);
}
}
17 changes: 4 additions & 13 deletions lib/components/pages/refund_page_part.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,19 @@ import 'dart:math';
import 'package:flutter/material.dart';
import 'package:shared/model/participant.dart';
import 'package:shared/model/project.dart';
import 'package:shared/utils/tiles/header_tile.dart';

List<ListTile> getRefundPageTiles({
List<Widget> getRefundPageTiles({
required final Project project,
required final Map<Participant, double> parts,
required final List<Participant> sortedParticipants,
required final BuildContext context,
}) {
bool hasMe = project.currentParticipant != null;

List<ListTile> tiles = [];
List<Widget> tiles = [];

tiles.add(
ListTile(
title: const Padding(
padding: EdgeInsets.only(left: 8.0),
child: Text(
"How to refund ?",
),
),
tileColor: Theme.of(context).splashColor,
),
);
tiles.add(const HeaderTile("How to refund ?"));

if (hasMe) {
tiles.add(
Expand Down
26 changes: 26 additions & 0 deletions lib/utils/dialogs/confirm_box.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:flutter/material.dart';

Future<dynamic> confirmBox({
required BuildContext context,
required String title,
required String content,
required Function()? onValidate,
}) {
return showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(title),
content: Text(content),
actions: [
TextButton(
onPressed: onValidate,
child: const Text("Yes"),
),
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text("No"),
),
],
),
);
}
23 changes: 23 additions & 0 deletions lib/utils/tiles/header_tile.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'dart:ui';

import 'package:flutter/material.dart';

class HeaderTile extends StatelessWidget {
const HeaderTile(this.text, {super.key});

final String text;

@override
Widget build(BuildContext context) {
return ListTile(
title: Text(
text,
style: const TextStyle(
fontFeatures: [FontFeature.enable('smcp')],
),
),
tileColor: Theme.of(context).splashColor,
dense: false,
);
}
}
113 changes: 113 additions & 0 deletions lib/utils/tiles/participant_tile.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../../model/participant.dart';
import '../../model/project.dart';
import '../dialogs/confirm_box.dart';

class ParticipantTile extends StatefulWidget {
ParticipantTile({
super.key,
required this.project,
required this.participant,
required this.onChange,
required this.setHasNew,
});

final Project project;
Participant? participant;
final Function() onChange;
final void Function(bool v) setHasNew;

@override
State<ParticipantTile> createState() => _ParticipantTileState();
}

class _ParticipantTileState extends State<ParticipantTile> {
late TextEditingController controller;

bool edit = false;

@override
Widget build(BuildContext context) {
bool hasParticipant = widget.participant != null;
if (!hasParticipant) edit = true;

bool isMe = widget.participant == widget.project.currentParticipant;

controller = TextEditingController(
text: hasParticipant
? widget.participant!.pseudo + (isMe && !edit ? ' (me)' : '')
: '',
);

return ListTile(
onTap: () {
widget.project.currentParticipant = widget.participant;
widget.onChange();
},
title: TextField(
autofocus: true,
enabled: edit,
maxLines: 1,
maxLength: 30,
maxLengthEnforcement: MaxLengthEnforcement.enforced,
decoration: InputDecoration(
counterText: "",
border: edit ? null : InputBorder.none,
),
controller: controller,
style: TextStyle(
fontWeight: isMe ? FontWeight.bold : FontWeight.normal,
),
),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
onPressed: () async {
edit = !edit;
if (!edit) {
if (hasParticipant) {
widget.participant!.pseudo = controller.text;
await widget.participant!.conn.save();
} else {
widget.participant = Participant(
pseudo: controller.text,
project: widget.project,
);
await widget.participant!.conn.save();
widget.project.participants.add(widget.participant!);
widget.setHasNew(false);
return;
}
}
setState(() {});
},
icon: Icon(edit ? Icons.done : Icons.edit),
),
IconButton(
icon: const Icon(Icons.close),
onPressed: () async {
if (hasParticipant) {
await confirmBox(
context: context,
title: "Remove ${widget.participant!.pseudo}",
content:
"Are you sure you want to remove ${widget.participant!.pseudo}? You will not be able to undo it.",
onValidate: () async {
await widget.project
.deleteParticipant(widget.participant!);
widget.onChange();
if (context.mounted) Navigator.of(context).pop();
},
);
} else {
widget.setHasNew(false);
}
}),
],
),
);
}
}

0 comments on commit 7d2f978

Please sign in to comment.