Merge lp:~ubuntu-calculator-dev/ubuntu-calculator-app/ubuntu-calculator-app-remember-last-formula into lp:ubuntu-calculator-app

Proposed by Bartosz Kosiorek
Status: Needs review
Proposed branch: lp:~ubuntu-calculator-dev/ubuntu-calculator-app/ubuntu-calculator-app-remember-last-formula
Merge into: lp:ubuntu-calculator-app
Diff against target: 233 lines (+50/-46)
2 files modified
app/ubuntu-calculator-app.qml (+49/-46)
debian/changelog (+1/-0)
To merge this branch: bzr merge lp:~ubuntu-calculator-dev/ubuntu-calculator-app/ubuntu-calculator-app-remember-last-formula
Reviewer Review Type Date Requested Status
Bartosz Kosiorek Needs Information
Jenkins Bot continuous-integration Approve
Review via email: mp+278900@code.launchpad.net

Commit message

Remember formula from InputField after application close (LP: #1520508)

Description of the change

Remember formula from InputField after application close (LP: #1520508)

To post a comment you must log in.
Revision history for this message
Jenkins Bot (ubuntu-core-apps-jenkins-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Riccardo Padovani (rpadovani) wrote :

Hi Bartosz,
thanks for working on this.

Unfortunately, I don't think this is the right way to fix the problem: Qt.labs.settings writes on a file the setting, so you add a very big overhead: every time user presses a button there is an I/O operation, that is very expensive.

IMO the right direction to fix this bug is saving the actual formula only when app is closed (using Component.onDestruction) and when it losts focus (creating a bind on Qt.application.state[0]).

Let me know what you think.

[0]: http://doc.qt.io/qt-5/qml-qtqml-qt.html#application-prop

Revision history for this message
Bartosz Kosiorek (gang65) wrote :

From user point of view it will be great to save progress every change.

I will check the impact on performance and give you performance comparison.

Revision history for this message
Bartosz Kosiorek (gang65) wrote :

I checked that and saving is very fast, so from user perspective there is more benefits of savings formula every change (due to out of memory, which happens when you will open too many tabs in webbrowser).

Could you please double check that issue with profiler?

review: Needs Information
Revision history for this message
Bartosz Kosiorek (gang65) wrote :

I checked that and saving is very fast, so from user perspective there is more benefits of savings formula every change (due to out of memory, which happens when you will open too many tabs in webbrowser).

Could you please double check that issue with profiler?

review: Needs Information

Unmerged revisions

247. By Bartosz Kosiorek

Remember formula from InputField after application close (LP: #1520508)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'app/ubuntu-calculator-app.qml'
--- app/ubuntu-calculator-app.qml 2015-11-28 20:44:02 +0000
+++ app/ubuntu-calculator-app.qml 2015-11-28 23:18:30 +0000
@@ -24,6 +24,7 @@
24import "engine"24import "engine"
25import "engine/math.js" as MathJs25import "engine/math.js" as MathJs
26import "engine/formula.js" as Formula26import "engine/formula.js" as Formula
27import Qt.labs.settings 1.0
2728
28MainView {29MainView {
29 id: mainView30 id: mainView
@@ -40,14 +41,16 @@
40 // This is our engine41 // This is our engine
41 property var mathJs: MathJs.mathJs;42 property var mathJs: MathJs.mathJs;
4243
43 // Long form of formula, which are saved in the storage/history44 property var settings: Settings {
44 property string longFormula: "";45 // Long form of formula, which are saved in the storage/history
4546 property string longFormula: "";
46 // Engine's short form of formula. It is displayed in TextInput field47
47 property string shortFormula: "";48 // Engine's short form of formula. It is displayed in TextInput field
4849 property string shortFormula: "";
49 // The formula converted to human eye, which will be displayed in text input field50
50 property string displayedInputText: "";51 // The formula converted to human eye, which will be displayed in text input field
52 property string displayedInputText: "";
53 }
5154
52 // If this is true we calculate a temporary result to show in the bottom label55 // If this is true we calculate a temporary result to show in the bottom label
53 property bool isFormulaIsValidToCalculate: false;56 property bool isFormulaIsValidToCalculate: false;
@@ -77,14 +80,14 @@
77 function deleteLastFormulaElement() {80 function deleteLastFormulaElement() {
78 isFormulaIsValidToCalculate = false;81 isFormulaIsValidToCalculate = false;
79 if (textInputField.cursorPosition === textInputField.length) {82 if (textInputField.cursorPosition === textInputField.length) {
80 longFormula = Formula.deleteLastFormulaElement(isLastCalculate, longFormula)83 settings.longFormula = Formula.deleteLastFormulaElement(isLastCalculate, settings.longFormula)
81 } else {84 } else {
82 var truncatedSubstring = Formula.deleteLastFormulaElement(isLastCalculate, longFormula.slice(0, textInputField.cursorPosition))85 var truncatedSubstring = Formula.deleteLastFormulaElement(isLastCalculate, settings.longFormula.slice(0, textInputField.cursorPosition))
83 longFormula = truncatedSubstring + longFormula.slice(textInputField.cursorPosition, longFormula.length);86 settings.longFormula = truncatedSubstring + settings.longFormula.slice(textInputField.cursorPosition, settings.longFormula.length);
84 }87 }
85 shortFormula = longFormula;88 settings.shortFormula = settings.longFormula;
8689
87 displayedInputText = longFormula;90 settings.displayedInputText = settings.longFormula;
88 if (truncatedSubstring) {91 if (truncatedSubstring) {
89 textInputField.cursorPosition = truncatedSubstring.length;92 textInputField.cursorPosition = truncatedSubstring.length;
90 }93 }
@@ -95,9 +98,9 @@
95 */98 */
96 function clearFormula() {99 function clearFormula() {
97 isFormulaIsValidToCalculate = false;100 isFormulaIsValidToCalculate = false;
98 shortFormula = "";101 settings.shortFormula = "";
99 longFormula = "";102 settings.longFormula = "";
100 displayedInputText = "";103 settings.displayedInputText = "";
101 }104 }
102105
103 /**106 /**
@@ -126,28 +129,28 @@
126 // formula, otherwise we continue with the old one129 // formula, otherwise we continue with the old one
127 if ((!isNaN(visual) || (visual === ".")) && isLastCalculate) {130 if ((!isNaN(visual) || (visual === ".")) && isLastCalculate) {
128 isFormulaIsValidToCalculate = false;131 isFormulaIsValidToCalculate = false;
129 longFormula = displayedInputText = shortFormula = "";132 settings.longFormula = settings.displayedInputText = settings.shortFormula = "";
130 }133 }
131 // Add zero when decimal separator is not after number134 // Add zero when decimal separator is not after number
132 if ((visual === ".") && ((isNaN(displayedInputText.slice(textInputField.cursorPosition - 1, textInputField.cursorPosition))) || (longFormula === ""))) {135 if ((visual === ".") && ((isNaN(settings.displayedInputText.slice(textInputField.cursorPosition - 1, textInputField.cursorPosition))) || (settings.longFormula === ""))) {
133 visual = "0.";136 visual = "0.";
134 }137 }
135 isLastCalculate = false;138 isLastCalculate = false;
136139
137 // Validate whole longFormula if the cursor is at the end of string140 // Validate whole settings.longFormula if the cursor is at the end of string
138 if (textInputField.cursorPosition === textInputField.length) {141 if (textInputField.cursorPosition === textInputField.length) {
139 if (visual === "()") {142 if (visual === "()") {
140 visual = Formula.determineBracketTypeToAdd(longFormula)143 visual = Formula.determineBracketTypeToAdd(settings.longFormula)
141 }144 }
142 if (Formula.validateStringForAddingToFormula(longFormula, visual) === false) {145 if (Formula.validateStringForAddingToFormula(settings.longFormula, visual) === false) {
143 errorAnimation.restart();146 errorAnimation.restart();
144 return;147 return;
145 }148 }
146 } else {149 } else {
147 if (visual === "()") {150 if (visual === "()") {
148 visual = Formula.determineBracketTypeToAdd(longFormula.slice(0, textInputField.cursorPosition))151 visual = Formula.determineBracketTypeToAdd(settings.longFormula.slice(0, textInputField.cursorPosition))
149 }152 }
150 if (Formula.validateStringForAddingToFormula(longFormula.slice(0, textInputField.cursorPosition), visual) === false) {153 if (Formula.validateStringForAddingToFormula(settings.longFormula.slice(0, textInputField.cursorPosition), visual) === false) {
151 errorAnimation.restart();154 errorAnimation.restart();
152 return;155 return;
153 }156 }
@@ -160,9 +163,9 @@
160 // we display a temporary result instead the all operation163 // we display a temporary result instead the all operation
161 if (isNaN(visual) && (visual.toString() !== ".") && isFormulaIsValidToCalculate) {164 if (isNaN(visual) && (visual.toString() !== ".") && isFormulaIsValidToCalculate) {
162 try {165 try {
163 shortFormula = formatBigNumber(mathJs.eval(shortFormula));166 settings.shortFormula = formatBigNumber(mathJs.eval(settings.shortFormula));
164 } catch(exception) {167 } catch(exception) {
165 console.log("Debug: Temp result: " + exception.toString() + " engine formula: " + shortFormula);168 console.log("Debug: Temp result: " + exception.toString() + " engine formula: " + settings.shortFormula);
166 }169 }
167170
168 isFormulaIsValidToCalculate = false;171 isFormulaIsValidToCalculate = false;
@@ -170,14 +173,14 @@
170173
171 // Adding the new operator to the formula174 // Adding the new operator to the formula
172 if (textInputField.cursorPosition === textInputField.length ) {175 if (textInputField.cursorPosition === textInputField.length ) {
173 longFormula += visual.toString();176 settings.longFormula += visual.toString();
174 shortFormula += visual.toString();177 settings.shortFormula += visual.toString();
175 displayedInputText = shortFormula;178 settings.displayedInputText = settings.shortFormula;
176 } else {179 } else {
177 longFormula = longFormula.slice(0, textInputField.cursorPosition) + visual.toString() + longFormula.slice(textInputField.cursorPosition, longFormula.length);180 settings.longFormula = settings.longFormula.slice(0, textInputField.cursorPosition) + visual.toString() + settings.longFormula.slice(textInputField.cursorPosition, settings.longFormula.length);
178 shortFormula = longFormula;181 settings.shortFormula = settings.longFormula;
179 var preservedCursorPosition = textInputField.cursorPosition;182 var preservedCursorPosition = textInputField.cursorPosition;
180 displayedInputText = shortFormula;183 settings.displayedInputText = settings.shortFormula;
181 textInputField.cursorPosition = preservedCursorPosition + visual.length;184 textInputField.cursorPosition = preservedCursorPosition + visual.length;
182 }185 }
183186
@@ -191,21 +194,21 @@
191 mathJs.config({194 mathJs.config({
192 number: 'bignumber'195 number: 'bignumber'
193 });196 });
194 if ((longFormula === '') || (isLastCalculate === true)) {197 if ((settings.longFormula === '') || (isLastCalculate === true)) {
195 errorAnimation.restart();198 errorAnimation.restart();
196 return;199 return;
197 }200 }
198201
199 // We try to balance brackets to avoid mathJs errors202 // We try to balance brackets to avoid mathJs errors
200 var numberOfOpenedBrackets = (longFormula.match(/\(/g) || []).length -203 var numberOfOpenedBrackets = (settings.longFormula.match(/\(/g) || []).length -
201 (longFormula.match(/\)/g) || []).length;204 (settings.longFormula.match(/\)/g) || []).length;
202205
203 for (var i = 0; i < numberOfOpenedBrackets; i++) {206 for (var i = 0; i < numberOfOpenedBrackets; i++) {
204 formulaPush(')');207 formulaPush(')');
205 }208 }
206209
207 try {210 try {
208 var result = mathJs.eval(longFormula);211 var result = mathJs.eval(settings.longFormula);
209212
210 result = formatBigNumber(result)213 result = formatBigNumber(result)
211214
@@ -220,17 +223,17 @@
220223
221 isLastCalculate = true;224 isLastCalculate = true;
222225
223 if (result === longFormula) {226 if (result === settings.longFormula) {
224 errorAnimation.restart();227 errorAnimation.restart();
225 return;228 return;
226 }229 }
227230
228 calculationHistory.addCalculationToScreen(longFormula, result, false, "");231 calculationHistory.addCalculationToScreen(settings.longFormula, result, false, "");
229 editedCalculationIndex = -1;232 editedCalculationIndex = -1;
230 longFormula = result;233 settings.longFormula = result;
231 shortFormula = result;234 settings.shortFormula = result;
232 favouriteTextField.text = "";235 favouriteTextField.text = "";
233 displayedInputText = result;236 settings.displayedInputText = result;
234 }237 }
235238
236 PageStack {239 PageStack {
@@ -403,9 +406,9 @@
403 iconName: "edit"406 iconName: "edit"
404 text: i18n.tr("Edit")407 text: i18n.tr("Edit")
405 onTriggered: {408 onTriggered: {
406 longFormula = model.formula;409 settings.longFormula = model.formula;
407 shortFormula = model.result;410 settings.shortFormula = model.result;
408 displayedInputText = model.formula;411 settings.displayedInputText = model.formula;
409 isLastCalculate = false;412 isLastCalculate = false;
410 previousVisual = "";413 previousVisual = "";
411 scrollableView.scrollToBottom();414 scrollableView.scrollToBottom();
@@ -608,7 +611,7 @@
608 }611 }
609 }612 }
610613
611 text: Formula.returnFormulaToDisplay(displayedInputText)614 text: Formula.returnFormulaToDisplay(settings.displayedInputText)
612 font.pixelSize: height * 0.7615 font.pixelSize: height * 0.7
613 horizontalAlignment: TextInput.AlignRight616 horizontalAlignment: TextInput.AlignRight
614 anchors {617 anchors {
@@ -693,10 +696,10 @@
693 if (cursorPosition !== length ) {696 if (cursorPosition !== length ) {
694 // Count cursor position from the end of line697 // Count cursor position from the end of line
695 var preservedCursorPosition = length - cursorPosition;698 var preservedCursorPosition = length - cursorPosition;
696 displayedInputText = longFormula;699 settings.displayedInputText = settings.longFormula;
697 cursorPosition = length - preservedCursorPosition;700 cursorPosition = length - preservedCursorPosition;
698 } else {701 } else {
699 displayedInputText = shortFormula;702 settings.displayedInputText = settings.shortFormula;
700 }703 }
701704
702 SequentialAnimation {705 SequentialAnimation {
703706
=== modified file 'debian/changelog'
--- debian/changelog 2015-11-28 20:44:02 +0000
+++ debian/changelog 2015-11-28 23:18:30 +0000
@@ -5,6 +5,7 @@
5 * Upgrade math.js to version 2.4.2 to fix complex numbers formatting 5 * Upgrade math.js to version 2.4.2 to fix complex numbers formatting
6 * Run Calculator in Landscape mode for Desktop (LP: #1468663)6 * Run Calculator in Landscape mode for Desktop (LP: #1468663)
7 * Add instruction how to enable profiling (workaround for LP: #1520551) 7 * Add instruction how to enable profiling (workaround for LP: #1520551)
8 * Remember formula from InputField after application close (LP: #1520508)
89
9 -- Bartosz Kosiorek <gang65@poczta.onet.pl> Thu, 12 Nov 2015 13:28:29 +010010 -- Bartosz Kosiorek <gang65@poczta.onet.pl> Thu, 12 Nov 2015 13:28:29 +0100
1011

Subscribers

People subscribed via source and target branches