From 2b2ec2c655cde5e4ea8438c856ec4aaa3754bb93 Mon Sep 17 00:00:00 2001
From: Laurent Cozic <laurent@cozic.net>
Date: Sun, 19 Apr 2020 10:11:46 +0100
Subject: [PATCH] All: Started resource fetcher service when a note has been
 decrypted

---
 .../lib/components/shared/reduxSharedMiddleware.js   | 12 +++++++++++-
 ReactNativeClient/lib/reducer.js                     |  3 +++
 ReactNativeClient/lib/services/DecryptionWorker.js   | 10 +++++++++-
 ReactNativeClient/main.js                            |  9 ++++++++-
 4 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/ReactNativeClient/lib/components/shared/reduxSharedMiddleware.js b/ReactNativeClient/lib/components/shared/reduxSharedMiddleware.js
index 7080617ce7..bb985557bf 100644
--- a/ReactNativeClient/lib/components/shared/reduxSharedMiddleware.js
+++ b/ReactNativeClient/lib/components/shared/reduxSharedMiddleware.js
@@ -1,5 +1,6 @@
 const Setting = require('lib/models/Setting');
 const Tag = require('lib/models/Tag');
+const BaseModel = require('lib/BaseModel');
 const Note = require('lib/models/Note');
 const { reg } = require('lib/registry.js');
 const ResourceFetcher = require('lib/services/ResourceFetcher');
@@ -18,8 +19,14 @@ const reduxSharedMiddleware = async function(store, next, action) {
 		reg.resetSyncTarget();
 	}
 
+	let mustAutoAddResources = false;
+
 	if (action.type === 'SETTING_UPDATE_ONE' && action.key === 'sync.resourceDownloadMode') {
-		ResourceFetcher.instance().autoAddResources();
+		mustAutoAddResources = true;
+	}
+
+	if (action.type === 'DECRYPTION_WORKER_SET' && action.state === 'idle' && action.decryptedItemCounts && !!action.decryptedItemCounts[BaseModel.TYPE_NOTE]) {
+		mustAutoAddResources = true;
 	}
 
 	// In general the DecryptionWorker is started via events, such as when an encrypted note
@@ -70,6 +77,9 @@ const reduxSharedMiddleware = async function(store, next, action) {
 		});
 	}
 
+	if (mustAutoAddResources) {
+		ResourceFetcher.instance().autoAddResources();
+	}
 
 	if (refreshTags) {
 		store.dispatch({
diff --git a/ReactNativeClient/lib/reducer.js b/ReactNativeClient/lib/reducer.js
index 45024fde10..0dacf413f8 100644
--- a/ReactNativeClient/lib/reducer.js
+++ b/ReactNativeClient/lib/reducer.js
@@ -45,6 +45,7 @@ const defaultState = {
 		state: 'idle',
 		itemIndex: 0,
 		itemCount: 0,
+		decryptedItemCounts: {},
 	},
 	selectedNoteTags: [],
 	resourceFetcher: {
@@ -415,6 +416,8 @@ function removeItemFromArray(array, property, value) {
 }
 
 const reducer = (state = defaultState, action) => {
+	// if (!['SIDE_MENU_OPEN_PERCENT'].includes(action.type)) console.info('Action', action.type);
+
 	let newState = state;
 
 	try {
diff --git a/ReactNativeClient/lib/services/DecryptionWorker.js b/ReactNativeClient/lib/services/DecryptionWorker.js
index c708b2a9eb..18b407cbfa 100644
--- a/ReactNativeClient/lib/services/DecryptionWorker.js
+++ b/ReactNativeClient/lib/services/DecryptionWorker.js
@@ -137,6 +137,7 @@ class DecryptionWorker {
 		this.state_ = 'started';
 
 		const excludedIds = [];
+		const decryptedItemCounts = {};
 
 		this.dispatch({ type: 'ENCRYPTION_HAS_DISABLED_ITEMS', value: false });
 		this.dispatchReport({ state: 'started' });
@@ -179,6 +180,10 @@ class DecryptionWorker {
 
 						await clearDecryptionCounter();
 
+						if (!decryptedItemCounts[decryptedItem.type_]) decryptedItemCounts[decryptedItem.type_] = 0;
+
+						decryptedItemCounts[decryptedItem.type_]++;
+
 						if (decryptedItem.type_ === Resource.modelType() && !!decryptedItem.encryption_blob_encrypted) {
 							// itemsThatNeedDecryption() will return the resource again if the blob has not been decrypted,
 							// but that will result in an infinite loop if the blob simply has not been downloaded yet.
@@ -240,7 +245,10 @@ class DecryptionWorker {
 
 		this.state_ = 'idle';
 
-		this.dispatchReport({ state: 'idle' });
+		this.dispatchReport({
+			state: 'idle',
+			decryptedItemCounts: decryptedItemCounts,
+		});
 
 		if (downloadedButEncryptedBlobCount) {
 			this.logger().info(`DecryptionWorker: Some resources have been downloaded but are not decrypted yet. Scheduling another decryption. Resource count: ${downloadedButEncryptedBlobCount}`);
diff --git a/ReactNativeClient/main.js b/ReactNativeClient/main.js
index e9e8fe0bcf..236ef98c27 100644
--- a/ReactNativeClient/main.js
+++ b/ReactNativeClient/main.js
@@ -8,7 +8,7 @@
 
 // console.disableYellowBox = true
 
-import { YellowBox, AppRegistry } from 'react-native';
+import { YellowBox, AppRegistry, NativeModules } from 'react-native';
 YellowBox.ignoreWarnings([
 	'Require cycle: node_modules/react-native-',
 	'Require cycle: node_modules/rn-fetch-blob',
@@ -18,6 +18,13 @@ YellowBox.ignoreWarnings([
 ]);
 const { Root } = require('./root.js');
 
+// Disable buggy Fast Refresh
+if (__DEV__) {
+	const { DevSettings } = NativeModules;
+	DevSettings.setHotLoadingEnabled(false);
+	DevSettings.setLiveReloadEnabled(false);
+}
+
 function main() {
 	AppRegistry.registerComponent('Joplin', () => Root);
 	console.ignoredYellowBox = ['Remote debugger'];