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.
 
 
 
 

134 lines
3.7 KiB

import 'dart:convert';
import 'package:deeply/deeply.dart';
import 'package:get/state_manager.dart';
import 'package:youtribe_lib/api/api.dart';
import 'package:localstorage/localstorage.dart';
import 'package:youtribe_lib/api/schema.dart';
class UserController extends GetxController {
final _localStorage = new LocalStorage('user-store');
final _api = Api();
final _user = Rx<Map<dynamic, dynamic>>({}).obs;
Map<dynamic, dynamic> patchStatuses;
get user => _user().value;
set user(payload) {
// patch(payload);
_user(Rx({..._user().value, ...payload}));
_localStorage.setItem('user', jsonEncode(user));
}
get projects => _user().value['projects'].map((p) => p['project_id']);
Future<Map<dynamic, dynamic>> authenticate(
Map<String, String> payload) async {
var res = await _api.authenticate(payload);
if (!res.containsKey('error')) {
user = res;
fetch();
}
return res;
}
@override
void onInit() async {
await _localStorage.ready;
var localUser = await _localStorage.getItem('user');
if (localUser != null) {
user = jsonDecode(localUser);
var canResume =
await _api.resumeSession(user['access_token'], user['refresh_token']);
if (canResume) {
user = {'refresh_token': _api.credentials['refreshToken']};
fetch();
} else {
await this._localStorage.deleteItem('user');
}
}
super.onInit();
}
Future<Map<dynamic, dynamic>> fetch() async {
var res = await _api.get('''
/users/me?fields=*,
agencies.agency_id.*,
agencies.agency_id.projects.project_id.*,
agencies.agency_id.projects.project_id.customers.*,
agencies.agency_id.projects.project_id.steps.*,
agencies.agency_id.projects.project_id.steps.repports.*,
projects.project_id.*,
projects.project_id.agencies.agency_id.*,
projects.project_id.initial_shots.*,
projects.project_id.steps.*,
projects.project_id.steps.repports.*
'''
.trim()
.replaceAll("\n", "")
.replaceAll(" ", ""));
if (!res.containsKey('error')) {
user = res;
}
return user;
}
Future<bool> patch(Map<dynamic, dynamic> item, {String collection}) async {
print("userController patch");
var id = item.containsKey("id") ? item["id"] : null;
item.remove("id");
var computedPath =
collection != null ? "/items/$collection/$id" : "/users/me";
var res = await _api.patch(computedPath, item);
if (!res.containsKey('error')) {
_updateCollection(collection, {
...item,
...{"id": id}
});
return true;
} else {
return false;
}
}
void _updateCollection(String collection, Map<dynamic, dynamic> item,
[List<String> startPath]) {
var currentPath = startPath != null ? startPath : [];
var subSchema = schema;
currentPath.forEach((value) {
subSchema = subSchema[value];
});
List<String> reserved = ["collection", "kind"];
subSchema.forEach((key, value) {
if (!reserved.contains(key) && value is Map) {
_updateCollection(collection, item, [
...currentPath,
...[key]
]);
if (value.containsKey("collection")) {
if (value["collection"] == collection) {
updateDeeply(currentPath, _user().value, (value) {
if (value is List) {
var index = value
.indexWhere((element) => element[key]["id"] == item["id"]);
value[index][key] = {...value[index][key], ...item};
} else if (value != null) {
value = {...value, ...item};
}
});
}
}
}
});
}
}