Update Collection providers

This commit is contained in:
Ankit Mahato
2023-05-03 18:04:45 +05:30
parent cd6c96376d
commit 7d44e2cd91
3 changed files with 23 additions and 29 deletions

View File

@ -28,6 +28,8 @@ final StateNotifierProvider<CollectionStateNotifier, List<RequestModel>?>
class CollectionStateNotifier extends StateNotifier<List<RequestModel>?> {
CollectionStateNotifier(this.ref, this.hiveHandler) : super(null) {
loadData();
Future.microtask(() =>
ref.read(activeIdStateProvider.notifier).update((s) => state?[0].id));
}
final Ref ref;
@ -42,12 +44,14 @@ class CollectionStateNotifier extends StateNotifier<List<RequestModel>?> {
return state![idx];
}
String add() {
void add() {
final newRequestModel = RequestModel(
id: uuid.v1(),
);
state = [newRequestModel, ...state!];
return newRequestModel.id;
ref
.read(activeIdStateProvider.notifier)
.update((state) => newRequestModel.id);
}
void reorder(int oldIdx, int newIdx) {
@ -56,11 +60,21 @@ class CollectionStateNotifier extends StateNotifier<List<RequestModel>?> {
}
void remove(String id) {
hiveHandler.delete(id);
int idx = idxOfId(id);
String? newId;
if (idx == 0 && state!.length > 1) {
newId = state![1].id;
} else if (state!.length > 2) {
newId = state![idx - 1].id;
} else {
newId = null;
}
state = [
for (final model in state!)
if (model.id != id) model,
];
ref.read(activeIdStateProvider.notifier).update((state) => newId);
}
void duplicate(String id) {
@ -146,16 +160,15 @@ class CollectionStateNotifier extends StateNotifier<List<RequestModel>?> {
state = [];
}
Future<void> loadData() async {
void loadData() {
var ids = hiveHandler.getIds();
if (ids == null) {
if (ids == null || ids.length == 0) {
state = [
RequestModel(
id: uuid.v1(),
),
];
} else {
await hiveHandler.removeUnused();
List<RequestModel> data = [];
for (var id in ids) {
var jsonModel = hiveHandler.getRequestModel(id);
@ -175,5 +188,6 @@ class CollectionStateNotifier extends StateNotifier<List<RequestModel>?> {
for (var e in state!) {
await hiveHandler.setRequestModel(e.id, e.toJson());
}
await hiveHandler.removeUnused();
}
}

View File

@ -38,21 +38,6 @@ class _CollectionPaneState extends ConsumerState<CollectionPane> {
Wrap(
alignment: WrapAlignment.spaceBetween,
children: [
TextButton.icon(
onPressed: () {
ref
.read(activeIdStateProvider.notifier)
.update((state) => null);
},
icon: const Icon(
Icons.home,
size: 20,
),
label: const Text(
'Home',
style: kTextStyleButton,
),
),
TextButton.icon(
onPressed: savingData
? null
@ -82,11 +67,7 @@ class _CollectionPaneState extends ConsumerState<CollectionPane> {
//const Spacer(),
ElevatedButton(
onPressed: () {
String newId =
ref.read(collectionStateNotifierProvider.notifier).add();
ref
.read(activeIdStateProvider.notifier)
.update((state) => newId);
ref.read(collectionStateNotifierProvider.notifier).add();
},
child: const Text(
kLabelPlusNew,
@ -205,7 +186,6 @@ class _RequestItemState extends ConsumerState<RequestItem> {
},
onMenuSelected: (RequestItemMenuOption item) {
if (item == RequestItemMenuOption.delete) {
ref.read(activeIdStateProvider.notifier).update((state) => null);
ref.read(collectionStateNotifierProvider.notifier).remove(widget.id);
}
if (item == RequestItemMenuOption.duplicate) {

View File

@ -71,15 +71,15 @@ class _DropdownButtonHTTPMethodState
@override
Widget build(BuildContext context) {
final activeId = ref.watch(activeIdStateProvider);
final method =
ref.watch(activeRequestModelProvider.select((value) => value?.method));
return DropdownButtonHttpMethod(
method: method,
onChanged: (HTTPVerb? value) {
final activeId = ref.read(activeRequestModelProvider)!.id;
ref
.read(collectionStateNotifierProvider.notifier)
.update(activeId!, method: value);
.update(activeId, method: value);
},
);
}