Skip to content

Commit

Permalink
some fixes & improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
DedInc committed Mar 17, 2024
1 parent fe82ffb commit 66ca2e8
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static boolean isReviseViolated(long learnDate, long[] reviseDates) {
for (int i = 0; i < reviseDates.length; i++) {
long daysSinceRevision = (reviseDates[i] - learnDate) / 86400;
for (int interval : intervals) {
if (daysSinceRevision > Math.round(interval + interval / 3.0)) {
if (daysSinceRevision > Math.round(interval + interval / 3.5)) {
return true;
}
}
Expand Down
58 changes: 25 additions & 33 deletions src/main/java/com/github/dedinc/mindvault/core/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
import com.github.dedinc.mindvault.core.objects.State;

import java.util.*;
import java.util.stream.Collectors;

public class Session {
private int typeSpeed;
private Map<State, List<Card>> cards;
private List<Double> grades;
private int perSessionCards;
private List<Card> allCardsCache;

private PomodoroTimer pomodoroTimer;

Expand All @@ -27,14 +25,12 @@ public Session(long startTime, String text) {
}
this.grades = new ArrayList<>();
this.perSessionCards = 5;
this.allCardsCache = null;
this.pomodoroTimer = new PomodoroTimer();
}

public void addCard(String question, String answer) {
Card newCard = new Card(question, answer, 0, new long[0]);
cards.get(State.LEARN).add(newCard);
invalidateAllCardsCache();
}

public void addCards(List<Card> cards) {
Expand All @@ -47,7 +43,6 @@ public void removeCard(String question) {
for (List<Card> cardList : cards.values()) {
cardList.removeIf(card -> card.getQuestion().equals(question));
}
invalidateAllCardsCache();
}

public void removeCards(List<String> questions) {
Expand All @@ -58,12 +53,15 @@ public void removeCards(List<String> questions) {

public void moveCard(Card card, State newState) {
State currentState = card.getCategory(this);

if (currentState == newState) {
return;
}
cards.get(currentState).removeIf(c -> c.getQuestion().equals(card.getQuestion()));

for (State category : Arrays.asList(State.REVISE, State.WEAK, State.MIDDLE, State.LEARN)) {
cards.get(category).removeIf(c -> c.getQuestion().equals(card.getQuestion()));
}
cards.get(newState).add(card);
invalidateAllCardsCache();
}

public void updateCards() {
Expand Down Expand Up @@ -94,21 +92,22 @@ public void updateCards() {
for (Map.Entry<State, List<Card>> entry : cardsToMove.entrySet()) {
cards.get(entry.getKey()).addAll(entry.getValue());
}
invalidateAllCardsCache();
}

public void checkCard(Card card, double grade) {
State currentState = card.getCategory(this);

if (currentState == State.LEARN) {
card.setLearnDate(Time.getUnix());
moveCard(card, State.WEAK);
return;
}
if (currentState == State.REVISE) {
List<Long> reviseDates = new ArrayList<>(Arrays.asList(Arrays.stream(card.getReviseDates()).boxed().toArray(Long[]::new)));
reviseDates.add(Time.getUnix());
card.setReviseDates(reviseDates.stream().mapToLong(Long::longValue).toArray());
if (reviseDates.size() == 1) {
int revises = card.getReviseDates().length;
long[] newReviseDates = Arrays.copyOf(card.getReviseDates(), revises + 1);
newReviseDates[revises] = Time.getUnix();
card.setReviseDates(newReviseDates);
if (revises == 0) {
moveCard(card, State.WEAK);
} else {
moveCard(card, grade >= 0.80 ? State.STRONG : grade <= 0.6 ? State.WEAK : State.MIDDLE);
Expand All @@ -121,27 +120,22 @@ public void checkCard(Card card, double grade) {
}
if (currentState == State.MIDDLE) {
if (grade >= 0.85) {
System.out.println("Move...");
moveCard(card, State.STRONG);
} else if (grade < 0.6) {
moveCard(card, State.WEAK);
}
return;
}
if (currentState == State.STRONG && grade <= 0.8) {
moveCard(card, State.MIDDLE);
}
}

public void updateLevel() {
if (!grades.isEmpty()) {
double grade = Grades.calculateTotalGrade(grades);
perSessionCards = Math.min(
grade >= 0.9 ? perSessionCards + 3 : grade >= 0.75 ? perSessionCards + 2 : grade <= 0.6 ? perSessionCards - 1 : perSessionCards,
10
);
perSessionCards = Math.max(perSessionCards, 5);
grades.clear();
}
double grade = Grades.calculateTotalGrade(grades);
perSessionCards = Math.min(
grade >= 0.9 ? perSessionCards + 3 : grade >= 0.75 ? perSessionCards + 2 : grade <= 0.6 ? perSessionCards - 1 : perSessionCards,
10
);
perSessionCards = Math.max(perSessionCards, 5);
grades.clear();
}

public List<Card> getCards() {
Expand Down Expand Up @@ -173,10 +167,13 @@ public Map<State, List<Card>> getCategories() {
}

public List<Card> getAllCards() {
if (allCardsCache == null) {
allCardsCache = cards.values().stream().flatMap(List::stream).collect(Collectors.toList());
List<Card> allCards = new ArrayList<>();
for (List<Card> cards : getCategories().values()) {
for (Card card : cards) {
allCards.add(card);
}
}
return allCardsCache;
return allCards;
}

public void setTypeSpeed(int typeSpeed) {
Expand All @@ -185,7 +182,6 @@ public void setTypeSpeed(int typeSpeed) {

public void setCards(Map<State, List<Card>> cards) {
this.cards = cards;
invalidateAllCardsCache();
}

public void setGrades(List<Double> grades) {
Expand All @@ -196,10 +192,6 @@ public void setPerSessionCards(int perSessionCards) {
this.perSessionCards = perSessionCards;
}

private void invalidateAllCardsCache() {
allCardsCache = null;
}

public PomodoroTimer getPomodoroTimer() {
return pomodoroTimer;
}
Expand Down
17 changes: 5 additions & 12 deletions src/main/java/com/github/dedinc/mindvault/core/objects/Card.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ public class Card {
private String answer;
private long learnDate;
private long[] reviseDates;
private State cachedCategory;

public Card(String question, String answer, long learnDate, long[] reviseDates) {
this.question = question;
this.answer = answer;
this.learnDate = learnDate;
this.reviseDates = reviseDates;
this.cachedCategory = null;
}

public String getQuestion() {
Expand All @@ -34,7 +32,6 @@ public long getLearnDate() {

public void setLearnDate(long learnDate) {
this.learnDate = learnDate;
this.cachedCategory = null;
}

public long[] getReviseDates() {
Expand All @@ -43,19 +40,15 @@ public long[] getReviseDates() {

public void setReviseDates(long[] reviseDates) {
this.reviseDates = reviseDates;
this.cachedCategory = null;
}

public State getCategory(Session session) {
if (cachedCategory == null) {
Map<State, List<Card>> categories = session.getCategories();
for (State state : categories.keySet()) {
if (categories.get(state).contains(this)) {
cachedCategory = state;
break;
}
Map<State, List<Card>> categories = session.getCategories();
for (State state : categories.keySet()) {
if (categories.get(state).contains(this)) {
return state;
}
}
return cachedCategory;
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private TimeSeriesCollection createDataset() {
long futureRevisionDate = (long) (lastRevisionDate + intervals[i] * 86400);
if (futureRevisionDate > currentDate) {
futureRevisionSeries.addOrUpdate(new Day(new Date(futureRevisionDate * 1000L)), 100);
violationSeries.addOrUpdate(new Day(new Date((futureRevisionDate + (long) (intervals[i] * 86400 / 3)) * 1000L)), 100);
violationSeries.addOrUpdate(new Day(new Date((futureRevisionDate + (long) (intervals[i] * 86400 / 3.5)) * 1000L)), 100);
break;
}
lastRevisionDate = futureRevisionDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ private void updatePomodoroStatus() {

private void createSession() {
session = FileUtils.createNewSession();
session.updateCards();
manageCardsButton.setEnabled(true);
startSessionButton.setEnabled(true);
saveSessionButton.setEnabled(true);
Expand All @@ -173,6 +174,7 @@ private void loadSession() {
File selectedFile = FileUtils.showLoadSessionDialog(this);
if (selectedFile != null) {
session = FileUtils.loadSession(selectedFile);
session.updateCards();
manageCardsButton.setEnabled(true);
startSessionButton.setEnabled(true);
saveSessionButton.setEnabled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.github.dedinc.mindvault.core.Time;
import com.github.dedinc.mindvault.core.objects.Card;
import com.github.dedinc.mindvault.ui.StatusBar;
import com.github.dedinc.mindvault.ui.UIComponents;

import javax.swing.*;
import java.awt.*;
Expand Down

0 comments on commit 66ca2e8

Please sign in to comment.