Skip to content

Commit e196f90

Browse files
committed
Added Undo button in delete snackbar
1 parent de1befe commit e196f90

24 files changed

+108
-26
lines changed

lib/Api/torrent_api.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import 'package:flood_mobile/Services/file_folder_nester.dart';
1717
import 'package:flood_mobile/l10n/l10n.dart';
1818

1919
class TorrentApi {
20+
static List<int>? undoDeletes;
21+
2022
// Gets list of torrents
2123
static Stream<List<TorrentModel>> getAllTorrents(
2224
{required BuildContext context}) async* {
@@ -245,7 +247,7 @@ class TorrentApi {
245247
String url =
246248
BlocProvider.of<ApiBloc>(context, listen: false).state.baseUrl +
247249
ApiEndpoints.deleteTorrent;
248-
print('---DELETE TORRENT---');
250+
print('---STARTING DELETE---');
249251
print(url);
250252
Response response;
251253
Dio dio = new Dio();
@@ -260,6 +262,9 @@ class TorrentApi {
260262
mp['deleteData'] = deleteWithData;
261263
String rawBody = json.encode(mp);
262264
print(rawBody);
265+
await Future.delayed(Duration(seconds: 4));
266+
if (undoDeletes == id) return;
267+
print('---DELETING TORRENT---');
263268
response = await dio.post(
264269
url,
265270
data: rawBody,
@@ -274,6 +279,14 @@ class TorrentApi {
274279
print('--ERROR IN TORRENT DELETE--');
275280
print(error.toString());
276281
}
282+
undoDeletes = null;
283+
}
284+
285+
static Future<void> undoDelete({
286+
required List<int> id,
287+
}) async {
288+
print('---UNDO DELETE---');
289+
undoDeletes = id;
277290
}
278291

279292
static Stream<Map<String, dynamic>> getTorrentContent({

lib/Pages/widgets/delete_torrent_sheet.dart

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import 'package:flood_mobile/Blocs/theme_bloc/theme_bloc.dart';
44
import 'package:flood_mobile/Model/torrent_model.dart';
55
import 'package:flood_mobile/Pages/widgets/flood_snackbar.dart';
66
import 'package:flood_mobile/l10n/l10n.dart';
7+
import 'package:flutter_bloc/flutter_bloc.dart';
8+
9+
import '../../Blocs/home_screen_bloc/home_screen_bloc.dart';
710

811
class DeleteTorrentSheet extends StatefulWidget {
912
final int themeIndex;
@@ -118,6 +121,21 @@ class _DeleteTorrentSheetState extends State<DeleteTorrentSheet> {
118121
),
119122
child: ElevatedButton(
120123
onPressed: () {
124+
final homeScreenBloc =
125+
BlocProvider.of<HomeScreenBloc>(context);
126+
List<TorrentModel> torrentList =
127+
homeScreenBloc.state.torrentList;
128+
129+
List<TorrentModel> remainingTorrents = torrentList
130+
.where((torrent) => !widget.torrents.any(
131+
(deletedTorrent) => deletedTorrent == torrent))
132+
.toList();
133+
134+
final SetTorrentListEvent setTorrentListEvent =
135+
SetTorrentListEvent(
136+
newTorrentList: remainingTorrents);
137+
homeScreenBloc.add(setTorrentListEvent);
138+
121139
List<String> hashes = [];
122140
widget.torrents.forEach((element) {
123141
hashes.add(element.hash);
@@ -132,7 +150,13 @@ class _DeleteTorrentSheetState extends State<DeleteTorrentSheet> {
132150
final deleteTorrentSnackBar = addFloodSnackBar(
133151
SnackbarType.caution,
134152
context.l10n.torrent_delete_snackbar,
135-
context.l10n.button_dismiss);
153+
context.l10n.button_dismiss,
154+
undoText: context.l10n.button_undo, undoFunction: () {
155+
final SetTorrentListEvent setTorrentListEvent =
156+
SetTorrentListEvent(newTorrentList: torrentList);
157+
homeScreenBloc.add(setTorrentListEvent);
158+
TorrentApi.undoDelete(id: widget.indexes);
159+
});
136160

137161
ScaffoldMessenger.of(context).clearSnackBars();
138162
ScaffoldMessenger.of(context)

lib/Pages/widgets/flood_snackbar.dart

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ enum SnackbarType {
99
SnackBar addFloodSnackBar(
1010
SnackbarType snackbarType,
1111
String title,
12-
String ctaText,
13-
) {
12+
String ctaText, {
13+
String? undoText,
14+
VoidCallback? undoFunction,
15+
}) {
1416
return SnackBar(
1517
backgroundColor: snackbarType == SnackbarType.success
1618
? Colors.greenAccent
1719
: snackbarType == SnackbarType.information
1820
? Colors.lightBlueAccent
1921
: Colors.orange,
2022
content: Row(
23+
mainAxisAlignment: MainAxisAlignment.spaceBetween,
2124
children: [
2225
Icon(
2326
snackbarType == SnackbarType.success
@@ -29,7 +32,7 @@ SnackBar addFloodSnackBar(
2932
size: 20,
3033
),
3134
SizedBox(
32-
width: 8,
35+
width: undoText == null ? 8 : 12,
3336
),
3437
Expanded(
3538
child: Text(
@@ -41,6 +44,27 @@ SnackBar addFloodSnackBar(
4144
),
4245
),
4346
),
47+
Visibility(
48+
visible: undoText != null,
49+
child: Row(
50+
children: [
51+
SizedBox(
52+
width: 8,
53+
),
54+
TextButton(
55+
onPressed: undoFunction,
56+
child: Text(
57+
undoText ?? "Undo",
58+
overflow: TextOverflow.ellipsis,
59+
maxLines: 2,
60+
style: TextStyle(
61+
color: Colors.white,
62+
),
63+
),
64+
),
65+
],
66+
),
67+
)
4468
],
4569
),
4670
action: SnackBarAction(

lib/l10n/arb/app_ar.arb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,5 +326,6 @@
326326
"tag_selection_heading": "تفضيلات محدد الوسوم",
327327
"single_selection_radio_button": "اختيار واحد",
328328
"multi_selection_radio_button": "اختيار متعدد",
329-
"show_progress_bar_option": "عرض شريط التقدم"
329+
"show_progress_bar_option": "عرض شريط التقدم",
330+
"button_undo": "تراجع"
330331
}

lib/l10n/arb/app_cs.arb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,5 +326,6 @@
326326
"tag_selection_heading": "Předvolba výběru značek",
327327
"single_selection_radio_button": "Jednoduchý výběr",
328328
"multi_selection_radio_button": "Více výběrů",
329-
"show_progress_bar_option": "Zobrazovat pruh postupu"
329+
"show_progress_bar_option": "Zobrazovat pruh postupu",
330+
"button_undo": "Zpět"
330331
}

lib/l10n/arb/app_de.arb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,5 +325,6 @@
325325
"sort_by_upload_speed": "Upload-Geschwindigkeit",
326326
"sort_by_file_size": "Dateigröße",
327327
"login_field_tooltip_message": "URL für Ihre Flood-Instanz (lokal, seedbox...)",
328-
"sort_by_percent_completed": "Prozent abgeschlossen","show_progress_bar_option": "Fortschrittsbalken anzeigen"
328+
"sort_by_percent_completed": "Prozent abgeschlossen","show_progress_bar_option": "Fortschrittsbalken anzeigen",
329+
"button_undo": "Rückgängig"
329330
}

lib/l10n/arb/app_en.arb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@
340340
"tag_selection_heading": "Tag Selector Preference",
341341
"single_selection_radio_button":"Single Selection",
342342
"multi_selection_radio_button":"Multi Selection",
343-
"show_progress_bar_option":"Show Progress Bar"
343+
"show_progress_bar_option":"Show Progress Bar",
344+
"button_undo": "Undo"
344345
}
345346

lib/l10n/arb/app_es.arb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,5 +326,6 @@
326326
"tag_selection_heading": "Preferencia del selector de etiquetas",
327327
"single_selection_radio_button": "Selección única",
328328
"multi_selection_radio_button": "Selección múltiple",
329-
"show_progress_bar_option": "Mostrar barra de progreso"
329+
"show_progress_bar_option": "Mostrar barra de progreso",
330+
"button_undo": "Deshacer"
330331
}

lib/l10n/arb/app_fi.arb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,5 +326,6 @@
326326
"tag_selection_heading": "Tunnistevalitsimen asetukset",
327327
"single_selection_radio_button": "Yksittäinen valinta",
328328
"multi_selection_radio_button": "Useita valintoja",
329-
"show_progress_bar_option": "Näytä edistymispalkki"
329+
"show_progress_bar_option": "Näytä edistymispalkki",
330+
"button_undo": "Kumoa"
330331
}

lib/l10n/arb/app_fr.arb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,5 +327,6 @@
327327
"tag_selection_heading": "Préférence du sélecteur de balises",
328328
"single_selection_radio_button": "Sélection unique",
329329
"multi_selection_radio_button": "Sélection multiple",
330-
"show_progress_bar_option": "Afficher la barre de progression"
330+
"show_progress_bar_option": "Afficher la barre de progression",
331+
"button_undo": "Annuler"
331332
}

0 commit comments

Comments
 (0)