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
1=== modified file 'app/ubuntu-calculator-app.qml'
2--- app/ubuntu-calculator-app.qml 2015-11-28 20:44:02 +0000
3+++ app/ubuntu-calculator-app.qml 2015-11-28 23:18:30 +0000
4@@ -24,6 +24,7 @@
5 import "engine"
6 import "engine/math.js" as MathJs
7 import "engine/formula.js" as Formula
8+import Qt.labs.settings 1.0
9
10 MainView {
11 id: mainView
12@@ -40,14 +41,16 @@
13 // This is our engine
14 property var mathJs: MathJs.mathJs;
15
16- // Long form of formula, which are saved in the storage/history
17- property string longFormula: "";
18-
19- // Engine's short form of formula. It is displayed in TextInput field
20- property string shortFormula: "";
21-
22- // The formula converted to human eye, which will be displayed in text input field
23- property string displayedInputText: "";
24+ property var settings: Settings {
25+ // Long form of formula, which are saved in the storage/history
26+ property string longFormula: "";
27+
28+ // Engine's short form of formula. It is displayed in TextInput field
29+ property string shortFormula: "";
30+
31+ // The formula converted to human eye, which will be displayed in text input field
32+ property string displayedInputText: "";
33+ }
34
35 // If this is true we calculate a temporary result to show in the bottom label
36 property bool isFormulaIsValidToCalculate: false;
37@@ -77,14 +80,14 @@
38 function deleteLastFormulaElement() {
39 isFormulaIsValidToCalculate = false;
40 if (textInputField.cursorPosition === textInputField.length) {
41- longFormula = Formula.deleteLastFormulaElement(isLastCalculate, longFormula)
42+ settings.longFormula = Formula.deleteLastFormulaElement(isLastCalculate, settings.longFormula)
43 } else {
44- var truncatedSubstring = Formula.deleteLastFormulaElement(isLastCalculate, longFormula.slice(0, textInputField.cursorPosition))
45- longFormula = truncatedSubstring + longFormula.slice(textInputField.cursorPosition, longFormula.length);
46+ var truncatedSubstring = Formula.deleteLastFormulaElement(isLastCalculate, settings.longFormula.slice(0, textInputField.cursorPosition))
47+ settings.longFormula = truncatedSubstring + settings.longFormula.slice(textInputField.cursorPosition, settings.longFormula.length);
48 }
49- shortFormula = longFormula;
50+ settings.shortFormula = settings.longFormula;
51
52- displayedInputText = longFormula;
53+ settings.displayedInputText = settings.longFormula;
54 if (truncatedSubstring) {
55 textInputField.cursorPosition = truncatedSubstring.length;
56 }
57@@ -95,9 +98,9 @@
58 */
59 function clearFormula() {
60 isFormulaIsValidToCalculate = false;
61- shortFormula = "";
62- longFormula = "";
63- displayedInputText = "";
64+ settings.shortFormula = "";
65+ settings.longFormula = "";
66+ settings.displayedInputText = "";
67 }
68
69 /**
70@@ -126,28 +129,28 @@
71 // formula, otherwise we continue with the old one
72 if ((!isNaN(visual) || (visual === ".")) && isLastCalculate) {
73 isFormulaIsValidToCalculate = false;
74- longFormula = displayedInputText = shortFormula = "";
75+ settings.longFormula = settings.displayedInputText = settings.shortFormula = "";
76 }
77 // Add zero when decimal separator is not after number
78- if ((visual === ".") && ((isNaN(displayedInputText.slice(textInputField.cursorPosition - 1, textInputField.cursorPosition))) || (longFormula === ""))) {
79+ if ((visual === ".") && ((isNaN(settings.displayedInputText.slice(textInputField.cursorPosition - 1, textInputField.cursorPosition))) || (settings.longFormula === ""))) {
80 visual = "0.";
81 }
82 isLastCalculate = false;
83
84- // Validate whole longFormula if the cursor is at the end of string
85+ // Validate whole settings.longFormula if the cursor is at the end of string
86 if (textInputField.cursorPosition === textInputField.length) {
87 if (visual === "()") {
88- visual = Formula.determineBracketTypeToAdd(longFormula)
89+ visual = Formula.determineBracketTypeToAdd(settings.longFormula)
90 }
91- if (Formula.validateStringForAddingToFormula(longFormula, visual) === false) {
92+ if (Formula.validateStringForAddingToFormula(settings.longFormula, visual) === false) {
93 errorAnimation.restart();
94 return;
95 }
96 } else {
97 if (visual === "()") {
98- visual = Formula.determineBracketTypeToAdd(longFormula.slice(0, textInputField.cursorPosition))
99+ visual = Formula.determineBracketTypeToAdd(settings.longFormula.slice(0, textInputField.cursorPosition))
100 }
101- if (Formula.validateStringForAddingToFormula(longFormula.slice(0, textInputField.cursorPosition), visual) === false) {
102+ if (Formula.validateStringForAddingToFormula(settings.longFormula.slice(0, textInputField.cursorPosition), visual) === false) {
103 errorAnimation.restart();
104 return;
105 }
106@@ -160,9 +163,9 @@
107 // we display a temporary result instead the all operation
108 if (isNaN(visual) && (visual.toString() !== ".") && isFormulaIsValidToCalculate) {
109 try {
110- shortFormula = formatBigNumber(mathJs.eval(shortFormula));
111+ settings.shortFormula = formatBigNumber(mathJs.eval(settings.shortFormula));
112 } catch(exception) {
113- console.log("Debug: Temp result: " + exception.toString() + " engine formula: " + shortFormula);
114+ console.log("Debug: Temp result: " + exception.toString() + " engine formula: " + settings.shortFormula);
115 }
116
117 isFormulaIsValidToCalculate = false;
118@@ -170,14 +173,14 @@
119
120 // Adding the new operator to the formula
121 if (textInputField.cursorPosition === textInputField.length ) {
122- longFormula += visual.toString();
123- shortFormula += visual.toString();
124- displayedInputText = shortFormula;
125+ settings.longFormula += visual.toString();
126+ settings.shortFormula += visual.toString();
127+ settings.displayedInputText = settings.shortFormula;
128 } else {
129- longFormula = longFormula.slice(0, textInputField.cursorPosition) + visual.toString() + longFormula.slice(textInputField.cursorPosition, longFormula.length);
130- shortFormula = longFormula;
131+ settings.longFormula = settings.longFormula.slice(0, textInputField.cursorPosition) + visual.toString() + settings.longFormula.slice(textInputField.cursorPosition, settings.longFormula.length);
132+ settings.shortFormula = settings.longFormula;
133 var preservedCursorPosition = textInputField.cursorPosition;
134- displayedInputText = shortFormula;
135+ settings.displayedInputText = settings.shortFormula;
136 textInputField.cursorPosition = preservedCursorPosition + visual.length;
137 }
138
139@@ -191,21 +194,21 @@
140 mathJs.config({
141 number: 'bignumber'
142 });
143- if ((longFormula === '') || (isLastCalculate === true)) {
144+ if ((settings.longFormula === '') || (isLastCalculate === true)) {
145 errorAnimation.restart();
146 return;
147 }
148
149 // We try to balance brackets to avoid mathJs errors
150- var numberOfOpenedBrackets = (longFormula.match(/\(/g) || []).length -
151- (longFormula.match(/\)/g) || []).length;
152+ var numberOfOpenedBrackets = (settings.longFormula.match(/\(/g) || []).length -
153+ (settings.longFormula.match(/\)/g) || []).length;
154
155 for (var i = 0; i < numberOfOpenedBrackets; i++) {
156 formulaPush(')');
157 }
158
159 try {
160- var result = mathJs.eval(longFormula);
161+ var result = mathJs.eval(settings.longFormula);
162
163 result = formatBigNumber(result)
164
165@@ -220,17 +223,17 @@
166
167 isLastCalculate = true;
168
169- if (result === longFormula) {
170+ if (result === settings.longFormula) {
171 errorAnimation.restart();
172 return;
173 }
174
175- calculationHistory.addCalculationToScreen(longFormula, result, false, "");
176+ calculationHistory.addCalculationToScreen(settings.longFormula, result, false, "");
177 editedCalculationIndex = -1;
178- longFormula = result;
179- shortFormula = result;
180+ settings.longFormula = result;
181+ settings.shortFormula = result;
182 favouriteTextField.text = "";
183- displayedInputText = result;
184+ settings.displayedInputText = result;
185 }
186
187 PageStack {
188@@ -403,9 +406,9 @@
189 iconName: "edit"
190 text: i18n.tr("Edit")
191 onTriggered: {
192- longFormula = model.formula;
193- shortFormula = model.result;
194- displayedInputText = model.formula;
195+ settings.longFormula = model.formula;
196+ settings.shortFormula = model.result;
197+ settings.displayedInputText = model.formula;
198 isLastCalculate = false;
199 previousVisual = "";
200 scrollableView.scrollToBottom();
201@@ -608,7 +611,7 @@
202 }
203 }
204
205- text: Formula.returnFormulaToDisplay(displayedInputText)
206+ text: Formula.returnFormulaToDisplay(settings.displayedInputText)
207 font.pixelSize: height * 0.7
208 horizontalAlignment: TextInput.AlignRight
209 anchors {
210@@ -693,10 +696,10 @@
211 if (cursorPosition !== length ) {
212 // Count cursor position from the end of line
213 var preservedCursorPosition = length - cursorPosition;
214- displayedInputText = longFormula;
215+ settings.displayedInputText = settings.longFormula;
216 cursorPosition = length - preservedCursorPosition;
217 } else {
218- displayedInputText = shortFormula;
219+ settings.displayedInputText = settings.shortFormula;
220 }
221
222 SequentialAnimation {
223
224=== modified file 'debian/changelog'
225--- debian/changelog 2015-11-28 20:44:02 +0000
226+++ debian/changelog 2015-11-28 23:18:30 +0000
227@@ -5,6 +5,7 @@
228 * Upgrade math.js to version 2.4.2 to fix complex numbers formatting
229 * Run Calculator in Landscape mode for Desktop (LP: #1468663)
230 * Add instruction how to enable profiling (workaround for LP: #1520551)
231+ * Remember formula from InputField after application close (LP: #1520508)
232
233 -- Bartosz Kosiorek <gang65@poczta.onet.pl> Thu, 12 Nov 2015 13:28:29 +0100
234

Subscribers

People subscribed via source and target branches