Skip to content

Commit e1bd4ed

Browse files
committed
Made Cdn Helper a Singleton
1 parent c0841b5 commit e1bd4ed

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

lib/src/discord_cdn_helper.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
import 'package:discord_api/src/models/discord_image_format.dart';
22

33
class DiscordCdnHelper {
4+
static final instance = DiscordCdnHelper._();
5+
DiscordCdnHelper._();
6+
47
static final cndUrl = Uri(scheme: "https", host: "cdn.discordapp.com");
58

69
/// Available formats : PNG, JPEG, WebP, GIF
7-
static final formatList1 = [
10+
static const formatList1 = [
811
DiscordImageFormat.png,
912
DiscordImageFormat.jpeg,
1013
DiscordImageFormat.webp,
1114
DiscordImageFormat.gif,
1215
];
1316

1417
/// Available formats : PNG, JPEG, WebP
15-
static final formatList2 = [
18+
static const formatList2 = [
1619
DiscordImageFormat.png,
1720
DiscordImageFormat.jpeg,
1821
DiscordImageFormat.webp,
1922
];
2023

2124
/// Available formats : PNG, Lottie
22-
static final formatList3 = [
25+
static const formatList3 = [
2326
DiscordImageFormat.png,
2427
DiscordImageFormat.lottie,
2528
];
@@ -28,6 +31,8 @@ class DiscordCdnHelper {
2831
///
2932
/// The size is only valid if it's a power of 2,
3033
/// between 16 and 4096.
34+
///
35+
/// **NOTE**: Apparently, 160 also is a valid size, even though it doesn't meet the requirements.
3136
bool isValidSize(int x) {
3237
return (x >= 16) && (x <= 4096) && ((x & (x - 1)) == 0);
3338
}
@@ -40,7 +45,7 @@ class DiscordCdnHelper {
4045
/// Returns the CDN URL for a given path, as an URI.
4146
///
4247
/// Probably shouldn't be used, as specialized functions exists.
43-
Uri getUrl(String path) => cndUrl.replace(pathSegments: path.split('/'));
48+
Uri getUrl(String path) => cndUrl.resolve(path);
4449

4550
/// Tries to create the right URL slug for an emoji, given its emoji_id, in a given format
4651
/// (defaults to PNG), with a given size (defaults to 128).

lib/src/discord_client.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DiscordClient {
2727
final DiscordHttpClient discordHttpClient;
2828

2929
/// Contains all methods necessary to build elements from Discord's CDN endpoints.
30-
final DiscordCdnHelper cdn = DiscordCdnHelper();
30+
final DiscordCdnHelper cdn = DiscordCdnHelper.instance;
3131

3232
/// If you need it, you can get the [DiscordDioProvider] from the discord_api_dio_provider package.
3333
/// Otherwise, you'll have to implement `discordHttpClient` yourself.

0 commit comments

Comments
 (0)