diff --git a/.gitignore b/.gitignore index 1c5a2fc6..f12a06e0 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ dist .nyc_output .tox .pytest_cache +android/app/src/main/assets/vendor.js +android/app/src/main/assets/android.js + diff --git a/android/android.iml b/android/android.iml index 686bd2aa..8dac7322 100644 --- a/android/android.iml +++ b/android/android.iml @@ -13,7 +13,7 @@ - + \ No newline at end of file diff --git a/android/android.js b/android/android.js new file mode 100644 index 00000000..b0ca7557 --- /dev/null +++ b/android/android.js @@ -0,0 +1,159 @@ +/* global window, document, fetch */ + +const MAXFILESIZE = 1024 * 1024 * 1024 * 2; + +const EventEmitter = require('events'); +const emitter = new EventEmitter(); + +function dom(tagName, attributes, children = []) { + const node = document.createElement(tagName); + for (const name in attributes) { + if (name.indexOf('on') === 0) { + node[name] = attributes[name]; + } else if (name === 'htmlFor') { + node.htmlFor = attributes.htmlFor; + } else if (name === 'className') { + node.className = attributes.className; + } else { + node.setAttribute(name, attributes[name]); + } + } + if (!(children instanceof Array)) { + children = [children]; + } + for (let child of children) { + if (typeof child === 'string') { + child = document.createTextNode(child); + } + node.appendChild(child); + } + return node; +} + +function uploadComplete(file) { + document.body.innerHTML = ''; + const input = dom('input', { id: 'url', value: file.url }); + const copy = dom( + 'button', + { + id: 'copy-button', + className: 'button', + onclick: () => { + input.select(); + document.execCommand('copy'); + input.blur(); + copy.textContent = 'Copied!'; + setTimeout(function() { + copy.textContent = 'Copy to clipboard'; + }, 2000); + } + }, + 'Copy to clipboard' + ); + const node = dom( + 'div', + { id: 'striped' }, + dom('div', { id: 'white' }, [ + input, + copy, + dom( + 'button', + { id: 'send-another', className: 'button', onclick: render }, + 'Send another file' + ) + ]) + ); + document.body.appendChild(node); +} + +const state = { + storage: { + files: [], + remove: function(fileId) { + console.log('REMOVE FILEID', fileId); + }, + writeFile: function(file) { + console.log('WRITEFILE', file); + }, + addFile: uploadComplete, + totalUploads: 0 + }, + transfer: null, + uploading: false, + settingPassword: false, + passwordSetError: null, + route: '/' +}; + +function upload(event) { + event.preventDefault(); + const target = event.target; + const file = target.files[0]; + if (file.size === 0) { + return; + } + if (file.size > MAXFILESIZE) { + console.log('file too big (no bigger than ' + MAXFILESIZE + ')'); + return; + } + + emitter.emit('upload', { file: file, type: 'click' }); +} + +function render() { + document.body.innerHTML = ''; + const striped = dom( + 'div', + { id: 'striped' }, + dom('div', { id: 'white' }, [ + dom('label', { id: 'label', htmlFor: 'input' }, 'Choose file'), + dom('input', { + id: 'input', + type: 'file', + name: 'input', + onchange: upload + }) + ]) + ); + document.body.appendChild(striped); +} + +emitter.on('render', function() { + document.body.innerHTML = ''; + const percent = + (state.transfer.progress[0] / state.transfer.progress[1]) * 100; + const node = dom( + 'div', + { style: 'background-color: white; width: 100%' }, + dom('span', { + style: `display: inline-block; width: ${percent}%; background-color: blue` + }) + ); + document.body.appendChild(node); +}); + +emitter.on('pushState', function(path) { + console.log('pushState ' + path + ' ' + JSON.stringify(state)); +}); + +const fileManager = require('../app/fileManager').default; +try { + fileManager(state, emitter); +} catch (e) { + console.error('error' + e); + console.error(e); +} + +window.addEventListener( + 'message', + event => { + fetch(event.data) + .then(res => res.blob()) + .then(blob => { + emitter.emit('upload', { file: blob, type: 'share' }); + }); + }, + false +); + +render(); diff --git a/android/app/app.iml b/android/app/app.iml index 418506f8..5f5dbac2 100644 --- a/android/app/app.iml +++ b/android/app/app.iml @@ -22,13 +22,14 @@ - +