You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

80 lines
1.9 KiB

import 'dart:convert';
import 'dart:developer';
import 'package:get/state_manager.dart';
import 'package:youtribe_lib/api/api.dart';
import 'package:localstorage/localstorage.dart';
class Projects extends GetxController {
final _collection = 'project';
final _fields = '''
project.*,
project.agencies.agency_id,
project.customers.customer_id,
'''
.trim()
.replaceAll(" ", "");
var _filters = "";
final _api = Api();
final _items = Rx<List<dynamic>>().obs;
final isLoading = false.obs;
var _localStorage;
get items => _items().value;
set items(payload) {
_items(Rx(payload));
_localStorage.setItem(_collection, jsonEncode(items));
}
@override
void onInit() async {
_localStorage = new LocalStorage('$_collection-store');
await _localStorage.ready;
var localItems = await _localStorage.getItem('items');
if (localItems != null) {
items = jsonDecode(localItems);
fetch();
}
super.onInit();
}
Future<bool> fetch() async {
var res = await _api.get('/$_collection?$_fields&$_filters');
if (!res.containsKey('error')) {
items = res;
_localStorage.setItem(_collection, jsonEncode(items));
return true;
} else {
return false;
}
}
Future<bool> patch(String collection, Map<dynamic, dynamic> item) async {
if (!item.containsKey('id')) {
log('id needed', level: 3);
return false;
}
var itemId = item['id'];
item.remove('id');
var res = await _api.patch('/$collection/$itemId', item);
if (!res.containsKey('error')) {
int index = items.indexWhere((element) => element['id'] == item['id']);
items[index] = {...items[index], ...item};
return true;
} else {
return false;
}
}
// Future<bool> add(String collection, Map<dynamic, dynamic> item) async {}
// Future<bool> remove(String collection, {String id, int index}) async {}
}