Skip to content

Commit

Permalink
Merge branch 'refactor_manager'
Browse files Browse the repository at this point in the history
  • Loading branch information
Dametrious Peyton committed Jan 16, 2015
2 parents 28703c9 + 7252342 commit 518d51d
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 102 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Mnemosyne port to Sailfish OS. Mnemosyne is a flash card program (written in Qt)

This project is GPLv3 unless otherwise indicated on the file itself. Files with other licenses are copyrighted to their respective owners.
<p>
Version 1.2.1
Version 1.2.2
<p>
<img src="http://www.gnu.org/graphics/gplv3-127x51.png" width="254" height="102" />

Expand Down
198 changes: 105 additions & 93 deletions harbour/nemosyne/Nemosyne/Manager.qml
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,13 @@ SQLiteDatabase {
property Card card

readonly property int late: 0
readonly property int early: 0
readonly property int onTime: 0
readonly property int early: 1
readonly property int onTime: 2

signal validateDatabase(string filePath)
/*!
The Mnemosyne 2.3 algorithm is more complex allowing for randomization of
cards on a stack and avoiding sister cards.
Until requested otherwise, this algorithm will just stick to the basics.
*/
signal next(int rating)
signal initializedDone()
signal deleteCard()
signal cardDeleted()
signal cardAdded()
signal databaseValid(bool valid)

DynamicLoader {
id: cardCreator
Expand All @@ -42,7 +36,7 @@ SQLiteDatabase {
fileName: ":/data/nemosyne.sql"
}

onDeleteCard: {
function deleteCard() {
Console.info("Manager: delete card selected")

if(!opened || !card) return
Expand Down Expand Up @@ -97,9 +91,10 @@ SQLiteDatabase {
}

card = null
cardDeleted()
}

onValidateDatabase: {
function validateDatabase(filePath) {
//Check if this is an sql lite db
if(opened) close()

Expand All @@ -122,9 +117,16 @@ SQLiteDatabase {
if(query.indexOf("value") !== -1) initTrackingValues()

validDb = result
databaseValid(result)
}

onNext: {
/*!
The Mnemosyne 2.3 algorithm is more complex allowing for randomization of
cards on a stack and avoiding sister cards.
Until requested otherwise, this algorithm will just stick to the basics.
*/
function next(rating) {
Console.debug("Manager::next")
if(!opened || !valid) return

Expand All @@ -133,7 +135,7 @@ SQLiteDatabase {
Console.debug("Card is not null")
if(rating != -1) {
//If the database was just opened, current card is orphaned.
grade(rating)
_grade(rating)
saveCard()
}
card = null
Expand Down Expand Up @@ -250,7 +252,87 @@ SQLiteDatabase {
unmemorized = query.value("count");
}

function calculateInitialInterval(rating, timing, actualInterval, scheduledInterval) {
function saveCard() {
_save(card, true)
}

function addCard(cardType, question, answer) {
//TODO: use transactions
//transaction()

Console.debug("addCard: create fact entry")
prepare("INSERT INTO facts (id) VALUES (:id)")

var factHash = _randUuid()
bind(":id", factHash)
if(!exec()) {
Console.error("addCard:" + "error" + lastError )
return
}

prepare("SELECT * from facts where id = :id ORDER BY _id DESC LIMIT 1")
bind(":id", factHash)
if(!exec() || !query.first()) {
Console.error("addCard:" + "error" + lastError )
return
}
var factId = query.value("_id")

Console.debug("addCard: create data for fact entry")
prepare("INSERT INTO data_for_fact (_fact_id, key, value) VALUES " +
"(:fact_id, :key, :value)")
bind(":fact_id", factId)
bind(":key", "f")
bind(":value", question)
if(!exec()) {
Console.error("addCard:" + "error" + lastError )
return
}
prepare("INSERT INTO data_for_fact (_fact_id, key, value) VALUES " +
"(:fact_id, :key, :value)")
bind(":fact_id", factId)
bind(":key", "b")
bind(":value", answer)
if(!exec()) {
Console.error("addCard:" + "error" + lastError )
return
}

Console.debug("addCard: add card")
var iter = cardType == CardTypes.FrontToBackAndBackToFront
var i = 0
do {
var card = {
grade: -1,
nextRep: -1,
lastRep: -1,
easiness: 2.5,
acquisition: 0,
retentionRep: 0,
lapses: 0,
acquisitionRepsSinceLapse: 0,
retentionRepsSinceLapse: 0,
question: !i ? question : answer,
answer: !i ? answer : question,
hash: _randUuid(),
cardTypeId: _convertCardType(cardType),
factId: factId,
factViewId: !i ? "2.1" : "2.2",
tags: ""
}
_save(card, false)
i++
} while(iter--)

cardAdded()
}

// Internal functions

/*!
\internal
*/
function _calculateInitialInterval(rating, timing, actualInterval, scheduledInterval) {
var day = 60*60*24
var intervals = [0, 0, day, 3 * day, 4 * day, 7 * day]
var choice3 = [1, 1, 2]
Expand Down Expand Up @@ -303,8 +385,8 @@ SQLiteDatabase {
memorized cards. One purpose is to avoid pulling in sister cards in the schedule.
This class will ignore facts for simplicity; although, I'm not sure how this
will Mnemosyne proper when shuffling the database.
*/
function grade(rating) {
*/
function _grade(rating) {
if(rating == -1) return

// First calculate timing
Expand All @@ -322,7 +404,7 @@ SQLiteDatabase {
var interval = card.nextRep - card.lastRep
var actualInterval = tstamp - card.lastRep
Console.debug(" actual interval " + actualInterval + "scheduled interval " + interval)
var intervalNew = calculateInitialInterval(rating, timing, oldGrade < 1 ? 0 : actualInterval, interval)
var intervalNew = _calculateInitialInterval(rating, timing, oldGrade < 1 ? 0 : actualInterval, interval)

// Set acquisition stage
if(oldGrade == -1) {
Expand Down Expand Up @@ -374,11 +456,10 @@ SQLiteDatabase {
//Ignoring log entry
}

function saveCard() {
save(card, true)
}

function save(card, update) {
/*!
\internal
*/
function _save(card, update) {
if(!opened || !card) return;

Console.debug("saving card " + card.seq + ", update: " + update)
Expand Down Expand Up @@ -440,75 +521,6 @@ SQLiteDatabase {
}
}

function addCard(cardType, question, answer) {
//TODO: use transactions
//transaction()

Console.debug("addCard: create fact entry")
prepare("INSERT INTO facts (id) VALUES (:id)")

var factHash = _randUuid()
bind(":id", factHash)
if(!exec()) {
Console.error("addCard:" + "error" + lastError )
return
}

prepare("SELECT * from facts where id = :id ORDER BY _id DESC LIMIT 1")
bind(":id", factHash)
if(!exec() || !query.first()) {
Console.error("addCard:" + "error" + lastError )
return
}
var factId = query.value("_id")

Console.debug("addCard: create data for fact entry")
prepare("INSERT INTO data_for_fact (_fact_id, key, value) VALUES " +
"(:fact_id, :key, :value)")
bind(":fact_id", factId)
bind(":key", "f")
bind(":value", question)
if(!exec()) {
Console.error("addCard:" + "error" + lastError )
return
}
prepare("INSERT INTO data_for_fact (_fact_id, key, value) VALUES " +
"(:fact_id, :key, :value)")
bind(":fact_id", factId)
bind(":key", "b")
bind(":value", answer)
if(!exec()) {
Console.error("addCard:" + "error" + lastError )
return
}

Console.debug("addCard: add card")
var iter = cardType == CardTypes.FrontToBackAndBackToFront
var i = 0
do {
var card = {
grade: -1,
nextRep: -1,
lastRep: -1,
easiness: 2.5,
acquisition: 0,
retentionRep: 0,
lapses: 0,
acquisitionRepsSinceLapse: 0,
retentionRepsSinceLapse: 0,
question: !i ? question : answer,
answer: !i ? answer : question,
hash: _randUuid(),
cardTypeId: _convertCardType(cardType),
factId: factId,
factViewId: !i ? "2.1" : "2.2",
tags: ""
}
save(card, false)
i++
} while(iter--)
}

/*!
\internal
Copied from Mnemosyne's util.py
Expand Down
2 changes: 1 addition & 1 deletion harbour/nemosyne/Nemosyne/UIConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var appLicense = ["GPLv3"]
var appProjectInfo = ["https://github.com/prplmnky/harbour-nemosyne",
"https://github.com/prplmnky/sailfish-widgets",
"https://github.com/Armadill0/harbour-tasklist"]
var appVersion = "1.2.1"
var appVersion = "1.2.2"
var appYear = 2015

var defaultDb = "nemosyne.db"
8 changes: 4 additions & 4 deletions qml/pages/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ Page {
Manager {
id:manager

onValidateDatabase: {
Console.info("Main::openDb: db is valid " + validDb)
if(validDb) {
onDatabaseValid: {
Console.info("Main::openDb: db is valid " + valid)
if(valid) {
errorLabel.text = ""

var filePath = currentFile.absoluteFilePath
if(settings.recentFile == filePath) {
Console.info("Main::openDb: currentFile is equal to recentFile")
} else {
Expand Down
2 changes: 1 addition & 1 deletion qml/pages/Question.qml
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ Dialog {

onManagerChanged: {
if(!!manager) {
manager.deleteCard.connect(_next)
manager.cardDeleted.connect(_next)
}
}

Expand Down
3 changes: 3 additions & 0 deletions rpm/harbour-nemosyne.changes.in
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@
* Mon Jan 16 2015 Dametrious Peyton <dametriouspeyton@countermail.com> 1.2.1-1
- Fixed Cover Action so it doesn't display "No Cards" when flipping between card displays
- Add font size settings

* Mon Jan 16 2015 Dametrious Peyton <dametriouspeyton@countermail.com> 1.2.2-1
- Found bug with timing constants being set to 0. Could cause next repetition to be uncalculated.
2 changes: 1 addition & 1 deletion rpm/harbour-nemosyne.spec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Name: harbour-nemosyne
%{?qtc_builddir:%define _builddir %qtc_builddir}
Summary: Mobile Flash Cards
Version: 1.2
Release: 1
Release: 2
Group: Applications/Productivity
License: GPLv3
URL: https://github.com/prplmnky/harbour-nemosyne
Expand Down
2 changes: 1 addition & 1 deletion rpm/harbour-nemosyne.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Name: harbour-nemosyne
Summary: Mobile Flash Cards
Version: 1.2
Release: 1
Release: 2
# The contents of the Group field should be one of the groups listed here:
# http://gitorious.org/meego-developer-tools/spectacle/blobs/master/data/GROUPS
Group: Applications/Productivity
Expand Down

0 comments on commit 518d51d

Please sign in to comment.