OpenContacts/lib/clients/inventory_client.dart

109 lines
3.2 KiB
Dart
Raw Normal View History

2023-06-17 10:58:32 -04:00
import 'package:contacts_plus_plus/apis/record_api.dart';
import 'package:contacts_plus_plus/clients/api_client.dart';
import 'package:contacts_plus_plus/models/inventory/neos_path.dart';
import 'package:contacts_plus_plus/models/records/record.dart';
import 'package:flutter/material.dart';
class InventoryClient extends ChangeNotifier {
final ApiClient apiClient;
Future<NeosDirectory>? _currentDirectory;
Future<NeosDirectory>? get directoryFuture => _currentDirectory;
InventoryClient({required this.apiClient});
Future<List<Record>> _getDirectory(Record record) async {
2023-06-17 11:36:52 -04:00
NeosDirectory? dir;
try {
dir = await _currentDirectory;
} catch(_) {}
2023-06-17 10:58:32 -04:00
final List<Record> records;
if (dir == null || record.isRoot) {
records = await RecordApi.getUserRecordsAt(
apiClient,
path: NeosDirectory.rootName,
);
} else {
if (record.recordType == RecordType.link) {
final linkRecord = await RecordApi.getUserRecord(apiClient, recordId: record.linkRecordId, user: record.linkOwnerId);
records = await RecordApi.getUserRecordsAt(apiClient, path: "${linkRecord.path}\\${record.name}", user: linkRecord.ownerId);
} else {
records = await RecordApi.getUserRecordsAt(
apiClient,
path: "${record.path}\\${record.name}",
user: record.ownerId
);
}
}
return records;
}
void loadInventoryRoot() {
final rootRecord = Record.inventoryRoot();
final rootFuture = _getDirectory(rootRecord).then(
(records) {
final rootDir = NeosDirectory(
record: rootRecord,
children: [],
);
rootDir.children.addAll(
records.map((e) => NeosDirectory.fromRecord(record: e, parent: rootDir)).toList(),
);
return rootDir;
},
);
_currentDirectory = rootFuture;
notifyListeners();
}
Future<void> navigateTo(Record record) async {
final dir = await _currentDirectory;
if (dir == null) {
throw "Failed to open: No directory loaded.";
}
if (record.recordType != RecordType.directory && record.recordType != RecordType.link) {
throw "Failed to open: Record is not a directory.";
}
final childDir = dir.findChildByRecord(record);
if (childDir == null) {
throw "Failed to open: Record is not a child of current directory.";
}
if (childDir.isLoaded) {
_currentDirectory = Future.value(childDir);
} else {
_currentDirectory = _getDirectory(record).then(
(records) {
childDir.children.clear();
childDir.children.addAll(records.map((record) => NeosDirectory.fromRecord(record: record, parent: childDir)));
return childDir;
},
2023-06-17 13:28:23 -04:00
).onError((error, stackTrace) {
return dir;
});
2023-06-17 10:58:32 -04:00
}
notifyListeners();
}
2023-06-17 11:36:52 -04:00
Future<void> navigateUp({int times = 1}) async {
var dir = await _currentDirectory;
2023-06-17 10:58:32 -04:00
if (dir == null) {
throw "Failed to navigate up: No directory loaded.";
}
if (dir.record.isRoot) {
throw "Failed navigate up: Already at root";
}
2023-06-17 11:36:52 -04:00
for (int i = 0; i < times; i++) {
dir = dir?.parent;
}
_currentDirectory = Future.value(dir);
2023-06-17 10:58:32 -04:00
notifyListeners();
}
}