-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from RedCommander735/6-select-item-on-longpress
[FEATURE] Select item on long press
- Loading branch information
Showing
8 changed files
with
279 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
class Popup { | ||
static showAlert( | ||
{required BuildContext context, | ||
required String title, | ||
required String content, | ||
String? snackBarText, | ||
required void Function() onConfirm, | ||
void Function()? onDeny}) { | ||
showDialog<void>( | ||
context: context, | ||
builder: (context) => AlertDialog( | ||
title: Text(title), | ||
content: Text(content), | ||
actions: [ | ||
TextButton( | ||
child: const Text('Abbrechen'), | ||
onPressed: () { | ||
if (onDeny != null) onDeny(); | ||
Navigator.of(context).pop(); | ||
}, | ||
), | ||
TextButton( | ||
child: const Text('Bestätigen'), | ||
onPressed: () { | ||
if (snackBarText != null) { | ||
ScaffoldMessenger.of(context).showSnackBar(SnackBar( | ||
content: Text(snackBarText), | ||
showCloseIcon: true, | ||
)); | ||
} | ||
|
||
onConfirm(); | ||
|
||
Navigator.of(context).pop(); | ||
}, | ||
), | ||
], | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,96 @@ | ||
import 'dart:collection'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:spritverbrauch/src/utils/sqlite_service.dart'; | ||
|
||
class ItemListModel extends ChangeNotifier { | ||
|
||
late SqliteService _sqliteService; | ||
bool _sqliteInitialized = false; | ||
|
||
List<ListItem> _items = []; | ||
int _hiddenEntries = 0; | ||
DateTime? _start; | ||
DateTime? _end; | ||
|
||
bool _multiselect = false; | ||
ValueNotifier<bool> openCloseDial = ValueNotifier(false); | ||
int selected = 0; | ||
|
||
UnmodifiableListView<ListItem> get items => UnmodifiableListView(_items); | ||
int get hiddenEntries => _hiddenEntries; | ||
|
||
bool get multiselect => _multiselect; | ||
|
||
set multiselect(bool enabled) { | ||
if (!enabled) { | ||
deselectAll(); | ||
} else { | ||
openCloseDial.value = true; | ||
} | ||
_multiselect = enabled; | ||
} | ||
|
||
void load() async { | ||
final sqlitesevice = SqliteService(); | ||
final list = await sqlitesevice.getItems(); | ||
if (!_sqliteInitialized) { | ||
_sqliteService = SqliteService(); | ||
_sqliteInitialized = true; | ||
} | ||
final list = await _sqliteService.getItems(); | ||
_items = list; | ||
|
||
notifyListeners(); | ||
} | ||
|
||
void loadFiltered({DateTime? start, DateTime? end}) async { | ||
_start = start; | ||
_end = end; | ||
final sqlitesevice = SqliteService(); | ||
final list = await sqlitesevice.getItems(); | ||
final listFiltered = await sqlitesevice.getItemsFiltered(start ?? DateTime(1970), end ?? DateTime.now()); | ||
final list = await _sqliteService.getItems(); | ||
final listFiltered = await _sqliteService.getItemsFiltered(start ?? DateTime(1970), end ?? DateTime.now()); | ||
_items = listFiltered; | ||
_hiddenEntries = list.length - listFiltered.length; | ||
|
||
notifyListeners(); | ||
} | ||
|
||
|
||
void add(ListItem item) { | ||
final sqlitesevice = SqliteService(); | ||
sqlitesevice.createItem(item); | ||
_sqliteService.createItem(item); | ||
_items.add(item); | ||
|
||
notifyListeners(); | ||
} | ||
|
||
void remove(List<ListItem> passedItems) { | ||
for (var item in passedItems) { | ||
_sqliteService.deleteItem(item.id); | ||
_items.remove(item); | ||
} | ||
} | ||
|
||
List<ListItem> get getSelected { | ||
return UnmodifiableListView(_items.where((element) => element.selected)); | ||
} | ||
|
||
void select(List<ListItem> passedItems) { | ||
for (var item in passedItems) { | ||
item.selected = !item.selected; | ||
|
||
if (item.selected) { | ||
selected += 1; | ||
} else { | ||
selected -= 1; | ||
} | ||
|
||
if (selected < 1) { | ||
_multiselect = false; | ||
openCloseDial.value = false; | ||
} | ||
} | ||
|
||
void remove(ListItem item) { | ||
final sqlitesevice = SqliteService(); | ||
sqlitesevice.deleteItem(item.id).then((value) => { | ||
if (value > 0) {loadFiltered(start: _start, end: _end)}}); | ||
notifyListeners(); | ||
} | ||
|
||
void deselectAll() { | ||
for (ListItem item in _items) { | ||
item.selected = false; | ||
} | ||
selected = 0; | ||
notifyListeners(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.