Solian/lib/providers/stickers.dart

49 lines
1.3 KiB
Dart
Raw Normal View History

2024-09-16 13:00:19 +00:00
import 'dart:async';
import 'package:get/get.dart';
import 'package:solian/exceptions/request.dart';
2024-08-03 17:37:54 +00:00
import 'package:solian/models/stickers.dart';
import 'package:solian/services.dart';
class StickerProvider extends GetxController {
2024-09-16 13:00:19 +00:00
final RxMap<String, FutureOr<Sticker?>> stickerCache = RxMap();
2024-09-16 13:00:19 +00:00
Future<Sticker?> getStickerByAlias(String alias) {
if (stickerCache.containsKey(alias)) {
return Future.value(stickerCache[alias]);
}
2024-08-06 17:47:53 +00:00
2024-09-16 13:00:19 +00:00
stickerCache[alias] = Future(() async {
final client = await ServiceFinder.configureClient('files');
final resp = await client.get(
'/stickers/lookup/$alias',
);
if (resp.statusCode != 200) {
if (resp.statusCode == 404) {
stickerCache[alias] = null;
2024-08-03 17:37:54 +00:00
}
2024-09-16 13:00:19 +00:00
throw RequestException(resp);
2024-08-03 17:37:54 +00:00
}
2024-09-16 13:00:19 +00:00
return Sticker.fromJson(resp.body);
}).then((result) {
stickerCache[alias] = result;
return result;
});
return Future.value(stickerCache[alias]);
2024-08-03 17:37:54 +00:00
}
2024-09-16 13:00:19 +00:00
Future<List<Sticker>> searchStickerByAlias(String alias) async {
final client = await ServiceFinder.configureClient('files');
final resp = await client.get(
2024-09-16 13:00:19 +00:00
'/stickers/lookup?probe=$alias',
);
if (resp.statusCode != 200) {
throw RequestException(resp);
}
2024-09-16 13:00:19 +00:00
return List<Sticker>.from(resp.body.map((x) => Sticker.fromJson(x)));
}
2024-08-03 17:37:54 +00:00
}