Skip to content

Commit

Permalink
## 0.8.5
Browse files Browse the repository at this point in the history
- Add Freezed instead Menual JSON Serialize
  • Loading branch information
ChenDoXiu committed Jan 4, 2025
1 parent 588059c commit 9e97616
Show file tree
Hide file tree
Showing 42 changed files with 1,145 additions and 2,659 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ migrate_working_dir/
*.iws
.idea/
*.g.dart
*.freezed.dart
# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.8.5

- Add Freezed instead Menual JSON Serialize

## 0.8.1

### Feature
Expand Down
165 changes: 37 additions & 128 deletions lib/apis/models/announcement.dart
Original file line number Diff line number Diff line change
@@ -1,140 +1,49 @@
import 'dart:convert';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter/foundation.dart';

///Announcement
class Announcement {
double? closeDuration;
DateTime createdAt;
AnnouncementDisplay display;
double? displayOrder;
bool forYou;
AnnouncementIcon icon;
String id;
String? imageUrl;
bool isRead;
bool needConfirmationToRead;
bool silence;
String text;
String title;
DateTime? updatedAt;
part 'announcement.freezed.dart';

Announcement({
required this.closeDuration,
required this.createdAt,
required this.display,
required this.displayOrder,
required this.forYou,
required this.icon,
required this.id,
required this.imageUrl,
required this.isRead,
required this.needConfirmationToRead,
required this.silence,
required this.text,
required this.title,
required this.updatedAt,
});
part 'announcement.g.dart';

Announcement copyWith({
@unfreezed
class Announcement with _$Announcement {
factory Announcement({
double? closeDuration,
DateTime? createdAt,
AnnouncementDisplay? display,
required DateTime createdAt,
required AnnouncementDisplay display,
double? displayOrder,
bool? forYou,
AnnouncementIcon? icon,
String? id,
required bool forYou,
required AnnouncementIcon icon,
required String id,
String? imageUrl,
bool? isRead,
bool? needConfirmationToRead,
bool? silence,
String? text,
String? title,
required bool isRead,
required bool needConfirmationToRead,
required bool silence,
required String text,
required String title,
DateTime? updatedAt,
}) =>
Announcement(
closeDuration: closeDuration ?? this.closeDuration,
createdAt: createdAt ?? this.createdAt,
display: display ?? this.display,
displayOrder: displayOrder ?? this.displayOrder,
forYou: forYou ?? this.forYou,
icon: icon ?? this.icon,
id: id ?? this.id,
imageUrl: imageUrl ?? this.imageUrl,
isRead: isRead ?? this.isRead,
needConfirmationToRead:
needConfirmationToRead ?? this.needConfirmationToRead,
silence: silence ?? this.silence,
text: text ?? this.text,
title: title ?? this.title,
updatedAt: updatedAt ?? this.updatedAt,
);
}) = _Announcement;

factory Announcement.fromJson(String str) =>
Announcement.fromMap(json.decode(str));

String toJson() => json.encode(toMap());

factory Announcement.fromMap(Map<String, dynamic> json) => Announcement(
closeDuration: json["closeDuration"]?.toDouble(),
createdAt: DateTime.parse(json["createdAt"]),
display: displayValues.map[json["display"]]!,
displayOrder: json["displayOrder"]?.toDouble(),
forYou: json["forYou"],
icon: iconValues.map[json["icon"]]!,
id: json["id"],
imageUrl: json["imageUrl"],
isRead: json["isRead"] ?? false,
needConfirmationToRead: json["needConfirmationToRead"],
silence: json["silence"],
text: json["text"],
title: json["title"],
updatedAt: json["updatedAt"] == null
? null
: DateTime.parse(json["updatedAt"]),
);

Map<String, dynamic> toMap() => {
"closeDuration": closeDuration,
"createdAt": createdAt.toIso8601String(),
"display": displayValues.reverse[display],
"displayOrder": displayOrder,
"forYou": forYou,
"icon": iconValues.reverse[icon],
"id": id,
"imageUrl": imageUrl,
"isRead": isRead,
"needConfirmationToRead": needConfirmationToRead,
"silence": silence,
"text": text,
"title": title,
"updatedAt": updatedAt?.toIso8601String(),
};
factory Announcement.fromJson(Map<String, dynamic> json) =>
_$AnnouncementFromJson(json);
}

enum AnnouncementDisplay { BANNER, DIALOG, NORMAL }

final displayValues = EnumValues({
"banner": AnnouncementDisplay.BANNER,
"dialog": AnnouncementDisplay.DIALOG,
"normal": AnnouncementDisplay.NORMAL
});

enum AnnouncementIcon { ERROR, INFO, SUCCESS, WARNING }

final iconValues = EnumValues({
"error": AnnouncementIcon.ERROR,
"info": AnnouncementIcon.INFO,
"success": AnnouncementIcon.SUCCESS,
"warning": AnnouncementIcon.WARNING
});

class EnumValues<T> {
Map<String, T> map;
late Map<T, String> reverseMap;

EnumValues(this.map);
enum AnnouncementDisplay {
@JsonValue('banner')
BANNER,
@JsonValue('dialog')
DIALOG,
@JsonValue('normal')
NORMAL,
}

Map<T, String> get reverse {
reverseMap = map.map((k, v) => MapEntry(v, k));
return reverseMap;
}
enum AnnouncementIcon {
@JsonValue('error')
ERROR,
@JsonValue('info')
INFO,
@JsonValue('success')
SUCCESS,
@JsonValue('warning')
WARNING,
}
63 changes: 13 additions & 50 deletions lib/apis/models/app.dart
Original file line number Diff line number Diff line change
@@ -1,58 +1,21 @@
import 'dart:convert';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter/foundation.dart';

class AppModel {
final String? callbackUrl;
final String id;
final bool? isAuthorized;
final String name;
final List<String> permission;
final String? secret;
part 'app.freezed.dart';

AppModel({
required this.callbackUrl,
required this.id,
this.isAuthorized,
required this.name,
required this.permission,
this.secret,
});
part 'app.g.dart';

AppModel copyWith({
@freezed
class AppModel with _$AppModel {
const factory AppModel({
String? callbackUrl,
String? id,
required String id,
bool? isAuthorized,
String? name,
List<String>? permission,
required String name,
required List<String> permission,
String? secret,
}) =>
AppModel(
callbackUrl: callbackUrl ?? this.callbackUrl,
id: id ?? this.id,
isAuthorized: isAuthorized ?? this.isAuthorized,
name: name ?? this.name,
permission: permission ?? this.permission,
secret: secret ?? this.secret,
);
}) = _AppModel;

factory AppModel.fromJson(String str) => AppModel.fromMap(json.decode(str));

String toJson() => json.encode(toMap());

factory AppModel.fromMap(Map<String, dynamic> json) => AppModel(
callbackUrl: json["callbackUrl"],
id: json["id"],
isAuthorized: json["isAuthorized"],
name: json["name"],
permission: List<String>.from(json["permission"].map((x) => x)),
secret: json["secret"],
);

Map<String, dynamic> toMap() => {
"callbackUrl": callbackUrl,
"id": id,
"isAuthorized": isAuthorized,
"name": name,
"permission": List<dynamic>.from(permission.map((x) => x)),
"secret": secret,
};
factory AppModel.fromJson(Map<String, dynamic> json) =>
_$AppModelFromJson(json);
}
43 changes: 12 additions & 31 deletions lib/apis/models/auth.dart
Original file line number Diff line number Diff line change
@@ -1,36 +1,17 @@
import 'dart:convert';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter/foundation.dart';

class SessionGenerateModel {
final String token;
final String url;
part 'auth.freezed.dart';

SessionGenerateModel({
required this.token,
required this.url,
});
part 'auth.g.dart';

SessionGenerateModel copyWith({
String? token,
String? url,
}) =>
SessionGenerateModel(
token: token ?? this.token,
url: url ?? this.url,
);
@freezed
class SessionGenerateModel with _$SessionGenerateModel {
const factory SessionGenerateModel({
required String token,
required String url,
}) = _SessionGenerateModel;

factory SessionGenerateModel.fromJson(String str) =>
SessionGenerateModel.fromMap(json.decode(str));

String toJson() => json.encode(toMap());

factory SessionGenerateModel.fromMap(Map<String, dynamic> json) =>
SessionGenerateModel(
token: json["token"],
url: json["url"],
);

Map<String, dynamic> toMap() => {
"token": token,
"url": url,
};
factory SessionGenerateModel.fromJson(Map<String, dynamic> json) =>
_$SessionGenerateModelFromJson(json);
}
59 changes: 21 additions & 38 deletions lib/apis/models/clips.dart
Original file line number Diff line number Diff line change
@@ -1,43 +1,26 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter/foundation.dart';
import 'package:moekey/apis/models/user_lite.dart';

///Clip
class ClipsModel {
DateTime createdAt;
String? description;
int favoritedCount;
String id;
bool isFavorited;
bool isPublic;
DateTime? lastClippedAt;
String name;
UserLiteModel user;
String userId;
part 'clips.freezed.dart';

ClipsModel({
required this.createdAt,
required this.description,
required this.favoritedCount,
required this.id,
required this.isFavorited,
required this.isPublic,
required this.lastClippedAt,
required this.name,
required this.user,
required this.userId,
});
part 'clips.g.dart';

factory ClipsModel.fromMap(Map<String, dynamic> json) => ClipsModel(
createdAt: DateTime.parse(json["createdAt"]),
description: json["description"],
favoritedCount: json["favoritedCount"],
id: json["id"],
isFavorited: json["isFavorited"],
isPublic: json["isPublic"],
lastClippedAt: json["lastClippedAt"] == null
? null
: DateTime.parse(json["lastClippedAt"]),
name: json["name"],
user: UserLiteModel.fromMap(json["user"]),
userId: json["userId"],
);
@freezed
class ClipsModel with _$ClipsModel {
const factory ClipsModel({
required DateTime createdAt,
String? description,
required int favoritedCount,
required String id,
required bool isFavorited,
required bool isPublic,
DateTime? lastClippedAt,
required String name,
required UserLiteModel user,
required String userId,
}) = _ClipsModel;

factory ClipsModel.fromJson(Map<String, dynamic> json) =>
_$ClipsModelFromJson(json);
}
Loading

0 comments on commit 9e97616

Please sign in to comment.