2022-08-29 20:38:28 -06:00
|
|
|
import 'dart:collection';
|
|
|
|
|
|
|
|
import 'package:wonders/common_libs.dart';
|
2022-11-29 22:05:14 -07:00
|
|
|
import 'package:wonders/logic/common/http_client.dart';
|
2022-08-29 20:38:28 -06:00
|
|
|
import 'package:wonders/logic/data/artifact_data.dart';
|
2023-10-03 14:15:36 -06:00
|
|
|
import 'package:wonders/logic/artifact_api_service.dart';
|
2022-08-29 20:38:28 -06:00
|
|
|
|
2023-10-03 14:15:36 -06:00
|
|
|
class ArtifactAPILogic {
|
2022-08-29 20:38:28 -06:00
|
|
|
final HashMap<String, ArtifactData?> _artifactCache = HashMap();
|
|
|
|
|
2023-10-03 14:15:36 -06:00
|
|
|
ArtifactAPIService get service => GetIt.I.get<ArtifactAPIService>();
|
2022-08-29 20:38:28 -06:00
|
|
|
|
|
|
|
/// Returns artifact data by ID. Returns null if artifact cannot be found. */
|
2023-10-03 14:15:36 -06:00
|
|
|
Future<ArtifactData?> getArtifactByID(String id, {bool selfHosted = false}) async {
|
2022-08-29 20:38:28 -06:00
|
|
|
if (_artifactCache.containsKey(id)) return _artifactCache[id];
|
2023-10-03 14:15:36 -06:00
|
|
|
ServiceResult<ArtifactData?> result =
|
|
|
|
(await (selfHosted ? service.getSelfHostedObjectByID(id) : service.getMetObjectByID(id)));
|
2022-11-29 22:05:14 -07:00
|
|
|
if (!result.success) throw $strings.artifactDetailsErrorNotFound(id);
|
2022-08-29 20:38:28 -06:00
|
|
|
ArtifactData? artifact = result.content;
|
|
|
|
return _artifactCache[id] = artifact;
|
|
|
|
}
|
|
|
|
}
|