Skip to content

Commit

Permalink
🎨 闪卡支持设置复习上限 #7703
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Mar 28, 2023
1 parent cdac0da commit f1c55ff
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
4 changes: 2 additions & 2 deletions app/src/card/openCard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export const openCardByData = (cardsData: ICard[], html = "") => {
deckID: blocks[index].deckID,
cardID: blocks[index].cardID,
rating: parseInt(type),
reviewedCardIDs: blocks
reviewedCards: blocks
}, () => {
/// #if MOBILE
if (type !== "-3" &&
Expand All @@ -213,7 +213,7 @@ export const openCardByData = (cardsData: ICard[], html = "") => {
rootID: titleElement.getAttribute("data-id"),
deckID: selectElement?.value,
notebook: titleElement.getAttribute("data-notebookid"),
reviewedCardIDs: blocks
reviewedCards: blocks
}, (treeCards) => {
index = 0;
blocks = treeCards.data;
Expand Down
26 changes: 22 additions & 4 deletions kernel/api/riff.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ func reviewRiffCard(c *gin.Context) {
deckID := arg["deckID"].(string)
cardID := arg["cardID"].(string)
rating := int(arg["rating"].(float64))
err := model.ReviewFlashcard(deckID, cardID, riff.Rating(rating))
reviewedCardIDs := getReviewedCards(arg)
err := model.ReviewFlashcard(deckID, cardID, riff.Rating(rating), reviewedCardIDs)
if nil != err {
ret.Code = -1
ret.Msg = err.Error()
Expand Down Expand Up @@ -133,7 +134,8 @@ func getNotebookRiffDueCards(c *gin.Context) {
}

notebookID := arg["notebook"].(string)
cards, err := model.GetNotebookDueFlashcards(notebookID)
reviewedCardIDs := getReviewedCards(arg)
cards, err := model.GetNotebookDueFlashcards(notebookID, reviewedCardIDs)
if nil != err {
ret.Code = -1
ret.Msg = err.Error()
Expand All @@ -153,7 +155,8 @@ func getTreeRiffDueCards(c *gin.Context) {
}

rootID := arg["rootID"].(string)
cards, err := model.GetTreeDueFlashcards(rootID)
reviewedCardIDs := getReviewedCards(arg)
cards, err := model.GetTreeDueFlashcards(rootID, reviewedCardIDs)
if nil != err {
ret.Code = -1
ret.Msg = err.Error()
Expand All @@ -173,7 +176,8 @@ func getRiffDueCards(c *gin.Context) {
}

deckID := arg["deckID"].(string)
cards, err := model.GetDueFlashcards(deckID)
reviewedCardIDs := getReviewedCards(arg)
cards, err := model.GetDueFlashcards(deckID, reviewedCardIDs)
if nil != err {
ret.Code = -1
ret.Msg = err.Error()
Expand All @@ -183,6 +187,20 @@ func getRiffDueCards(c *gin.Context) {
ret.Data = cards
}

func getReviewedCards(arg map[string]interface{}) (ret []string) {
if nil == arg["reviewedCards"] {
return
}

reviewedCardsArg := arg["reviewedCards"].([]interface{})
for _, card := range reviewedCardsArg {
c := card.(map[string]interface{})
cardID := c["cardID"].(string)
ret = append(ret, cardID)
}
return
}

func removeRiffCards(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
Expand Down
32 changes: 18 additions & 14 deletions kernel/model/flashcard.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ var (
skipCardCache = map[string]riff.Card{}
)

func ReviewFlashcard(deckID, cardID string, rating riff.Rating) (err error) {
func ReviewFlashcard(deckID, cardID string, rating riff.Rating, reviewedCardIDs []string) (err error) {
deckLock.Lock()
defer deckLock.Unlock()

Expand Down Expand Up @@ -224,7 +224,7 @@ func ReviewFlashcard(deckID, cardID string, rating riff.Rating) (err error) {
return
}

dueCards := getDueFlashcards(deckID)
dueCards := getDueFlashcards(deckID, reviewedCardIDs)
if 1 > len(dueCards) {
// 该卡包中没有待复习的卡片了,说明最后一张卡片已经复习完了,清空撤销缓存和跳过缓存
reviewCardCache = map[string]riff.Card{}
Expand Down Expand Up @@ -273,7 +273,7 @@ func newFlashcard(card riff.Card, blockID, deckID string, now time.Time) *Flashc
}
}

func GetNotebookDueFlashcards(boxID string) (ret []*Flashcard, err error) {
func GetNotebookDueFlashcards(boxID string, reviewedCardIDs []string) (ret []*Flashcard, err error) {
deckLock.Lock()
defer deckLock.Unlock()

Expand Down Expand Up @@ -315,7 +315,7 @@ func GetNotebookDueFlashcards(boxID string) (ret []*Flashcard, err error) {
return
}

cards := getDeckDueCards(deck)
cards := getDeckDueCards(deck, reviewedCardIDs)
now := time.Now()
for _, card := range cards {
blockID := card.BlockID()
Expand All @@ -331,7 +331,7 @@ func GetNotebookDueFlashcards(boxID string) (ret []*Flashcard, err error) {
return
}

func GetTreeDueFlashcards(rootID string) (ret []*Flashcard, err error) {
func GetTreeDueFlashcards(rootID string, reviewedCardIDs []string) (ret []*Flashcard, err error) {
deckLock.Lock()
defer deckLock.Unlock()

Expand All @@ -346,7 +346,7 @@ func GetTreeDueFlashcards(rootID string) (ret []*Flashcard, err error) {
}

treeBlockIDs := getTreeSubTreeChildBlocks(rootID)
cards := getDeckDueCards(deck)
cards := getDeckDueCards(deck, reviewedCardIDs)
now := time.Now()
for _, card := range cards {
blockID := card.BlockID()
Expand Down Expand Up @@ -400,7 +400,7 @@ func getTreeSubTreeChildBlocks(rootID string) (treeBlockIDs map[string]bool) {
return
}

func GetDueFlashcards(deckID string) (ret []*Flashcard, err error) {
func GetDueFlashcards(deckID string, reviewedCardIDs []string) (ret []*Flashcard, err error) {
deckLock.Lock()
defer deckLock.Unlock()

Expand All @@ -410,22 +410,22 @@ func GetDueFlashcards(deckID string) (ret []*Flashcard, err error) {
}

if "" == deckID {
ret = getAllDueFlashcards()
ret = getAllDueFlashcards(reviewedCardIDs)
return
}

ret = getDueFlashcards(deckID)
ret = getDueFlashcards(deckID, reviewedCardIDs)
return
}

func getDueFlashcards(deckID string) (ret []*Flashcard) {
func getDueFlashcards(deckID string, reviewedCardIDs []string) (ret []*Flashcard) {
deck := Decks[deckID]
if nil == deck {
logging.LogWarnf("deck not found [%s]", deckID)
return
}

cards := getDeckDueCards(deck)
cards := getDeckDueCards(deck, reviewedCardIDs)
now := time.Now()
for _, card := range cards {
blockID := card.BlockID()
Expand All @@ -442,10 +442,10 @@ func getDueFlashcards(deckID string) (ret []*Flashcard) {
return
}

func getAllDueFlashcards() (ret []*Flashcard) {
func getAllDueFlashcards(reviewedCardIDs []string) (ret []*Flashcard) {
now := time.Now()
for _, deck := range Decks {
cards := getDeckDueCards(deck)
cards := getDeckDueCards(deck, reviewedCardIDs)
for _, card := range cards {
blockID := card.BlockID()
if nil == treenode.GetBlockTree(blockID) {
Expand Down Expand Up @@ -894,7 +894,7 @@ func getDeckIDs() (deckIDs []string) {
return
}

func getDeckDueCards(deck *riff.Deck) (ret []riff.Card) {
func getDeckDueCards(deck *riff.Deck, reviewedCardIDs []string) (ret []riff.Card) {
ret = []riff.Card{}
dues := deck.Dues()

Expand All @@ -918,6 +918,10 @@ func getDeckDueCards(deck *riff.Deck) (ret []riff.Card) {
}
}

if !gulu.Str.Contains(c.ID(), reviewedCardIDs) {
continue
}

ret = append(ret, c)
}
return
Expand Down

0 comments on commit f1c55ff

Please sign in to comment.