From 069dce69cdab6e805ab615d11a397f0dffb4c9cc Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Tue, 9 Oct 2018 22:01:50 +0100 Subject: [PATCH] Mobile: Added support for ResourceFetcher service --- .../lib/components/note-body-viewer.js | 2 -- .../lib/services/ResourceFetcher.js | 19 +++++++++++++------ ReactNativeClient/root.js | 9 +++++++++ 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/ReactNativeClient/lib/components/note-body-viewer.js b/ReactNativeClient/lib/components/note-body-viewer.js index 9c31c70a3f..dcfaa33701 100644 --- a/ReactNativeClient/lib/components/note-body-viewer.js +++ b/ReactNativeClient/lib/components/note-body-viewer.js @@ -76,8 +76,6 @@ class NoteBodyViewer extends Component { paddingBottom: '3.8em', // Extra bottom padding to make it possible to scroll past the action button (so that it doesn't overlap the text) }; - console.info('RRRRRRRRRRRR'); - let html = this.mdToHtml_.render(note ? note.body : '', this.props.webViewStyle, mdOptions); html = ` diff --git a/ReactNativeClient/lib/services/ResourceFetcher.js b/ReactNativeClient/lib/services/ResourceFetcher.js index b03d265a90..8919771ae2 100644 --- a/ReactNativeClient/lib/services/ResourceFetcher.js +++ b/ReactNativeClient/lib/services/ResourceFetcher.js @@ -80,8 +80,21 @@ class ResourceFetcher extends BaseService { if (this.fetchingItems_[resourceId]) return; this.fetchingItems_[resourceId] = true; + const completeDownload = (emitDownloadComplete = true) => { + delete this.fetchingItems_[resource.id]; + this.scheduleQueueProcess(); + if (emitDownloadComplete) this.eventEmitter_.emit('downloadComplete', { id: resource.id }); + } + const resource = await Resource.load(resourceId); + // Shouldn't happen, but just to be safe don't re-download the + // resource if it's already been downloaded. + if (resource.fetch_status === Resource.FETCH_STATUS_DONE) { + completeDownload(false); + return; + } + this.fetchingItems_[resourceId] = resource; const localResourceContentPath = Resource.fullPath(resource); @@ -93,12 +106,6 @@ class ResourceFetcher extends BaseService { this.logger().debug('ResourceFetcher: Downloading resource: ' + resource.id); - const completeDownload = () => { - delete this.fetchingItems_[resource.id]; - this.scheduleQueueProcess(); - this.eventEmitter_.emit('downloadComplete', { id: resource.id }); - } - fileApi.get(remoteResourceContentPath, { path: localResourceContentPath, target: "file" }).then(async () => { await Resource.saveFetchStatus(resource.id, Resource.FETCH_STATUS_DONE); this.logger().debug('ResourceFetcher: Resource downloaded: ' + resource.id); diff --git a/ReactNativeClient/root.js b/ReactNativeClient/root.js index fe679d4fe8..b75d4b8f53 100644 --- a/ReactNativeClient/root.js +++ b/ReactNativeClient/root.js @@ -51,6 +51,7 @@ const { reducer, defaultState } = require('lib/reducer.js'); const { FileApiDriverLocal } = require('lib/file-api-driver-local.js'); const DropdownAlert = require('react-native-dropdownalert').default; const ShareExtension = require('react-native-share-extension').default; +const ResourceFetcher = require('lib/services/ResourceFetcher'); const SyncTargetRegistry = require('lib/SyncTargetRegistry.js'); const SyncTargetOneDrive = require('lib/SyncTargetOneDrive.js'); @@ -138,6 +139,10 @@ const generalMiddleware = store => next => async (action) => { DecryptionWorker.instance().scheduleStart(); } + if (action.type === 'SYNC_CREATED_RESOURCE') { + ResourceFetcher.instance().queueDownload(action.id); + } + return result; } @@ -488,6 +493,10 @@ async function initialize(dispatch) { ResourceService.runInBackground(); + ResourceFetcher.instance().setFileApi(() => { return reg.syncTarget().fileApi() }); + ResourceFetcher.instance().setLogger(reg.logger()); + ResourceFetcher.instance().start(); + reg.scheduleSync().then(() => { // Wait for the first sync before updating the notifications, since synchronisation // might change the notifications.