Merge lp:~diegosarmentero/click-update-manager/bytes-to-size into lp:click-update-manager

Proposed by Diego Sarmentero
Status: Merged
Approved by: Diego Sarmentero
Approved revision: 44
Merged at revision: 36
Proposed branch: lp:~diegosarmentero/click-update-manager/bytes-to-size
Merge into: lp:click-update-manager
Diff against target: 105 lines (+47/-2)
5 files modified
CMakeLists.txt (+1/-0)
Components/PageUpdate.qml (+2/-1)
js/utils.js (+39/-0)
tests/unit/tst_pageupdate.qml (+1/-1)
updatemanager.qmlproject (+4/-0)
To merge this branch: bzr merge lp:~diegosarmentero/click-update-manager/bytes-to-size
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve
Mike McCracken (community) Approve
dobey (community) Approve
Roberto Alsina (community) Approve
Review via email: mp+190149@code.launchpad.net

Commit message

- Show app size in the proper way (BUG: #1232256).

To post a comment you must log in.
37. By Diego Sarmentero

fix if comparison

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
Mike McCracken (mikemc) wrote :

In utils.js, the calculation "parseFloat(bytes / 1024).toFixed(2)" is strange.
per IRC, bytes will be an int, so we're dividing two ints then apparently implicitly converting them to strings before passing them to parseFloat(), which generates a float.

If you just remove "parseFloat" and divide by a float, it works the same but makes more sense to me.

like this:

var size = (bytes / 1024.0).toFixed(2);

review: Needs Fixing
Revision history for this message
Mike McCracken (mikemc) wrote :

http://paste.ubuntu.com/6214313/ is the QML I was using to poke at the convert_bytes_to_size function

38. By Diego Sarmentero

removing parseFloat

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
39. By Diego Sarmentero

update cmake

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
40. By Diego Sarmentero

revert desktop file name

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
41. By Diego Sarmentero

adding desktop file

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Roberto Alsina (ralsina) wrote :

Asked for GiB to be added just in case.

review: Approve
42. By Diego Sarmentero

adding GiB to convert function

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
dobey (dobey) wrote :

> === added file 'click-update-manager.desktop'
> === removed file 'click-update-manager.desktop'

Huh? How can it be both added and removed in the same diff? And which one is correct?

82 +function convert_bytes_to_size(bytes) {
83 + var result = "";
84 + var size = (bytes / 1024.0).toFixed(2);
85 + if(size < 1024) {
86 + result = size + " KiB";
87 + } else if(size < 1048576) {
88 + size = (size / 1024.0).toFixed(2);
89 + result = size + " MiB";
90 + } else {
91 + size = (size / 1048576.0).toFixed(2);
92 + result = size + " GiB";
93 + }
94 +
95 + return result;
96 +}

This code is really hard to read, because the checks are inconsistent, and you're converting bytes to KiB from the start, but use "size" as a variable name. Why not just use the input "bytes" variable across the board for checking, and only place the reduced size in the result string? Something like this perhaps:

var SIZE_IN_GIB = 1024.0 * 1024.0 * 1024.0;
var SIZE_IN_MIB = 1024.0 * 1024.0;
var SIZE_IN_KIB = 1024.0;

var result = "";
var size = 0;
if (bytes / SIZE_IN_KIB < SIZE_IN_KIB) {
    size = bytes / SIZE_IN_KIB;
    result = size + " KiB";
} else if (bytes / SIZE_IN_MIB < SIZE_IN_KIB) {
    size = bytes / SIZE_IN_MIB;
    result = size + " MiB";
} else if (bytes / SIZE_IN_GIB < SIZE_IN_KIB) {
    size = bytes / SIZE_IN_GIB;
    result = size + " GiB";
} else {
    result = bytes + " bytes";
}

return result;

review: Needs Fixing
43. By Diego Sarmentero

improve convert function

Revision history for this message
Diego Sarmentero (diegosarmentero) wrote :

> > === added file 'click-update-manager.desktop'
> > === removed file 'click-update-manager.desktop'
>
> Huh? How can it be both added and removed in the same diff? And which one is
> correct?
>
> 82 +function convert_bytes_to_size(bytes) {
> 83 + var result = "";
> 84 + var size = (bytes / 1024.0).toFixed(2);
> 85 + if(size < 1024) {
> 86 + result = size + " KiB";
> 87 + } else if(size < 1048576) {
> 88 + size = (size / 1024.0).toFixed(2);
> 89 + result = size + " MiB";
> 90 + } else {
> 91 + size = (size / 1048576.0).toFixed(2);
> 92 + result = size + " GiB";
> 93 + }
> 94 +
> 95 + return result;
> 96 +}
>
> This code is really hard to read, because the checks are inconsistent, and
> you're converting bytes to KiB from the start, but use "size" as a variable
> name. Why not just use the input "bytes" variable across the board for
> checking, and only place the reduced size in the result string? Something like
> this perhaps:
>
> var SIZE_IN_GIB = 1024.0 * 1024.0 * 1024.0;
> var SIZE_IN_MIB = 1024.0 * 1024.0;
> var SIZE_IN_KIB = 1024.0;
>
> var result = "";
> var size = 0;
> if (bytes / SIZE_IN_KIB < SIZE_IN_KIB) {
> size = bytes / SIZE_IN_KIB;
> result = size + " KiB";
> } else if (bytes / SIZE_IN_MIB < SIZE_IN_KIB) {
> size = bytes / SIZE_IN_MIB;
> result = size + " MiB";
> } else if (bytes / SIZE_IN_GIB < SIZE_IN_KIB) {
> size = bytes / SIZE_IN_GIB;
> result = size + " GiB";
> } else {
> result = bytes + " bytes";
> }
>
> return result;

Fixed

44. By Diego Sarmentero

revert renaming desktop file

Revision history for this message
dobey (dobey) :
review: Approve
Revision history for this message
Mike McCracken (mikemc) :
review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'CMakeLists.txt'
--- CMakeLists.txt 2013-10-03 02:35:10 +0000
+++ CMakeLists.txt 2013-10-16 18:03:12 +0000
@@ -8,6 +8,7 @@
8file(GLOB QML_JS_FILES *.qml)8file(GLOB QML_JS_FILES *.qml)
9install(FILES ${QML_JS_FILES} DESTINATION ${CLICK_UPDATE_MANAGER_DIR})9install(FILES ${QML_JS_FILES} DESTINATION ${CLICK_UPDATE_MANAGER_DIR})
10install(DIRECTORY Components DESTINATION ${CLICK_UPDATE_MANAGER_DIR})10install(DIRECTORY Components DESTINATION ${CLICK_UPDATE_MANAGER_DIR})
11install(DIRECTORY js DESTINATION ${CLICK_UPDATE_MANAGER_DIR})
1112
12install(FILES click-update-manager.desktop DESTINATION ${CMAKE_INSTALL_DATADIR}/applications)13install(FILES click-update-manager.desktop DESTINATION ${CMAKE_INSTALL_DATADIR}/applications)
1314
1415
=== modified file 'Components/PageUpdate.qml'
--- Components/PageUpdate.qml 2013-10-08 13:15:38 +0000
+++ Components/PageUpdate.qml 2013-10-16 18:03:12 +0000
@@ -18,6 +18,7 @@
18import Ubuntu.Components 0.118import Ubuntu.Components 0.1
19import Ubuntu.Components.ListItems 0.1 as ListItem19import Ubuntu.Components.ListItems 0.1 as ListItem
20import com.ubuntu.click 0.120import com.ubuntu.click 0.1
21import "../js/utils.js" as Utils
2122
22Page {23Page {
23 id: root24 id: root
@@ -160,7 +161,7 @@
160 }161 }
161 }162 }
162 Label {163 Label {
163 text: parseFloat((modelData.binaryFilesize / 1024) / 1024).toFixed(2) + " Mb"164 text: Utils.convert_bytes_to_size(modelData.binaryFilesize)
164 height: textArea.height / 2165 height: textArea.height / 2
165 anchors.right: parent.right166 anchors.right: parent.right
166 }167 }
167168
=== added directory 'js'
=== added file 'js/utils.js'
--- js/utils.js 1970-01-01 00:00:00 +0000
+++ js/utils.js 2013-10-16 18:03:12 +0000
@@ -0,0 +1,39 @@
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16.pragma library
17
18function convert_bytes_to_size(bytes) {
19 var SIZE_IN_GIB = 1024.0 * 1024.0 * 1024.0;
20 var SIZE_IN_MIB = 1024.0 * 1024.0;
21 var SIZE_IN_KIB = 1024.0;
22
23 var result = "";
24 var size = 0;
25 if (bytes / SIZE_IN_KIB < SIZE_IN_KIB) {
26 size = (bytes / SIZE_IN_KIB).toFixed(2);
27 result = size + " KiB";
28 } else if (bytes / SIZE_IN_MIB < SIZE_IN_KIB) {
29 size = (bytes / SIZE_IN_MIB).toFixed(2);
30 result = size + " MiB";
31 } else if (bytes / SIZE_IN_GIB < SIZE_IN_KIB) {
32 size = (bytes / SIZE_IN_GIB).toFixed(2);
33 result = size + " GiB";
34 } else {
35 result = bytes + " bytes";
36 }
37
38 return result;
39}
040
=== modified file 'tests/unit/tst_pageupdate.qml'
--- tests/unit/tst_pageupdate.qml 2013-10-08 13:15:38 +0000
+++ tests/unit/tst_pageupdate.qml 2013-10-16 18:03:12 +0000
@@ -154,7 +154,7 @@
154 compare(colLeft.children[1].visible, true, "Should be visible");154 compare(colLeft.children[1].visible, true, "Should be visible");
155 compare(colLeft.children[1].text, "Version: " + fakeModel[i].remoteVersion, "Remote versions don't match");155 compare(colLeft.children[1].text, "Version: " + fakeModel[i].remoteVersion, "Remote versions don't match");
156 var colRight = UT.findChild(obj, "colRight");156 var colRight = UT.findChild(obj, "colRight");
157 var mbvalue = parseFloat((fakeModel[i].binaryFilesize / 1024) / 1024).toFixed(2) + " Mb";157 var mbvalue = "4.88 KiB";
158 compare(colRight.children[1].text, mbvalue, "Other Mb size expected");158 compare(colRight.children[1].text, mbvalue, "Other Mb size expected");
159 }159 }
160 }160 }
161161
=== modified file 'updatemanager.qmlproject'
--- updatemanager.qmlproject 2013-09-20 20:04:44 +0000
+++ updatemanager.qmlproject 2013-10-16 18:03:12 +0000
@@ -26,6 +26,10 @@
26 filter: "*"26 filter: "*"
27 }27 }
28 Files {28 Files {
29 directory: "js/"
30 filter: "*"
31 }
32 Files {
29 directory: "img/"33 directory: "img/"
30 filter: "*"34 filter: "*"
31 }35 }

Subscribers

People subscribed via source and target branches