Skip to content

Commit

Permalink
Merge pull request #3 from OpenRowingCommunity/immutablility
Browse files Browse the repository at this point in the history
Migrate to using `freezed` package for immutability instead of `equatable`
  • Loading branch information
MoralCode committed Mar 5, 2024
2 parents 1699470 + 7339eef commit 6ce6eb7
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 178 deletions.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`

# Files generated by dart tools
.dart_tool/
.packages
doc/

# Avoid committing pubspec.lock for library packages; see
# https://dart.dev/guides/libraries/private-files#pubspeclock.
pubspec.lock

# Common build output
/*/build/
build/

# Include .packages files from tests which are hand coded
!build_runner_core/test/fixtures/**/.packages
!build_runner_core/test/fixtures/**/pubspec.lock

# Extra files from dart2js that we don't want
build_runner/lib/src/server/graph_viz_main.dart.js.deps
build_runner/lib/src/server/graph_viz_main.dart.js.tar.gz
build_runner/lib/src/server/build_updates_client/hot_reload_client.dart.js.deps
build_runner/lib/src/server/build_updates_client/hot_reload_client.dart.js.tar.gz


*.g.dart

*.freezed.dart
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ children = [
```


## Development and Running locally

`dart run build_runner build` is needed to generate code before things will likely work (if running locally)

## Testing
```
dart run build_runner build
flutter test
```

## Additional information

This package is still in a relatively early stage of development. Things might break. I'll do my best to fix these issues and add new features in a timely manner. Please file a Github Issue if you find any bugs, or start a discussion if you have feature requests or want to ask questions.
24 changes: 16 additions & 8 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.6"
equatable:
dependency: transitive
description:
name: equatable
sha256: c2b87cb7756efdf69892005af546c56c0b5037f54d2a88269b4f347a505e3ca2
url: "https://pub.dev"
source: hosted
version: "2.0.5"
fake_async:
dependency: transitive
description:
Expand Down Expand Up @@ -167,6 +159,14 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
freezed_annotation:
dependency: transitive
description:
name: freezed_annotation
sha256: c3fd9336eb55a38cc1bbd79ab17573113a8deccd0ecbbf926cca3c62803b5c2d
url: "https://pub.dev"
source: hosted
version: "2.4.1"
http:
dependency: transitive
description:
Expand All @@ -191,6 +191,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.6.7"
json_annotation:
dependency: transitive
description:
name: json_annotation
sha256: b10a7b2ff83d83c777edba3c6a0f97045ddadd56c944e1a23a3fdf43a1bf4467
url: "https://pub.dev"
source: hosted
version: "4.8.1"
lints:
dependency: transitive
description:
Expand Down
134 changes: 37 additions & 97 deletions lib/src/types/c2_results.dart
Original file line number Diff line number Diff line change
@@ -1,99 +1,39 @@
import 'c2_types.dart';
import 'package:equatable/equatable.dart';

