Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexios80 committed Nov 19, 2023
1 parent 018d6ba commit 72fedc6
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 28 deletions.
6 changes: 3 additions & 3 deletions pub_stats/lib/controller/user_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class UserController {
/// Return true on success
Future<bool> addConfig({
required String slug,
required AlertType type,
required AlertConfigType type,
required String extra,
required Set<PackageDataField> ignore,
}) async {
Expand All @@ -84,7 +84,7 @@ class UserController {
}

final AlertConfig config;
if (type == AlertType.discord) {
if (type == AlertConfigType.discord) {
final match = RegExp(r'https:\/\/discord\.com\/api\/webhooks\/(.+)\/(.+)')
.firstMatch(extra);

Expand All @@ -94,7 +94,7 @@ class UserController {
);
return false;
}
config = DiscordConfig(
config = DiscordAlertConfig(
slug: slug,
ignore: ignore,
id: match[1]!,
Expand Down
6 changes: 3 additions & 3 deletions pub_stats/lib/view/screen/alerts_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:recase/recase.dart';
class AlertsManager extends StatelessWidget {
static final _user = GetIt.I<UserController>();

final selectedType = AlertType.discord.rx;
final selectedType = AlertConfigType.discord.rx;
final slugController = TextEditingController();
final extraController = TextEditingController();
final ignoredFields = <PackageDataField>{}.rx;
Expand Down Expand Up @@ -58,7 +58,7 @@ class AlertsManager extends StatelessWidget {
border: OutlineInputBorder(),
),
value: selectedType.value,
items: AlertType.values
items: AlertConfigType.values
.map(
(e) => DropdownMenuItem(
value: e,
Expand Down Expand Up @@ -181,7 +181,7 @@ class AlertsManager extends StatelessWidget {
}

void addConfig() async {
if (await _user.addConfig(
if (!await _user.addConfig(
slug: slugController.text,
ignore: ignoredFields,
type: selectedType.value,
Expand Down
24 changes: 12 additions & 12 deletions pub_stats_core/lib/src/model/alert_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ abstract class AlertConfig {
final Set<PackageDataField> ignore;

/// The type of alert
final AlertType type;
final AlertConfigType type;

AlertConfig({
required this.slug,
Expand All @@ -20,18 +20,18 @@ abstract class AlertConfig {
});

factory AlertConfig.fromJson(Map<String, dynamic> json) =>
switch (AlertType.values.byName(json['type'])) {
AlertType.discord => DiscordConfig.fromJson(json),
switch (AlertConfigType.values.byName(json['type'])) {
AlertConfigType.discord => DiscordAlertConfig.fromJson(json),
};

Map<String, dynamic> toJson();

String get extra => switch (type) {
AlertType.discord => (this as DiscordConfig).id,
AlertConfigType.discord => (this as DiscordAlertConfig).id,
};
}

enum AlertType {
enum AlertConfigType {
discord;

String get extraLabel => switch (this) {
Expand All @@ -42,21 +42,21 @@ enum AlertType {
abstract class AlertServiceConfig {}

@JsonSerializable()
class DiscordConfig extends AlertConfig {
class DiscordAlertConfig extends AlertConfig {
final String id;
final String token;

DiscordConfig({
DiscordAlertConfig({
required super.slug,
super.ignore,
required this.id,
required this.token,
super.type = AlertType.discord,
}) : assert(type == AlertType.discord);
super.type = AlertConfigType.discord,
}) : assert(type == AlertConfigType.discord);

factory DiscordConfig.fromJson(Map<String, dynamic> json) =>
_$DiscordConfigFromJson(json);
factory DiscordAlertConfig.fromJson(Map<String, dynamic> json) =>
_$DiscordAlertConfigFromJson(json);

@override
Map<String, dynamic> toJson() => _$DiscordConfigToJson(this);
Map<String, dynamic> toJson() => _$DiscordAlertConfigToJson(this);
}
16 changes: 8 additions & 8 deletions pub_stats_core/lib/src/model/alert_config.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pub_stats_core/lib/src/model/package_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ class PackageData {}
enum PackageDataField {
likeCount,
popularityScore,
pubPoints,
pubPoints;
}
2 changes: 1 addition & 1 deletion pub_stats_core/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ environment:
dependencies:
equatable: ^2.0.5
http: ^1.1.0
json_annotation: ^4.4.0
json_annotation: ^4.8.1
pub_api_client: ^2.2.0

dev_dependencies:
Expand Down

0 comments on commit 72fedc6

Please sign in to comment.