From 504ee9c8639f4cfbb0a091f1f33620c144bcfd53 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Sun, 30 Oct 2022 14:41:12 +0800 Subject: [PATCH] refactor: use freezed --- lib/models/account.dart | 52 ++---- lib/models/account.freezed.dart | 300 ++++++++++++++++++++++++++++++++ lib/models/account.g.dart | 4 +- pubspec.lock | 14 ++ pubspec.yaml | 2 + 5 files changed, 333 insertions(+), 39 deletions(-) create mode 100644 lib/models/account.freezed.dart diff --git a/lib/models/account.dart b/lib/models/account.dart index f8d8166..9351e7e 100644 --- a/lib/models/account.dart +++ b/lib/models/account.dart @@ -1,44 +1,22 @@ -import 'package:json_annotation/json_annotation.dart'; +import 'package:freezed_annotation/freezed_annotation.dart'; +part 'account.freezed.dart'; part 'account.g.dart'; -@JsonSerializable(includeIfNull: false) -class Account { - // For Bitbucket - - // equals(Account a) { - // final uri = Uri.parse(domain); - // final uriA = Uri.parse(a.domain); - - // // Treat domains as the same if they have the same hosts and ports - // return a.platform == platform && - // a.login == login && - // a.gitlabId == gitlabId && - // uri.host == uriA.host && - // uri.port == uriA.port; - // } - - Account({ - required this.platform, - required this.domain, - required this.token, - required this.login, - required this.avatarUrl, - this.gitlabId, - this.appPassword, - this.accountId, - }); +@freezed +class Account with _$Account { + @JsonSerializable(includeIfNull: false) + factory Account({ + required String platform, + required String domain, + required String token, + required String login, + required String avatarUrl, + int? gitlabId, // For GitLab + String? appPassword, // For Bitbucket + String? accountId, // For Bitbucket + }) = _Account; factory Account.fromJson(Map json) => _$AccountFromJson(json); - String platform; - String domain; - String token; - String login; - String avatarUrl; - int? gitlabId; // For GitLab - String? appPassword; // For Bitbucket - String? accountId; - - Map toJson() => _$AccountToJson(this); } diff --git a/lib/models/account.freezed.dart b/lib/models/account.freezed.dart new file mode 100644 index 0000000..a31d5c1 --- /dev/null +++ b/lib/models/account.freezed.dart @@ -0,0 +1,300 @@ +// coverage:ignore-file +// GENERATED CODE - DO NOT MODIFY BY HAND +// ignore_for_file: type=lint +// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target + +part of 'account.dart'; + +// ************************************************************************** +// FreezedGenerator +// ************************************************************************** + +T _$identity(T value) => value; + +final _privateConstructorUsedError = UnsupportedError( + 'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#custom-getters-and-methods'); + +Account _$AccountFromJson(Map json) { + return _Account.fromJson(json); +} + +/// @nodoc +mixin _$Account { + String get platform => throw _privateConstructorUsedError; + String get domain => throw _privateConstructorUsedError; + String get token => throw _privateConstructorUsedError; + String get login => throw _privateConstructorUsedError; + String get avatarUrl => throw _privateConstructorUsedError; + int? get gitlabId => throw _privateConstructorUsedError; // For GitLab + String? get appPassword => + throw _privateConstructorUsedError; // For Bitbucket + String? get accountId => throw _privateConstructorUsedError; + + Map toJson() => throw _privateConstructorUsedError; + @JsonKey(ignore: true) + $AccountCopyWith get copyWith => throw _privateConstructorUsedError; +} + +/// @nodoc +abstract class $AccountCopyWith<$Res> { + factory $AccountCopyWith(Account value, $Res Function(Account) then) = + _$AccountCopyWithImpl<$Res, Account>; + @useResult + $Res call( + {String platform, + String domain, + String token, + String login, + String avatarUrl, + int? gitlabId, + String? appPassword, + String? accountId}); +} + +/// @nodoc +class _$AccountCopyWithImpl<$Res, $Val extends Account> + implements $AccountCopyWith<$Res> { + _$AccountCopyWithImpl(this._value, this._then); + + // ignore: unused_field + final $Val _value; + // ignore: unused_field + final $Res Function($Val) _then; + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? platform = null, + Object? domain = null, + Object? token = null, + Object? login = null, + Object? avatarUrl = null, + Object? gitlabId = freezed, + Object? appPassword = freezed, + Object? accountId = freezed, + }) { + return _then(_value.copyWith( + platform: null == platform + ? _value.platform + : platform // ignore: cast_nullable_to_non_nullable + as String, + domain: null == domain + ? _value.domain + : domain // ignore: cast_nullable_to_non_nullable + as String, + token: null == token + ? _value.token + : token // ignore: cast_nullable_to_non_nullable + as String, + login: null == login + ? _value.login + : login // ignore: cast_nullable_to_non_nullable + as String, + avatarUrl: null == avatarUrl + ? _value.avatarUrl + : avatarUrl // ignore: cast_nullable_to_non_nullable + as String, + gitlabId: freezed == gitlabId + ? _value.gitlabId + : gitlabId // ignore: cast_nullable_to_non_nullable + as int?, + appPassword: freezed == appPassword + ? _value.appPassword + : appPassword // ignore: cast_nullable_to_non_nullable + as String?, + accountId: freezed == accountId + ? _value.accountId + : accountId // ignore: cast_nullable_to_non_nullable + as String?, + ) as $Val); + } +} + +/// @nodoc +abstract class _$$_AccountCopyWith<$Res> implements $AccountCopyWith<$Res> { + factory _$$_AccountCopyWith( + _$_Account value, $Res Function(_$_Account) then) = + __$$_AccountCopyWithImpl<$Res>; + @override + @useResult + $Res call( + {String platform, + String domain, + String token, + String login, + String avatarUrl, + int? gitlabId, + String? appPassword, + String? accountId}); +} + +/// @nodoc +class __$$_AccountCopyWithImpl<$Res> + extends _$AccountCopyWithImpl<$Res, _$_Account> + implements _$$_AccountCopyWith<$Res> { + __$$_AccountCopyWithImpl(_$_Account _value, $Res Function(_$_Account) _then) + : super(_value, _then); + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? platform = null, + Object? domain = null, + Object? token = null, + Object? login = null, + Object? avatarUrl = null, + Object? gitlabId = freezed, + Object? appPassword = freezed, + Object? accountId = freezed, + }) { + return _then(_$_Account( + platform: null == platform + ? _value.platform + : platform // ignore: cast_nullable_to_non_nullable + as String, + domain: null == domain + ? _value.domain + : domain // ignore: cast_nullable_to_non_nullable + as String, + token: null == token + ? _value.token + : token // ignore: cast_nullable_to_non_nullable + as String, + login: null == login + ? _value.login + : login // ignore: cast_nullable_to_non_nullable + as String, + avatarUrl: null == avatarUrl + ? _value.avatarUrl + : avatarUrl // ignore: cast_nullable_to_non_nullable + as String, + gitlabId: freezed == gitlabId + ? _value.gitlabId + : gitlabId // ignore: cast_nullable_to_non_nullable + as int?, + appPassword: freezed == appPassword + ? _value.appPassword + : appPassword // ignore: cast_nullable_to_non_nullable + as String?, + accountId: freezed == accountId + ? _value.accountId + : accountId // ignore: cast_nullable_to_non_nullable + as String?, + )); + } +} + +/// @nodoc + +@JsonSerializable(includeIfNull: false) +class _$_Account implements _Account { + _$_Account( + {required this.platform, + required this.domain, + required this.token, + required this.login, + required this.avatarUrl, + this.gitlabId, + this.appPassword, + this.accountId}); + + factory _$_Account.fromJson(Map json) => + _$$_AccountFromJson(json); + + @override + final String platform; + @override + final String domain; + @override + final String token; + @override + final String login; + @override + final String avatarUrl; + @override + final int? gitlabId; +// For GitLab + @override + final String? appPassword; +// For Bitbucket + @override + final String? accountId; + + @override + String toString() { + return 'Account(platform: $platform, domain: $domain, token: $token, login: $login, avatarUrl: $avatarUrl, gitlabId: $gitlabId, appPassword: $appPassword, accountId: $accountId)'; + } + + @override + bool operator ==(dynamic other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$_Account && + (identical(other.platform, platform) || + other.platform == platform) && + (identical(other.domain, domain) || other.domain == domain) && + (identical(other.token, token) || other.token == token) && + (identical(other.login, login) || other.login == login) && + (identical(other.avatarUrl, avatarUrl) || + other.avatarUrl == avatarUrl) && + (identical(other.gitlabId, gitlabId) || + other.gitlabId == gitlabId) && + (identical(other.appPassword, appPassword) || + other.appPassword == appPassword) && + (identical(other.accountId, accountId) || + other.accountId == accountId)); + } + + @JsonKey(ignore: true) + @override + int get hashCode => Object.hash(runtimeType, platform, domain, token, login, + avatarUrl, gitlabId, appPassword, accountId); + + @JsonKey(ignore: true) + @override + @pragma('vm:prefer-inline') + _$$_AccountCopyWith<_$_Account> get copyWith => + __$$_AccountCopyWithImpl<_$_Account>(this, _$identity); + + @override + Map toJson() { + return _$$_AccountToJson( + this, + ); + } +} + +abstract class _Account implements Account { + factory _Account( + {required final String platform, + required final String domain, + required final String token, + required final String login, + required final String avatarUrl, + final int? gitlabId, + final String? appPassword, + final String? accountId}) = _$_Account; + + factory _Account.fromJson(Map json) = _$_Account.fromJson; + + @override + String get platform; + @override + String get domain; + @override + String get token; + @override + String get login; + @override + String get avatarUrl; + @override + int? get gitlabId; + @override // For GitLab + String? get appPassword; + @override // For Bitbucket + String? get accountId; + @override + @JsonKey(ignore: true) + _$$_AccountCopyWith<_$_Account> get copyWith => + throw _privateConstructorUsedError; +} diff --git a/lib/models/account.g.dart b/lib/models/account.g.dart index cbf88f5..1be33f7 100644 --- a/lib/models/account.g.dart +++ b/lib/models/account.g.dart @@ -6,7 +6,7 @@ part of 'account.dart'; // JsonSerializableGenerator // ************************************************************************** -Account _$AccountFromJson(Map json) => Account( +_$_Account _$$_AccountFromJson(Map json) => _$_Account( platform: json['platform'] as String, domain: json['domain'] as String, token: json['token'] as String, @@ -17,7 +17,7 @@ Account _$AccountFromJson(Map json) => Account( accountId: json['accountId'] as String?, ); -Map _$AccountToJson(Account instance) { +Map _$$_AccountToJson(_$_Account instance) { final val = { 'platform': instance.platform, 'domain': instance.domain, diff --git a/pubspec.lock b/pubspec.lock index ac265e3..4b9f8e3 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -322,6 +322,20 @@ packages: description: flutter source: sdk version: "0.0.0" + freezed: + dependency: "direct dev" + description: + name: freezed + url: "https://pub.dartlang.org" + source: hosted + version: "2.2.0" + freezed_annotation: + dependency: "direct main" + description: + name: freezed_annotation + url: "https://pub.dartlang.org" + source: hosted + version: "2.2.0" from_css_color: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 02eda32..a65f8d6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -76,6 +76,7 @@ dependencies: from_css_color: ^2.0.0 maps_launcher: ^2.0.1 path: ^1.8.2 + freezed_annotation: ^2.2.0 dev_dependencies: build_runner: ^2.3.0 @@ -83,6 +84,7 @@ dev_dependencies: flutter_lints: ^2.0.1 flutter_test: sdk: flutter + freezed: ^2.2.0 json_serializable: ^6.5.1 # For information on the generic Dart part of this file, see the