Skip to content

Commit

Permalink
Merge pull request #50 from mineral-dart/create_getters_for_all_prope…
Browse files Browse the repository at this point in the history
…rties

feat: Create getters for all properties
  • Loading branch information
LeadcodeDev authored Jul 28, 2022
2 parents 8057f82 + 0313f85 commit 5a329a8
Show file tree
Hide file tree
Showing 52 changed files with 1,148 additions and 1,172 deletions.
1 change: 0 additions & 1 deletion lib/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export 'src/api/channels/text_based_channel.dart' show TextBasedChannel;
export 'src/api/channels/text_channel.dart' show TextChannel;
export 'src/api/channels/category_channel.dart' show CategoryChannel;
export 'src/api/channels/permission_overwrite.dart' show PermissionOverwrite, PermissionOverwriteType;
export 'src/api/managers/permission_overwrite_manager.dart' show PermissionOverwriteManager;

export 'src/api/messages/message.dart' show Message;
export 'src/api/messages/message_embed.dart' show MessageEmbed, Footer, Image, Thumbnail, Author, Field;
Expand Down
70 changes: 39 additions & 31 deletions lib/src/api/activity.dart
Original file line number Diff line number Diff line change
@@ -1,48 +1,56 @@
import 'dart:convert';

import 'package:mineral/api.dart';
import 'package:mineral/src/api/client/client_presence.dart';

class Activity {
String? label;
GamePresence type;
String? url;
DateTime createdAt;
Timestamp? timestamps;
Snowflake? applicationId;
String? details;
String? state;
String? _label;
GamePresence _type;
String? _url;
DateTime _createdAt;
Timestamp? _timestamps;
Snowflake? _applicationId;
String? _details;
String? _state;
// Emoji? emoji;
// Party? party;
// Asset? assets;
// Secret? secrets;
// Instance? instance;
int? flags;
int? _flags;
// List<Button> buttons;

Activity({
required this.label,
required this.type,
required this.url,
required this.createdAt,
required this.timestamps,
required this.applicationId,
required this.details,
required this.state,
required this.flags,
});
Activity(
this._label,
this._type,
this._url,
this._createdAt,
this._timestamps,
this._applicationId,
this._details,
this._state,
this._flags,
);

String? get label => _label;
GamePresence get type => _type;
String? get url => _url;
DateTime get createdAt => _createdAt;
Timestamp? get timestamps => _timestamps;
Snowflake? get applicationId => _applicationId;
String? get details => _details;
String? get state => _state;
int? get flags => _flags;

factory Activity.from({ required dynamic payload }) {
return Activity(
label: payload['name'],
type: GamePresence.values.firstWhere((status) => status.value == payload['type']),
url: payload['url'],
createdAt: DateTime.fromMillisecondsSinceEpoch(payload['created_at']),
timestamps: payload['timestamps'] != null ? Timestamp.from(payload: payload['timestamps']) : null,
applicationId: payload['application_id'],
details: payload['details'],
state: payload['state'],
flags: payload['flags']
payload['name'],
GamePresence.values.firstWhere((status) => status.value == payload['type']),
payload['url'],
DateTime.fromMillisecondsSinceEpoch(payload['created_at']),
payload['timestamps'] != null ? Timestamp.from(payload: payload['timestamps']) : null,
payload['application_id'],
payload['details'],
payload['state'],
payload['flags']
);
}
}
13 changes: 8 additions & 5 deletions lib/src/api/application.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import 'package:mineral/api.dart';

class Application {
Snowflake id;
int flags;
Snowflake _id;
int _flags;

Application({ required this.id, required this.flags });
Application(this._id, this._flags);

Snowflake get id => _id;
int get flags => _flags;

factory Application.from(dynamic payload) {
return Application(
id: payload['id'],
flags: payload['flags']
payload['id'],
payload['flags']
);
}
}
70 changes: 32 additions & 38 deletions lib/src/api/channels/category_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,19 @@ import 'dart:convert';
import 'package:http/http.dart';
import 'package:mineral/api.dart';
import 'package:mineral/core.dart';
import 'package:mineral/src/api/channels/permission_overwrite.dart';
import 'package:mineral/src/api/managers/permission_overwrite_manager.dart';
import 'package:mineral/src/api/managers/webhook_manager.dart';

