2024-09-16 13:00:19 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2024-08-03 13:29:48 +00:00
|
|
|
import 'package:get/get.dart';
|
2024-09-16 12:33:34 +00:00
|
|
|
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';
|
2024-08-03 13:29:48 +00:00
|
|
|
|
|
|
|
class StickerProvider extends GetxController {
|
2024-09-16 13:00:19 +00:00
|
|
|
final RxMap<String, FutureOr<Sticker?>> stickerCache = RxMap();
|
2024-08-03 13:29:48 +00:00
|
|
|
|
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 12:33:34 +00:00
|
|
|
|
2024-09-16 13:00:19 +00:00
|
|
|
Future<List<Sticker>> searchStickerByAlias(String alias) async {
|
2024-09-16 12:33:34 +00:00
|
|
|
final client = await ServiceFinder.configureClient('files');
|
|
|
|
final resp = await client.get(
|
2024-09-16 13:00:19 +00:00
|
|
|
'/stickers/lookup?probe=$alias',
|
2024-09-16 12:33:34 +00:00
|
|
|
);
|
|
|
|
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-09-16 12:33:34 +00:00
|
|
|
}
|
2024-08-03 17:37:54 +00:00
|
|
|
}
|