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
This commit is contained in:
Mahdi Dibaiee
2015-09-15 17:26:07 +04:30
parent 66504df4cb
commit 6e52ca6246
10 changed files with 67 additions and 23 deletions

View File

@ -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
});
}

View File

@ -31,7 +31,7 @@ export default class Directory extends Component {
<i></i>
<p>{this.props.name}</p>
<span>{this.props.children} items</span>
<span>{this.props.children ? this.props.children + ' items' : ''}</span>
</div>
);
}

View File

@ -18,6 +18,8 @@ export default class File extends Component {
label = <label htmlFor={checkId}></label>;
}
console.log(this.props.type);
let clickHandler = this.props.selectView ? this.select.bind(this)
: this.open.bind(this);

View File

@ -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);

View File

@ -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 => {

View File

@ -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),