From 832dbdbab8e3dbbf192314620bc4b412750d9574 Mon Sep 17 00:00:00 2001 From: perol Date: Mon, 26 Apr 2021 19:45:45 +0800 Subject: [PATCH] fix json error --- android/app/build.gradle | 4 +- lib/constants.dart | 4 +- lib/models/bookmark.dart | 5 - lib/models/spotlight_response.dart | 125 ++++------------------- lib/models/tags.dart | 40 ++------ lib/models/ugoira_metadata_response.dart | 52 ++++------ lib/models/user_preview.dart | 72 +++---------- 7 files changed, 63 insertions(+), 239 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index ae63cce52..2e300b73b 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -85,8 +85,8 @@ android { applicationId packageName minSdkVersion 21 targetSdkVersion 30 - versionCode 10004063 - versionName "0.4.7 X " + versionCode 10004070 + versionName "0.4.7 Los" } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 diff --git a/lib/constants.dart b/lib/constants.dart index 696acbf79..d8d4d78c6 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -15,11 +15,11 @@ */ class Constants { - static String tagName = "0.4.6"; + static String tagName = "0.4.7"; // static bool isGooglePlay = // bool.fromEnvironment("IS_GOOGLEPLAY", defaultValue: false);//为何没用啊咕鸽? - static bool isGooglePlay = false; + static bool isGooglePlay = true; static int type = 0; static String? code_verifier = null; } diff --git a/lib/models/bookmark.dart b/lib/models/bookmark.dart index be870db3a..a91ad5161 100644 --- a/lib/models/bookmark.dart +++ b/lib/models/bookmark.dart @@ -27,11 +27,6 @@ class BookmarkRsp { factory BookmarkRsp.fromJson(Map json) => _$BookmarkRspFromJson(json); Map toJson() => _$BookmarkRspToJson(this); - - @override - String toString() { - return json.encode(this); - } } @JsonSerializable() diff --git a/lib/models/spotlight_response.dart b/lib/models/spotlight_response.dart index c24cf3929..424a36505 100644 --- a/lib/models/spotlight_response.dart +++ b/lib/models/spotlight_response.dart @@ -17,56 +17,42 @@ // To parse this JSON data, do // // final spotlightResponse = spotlightResponseFromJson(jsonString); -import 'dart:convert'; +import 'package:json_annotation/json_annotation.dart'; -SpotlightResponse spotlightResponseFromJson(String str) => - SpotlightResponse.fromJson(json.decode(str)); - -String spotlightResponseToJson(SpotlightResponse data) => - json.encode(data.toJson()); +part 'spotlight_response.g.dart'; +@JsonSerializable() class SpotlightResponse { + @JsonKey(name: 'spotlight_articles') List spotlightArticles; - String nextUrl; + @JsonKey(name: 'next_url') + String? nextUrl; SpotlightResponse({ required this.spotlightArticles, - required this.nextUrl, + this.nextUrl, }); - - SpotlightResponse copyWith({ - List? spotlightArticles, - String? nextUrl, - }) => - SpotlightResponse( - spotlightArticles: spotlightArticles ?? this.spotlightArticles, - nextUrl: nextUrl ?? this.nextUrl, - ); - factory SpotlightResponse.fromJson(Map json) => - SpotlightResponse( - spotlightArticles: List.from( - json["spotlight_articles"] - .map((x) => SpotlightArticle.fromJson(x))), - nextUrl: json["next_url"], - ); + _$SpotlightResponseFromJson(json); - Map toJson() => { - "spotlight_articles": - List.from(spotlightArticles.map((x) => x.toJson())), - "next_url": nextUrl, - }; + Map toJson() => _$SpotlightResponseToJson(this); } +@JsonSerializable() class SpotlightArticle { int id; String title; + @JsonKey(name: 'pure_title') String pureTitle; + @JsonKey(name: 'thumbnail') String thumbnail; + @JsonKey(name: 'article_url') String articleUrl; + @JsonKey(name: 'publish_date') DateTime publishDate; - Category? category; - SubcategoryLabel? subcategoryLabel; + // Category? category; + // @JsonKey(name: 'subcategory_label') + // SubcategoryLabel? subcategoryLabel; SpotlightArticle({ required this.id, @@ -75,79 +61,12 @@ class SpotlightArticle { required this.thumbnail, required this.articleUrl, required this.publishDate, - required this.category, - required this.subcategoryLabel, + // this.category, + // required this.subcategoryLabel, }); - SpotlightArticle copyWith({ - required int id, - required String title, - required String pureTitle, - required String thumbnail, - required String articleUrl, - required DateTime publishDate, - Category? category, - SubcategoryLabel? subcategoryLabel, - }) => - SpotlightArticle( - id: id, - title: title, - pureTitle: pureTitle, - thumbnail: thumbnail, - articleUrl: articleUrl, - publishDate: publishDate, - category: category ?? this.category, - subcategoryLabel: subcategoryLabel ?? this.subcategoryLabel, - ); - factory SpotlightArticle.fromJson(Map json) => - SpotlightArticle( - id: json["id"], - title: json["title"], - pureTitle: json["pure_title"], - thumbnail: json["thumbnail"], - articleUrl: json["article_url"], - publishDate: DateTime.parse(json["publish_date"]), - category: categoryValues.map[json["category"]], - subcategoryLabel: subcategoryLabelValues.map[json["subcategory_label"]], - ); - - Map toJson() => { - "id": id, - "title": title, - "pure_title": pureTitle, - "thumbnail": thumbnail, - "article_url": articleUrl, - "publish_date": publishDate.toIso8601String(), - "category": categoryValues.reverse[category], - "subcategory_label": subcategoryLabelValues.reverse[subcategoryLabel], - }; -} - -enum Category { SPOTLIGHT, INSPIRATION } + _$SpotlightArticleFromJson(json); -final categoryValues = EnumValues( - {"inspiration": Category.INSPIRATION, "spotlight": Category.SPOTLIGHT}); - -enum SubcategoryLabel { EMPTY, SUBCATEGORY_LABEL, PURPLE } - -final subcategoryLabelValues = EnumValues({ - "イラスト": SubcategoryLabel.EMPTY, - "おすすめ": SubcategoryLabel.PURPLE, - "マンガ": SubcategoryLabel.SUBCATEGORY_LABEL -}); - -class EnumValues { - Map map; - - Map? reverseMap; - - EnumValues(this.map); - - Map get reverse { - if (reverseMap == null) { - reverseMap = map.map((k, v) => new MapEntry(v, k)); - } - return reverseMap!; - } -} + Map toJson() => _$SpotlightArticleToJson(this); +} \ No newline at end of file diff --git a/lib/models/tags.dart b/lib/models/tags.dart index b13502587..0dabb29c1 100644 --- a/lib/models/tags.dart +++ b/lib/models/tags.dart @@ -13,7 +13,6 @@ * this program. If not, see . * */ -import 'dart:convert' show json; import 'package:json_annotation/json_annotation.dart'; import 'package:path/path.dart'; @@ -21,6 +20,7 @@ import 'package:sqflite/sqflite.dart'; part 'tags.g.dart'; +@JsonSerializable() class AutoWords { List tags; @@ -28,28 +28,13 @@ class AutoWords { required this.tags, }); - factory AutoWords.fromJson(jsonRes) { - List tags = []; - for (var item in jsonRes['tags']) { - if (item != null) { - tags.add(Tags.fromJson(item)); - } - } - return AutoWords( - tags: tags, - ); - } - - Map toJson() => { - 'tags': tags, - }; + factory AutoWords.fromJson(Map json) => + _$AutoWordsFromJson(json); - @override - String toString() { - return json.encode(this); - } + Map toJson() => _$AutoWordsToJson(this); } +@JsonSerializable() class Tags { String name; String? translated_name; @@ -59,20 +44,9 @@ class Tags { this.translated_name, }); - factory Tags.fromJson(jsonRes) => Tags( - name: jsonRes['name'], - translated_name: jsonRes['translated_name'], - ); + factory Tags.fromJson(Map json) => _$TagsFromJson(json); - Map toJson() => { - 'name': name, - 'translated_name': translated_name, - }; - - @override - String toString() { - return json.encode(this); - } + Map toJson() => _$TagsToJson(this); } @JsonSerializable() diff --git a/lib/models/ugoira_metadata_response.dart b/lib/models/ugoira_metadata_response.dart index 87a4ce2e3..7df3855c5 100644 --- a/lib/models/ugoira_metadata_response.dart +++ b/lib/models/ugoira_metadata_response.dart @@ -13,15 +13,13 @@ * this program. If not, see . * */ -import 'dart:convert'; +import 'package:json_annotation/json_annotation.dart'; -UgoiraMetadataResponse ugoiraMetadataResponseFromJson(String str) => - UgoiraMetadataResponse.fromJson(json.decode(str)); - -String ugoiraMetadataResponseToJson(UgoiraMetadataResponse data) => - json.encode(data.toJson()); +part 'ugoira_metadata_response.g.dart'; +@JsonSerializable() class UgoiraMetadataResponse { + @JsonKey(name: 'ugoira_metadata') UgoiraMetadata ugoiraMetadata; UgoiraMetadataResponse({ @@ -29,16 +27,14 @@ class UgoiraMetadataResponse { }); factory UgoiraMetadataResponse.fromJson(Map json) => - UgoiraMetadataResponse( - ugoiraMetadata: UgoiraMetadata.fromJson(json["ugoira_metadata"]), - ); + _$UgoiraMetadataResponseFromJson(json); - Map toJson() => { - "ugoira_metadata": ugoiraMetadata.toJson(), - }; + Map toJson() => _$UgoiraMetadataResponseToJson(this); } +@JsonSerializable() class UgoiraMetadata { + @JsonKey(name: 'zip_urls') ZipUrls zipUrls; List frames; @@ -47,17 +43,13 @@ class UgoiraMetadata { required this.frames, }); - factory UgoiraMetadata.fromJson(Map json) => UgoiraMetadata( - zipUrls: ZipUrls.fromJson(json["zip_urls"]), - frames: List.from(json["frames"].map((x) => Frame.fromJson(x))), - ); + factory UgoiraMetadata.fromJson(Map json) => + _$UgoiraMetadataFromJson(json); - Map toJson() => { - "zip_urls": zipUrls.toJson(), - "frames": List.from(frames.map((x) => x.toJson())), - }; + Map toJson() => _$UgoiraMetadataToJson(this); } +@JsonSerializable() class Frame { String file; int delay; @@ -67,17 +59,12 @@ class Frame { required this.delay, }); - factory Frame.fromJson(Map json) => Frame( - file: json["file"], - delay: json["delay"], - ); + factory Frame.fromJson(Map json) => _$FrameFromJson(json); - Map toJson() => { - "file": file, - "delay": delay, - }; + Map toJson() => _$FrameToJson(this); } +@JsonSerializable() class ZipUrls { String medium; @@ -85,11 +72,8 @@ class ZipUrls { required this.medium, }); - factory ZipUrls.fromJson(Map json) => ZipUrls( - medium: json["medium"], - ); + factory ZipUrls.fromJson(Map json) => + _$ZipUrlsFromJson(json); - Map toJson() => { - "medium": medium, - }; + Map toJson() => _$ZipUrlsToJson(this); } diff --git a/lib/models/user_preview.dart b/lib/models/user_preview.dart index d1b4a5ab6..2e33fd5a5 100644 --- a/lib/models/user_preview.dart +++ b/lib/models/user_preview.dart @@ -15,42 +15,25 @@ */ import 'dart:convert' show json; +import 'package:json_annotation/json_annotation.dart'; import 'package:pixez/models/illust.dart'; +part 'user_preview.g.dart'; +@JsonSerializable() class UserPreviewsResponse { List user_previews; - String next_url; + String? next_url; UserPreviewsResponse({ required this.user_previews, - required this.next_url, + this.next_url, }); - - factory UserPreviewsResponse.fromJson(jsonRes) { - List user_previews = []; - for (var item in jsonRes['user_previews']) { - if (item != null) { - user_previews.add(UserPreviews.fromJson(item)); - } - } - - return UserPreviewsResponse( - user_previews: user_previews, - next_url: jsonRes['next_url'], - ); - } - - Map toJson() => { - 'user_previews': user_previews, - 'next_url': next_url, - }; - - @override - String toString() { - return json.encode(this); - } + factory UserPreviewsResponse.fromJson(Map json) => + _$UserPreviewsResponseFromJson(json); + Map toJson() => _$UserPreviewsResponseToJson(this); } +@JsonSerializable() class UserPreviews { User user; List illusts; @@ -64,38 +47,7 @@ class UserPreviews { required this.is_muted, }); - factory UserPreviews.fromJson(jsonRes) { - List illusts = []; - for (var item in jsonRes['illusts']) { - if (item != null) { - illusts.add(Illusts.fromJson(item)); - } - } - - List novels = []; - for (var item in jsonRes['novels']) { - if (item != null) { - novels.add(item); - } - } - - return UserPreviews( - user: User.fromJson(jsonRes['user']), - illusts: illusts, - novels: novels, - is_muted: jsonRes['is_muted'], - ); - } - - Map toJson() => { - 'user': user, - 'illusts': illusts, - 'novels': novels, - 'is_muted': is_muted, - }; - - @override - String toString() { - return json.encode(this); - } + factory UserPreviews.fromJson(Map json) => + _$UserPreviewsFromJson(json); + Map toJson() => _$UserPreviewsToJson(this); }