class C2Results extends Equatable {
int id;
int userId;

// DateTime date;
String? timezone;
DateTime? get dateUtc => null;

int distance;
C2ResultType type;

int time;
String get timeFormatted => "";

C2APIWorkoutType workoutType;
// ignore_for_file: invalid_annotation_target

String source;
C2WeightClass weightClass;
bool verified;
bool ranked;
String? comments;
C2PrivacyLevel privacy;

C2Results({
this.id = 0,
this.userId = 0,
// this.date = DateTime(1970,1,1),
this.timezone,
this.distance = 0,
this.type = C2ResultType.rower,
this.time = 0,
this.workoutType = C2APIWorkoutType.JustRow,
this.source = "c2logbook dart",
this.weightClass = C2WeightClass.heavyweight,
this.verified = false,
this.ranked = false,
this.comments,
this.privacy = C2PrivacyLevel.private,
});

@override
List<Object> get props => [
id,
userId,
// date,
// timezone,
distance,
type,
time,
workoutType,
source,
weightClass,
verified,
ranked,
// comments,
privacy
];

factory C2Results.fromJson(Map<String, dynamic> json) {
// {
// "id": 3,
// "user_id": 1,
// "date": "2013-06-21 00:00:00",
// "timezone": null,
// "date_utc": null,
// "distance": 23000,
// "type": "rower",
// "time": 152350,
// "time_formatted": "4:13:55.0",
// "workout_type": "unknown",
// "source": "Web",
// "weight_class": "H",
// "verified": false,
// "ranked": false,
// "comments": null,
// "privacy": "partners"
// },

return C2Results(
id: json['id'],
userId: json['user_id'],
// date: json['date'],
timezone: json['timezone'],
distance: json['distance'],
type: C2ResultTypeExtension.fromString(json['type']),
time: json['time'],
workoutType: C2APIWorkoutTypeExtension.fromString(json['workout_type']),
source: json['source'],
weightClass: C2WeightClassExtension.fromString(json['weight_class']),
verified: json['verified'],
ranked: json['ranked'],
comments: json['comments'],
privacy: C2PrivacyExtension.fromString(json['privacy']) ??
C2PrivacyLevel.private);
}
import 'c2_types.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'c2_results.freezed.dart';
part 'c2_results.g.dart';

@freezed
class C2Results with _$C2Results {
//TODO: figure out how to get this into JSON as date_utc
// DateTime? get dateUtc => null;
//TODO: figure out how to get this into JSON as time_formatted
// String get timeFormatted => "";

factory C2Results({
@Default(0) int id,
@JsonKey(name: "user_id") @Default(0) int userId,
// @JsonKey(name: "date") @Default(DateTime(1970, 1, 1)) DateTime date,
String? timezone,
@Default(0) int distance,
@Default(C2ResultType.rower) C2ResultType type,
@Default(0) int time,
@JsonKey(name: "workout_type")
@Default(C2APIWorkoutType.JustRow)
C2APIWorkoutType workoutType,
@Default("c2logbook dart") String source,
@JsonKey(name: "weight_class")
@Default(C2WeightClass.heavyweight)
C2WeightClass weightClass,
@Default(false) bool verified,
@Default(false) bool ranked,
String? comments,
@Default(C2PrivacyLevel.private) C2PrivacyLevel privacy,
}) = _C2Results;

factory C2Results.fromJson(Map<dynamic, dynamic> json) =>
_$C2ResultsFromJson(Map<String, dynamic>.from(json));
}
4 changes: 4 additions & 0 deletions lib/src/types/c2_types.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// TODO: add conversion to string to allow logged_in to be represented in dart as loggedIn
import 'package:freezed_annotation/freezed_annotation.dart';

enum C2PrivacyLevel { private, partners, logged_in, everyone }

//https://stackoverflow.com/a/44060511/
Expand Down Expand Up @@ -72,7 +74,9 @@ extension C2APIWorkoutTypeExtension on C2APIWorkoutType {
}

enum C2WeightClass {
@JsonValue("L")
lightweight,
@JsonValue("H")
heavyweight
// C2WeightClass fromString()
}
Expand Down
99 changes: 27 additions & 72 deletions lib/src/types/c2_user.dart
Original file line number Diff line number Diff line change
@@ -1,77 +1,32 @@
import 'package:c2logbook/src/types/c2_types.dart';
import 'package:equatable/equatable.dart';
// ignore_for_file: invalid_annotation_target

class C2User extends Equatable {
int id;
String username;
String firstName;
String lastName;
String gender;
String birthday;
String email;
String country;
String? profileImage;
bool ageRestricted;
bool emailPermission;
int? maxHeartRate;
int? weight;
List<String> roles;
C2PrivacyLevel logbookPrivacy;
import 'package:c2logbook/src/types/c2_types.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

C2User(
{this.id = 0,
this.username = '',
this.firstName = '',
this.lastName = '',
this.gender = 'F',
this.birthday = '1970-01-01',
this.email = '',
this.country = '',
this.profileImage,
this.ageRestricted = false,
this.emailPermission = false,
this.maxHeartRate,
this.weight,
this.roles = const [],
this.logbookPrivacy = C2PrivacyLevel.private});
part 'c2_user.freezed.dart';
part 'c2_user.g.dart';

@override
List<Object> get props => [
id,
username,
firstName,
lastName,
gender,
birthday,
email,
country,
// profileImage,
ageRestricted,
emailPermission,
// maxHeartRate,
// weight,
roles,
logbookPrivacy
];
@freezed
class C2User with _$C2User {
const factory C2User(
{@Default(0) int id,
@Default('') String username,
@JsonKey(name: 'first_name') @Default('') String firstName,
@JsonKey(name: 'last_name') @Default('') String lastName,
@Default('F') String gender,
@JsonKey(name: 'dob') @Default('1970-01-01') String birthday,
@Default('') String email,
@Default('') String country,
@JsonKey(name: 'profile_image') String? profileImage,
@JsonKey(name: 'age_restricted') @Default(false) bool ageRestricted,
@JsonKey(name: 'email_permission') @Default(false) bool emailPermission,
@JsonKey(name: 'max_heart_rate') int? maxHeartRate,
int? weight,
@Default(<String>[]) List<String> roles,
@JsonKey(name: 'logbook_privacy')
@Default(C2PrivacyLevel.private)
C2PrivacyLevel logbookPrivacy}) = _C2User;

factory C2User.fromJson(Map<String, dynamic> json) {
return C2User(
id: json['id'],
username: json['username'],
firstName: json['first_name'],
lastName: json['last_name'],
gender: json['gender'],
birthday: json['dob'],
email: json['email'],
country: json['country'],
profileImage: json['profile_image'],
ageRestricted: json['age_restricted'],
emailPermission: json['email_permission'],
// maxHeartRate: json['max_heart_rate'],
// weight: json['weight'],
// roles: json['roles'],
logbookPrivacy:
C2PrivacyExtension.fromString(json['logbook_privacy']) ??
C2PrivacyLevel.private);
}
factory C2User.fromJson(Map<dynamic, dynamic> json) =>
_$C2UserFromJson(Map<String, dynamic>.from(json));
}
6 changes: 5 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ environment:

# Add regular dependencies here.
dependencies:
equatable: ^2.0.5
freezed_annotation: ^2.4.1
http: ^1.2.0
json_annotation: ^4.8.1
oauth2_client: ^3.2.2
# path: ^1.8.0

dev_dependencies:
analysis_options: any
build_runner: ^2.4.8
freezed: ^2.4.7
json_serializable: ^6.7.1
lints: ^2.1.0
test: ^1.24.0

0 comments on commit 6ce6eb7

Please sign in to comment.