mirror of
https://github.com/foss42/apidash.git
synced 2025-10-16 19:22:23 +08:00
Update Collection providers
This commit is contained in:
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user