Skip to content

Commit

Permalink
Merge pull request #6 from sonbui00/feat-5
Browse files Browse the repository at this point in the history
Feat: add support json for card class #5
  • Loading branch information
sonbui00 authored May 1, 2024
2 parents 901f11d + d95c5e8 commit b586f55
Show file tree
Hide file tree
Showing 6 changed files with 540 additions and 52 deletions.
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

0 comments on commit b586f55

Please sign in to comment.