Merge lp:~fredoust/sudoku-app/profiles into lp:sudoku-app

Proposed by Frédéric Delgado
Status: Merged
Approved by: Dinko Osmankovic
Approved revision: 66
Merged at revision: 67
Proposed branch: lp:~fredoust/sudoku-app/profiles
Merge into: lp:sudoku-app
Diff against target: 755 lines (+547/-42)
6 files modified
components/AddProfileDialog.qml (+97/-0)
components/ManageProfileDialog.qml (+115/-0)
components/SudokuDialogButton.qml (+3/-3)
js/SudokuCU.js (+0/-7)
js/localStorage.js (+112/-0)
sudoku-app.qml (+220/-32)
To merge this branch: bzr merge lp:~fredoust/sudoku-app/profiles
Reviewer Review Type Date Requested Status
Dinko Osmankovic Approve
Review via email: mp+173681@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Dinko Osmankovic (dinko-metalac) :
review: Approve
lp:~fredoust/sudoku-app/profiles updated
67. By Frédéric Delgado

Clean code

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'components/AddProfileDialog.qml'
--- components/AddProfileDialog.qml 1970-01-01 00:00:00 +0000
+++ components/AddProfileDialog.qml 2013-07-09 09:59:26 +0000
@@ -0,0 +1,97 @@
1import QtQuick 2.0
2import Ubuntu.Components 0.1
3import Ubuntu.Components.Popups 0.1
4import "../js/localStorage.js" as Settings
5import QtQuick.LocalStorage 2.0
6
7Component {
8 id: addProfileDialog
9
10 Dialog {
11 id: addProfileDialogue
12 title: i18n.tr("Add new profile")
13 width: parent.width
14
15
16 Column {
17 width: parent.width
18 spacing: units.gu(2)
19
20 Rectangle{
21 width: units.gu(25)
22 height: units.gu(4)
23 radius: 9
24 anchors.horizontalCenter: parent.horizontalCenter
25 TextField {
26 id:lastnameField
27
28 placeholderText: i18n.tr("Lastname")
29
30 }
31 }
32 Rectangle{
33 width: units.gu(25)
34 height: units.gu(4)
35 radius: 9
36 anchors.horizontalCenter: parent.horizontalCenter
37 TextField {
38 id:firstnameField
39 anchors.horizontalCenter: parent.horizontalCenter
40 placeholderText: i18n.tr("Firstname")
41 }
42 }
43
44
45 Row{
46 width: parent.width
47 spacing: units.gu(1)
48
49 SudokuDialogButton{
50 id:okButton
51 buttonText: i18n.tr("OK")
52 width: parent.width/2;
53 size: units.gu(5)
54 onTriggered: {
55
56 if(lastnameField.text!="" && firstnameField.text!="")
57 {
58
59 if(!Settings.existProfile(lastnameField.text, firstnameField.text))
60 {
61
62 Settings.insertProfile(lastnameField.text, firstnameField.text);
63 PopupUtils.close(addProfileDialogue)
64 }else{
65 mainView.showAlert(i18n.tr("Warning"), i18n.tr("User already exist."), okButton)
66 }
67
68
69 } else{
70mainView.showAlert(i18n.tr("Warning"), i18n.tr("Lastname and firstname must not be empty."), okButton)
71 }
72
73
74
75
76
77 }
78 }
79
80 SudokuDialogButton{
81 buttonText: i18n.tr("Cancel")
82 width: parent.width/2;
83 size: units.gu(5)
84 onTriggered: {
85 PopupUtils.close(addProfileDialogue)
86 }
87 }
88 }
89
90
91
92 }
93
94
95 }
96}
97
098
=== added file 'components/ManageProfileDialog.qml'
--- components/ManageProfileDialog.qml 1970-01-01 00:00:00 +0000
+++ components/ManageProfileDialog.qml 2013-07-09 09:59:26 +0000
@@ -0,0 +1,115 @@
1import QtQuick 2.0
2import Ubuntu.Components 0.1
3import Ubuntu.Components.Popups 0.1
4import "../js/localStorage.js" as Settings
5import QtQuick.LocalStorage 2.0
6
7Component {
8 id: editProfileDialog
9
10 Dialog {
11 id: editProfileDialogue
12 title: i18n.tr("Edit profile")
13 width: parent.width
14
15
16 Column {
17 width: parent.width
18 spacing: units.gu(2)
19
20 Rectangle{
21 width: 200
22 height: 32
23 radius: 9
24 anchors.horizontalCenter: parent.horizontalCenter
25 TextField {
26 id:lastnameField
27 text: Settings.getUserLastName(editUserId)
28 placeholderText: i18n.tr("Lastname")
29
30 }
31 }
32 Rectangle{
33 width: 200
34 height: 32
35 radius: 9
36 anchors.horizontalCenter: parent.horizontalCenter
37 TextField {
38 id:firstnameField
39 anchors.horizontalCenter: parent.horizontalCenter
40 placeholderText: i18n.tr("Firstname")
41 text: Settings.getUserFirstName(editUserId)
42 }
43 }
44
45
46 Row{
47 width: parent.width
48 spacing: units.gu(1)
49
50 SudokuDialogButton{
51 id:okButton
52 buttonText: i18n.tr("OK")
53 width: parent.width/2;
54 size: units.gu(5)
55 onTriggered: {
56
57 if(lastnameField.text!="" && firstnameField.text!="")
58 {
59
60 if(!Settings.existProfile( lastnameField.text, firstnameField.text))
61 {
62
63 Settings.updateProfile(editUserId, lastnameField.text, firstnameField.text);
64 var userId = currentUserId
65 currentUserId = -1
66 currentUserId = userId
67
68 PopupUtils.close(editProfileDialogue)
69 }else{
70 PopupUtils.close(editProfileDialogue)
71 }
72
73
74 } else{
75 mainView.showAlert(i18n.tr("Warning"), i18n.tr("Lastname and firstname must not be empty."), okButton)
76 }
77
78
79
80
81
82 }
83 }
84
85 SudokuDialogButton{
86 buttonText: i18n.tr("Delete")
87 width: parent.width/2;
88 size: units.gu(5)
89 onTriggered: {
90 Settings.deleteProfile(editUserId)
91 if(editUserId == currentUserId)
92 currentUserId = -1
93
94
95 PopupUtils.close(editProfileDialogue)
96 }
97 }
98 }
99 SudokuDialogButton{
100 anchors.horizontalCenter: parent.horizontalCenter
101 buttonText: i18n.tr("Cancel")
102 width: parent.width/2;
103 size: units.gu(5)
104 onTriggered: {
105 PopupUtils.close(editProfileDialogue)
106 }
107 }
108
109
110 }
111
112
113 }
114}
115
0116
=== modified file 'components/SudokuDialogButton.qml'
--- components/SudokuDialogButton.qml 2013-07-05 21:36:31 +0000
+++ components/SudokuDialogButton.qml 2013-07-09 09:59:26 +0000
@@ -3,9 +3,9 @@
33
4SudokuButton {4SudokuButton {
5 id: dialogButton5 id: dialogButton
6 border.color: defaultBorderColor6 border.color: sudokuBlocksGrid.defaultBorderColor
7 //border.width: 37 //border.width: 3
8 textColor: defaultTextColor;8 textColor: sudokuBlocksGrid.defaultTextColor;
99
10 signal triggered;10 signal triggered;
1111
@@ -43,5 +43,5 @@
43 animateDialogButton.start();43 animateDialogButton.start();
44 }44 }
45 }45 }
46 buttonColor: buttonMouseArea.pressed ? String(Qt.darker(defaultColor,1.2)):defaultColor46 buttonColor: buttonMouseArea.pressed ? String(Qt.darker(sudokuBlocksGrid.defaultColor,1.2)):sudokuBlocksGrid.defaultColor
47}47}
4848
=== modified file 'js/SudokuCU.js'
--- js/SudokuCU.js 2013-07-03 17:08:32 +0000
+++ js/SudokuCU.js 2013-07-09 09:59:26 +0000
@@ -159,20 +159,17 @@
159 if(value == 0)159 if(value == 0)
160 return false;160 return false;
161161
162 console.log("test for "+value)
163 for(var i = 0; i < 9; i++)162 for(var i = 0; i < 9; i++)
164 {163 {
165 console.log("test with col"+this.rows[i][column])164 console.log("test with col"+this.rows[i][column])
166 if(i != row && this.rows[i][column] == value)165 if(i != row && this.rows[i][column] == value)
167 {166 {
168 console.log("find row")
169 return true;167 return true;
170 }168 }
171169
172 // console.log("test with row"+this.rows[row][i])170 // console.log("test with row"+this.rows[row][i])
173 if(i != column && this.rows[row][i] == value)171 if(i != column && this.rows[row][i] == value)
174 {172 {
175 console.log("find col")
176 return true;173 return true;
177 }174 }
178 }175 }
@@ -209,8 +206,6 @@
209 if(value == 0)206 if(value == 0)
210 continue;207 continue;
211208
212 console.log("test minigrid "+value)
213
214 //if(App.Utils.arrayContains(numbers, value))209 //if(App.Utils.arrayContains(numbers, value))
215 // return false;210 // return false;
216 if (numbers.indexOf(value) != -1 )211 if (numbers.indexOf(value) != -1 )
@@ -252,8 +247,6 @@
252 if(value == 0)247 if(value == 0)
253 continue;248 continue;
254249
255 console.log("test minigrid "+value)
256
257 //if(App.Utils.arrayContains(numbers, value))250 //if(App.Utils.arrayContains(numbers, value))
258 // return false;251 // return false;
259 if (numbers.indexOf(value) != -1 && value == val)252 if (numbers.indexOf(value) != -1 && value == val)
260253
=== modified file 'js/localStorage.js'
--- js/localStorage.js 2013-07-06 06:18:26 +0000
+++ js/localStorage.js 2013-07-09 09:59:26 +0000
@@ -12,14 +12,19 @@
12 // Create the settings table if it doesn't already exist12 // Create the settings table if it doesn't already exist
13 // If the table exists, this is skipped13 // If the table exists, this is skipped
14 tx.executeSql('PRAGMA foreign_keys = ON;');14 tx.executeSql('PRAGMA foreign_keys = ON;');
15
16
15 tx.executeSql('CREATE TABLE IF NOT EXISTS settings(setting TEXT UNIQUE, value TEXT)');17 tx.executeSql('CREATE TABLE IF NOT EXISTS settings(setting TEXT UNIQUE, value TEXT)');
16 print("setting table created.")18 print("setting table created.")
17 tx.executeSql('CREATE TABLE IF NOT EXISTS profiles(id INTEGER PRIMARY KEY AUTOINCREMENT, first_name TEXT, last_name TEXT, UNIQUE(first_name, last_name) ON CONFLICT ROLLBACK)');19 tx.executeSql('CREATE TABLE IF NOT EXISTS profiles(id INTEGER PRIMARY KEY AUTOINCREMENT, first_name TEXT, last_name TEXT, UNIQUE(first_name, last_name) ON CONFLICT ROLLBACK)');
18 print("profile table created.")20 print("profile table created.")
21
19 tx.executeSql('INSERT OR IGNORE INTO profiles VALUES (null,?,?);', ["Sudoku","User"]);22 tx.executeSql('INSERT OR IGNORE INTO profiles VALUES (null,?,?);', ["Sudoku","User"]);
20 tx.executeSql('CREATE TABLE IF NOT EXISTS scores(id INTEGER PRIMARY KEY AUTOINCREMENT, profile_id INTEGER, score INTEGER NOT NULL, game_date DATE, FOREIGN KEY(profile_id) REFERENCES profiles(id))');23 tx.executeSql('CREATE TABLE IF NOT EXISTS scores(id INTEGER PRIMARY KEY AUTOINCREMENT, profile_id INTEGER, score INTEGER NOT NULL, game_date DATE, FOREIGN KEY(profile_id) REFERENCES profiles(id))');
21 print("scores table created.")24 print("scores table created.")
2225
26
27
23 });28 });
24}29}
25function setSetting(setting, value) {30function setSetting(setting, value) {
@@ -113,6 +118,113 @@
113 return res;118 return res;
114}119}
115120
121function printObject(o)
122{
123 var out = '';
124 for(var p in o){
125 out+=p+': '+o[p]+'\n';
126 }
127 console.log(out)
128}
129
130function getAllProfiles()
131{
132 var db = getDatabase();
133 var res=new Array();
134
135 print("GETTING ALL PROFILES")
136 db.transaction( function(tx) {
137 var rs = tx.executeSql("SELECT * FROM profiles limit 10;");
138 for(var i = 0; i < rs.rows.length; i++) {
139 var dbItem = rs.rows.item(i);
140 var o = new Object();
141 o["id"] = dbItem.id;
142 o["lastname"] = dbItem.last_name;
143 o["firstname"] = dbItem.first_name;
144 res.push(o)
145 }
146 });
147 print(res);
148 return res;
149}
150
151
152function deleteProfile(id)
153{
154
155 var db = getDatabase();
156 var res="";
157 db.transaction(function(tx) {
158 var rs = tx.executeSql('DELETE FROM profiles WHERE id=? ;', [id]);
159
160 if (rs.rowsAffected > 0) {
161 res = "OK";
162 } else {
163 res = "Error";
164 }
165 }
166 );
167 // The function returns “OK” if it was successful, or “Error” if it wasn't
168 return res;
169}
170
171
172function updateProfile(id, lastname, firstname)
173{
174
175 var db = getDatabase();
176 var res="";
177 db.transaction(function(tx) {
178 var rs = tx.executeSql('UPDATE profiles SET first_name=?, last_name=? WHERE id=? ;', [firstname, lastname, id]);
179
180 if (rs.rowsAffected > 0) {
181 res = "OK";
182 } else {
183 res = "Error";
184 }
185 }
186 );
187 // The function returns “OK” if it was successful, or “Error” if it wasn't
188 return res;
189}
190
191function insertProfile(lastname, firstname)
192{
193
194 var db = getDatabase();
195 var res="";
196 db.transaction(function(tx) {
197 var rs = tx.executeSql('INSERT OR IGNORE INTO profiles VALUES (null,?,?);', [firstname, lastname]);
198
199 if (rs.rowsAffected > 0) {
200 res = "OK";
201 } else {
202 res = "Error";
203 }
204 }
205 );
206 // The function returns “OK” if it was successful, or “Error” if it wasn't
207 return res;
208}
209
210function existProfile(lastname, firstname)
211{
212 var db = getDatabase();
213 var res="";
214
215 db.transaction(function(tx) {
216 var rs = tx.executeSql('SELECT id FROM profiles WHERE last_name=? AND first_name=?;', [lastname, firstname]);
217 if (rs.rows.length > 0) {
218 res = true
219 } else {
220 res = false
221 }
222 })
223
224
225 return res
226}
227
116function getUserFirstName(profile_id)228function getUserFirstName(profile_id)
117{229{
118 var db = getDatabase();230 var db = getDatabase();
119231
=== modified file 'sudoku-app.qml'
--- sudoku-app.qml 2013-07-08 09:38:32 +0000
+++ sudoku-app.qml 2013-07-09 09:59:26 +0000
@@ -2,6 +2,7 @@
2import Ubuntu.Components 0.12import Ubuntu.Components 0.1
3import Ubuntu.Components.ListItems 0.1 as ListItem3import Ubuntu.Components.ListItems 0.1 as ListItem
4import QtQuick.LocalStorage 2.04import QtQuick.LocalStorage 2.0
5import Ubuntu.Components.Popups 0.1
5import "js/localStorage.js" as Settings6import "js/localStorage.js" as Settings
6import "components"7import "components"
78
@@ -17,12 +18,21 @@
17 property real blockDistance: pageWidth/200;18 property real blockDistance: pageWidth/200;
18 property bool alreadyCreated: false;19 property bool alreadyCreated: false;
19 property bool gridLoaded: false;20 property bool gridLoaded: false;
20 property int currentUserId: 1;21 property int currentUserId: -1;
21 property string highscoresHeaderText: i18n.tr("<b>Best scores for all players</b>")22 property string highscoresHeaderText: i18n.tr("<b>Best scores for all players</b>")
2223
24 property string alertTitle: ""
25 property string alertText : ""
26
27 property int editUserId : -1;
28
23 width: pageWidth;29 width: pageWidth;
24 height: pageHeight;30 height: pageHeight;
2531
32 onCurrentUserIdChanged: {
33 Settings.setSetting("currentUserId", currentUserId)
34 }
35
26 function newSize(width, height) {36 function newSize(width, height) {
27 pageWidth = width;37 pageWidth = width;
28 pageHeight = height;38 pageHeight = height;
@@ -58,6 +68,36 @@
58 //buttonsGridPublic.update();68 //buttonsGridPublic.update();
59 }69 }
6070
71
72 Component {
73 id: alertDialog
74 Dialog {
75 id: alertDialogue
76 title: alertTitle
77 text: alertText
78
79 SudokuDialogButton{
80 buttonText: i18n.tr("OK")
81 width: parent.width/2;
82 size: units.gu(5)
83 onTriggered: {
84 PopupUtils.close(alertDialogue)
85 }
86 }
87
88 }
89 }
90
91 function showAlert(title, text, caller)
92 {
93 alertTitle = title
94 alertText = text
95 PopupUtils.open(alertDialog, caller)
96
97 }
98
99
100
61 onHeightChanged: {101 onHeightChanged: {
62 if (!gridLoaded)102 if (!gridLoaded)
63 return;103 return;
@@ -98,6 +138,14 @@
98 'lastname': lastName,138 'lastname': lastName,
99 'score': rowItem[1] });139 'score': rowItem[1] });
100 }140 }
141
142 if(Settings.getSetting("currentUserId")=="Unknown")
143 currentUserId = -1;
144 else
145 {
146 currentUserId = Settings.getSetting("currentUserId")
147 }
148
101 }149 }
102150
103 Tabs {151 Tabs {
@@ -259,6 +307,7 @@
259 SudokuBlocksGrid {307 SudokuBlocksGrid {
260 id: sudokuBlocksGrid;308 id: sudokuBlocksGrid;
261 }309 }
310
262 }311 }
263 }312 }
264313
@@ -364,19 +413,102 @@
364 property alias difficultyIndex: difficultySelector.selectedIndex;413 property alias difficultyIndex: difficultySelector.selectedIndex;
365 property alias themeIndex: themeSelector.selectedIndex;414 property alias themeIndex: themeSelector.selectedIndex;
366415
416
417
367 title: i18n.tr("Settings")418 title: i18n.tr("Settings")
368 page: Page {419 page:
369 /*420
370 tools: ToolbarActions {421 Page {
371422
372 Action {
373 iconSource: Qt.resolvedUrl("icons/close.svg")
374 text: i18n.tr("Close");
375 onTriggered: Qt.quit()
376 }
377 }
378 */
379 Column {423 Column {
424
425 Component {
426 id: profileSelector
427 DefaultSheet {
428 title: i18n.tr("Select profile")
429 contentsHeight: mainView.height
430
431 /*
432 Column {
433 anchors {
434 top: parent.top
435 left: parent.left
436 right: parent.right
437 }
438 height: mainColumnSettings.height
439
440 ListItem.Header {
441 id: header
442 text: i18n.tr("Select profile")
443 }
444 */
445
446
447
448 ListView {
449 height: mainColumnSettings.height
450 id: profileListView
451 clip: true
452 width: parent.width
453 // height: parent.height - header.height
454 model: profilesModel
455
456 delegate:
457 ListItem.Standard {
458
459 text: firstname + " " + lastname
460 progression: true
461 onTriggered: {
462 console.log("clicked "+index)
463 currentUserId = profileId;
464 hide()
465 }
466 }
467
468 }
469 }
470 // }
471 }
472
473 Component {
474 id: manageProfileSelector
475 DefaultSheet {
476 title: i18n.tr("Select profile")
477 contentsHeight: mainView.height
478
479
480
481 ListView {
482 id: manageProfileListView
483 clip: true
484 width: parent.width
485 height: mainColumnSettings.height
486 model: profilesModel
487
488 delegate:
489
490 ListItem.Standard {
491
492 text: firstname + " " + lastname
493
494 progression: true
495 onTriggered: {
496 hide()
497 editUserId = profileId
498 PopupUtils.open(manageProfileDialog, selectorProfile)
499 }
500 }
501
502
503
504 }
505 }
506 }
507
508 ListModel{
509 id: profilesModel
510 }
511
380 id: mainColumnSettings;512 id: mainColumnSettings;
381 //width: settingsTab.width;513 //width: settingsTab.width;
382 //height: settingsTab.height;514 //height: settingsTab.height;
@@ -460,9 +592,76 @@
460 }592 }
461 }593 }
462 }594 }
595 ListItem.Header {
596 text: i18n.tr("<b>Profiles settings</b>")
597 }
598 ListItem.SingleValue {
599 text: "Current profile"
600 id: selectorProfile
601 value: {
602 if(currentUserId==-1)
603 return i18n.tr("None")
604 else
605 return Settings.getUserFirstName(currentUserId)+" "+Settings.getUserLastName(currentUserId);
606
607 }
608
609 onClicked: {
610
611 var allProfiles = new Array();
612 allProfiles = Settings.getAllProfiles()
613
614 profilesModel.clear()
615
616 for(var i = 0; i < allProfiles.length; i++)
617 {
618 profilesModel.append({"profileId":allProfiles[i].id,"lastname":allProfiles[i].lastname, "firstname":allProfiles[i].firstname})
619 }
620 PopupUtils.open(profileSelector, selectorProfile)
621 }
622 }
623
624 AddProfileDialog{
625 id:addProfileDialog
626 }
627
628 ManageProfileDialog{
629 id:manageProfileDialog
630 }
631
632
633 ListItem.SingleValue {
634 id:addSingleValue
635 text: i18n.tr("Add profile")
636 onClicked: {
637 PopupUtils.open(addProfileDialog, addSingleValue);
638 }
639 }
640
641 ListItem.SingleValue {
642 id:manageProfileSingleValue
643 text: i18n.tr("Manage profiles")
644 onClicked: {
645
646 var allProfiles = new Array();
647 allProfiles = Settings.getAllProfiles()
648
649 profilesModel.clear()
650
651 for(var i = 0; i < allProfiles.length; i++)
652 {
653 profilesModel.append({"profileId":allProfiles[i].id,"lastname":allProfiles[i].lastname, "firstname":allProfiles[i].firstname})
654 }
655
656 PopupUtils.open(manageProfileSelector, manageProfileSingleValue)
657 }
658 }
659
660
463 }661 }
464 }662 }
465663
664
466 }665 }
467666
468 Tab {667 Tab {
@@ -470,18 +669,7 @@
470 objectName: "aboutTab"669 objectName: "aboutTab"
471 title: i18n.tr("About")670 title: i18n.tr("About")
472 page: Page {671 page: Page {
473 /*672
474 tools: ToolbarActions {
475
476
477 Action {
478 iconSource: Qt.resolvedUrl("icons/close.svg");
479 text: i18n.tr("Close");
480 onTriggered: Qt.quit()
481 }
482
483 }
484 */
485 Column {673 Column {
486 id: aboutColumn;674 id: aboutColumn;
487 spacing: 5;675 spacing: 5;
@@ -489,15 +677,15 @@
489 anchors.horizontalCenter: parent.horizontalCenter;677 anchors.horizontalCenter: parent.horizontalCenter;
490 y: units.gu(8);678 y: units.gu(8);
491 Image {679 Image {
492 property real maxWidth: units.gu(100)680 property real maxWidth: units.gu(100)
493 anchors.horizontalCenter: parent.horizontalCenter681 anchors.horizontalCenter: parent.horizontalCenter
494 width: Math.min(mainView.width, maxWidth)/1.75682 width: Math.min(mainView.width, maxWidth)/1.75
495 //height: width683 //height: width
496 source: "icons/sudoko-vector-about.svg"684 source: "icons/sudoko-vector-about.svg"
497 smooth: true685 smooth: true
498 fillMode: Image.PreserveAspectFit686 fillMode: Image.PreserveAspectFit
499687
500 }688 }
501 Row {689 Row {
502 //anchors.horizontalCenter: parent.horizontalCenter;690 //anchors.horizontalCenter: parent.horizontalCenter;
503 anchors.left: aboutColumn.left691 anchors.left: aboutColumn.left

Subscribers

People subscribed via source and target branches