From 79bf56e72cafb04a59f3566a786daf6dd0dbf56e Mon Sep 17 00:00:00 2001 From: Shreesh Ayachit Date: Fri, 11 Nov 2022 07:09:09 -0800 Subject: [PATCH] Add export and import to Shared Preferences plugin Summary: We want to enable backing up and restoring debug settings between app installations. Currently it is a manual process to click into menu and perform multiple operations. With this feature, we can export and import shared preferences which will eliminate manual steps on devices. Reviewed By: mweststrate Differential Revision: D40987341 fbshipit-source-id: 15dd9600ee5cfd80a085117bdba4d434e4d2198f --- .../public/shared_preferences/src/index.tsx | 67 ++++++++++++++++++- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/desktop/plugins/public/shared_preferences/src/index.tsx b/desktop/plugins/public/shared_preferences/src/index.tsx index 2eed60f74..4e898569d 100644 --- a/desktop/plugins/public/shared_preferences/src/index.tsx +++ b/desktop/plugins/public/shared_preferences/src/index.tsx @@ -18,11 +18,17 @@ import { styled, Select, } from 'flipper'; -import {PluginClient, createState, usePlugin, useValue} from 'flipper-plugin'; +import { + PluginClient, + createState, + usePlugin, + useValue, + getFlipperLib, +} from 'flipper-plugin'; import {clone} from 'lodash'; import React from 'react'; - +import {Button, notification} from 'antd'; type SharedPreferencesChangeEvent = { preferences: string; name: string; @@ -96,6 +102,51 @@ export function plugin(client: PluginClient) { }); } + async function saveToFile() { + if (selectedPreferences.get() != null) { + try { + const name = selectedPreferences.get() as string; + await getFlipperLib().exportFile( + JSON.stringify(sharedPreferences.get()[name]), + { + defaultPath: name, + }, + ); + } catch (e) { + notification.error({ + message: 'Save failed', + description: `Could not save shared preferences to file`, + duration: 15, + }); + } + } + } + async function loadFromFile() { + const file = await getFlipperLib().importFile(); + if (file?.path != undefined) { + const data = await getFlipperLib().remoteServerContext.fs.readFile( + file.path, + {encoding: 'utf-8'}, + ); + const preferences = JSON.parse(data) as SharedPreferencesEntry; + const name = selectedPreferences.get(); + if (name != null) { + updateSharedPreferences({ + name: name, + preferences: preferences.preferences, + }); + + for (const key in preferences.preferences) { + await client.send('setSharedPreference', { + sharedPreferencesName: name, + preferenceName: key, + preferenceValue: preferences.preferences[key], + }); + } + } + } + } + client.onMessage('sharedPreferencesChange', (change) => sharedPreferences.update((draft) => { const entry = draft[change.preferences]; @@ -124,6 +175,8 @@ export function plugin(client: PluginClient) { setSelectedPreferences, setSharedPreference, deleteSharedPreference, + saveToFile, + loadFromFile, }; } @@ -179,6 +232,16 @@ export function Component() { selected={selectedPreferences} onChange={instance.setSelectedPreferences} /> + Options + +