Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add support json for card class #5 #6

Merged
merged 2 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.1

- Add support json function on Card class

## 1.1.0

- Fix coding standard.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/fsrs_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class FSRS {
}

Map<Rating, SchedulingInfo> repeat(Card card, DateTime now) {
card = Card.copyFrom(card);
card = card.copyWith();
if (card.state == State.newState) {
card.elapsedDays = 0;
} else {
Expand Down
77 changes: 27 additions & 50 deletions lib/src/models.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import 'dart:core';
import 'dart:math';
import 'dart:convert';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'models.freezed.dart';
part 'models.g.dart';

enum State {
newState(0),
Expand Down Expand Up @@ -46,54 +50,27 @@ class ReviewLog {
}

/// Store card data
class Card {
/// The time when the card is scheduled to be reviewed again
late DateTime due;
double stability = 0;

/// How challenging or easy you find a specific flashcard during a review session
double difficulty = 0;
int elapsedDays = 0;
int scheduledDays = 0;
int reps = 0;
int lapses = 0;
State state = State.newState;
late DateTime lastReview;

@override
String toString() {
return jsonEncode({
"due": due.toString(),
"stability": stability,
"difficulty": difficulty,
"elapsedDays": elapsedDays,
"scheduledDays": scheduledDays,
"reps": reps,
"lapses": lapses,
"state": state.toString(),
"lastReview": lastReview.toString(),
});
}
@unfreezed
class Card with _$Card {
const Card._();

factory Card.def(
DateTime due,
DateTime lastReview, [
@Default(0) double stability,
@Default(0) double difficulty,
@Default(0) int elapsedDays,
@Default(0) int scheduledDays,
@Default(0) int reps,
@Default(0) int lapses,
@Default(State.newState) State state,
]) = _Card;

factory Card.fromJson(Map<String, Object?> json) => _$CardFromJson(json);

/// Construct current time for due and last review
Card() {
due = DateTime.now().toUtc();
lastReview = DateTime.now().toUtc();
}

/// Helper to clone from a card
factory Card.copyFrom(Card card) {
Card newCard = Card();
newCard.due = card.due;
newCard.stability = card.stability;
newCard.difficulty = card.difficulty;
newCard.elapsedDays = card.elapsedDays;
newCard.scheduledDays = card.scheduledDays;
newCard.reps = card.reps;
newCard.lapses = card.lapses;
newCard.state = card.state;
newCard.lastReview = card.lastReview;
return newCard;
factory Card() {
return _Card(DateTime.now().toUtc(), DateTime.now().toUtc());
}

double? getRetrievability(DateTime now) {
Expand Down Expand Up @@ -126,10 +103,10 @@ class SchedulingCards {
late Card easy;

SchedulingCards(Card card) {
again = Card.copyFrom(card);
hard = Card.copyFrom(card);
good = Card.copyFrom(card);
easy = Card.copyFrom(card);
again = card.copyWith();
hard = card.copyWith();
good = card.copyWith();
easy = card.copyWith();
}

void updateState(State state) {
Expand Down
Loading
Loading