mirror of
https://github.com/foss42/apidash.git
synced 2025-10-20 13:14:49 +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>?> {
|
class CollectionStateNotifier extends StateNotifier<List<RequestModel>?> {
|
||||||
CollectionStateNotifier(this.ref, this.hiveHandler) : super(null) {
|
CollectionStateNotifier(this.ref, this.hiveHandler) : super(null) {
|
||||||
loadData();
|
loadData();
|
||||||
|
Future.microtask(() =>
|
||||||
|
ref.read(activeIdStateProvider.notifier).update((s) => state?[0].id));
|
||||||
}
|
}
|
||||||
|
|
||||||
final Ref ref;
|
final Ref ref;
|
||||||
@ -42,12 +44,14 @@ class CollectionStateNotifier extends StateNotifier<List<RequestModel>?> {
|
|||||||
return state![idx];
|
return state![idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
String add() {
|
void add() {
|
||||||
final newRequestModel = RequestModel(
|
final newRequestModel = RequestModel(
|
||||||
id: uuid.v1(),
|
id: uuid.v1(),
|
||||||
);
|
);
|
||||||
state = [newRequestModel, ...state!];
|
state = [newRequestModel, ...state!];
|
||||||
return newRequestModel.id;
|
ref
|
||||||
|
.read(activeIdStateProvider.notifier)
|
||||||
|
.update((state) => newRequestModel.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
void reorder(int oldIdx, int newIdx) {
|
void reorder(int oldIdx, int newIdx) {
|
||||||
@ -56,11 +60,21 @@ class CollectionStateNotifier extends StateNotifier<List<RequestModel>?> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void remove(String id) {
|
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 = [
|
state = [
|
||||||
for (final model in state!)
|
for (final model in state!)
|
||||||
if (model.id != id) model,
|
if (model.id != id) model,
|
||||||
];
|
];
|
||||||
|
ref.read(activeIdStateProvider.notifier).update((state) => newId);
|
||||||
}
|
}
|
||||||
|
|
||||||
void duplicate(String id) {
|
void duplicate(String id) {
|
||||||
@ -146,16 +160,15 @@ class CollectionStateNotifier extends StateNotifier<List<RequestModel>?> {
|
|||||||
state = [];
|
state = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> loadData() async {
|
void loadData() {
|
||||||
var ids = hiveHandler.getIds();
|
var ids = hiveHandler.getIds();
|
||||||
if (ids == null) {
|
if (ids == null || ids.length == 0) {
|
||||||
state = [
|
state = [
|
||||||
RequestModel(
|
RequestModel(
|
||||||
id: uuid.v1(),
|
id: uuid.v1(),
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
} else {
|
} else {
|
||||||
await hiveHandler.removeUnused();
|
|
||||||
List<RequestModel> data = [];
|
List<RequestModel> data = [];
|
||||||
for (var id in ids) {
|
for (var id in ids) {
|
||||||
var jsonModel = hiveHandler.getRequestModel(id);
|
var jsonModel = hiveHandler.getRequestModel(id);
|
||||||
@ -175,5 +188,6 @@ class CollectionStateNotifier extends StateNotifier<List<RequestModel>?> {
|
|||||||
for (var e in state!) {
|
for (var e in state!) {
|
||||||
await hiveHandler.setRequestModel(e.id, e.toJson());
|
await hiveHandler.setRequestModel(e.id, e.toJson());
|
||||||
}
|
}
|
||||||
|
await hiveHandler.removeUnused();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,21 +38,6 @@ class _CollectionPaneState extends ConsumerState<CollectionPane> {
|
|||||||
Wrap(
|
Wrap(
|
||||||
alignment: WrapAlignment.spaceBetween,
|
alignment: WrapAlignment.spaceBetween,
|
||||||
children: [
|
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(
|
TextButton.icon(
|
||||||
onPressed: savingData
|
onPressed: savingData
|
||||||
? null
|
? null
|
||||||
@ -82,11 +67,7 @@ class _CollectionPaneState extends ConsumerState<CollectionPane> {
|
|||||||
//const Spacer(),
|
//const Spacer(),
|
||||||
ElevatedButton(
|
ElevatedButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
String newId =
|
|
||||||
ref.read(collectionStateNotifierProvider.notifier).add();
|
ref.read(collectionStateNotifierProvider.notifier).add();
|
||||||
ref
|
|
||||||
.read(activeIdStateProvider.notifier)
|
|
||||||
.update((state) => newId);
|
|
||||||
},
|
},
|
||||||
child: const Text(
|
child: const Text(
|
||||||
kLabelPlusNew,
|
kLabelPlusNew,
|
||||||
@ -205,7 +186,6 @@ class _RequestItemState extends ConsumerState<RequestItem> {
|
|||||||
},
|
},
|
||||||
onMenuSelected: (RequestItemMenuOption item) {
|
onMenuSelected: (RequestItemMenuOption item) {
|
||||||
if (item == RequestItemMenuOption.delete) {
|
if (item == RequestItemMenuOption.delete) {
|
||||||
ref.read(activeIdStateProvider.notifier).update((state) => null);
|
|
||||||
ref.read(collectionStateNotifierProvider.notifier).remove(widget.id);
|
ref.read(collectionStateNotifierProvider.notifier).remove(widget.id);
|
||||||
}
|
}
|
||||||
if (item == RequestItemMenuOption.duplicate) {
|
if (item == RequestItemMenuOption.duplicate) {
|
||||||
|
@ -71,15 +71,15 @@ class _DropdownButtonHTTPMethodState
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final activeId = ref.watch(activeIdStateProvider);
|
|
||||||
final method =
|
final method =
|
||||||
ref.watch(activeRequestModelProvider.select((value) => value?.method));
|
ref.watch(activeRequestModelProvider.select((value) => value?.method));
|
||||||
return DropdownButtonHttpMethod(
|
return DropdownButtonHttpMethod(
|
||||||
method: method,
|
method: method,
|
||||||
onChanged: (HTTPVerb? value) {
|
onChanged: (HTTPVerb? value) {
|
||||||
|
final activeId = ref.read(activeRequestModelProvider)!.id;
|
||||||
ref
|
ref
|
||||||
.read(collectionStateNotifierProvider.notifier)
|
.read(collectionStateNotifierProvider.notifier)
|
||||||
.update(activeId!, method: value);
|
.update(activeId, method: value);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user