From 6e52ca62469998d442d8aadf822420d3ae000edb Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Tue, 15 Sep 2015 17:26:07 +0430 Subject: [PATCH] Improve compatibility with old versions - Remove starting "/sdcard/" from file paths on old Firefox OS versions - Cache results for faster navigation on old Firefox OS devices --- README.md | 1 + build/main.js | 42 +++++++++++++++++++++++++-------- package.json | 1 - releases/hawk-1.0.0.zip | Bin 1245329 -> 1245795 bytes src/js/api/files.js | 28 +++++++++++++++------- src/js/components/directory.js | 2 +- src/js/components/file.js | 2 ++ src/js/reducers/cwd.js | 6 ++++- src/js/reducers/search.js | 4 ++-- src/js/utils.js | 4 ++++ 10 files changed, 67 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 606910f..1ceb9d0 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ Version 1.0 Version 2.0 ------------ - [x] Different views (List, Grid) +- [ ] Show storage usage statistics (free/used) - [ ] Sort Files - [ ] Zip / Unzip - [ ] Image Thumbnails diff --git a/build/main.js b/build/main.js index f9f12a2..6f55807 100644 --- a/build/main.js +++ b/build/main.js @@ -29952,6 +29952,10 @@ var root = _asyncToGenerator(function* () { if (ROOT_CACHE) return ROOT_CACHE; ROOT_CACHE = shimDirectory((yield sdcard().getRoot())); + Object.defineProperty(ROOT_CACHE, 'name', { + value: '', + enumerable: true + }); window.root = ROOT_CACHE; return ROOT_CACHE; }); @@ -29965,19 +29969,21 @@ var getFile = _asyncToGenerator(function* () { if (dir === '/' || !dir) return parent; - return yield parent.get(dir); + return yield parent.get((0, _utils.normalize)(dir)); }); exports.getFile = getFile; var children = _asyncToGenerator(function* (dir, gatherInfo) { + if (CACHE[dir]) return CACHE[dir]; + var parent = shimDirectory((yield getFile(dir))); if (!parent.path) { parent.path = dir.slice(0, dir.lastIndexOf('/') + 1); } var childs = yield parent.getFilesAndDirectories(); - if (gatherInfo) { + if (gatherInfo && !window.needsShim) { var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; @@ -30019,6 +30025,8 @@ var children = _asyncToGenerator(function* (dir, gatherInfo) { ; } + CACHE[dir] = childs; + return childs; }); @@ -30074,7 +30082,7 @@ var remove = _asyncToGenerator(function* (file, deep) { exports.remove = remove; var move = _asyncToGenerator(function* (file, newPath) { - var path = (file.path || '').replace(/^\//, ''); // remove starting slash + var path = (0, _utils.normalize)(file.path || ''); var oldPath = path + file.name; var process = yield copy(file, newPath); @@ -30084,10 +30092,10 @@ var move = _asyncToGenerator(function* (file, newPath) { exports.move = move; var copy = _asyncToGenerator(function* (file, newPath) { - var path = (file.path || '').replace(/^\//, ''); // remove starting slash + var path = (0, _utils.normalize)(file.path || '').replace(/^\//, ''); var oldPath = path + file.name; - newPath = newPath.replace(/^\//, ''); + newPath = (0, _utils.normalize)(newPath); var target = yield getFile(oldPath); var parent = yield root(); @@ -30106,7 +30114,8 @@ var copy = _asyncToGenerator(function* (file, newPath) { if ((0, _utils.type)(child) === 'File') { Object.defineProperty(child, 'path', { - value: oldPath + '/' + value: oldPath + '/', + enumerable: true }); } @@ -30159,12 +30168,15 @@ var _actionsFilesView = require('actions/files-view'); var _store = require('store'); var SD_CACHE = undefined; +var CACHE = {}; + +exports.CACHE = CACHE; +localStorage.setItem('cache', '{}'); function sdcard() { if (SD_CACHE) return SD_CACHE; SD_CACHE = navigator.getDeviceStorage('sdcard'); - SD_CACHE.onchange = (0, _store.bind)((0, _actionsFilesView.refresh)()); window.sdcard = SD_CACHE; return SD_CACHE; @@ -30445,8 +30457,7 @@ var Directory = (function (_Component) { _react2['default'].createElement( 'span', null, - this.props.children, - ' items' + this.props.children ? this.props.children + ' items' : '' ) ); } @@ -30645,6 +30656,8 @@ var File = (function (_Component) { label = _react2['default'].createElement('label', { htmlFor: checkId }); } + console.log(this.props.type); + var clickHandler = this.props.selectView ? this.select.bind(this) : this.open.bind(this); return _react2['default'].createElement( @@ -31968,6 +31981,10 @@ exports['default'] = function (state, action) { return action.dir; } + if (action.type === _actionsTypes.REFRESH) { + _apiFiles.CACHE[state] = null; + } + if (action.type === _actionsTypes.REFRESH || action.type === _actionsTypes.SETTINGS) { changeTo(state); @@ -32301,7 +32318,7 @@ function search(keywords) { var filtered = files.filter(function (file) { if ((0, _utils.type)(file) === 'Directory') { - var path = (file.path + file.name).replace(/^\//, ''); + var path = (0, _utils.normalize)(file.path + file.name); (0, _apiFiles.children)(path, true).then(showResults, _utils.reportError); } return keys.some(function (key) { @@ -32602,6 +32619,7 @@ exports.type = type; exports.template = template; exports.getKey = getKey; exports.reportError = reportError; +exports.normalize = normalize; exports.humanSize = humanSize; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } @@ -32644,6 +32662,10 @@ function reportError(err) { _store2['default'].dispatch(action); } +function normalize(path) { + return path.replace(/^\//, '').replace('sdcard/', ''); +} + var sizes = { 'GB': Math.pow(2, 30), 'MB': Math.pow(2, 20), diff --git a/package.json b/package.json index a51656e..7f0a091 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,6 @@ "grunt-contrib-copy": "^0.8.1", "grunt-contrib-less": "^1.0.1", "grunt-contrib-watch": "^0.6.1", - "grunt-fxos": "^0.1.2", "grunt-task-loader": "^0.6.0", "grunt-zip": "^0.17.0", "hammerjs": "^2.0.4", diff --git a/releases/hawk-1.0.0.zip b/releases/hawk-1.0.0.zip index c07b17830603a6c8063a8e9aa48b2eaabb25d28b..8e85e2a283830215d6f202d22842746111cf3ca0 100644 GIT binary patch delta 1275 zcmZvcUuYCZ9LI0&PyVc4_I58SO$?6FWbbmv-o;|7IonXJCKAE;2gN@zE_ZXeEt}nC zchA&bQjk_cT8j7(=b;Ee#1}QKgmyI`g8HEN5`0k*{~`FIXq2{4FUfUw?#_T@;k)zU zH#6V;&2MJ*^oboWe%j&h>GpUTm-B4?C3e~mJx&u}j$GgN@o>tl+s@YNLSO4oCA;ClsbO^ zD~nUpC&Axd8teTTB#OqrVeq7AH#81LKn?k9r~6;yad4D%1xH7?bAZTAUfC6 zdc)8_R@PAALMXvtJi$&cgU!uw^`NY#v_VltC}r-+$OCr&@9cJO?ZRmIB}`^yC1ob; zOOxz>-sVL=pOzIQ>XMa#LqjCza01U04dQ>#nL+tF0 zQ|y!*MDg$|<}=)J%IC)qPX+?^((CLyU-vP|D6WennHx?p4Ej#j49gU0`Xor(1fyt4 zN!ep*hSZNl6It6WG!PP!QZnPzTgVLyVVu9os`lh9R&8~|4jkyKt_{El6rfDTngVoo z0*|Kx5&P&r?8MrJq^6o0S+k<`hpdcjin^icrf4145n24*_v|ive_i0^?WS;PKl>zI z-bSiaO74(pNfzoQIjUJj!ST1mx$S9SzOiMoa)lP^8-bLavtl21b4OM$`*;c*v+{|s z|5(FKdDf=0YSNN5b)h5V>{2nQ(#gOxmjf%7iw4paDTzXYnMz7VD&|y1uu}r(D_#{P z%pIt3F38B87-!#!@#Z+aE?-Ual@W(nwgj0nT$trI z(5~mR{6-25bER2_=lCyZN!Rm@~|8-TUagB?$^TNsV9fDxWv_!vQ(^xV5f56RdQCH)d+b z&w!15YGvoarjeR33T_*y&3eHFYES)O#6(k`N$^%jt>YRf&6JKzfggIBJ9h^(W*Xl| z;C-gu)i4NxLUL?})w$i@5`;`kFepR-K}TQ-dV+zFN604_2_^zZC?M=2m#!KzU51H&mlbFw206V?x$HuT zC|`s>#o)3Ia42!lP=wc4;FbYNap;v!w-2C^yuhqaX4cj7F{RJ;?0iyr_ diff --git a/src/js/api/files.js b/src/js/api/files.js index cfe0a07..fe5c80a 100644 --- a/src/js/api/files.js +++ b/src/js/api/files.js @@ -1,13 +1,16 @@ -import { type } from 'utils'; +import { type, normalize } from 'utils'; import { refresh } from 'actions/files-view'; import { bind } from 'store'; let SD_CACHE; +export let CACHE = {}; + +localStorage.setItem('cache', '{}'); + export function sdcard() { if (SD_CACHE) return SD_CACHE; SD_CACHE = navigator.getDeviceStorage('sdcard'); - SD_CACHE.onchange = bind(refresh()); window.sdcard = SD_CACHE; return SD_CACHE; @@ -18,6 +21,10 @@ export async function root() { if (ROOT_CACHE) return ROOT_CACHE; ROOT_CACHE = shimDirectory(await sdcard().getRoot()); + Object.defineProperty(ROOT_CACHE, 'name', { + value: '', + enumerable: true + }); window.root = ROOT_CACHE; return ROOT_CACHE; } @@ -27,17 +34,19 @@ export async function getFile(dir = '/') { if (dir === '/' || !dir) return parent; - return await parent.get(dir); + return await parent.get(normalize(dir)); } export async function children(dir, gatherInfo) { + if (CACHE[dir]) return CACHE[dir]; + let parent = shimDirectory(await getFile(dir)); if (!parent.path) { parent.path = dir.slice(0, dir.lastIndexOf('/') + 1); } let childs = await parent.getFilesAndDirectories(); - if (gatherInfo) { + if (gatherInfo && !window.needsShim) { for (let child of childs) { if (type(child) === 'Directory') { let subchildren; @@ -56,6 +65,8 @@ export async function children(dir, gatherInfo) { }; } + CACHE[dir] = childs; + return childs; } @@ -97,7 +108,7 @@ export async function remove(file, deep) { } export async function move(file, newPath) { - let path = (file.path || '').replace(/^\//, ''); // remove starting slash + let path = normalize(file.path || ''); let oldPath = path + file.name; let process = await copy(file, newPath); @@ -105,10 +116,10 @@ export async function move(file, newPath) { } export async function copy(file, newPath) { - let path = (file.path || '').replace(/^\//, ''); // remove starting slash + let path = normalize(file.path || '').replace(/^\//, ''); let oldPath = path + file.name; - newPath = newPath.replace(/^\//, ''); + newPath = normalize(newPath); let target = await getFile(oldPath); let parent = await root(); @@ -120,7 +131,8 @@ export async function copy(file, newPath) { for (let child of childs) { if (type(child) === 'File') { Object.defineProperty(child, 'path', { - value: oldPath + '/' + value: oldPath + '/', + enumerable: true }); } diff --git a/src/js/components/directory.js b/src/js/components/directory.js index 81d48bf..b1ab90e 100644 --- a/src/js/components/directory.js +++ b/src/js/components/directory.js @@ -31,7 +31,7 @@ export default class Directory extends Component {

{this.props.name}

- {this.props.children} items + {this.props.children ? this.props.children + ' items' : ''} ); } diff --git a/src/js/components/file.js b/src/js/components/file.js index 2b6b99a..fa20cef 100644 --- a/src/js/components/file.js +++ b/src/js/components/file.js @@ -18,6 +18,8 @@ export default class File extends Component { label = ; } + console.log(this.props.type); + let clickHandler = this.props.selectView ? this.select.bind(this) : this.open.bind(this); diff --git a/src/js/reducers/cwd.js b/src/js/reducers/cwd.js index 71bbfa3..a88d4bf 100644 --- a/src/js/reducers/cwd.js +++ b/src/js/reducers/cwd.js @@ -1,5 +1,5 @@ import { CHANGE_DIRECTORY, REFRESH, SETTINGS } from 'actions/types'; -import { children } from 'api/files'; +import { children, CACHE } from 'api/files'; import store from 'store'; import { reportError } from 'utils'; import { listFiles } from 'actions/files-view'; @@ -11,6 +11,10 @@ export default function(state = '', action) { return action.dir; } + if (action.type === REFRESH) { + CACHE[state] = null; + } + if (action.type === REFRESH || action.type === SETTINGS) { changeTo(state); diff --git a/src/js/reducers/search.js b/src/js/reducers/search.js index 381521e..ee0ed0f 100644 --- a/src/js/reducers/search.js +++ b/src/js/reducers/search.js @@ -3,7 +3,7 @@ import store from 'store'; import { reportError } from 'utils'; import { listFiles } from 'actions/files-view'; import { children } from 'api/files'; -import { type } from 'utils'; +import { type, normalize } from 'utils'; export default function(state = '', action) { if (action.type === SEARCH) { @@ -36,7 +36,7 @@ function search(keywords) { let filtered = files.filter(file => { if (type(file) === 'Directory') { - let path = (file.path + file.name).replace(/^\//, ''); + let path = normalize(file.path + file.name); children(path, true).then(showResults, reportError); } return keys.some(key => { diff --git a/src/js/utils.js b/src/js/utils.js index 509c9f0..4357c31 100644 --- a/src/js/utils.js +++ b/src/js/utils.js @@ -31,6 +31,10 @@ export function reportError(err) { store.dispatch(action); } +export function normalize(path) { + return path.replace(/^\//, '').replace('sdcard/', ''); +} + const sizes = { 'GB': Math.pow(2, 30), 'MB': Math.pow(2, 20),