second draft
This commit is contained in:
@@ -75,8 +75,12 @@ class ApiService {
|
||||
String listId, {
|
||||
String? title,
|
||||
List<String>? members,
|
||||
String? layoutId,
|
||||
bool clearLayout = false,
|
||||
}) async {
|
||||
if (title == null && members == null) return;
|
||||
if (title == null && members == null && layoutId == null && !clearLayout) {
|
||||
return;
|
||||
}
|
||||
final body = <String, dynamic>{};
|
||||
if (title != null) {
|
||||
body['title'] = title;
|
||||
@@ -84,6 +88,12 @@ class ApiService {
|
||||
if (members != null) {
|
||||
body['members'] = members;
|
||||
}
|
||||
if (layoutId != null) {
|
||||
body['layout'] = layoutId;
|
||||
}
|
||||
if (clearLayout && layoutId == null) {
|
||||
body['layout'] = null;
|
||||
}
|
||||
await pb.collection('lists').update(listId, body: body);
|
||||
}
|
||||
|
||||
@@ -118,7 +128,12 @@ class ApiService {
|
||||
return res.map((r) => Item.fromJson(r.toJson())).toList();
|
||||
}
|
||||
|
||||
Future<Item> createItem(String listId, String name, int position) async {
|
||||
Future<Item> createItem(
|
||||
String listId,
|
||||
String name,
|
||||
int position, {
|
||||
String? sectionId,
|
||||
}) async {
|
||||
final RecordModel rec = await pb
|
||||
.collection('items')
|
||||
.create(
|
||||
@@ -127,6 +142,7 @@ class ApiService {
|
||||
'name': name,
|
||||
'position': position,
|
||||
'checked': false,
|
||||
if (sectionId != null) 'section': sectionId,
|
||||
},
|
||||
);
|
||||
return Item.fromJson(rec.toJson());
|
||||
@@ -136,8 +152,16 @@ class ApiService {
|
||||
await pb.collection('items').delete(itemId);
|
||||
}
|
||||
|
||||
Future<void> updateItem(String itemId, {bool? checked, String? name}) async {
|
||||
if (checked == null && name == null) return;
|
||||
Future<void> updateItem(
|
||||
String itemId, {
|
||||
bool? checked,
|
||||
String? name,
|
||||
String? sectionId,
|
||||
bool clearSection = false,
|
||||
}) async {
|
||||
if (checked == null && name == null && sectionId == null && !clearSection) {
|
||||
return;
|
||||
}
|
||||
final Map<String, dynamic> body = <String, dynamic>{};
|
||||
if (checked != null) {
|
||||
body['checked'] = checked;
|
||||
@@ -145,6 +169,12 @@ class ApiService {
|
||||
if (name != null) {
|
||||
body['name'] = name;
|
||||
}
|
||||
if (sectionId != null) {
|
||||
body['section'] = sectionId;
|
||||
}
|
||||
if (clearSection && sectionId == null) {
|
||||
body['section'] = null;
|
||||
}
|
||||
await pb.collection('items').update(itemId, body: body);
|
||||
}
|
||||
|
||||
@@ -162,6 +192,31 @@ class ApiService {
|
||||
return await pb.collection('users').getFullList(filter: filter);
|
||||
}
|
||||
|
||||
Stream<void> watchListsForLayout(String layoutId) {
|
||||
late final StreamController<void> controller;
|
||||
Future<void> Function()? unsubscribe;
|
||||
|
||||
controller = StreamController<void>(
|
||||
onListen: () async {
|
||||
unsubscribe = await pb.collection('lists').subscribe('*', (event) {
|
||||
if (!controller.isClosed) {
|
||||
controller.add(null);
|
||||
}
|
||||
}, filter: 'layout = "$layoutId"');
|
||||
},
|
||||
onCancel: () => unsubscribe?.call(),
|
||||
);
|
||||
|
||||
return controller.stream;
|
||||
}
|
||||
|
||||
Future<List<Liste>> getListsForLayout(String layoutId) async {
|
||||
final List<RecordModel> res = await pb
|
||||
.collection('lists')
|
||||
.getFullList(filter: 'layout = "$layoutId"');
|
||||
return res.map((r) => Liste.fromJson(r.toJson())).toList();
|
||||
}
|
||||
|
||||
Stream<void> watchStoreLayouts() {
|
||||
if (userId == null) return Stream.value(null);
|
||||
|
||||
@@ -318,18 +373,10 @@ class ApiService {
|
||||
required String layoutId,
|
||||
required String name,
|
||||
int position = 0,
|
||||
String? category,
|
||||
}) async {
|
||||
final RecordModel rec = await pb
|
||||
.collection('store_sections')
|
||||
.create(
|
||||
body: {
|
||||
'layout': layoutId,
|
||||
'name': name,
|
||||
'position': position,
|
||||
if (category != null && category.isNotEmpty) 'category': category,
|
||||
},
|
||||
);
|
||||
.create(body: {'layout': layoutId, 'name': name, 'position': position});
|
||||
return StoreSection.fromJson(rec.toJson());
|
||||
}
|
||||
|
||||
@@ -337,14 +384,10 @@ class ApiService {
|
||||
String sectionId, {
|
||||
String? name,
|
||||
int? position,
|
||||
String? category,
|
||||
bool clearCategory = false,
|
||||
}) async {
|
||||
final Map<String, dynamic> body = {};
|
||||
if (name != null) body['name'] = name;
|
||||
if (position != null) body['position'] = position;
|
||||
if (category != null) body['category'] = category;
|
||||
if (clearCategory && category == null) body['category'] = null;
|
||||
if (body.isEmpty) return;
|
||||
await pb.collection('store_sections').update(sectionId, body: body);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user