class CategoryChannel extends Channel {
CategoryChannel({
required Snowflake id,
required Snowflake? guildId,
required int? position,
required String label,
required Snowflake? applicationId,
required int? flags,
required PermissionOverwriteManager permissionOverwrites
}) : super(
id: id,
guildId: guildId,
position: position,
label: label,
applicationId: applicationId,
parentId: null,
type: ChannelType.guildCategory,
flags: flags,
webhooks: WebhookManager(guildId: guildId, channelId: id),
permissionOverwrites: permissionOverwrites
CategoryChannel(
super._id,
super._type,
super.position,
super.label,
super.applicationId,
super.flags,
super._webhooks,
super.permissionOverwrites,
super._guild
);

Future<CategoryChannel?> update ({ String? label, int? position, List<PermissionOverwrite>? permissionOverwrites }) async {
Expand All @@ -39,12 +28,12 @@ class CategoryChannel extends Channel {
});

dynamic payload = jsonDecode(response.body);
CategoryChannel channel = CategoryChannel.from(payload);
CategoryChannel channel = CategoryChannel.from(guild, payload);

// Define deep properties
channel.guildId = guildId;
channel.guild = guild;
channel.parent = channel.parentId != null ? guild?.channels.cache.get<CategoryChannel>(channel.parentId) : null;
channel.parent = channel.parent != null
? guild?.channels.cache.get<CategoryChannel>(payload['parent_id'])
: null;

guild?.channels.cache.set(channel.id, channel);
return channel;
Expand All @@ -59,24 +48,29 @@ class CategoryChannel extends Channel {
return response.statusCode == 200;
}

factory CategoryChannel.from(dynamic payload) {
final PermissionOverwriteManager permissionOverwriteManager = PermissionOverwriteManager(
guildId: payload['guild_id'],
channelId: payload['id']
);
factory CategoryChannel.from(Guild? guild, dynamic payload) {
MineralClient client = ioc.singleton(ioc.services.client);
final permissionOverwriteManager = PermissionOverwriteManager();

for(dynamic element in payload['permission_overwrites']) {
final PermissionOverwrite overwrite = PermissionOverwrite.from(payload: element);
permissionOverwriteManager.cache.putIfAbsent(overwrite.id, () => overwrite);
}

return CategoryChannel(
id: payload['id'],
guildId: payload['guild_id'],
position: payload['position'],
label: payload['name'],
applicationId: payload['application_id'],
flags: payload['flags'],
permissionOverwrites: permissionOverwriteManager
final category = CategoryChannel(
payload['id'],
ChannelType.guildCategory,
payload['position'],
payload['name'],
payload['application_id'],
payload['flags'],
null,
permissionOverwriteManager,
client.guilds.cache.get(payload['guild_id'])
);

category.permissionOverwrites?.channel = category;

return category;
}
}
68 changes: 37 additions & 31 deletions lib/src/api/channels/channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ enum ChannelType {
const ChannelType(this.value);
}

Map<ChannelType, Channel Function(dynamic payload)> channels = {
ChannelType.guildText: (dynamic payload) => TextChannel.from(payload),
Map<ChannelType, Channel Function(Guild? guild, dynamic payload)> channels = {
ChannelType.guildText: (Guild? guild, dynamic payload) => TextChannel.from(guild, payload),
// 'DM': () => ,
ChannelType.guildVoice: (dynamic payload) => VoiceChannel.from(payload),
ChannelType.guildVoice: (Guild? guild, dynamic payload) => VoiceChannel.from(guild, payload),
// 'GROUP_DM': () => ,
ChannelType.guildCategory: (dynamic payload) => CategoryChannel.from(payload),
ChannelType.guildCategory: (Guild? guild, dynamic payload) => CategoryChannel.from(guild, payload),
// 'GUILD_NEWS': () => ,
// 'GUILD_NEWS_THREAD': () => ,
// 'GUILD_PUBLIC_THREAD': () => ,
Expand All @@ -39,37 +39,44 @@ Map<ChannelType, Channel Function(dynamic payload)> channels = {
};

class Channel extends PartialChannel {
ChannelType type;
Snowflake? guildId;
late Guild? guild;
int? position;
String? label;
Snowflake? applicationId;
Snowflake? parentId;
late CategoryChannel? parent;
int? flags;
WebhookManager webhooks;
PermissionOverwriteManager? permissionOverwrites;

Channel({
required id,
required this.type,
required this.guildId,
required this.position,
required this.label,
required this.applicationId,
required this.parentId,
required this.flags,
required this.webhooks,
this.permissionOverwrites
}): super(id: id);
final Guild? _guild;
late Channel? parent;

final ChannelType _type;
int? _position;
String? _label;
final Snowflake? _applicationId;
final int? _flags;
final WebhookManager? _webhooks;
final PermissionOverwriteManager? _permissionOverwrites;

Channel(
super.id,
this._type,
this._position,
this._label,
this._applicationId,
this._flags,
this._webhooks,
this._permissionOverwrites,
this._guild,
);

ChannelType get type => _type;
Guild? get guild => _guild;
int? get position => _position;
String? get label => _label;
Snowflake? get applicationId => _applicationId;
int? get flags => _flags;
WebhookManager? get webhooks => _webhooks;
PermissionOverwriteManager? get permissionOverwrites => _permissionOverwrites;

Future<T> setLabel<T extends Channel> (String label) async {
Http http = ioc.singleton(ioc.services.http);
Response response = await http.patch(url: "/channels/$id", payload: { 'label': label });

if (response.statusCode == 200) {
this.label = label;
_label = label;
}

return this as T;
Expand All @@ -80,7 +87,7 @@ class Channel extends PartialChannel {
Response response = await http.patch(url: "/channels/$id", payload: { 'position': position });

if (response.statusCode == 200) {
this.position = position;
_position = position;
}

return this as T;
Expand All @@ -91,7 +98,6 @@ class Channel extends PartialChannel {
Response response = await http.patch(url: "/channels/$id", payload: { 'parent_id': channel.id });

if (response.statusCode == 200) {
parentId = channel.id;
parent = channel;
}

Expand Down
20 changes: 10 additions & 10 deletions lib/src/api/channels/dm_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class DmChannel extends PartialChannel {
MessageManager messages;
Map<Snowflake, User> recipients;

DmChannel({
required id,
required this.lastMessageId,
required this.messages,
required this.recipients,
}): super(id: id);
DmChannel(
super.id,
this.lastMessageId,
this.messages,
this.recipients,
);

factory DmChannel.from({ required dynamic payload }) {
MineralClient client = ioc.singleton(ioc.services.client);
Expand All @@ -29,10 +29,10 @@ class DmChannel extends PartialChannel {
}

return DmChannel(
id: payload['id'],
lastMessageId: payload['last_message_id'],
messages: MessageManager(payload['id'], null),
recipients: users
payload['id'],
payload['last_message_id'],
MessageManager(),
users,
);
}
}
3 changes: 1 addition & 2 deletions lib/src/api/channels/partial_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ import 'package:mineral/api.dart';

class PartialChannel {
Snowflake id;

PartialChannel({ required this.id });
PartialChannel(this.id);
}
Loading

0 comments on commit 5a329a8

Please sign in to comment.