You've already forked joplin
							
							
				mirror of
				https://github.com/laurent22/joplin.git
				synced 2025-10-31 00:07:48 +02:00 
			
		
		
		
	getting sync work in RN
This commit is contained in:
		| @@ -7,6 +7,7 @@ import { _ } from 'lib/locale.js'; | ||||
| import { Setting } from 'lib/models/setting.js'; | ||||
| import { FileApi } from 'lib/file-api.js'; | ||||
| import { FileApiDriverOneDrive } from 'lib/file-api-driver-onedrive.js'; | ||||
| import { reg } from 'lib/registry.js' | ||||
|  | ||||
| const styles = StyleSheet.create({ | ||||
| 	divider: { | ||||
| @@ -40,25 +41,20 @@ class ScreenHeaderComponent extends Component { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	menu_synchronize() { | ||||
| 		this.props.dispatch({ | ||||
| 			type: 'Navigation/NAVIGATE', | ||||
| 			routeName: 'OneDriveLogin', | ||||
| 		}); | ||||
|  | ||||
| 		// const CLIENT_ID = 'e09fc0de-c958-424f-83a2-e56a721d331b'; | ||||
| 		// const CLIENT_SECRET = 'JA3cwsqSGHFtjMwd5XoF5L5'; | ||||
|  | ||||
| 		// let driver = new FileApiDriverOneDrive(CLIENT_ID, CLIENT_SECRET); | ||||
| 		// let auth = Setting.value('sync.onedrive.auth'); | ||||
| 		 | ||||
| 		// if (auth) { | ||||
| 		// 	auth = JSON.parse(auth); | ||||
| 		// } else { | ||||
| 		// 	driver.api().oauthDance(vorpal); | ||||
| 		// 	//auth = driver.api().oauthDance(vorpal); | ||||
| 		// 	//Setting.setValue('sync.onedrive.auth', JSON.stringify(auth)); | ||||
| 		// } | ||||
| 	async menu_synchronize() { | ||||
| 		if (reg.oneDriveApi().auth()) { | ||||
| 			const sync = await reg.synchronizer(); | ||||
| 			try { | ||||
| 				sync.start(); | ||||
| 			} catch (error) { | ||||
| 				Log.error(error); | ||||
| 			} | ||||
| 		} else { | ||||
| 			this.props.dispatch({ | ||||
| 				type: 'Navigation/NAVIGATE', | ||||
| 				routeName: 'OneDriveLogin', | ||||
| 			}); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	render() { | ||||
|   | ||||
| @@ -3,8 +3,9 @@ import { View } from 'react-native'; | ||||
| import { WebView, Button } from 'react-native'; | ||||
| import { connect } from 'react-redux' | ||||
| import { Log } from 'lib/log.js' | ||||
| import { Setting } from 'lib/models/setting.js' | ||||
| import { ScreenHeader } from 'lib/components/screen-header.js'; | ||||
| import { OneDriveApi } from 'lib/onedrive-api.js'; | ||||
| import { reg } from 'lib/registry.js'; | ||||
| import { _ } from 'lib/locale.js'; | ||||
|  | ||||
| class OneDriveLoginScreenComponent extends React.Component { | ||||
| @@ -21,13 +22,10 @@ class OneDriveLoginScreenComponent extends React.Component { | ||||
|  | ||||
| 	componentWillMount() { | ||||
| 		this.setState({ | ||||
| 			webviewUrl: this.api().authCodeUrl(this.redirectUrl()), | ||||
| 			webviewUrl: reg.oneDriveApi().authCodeUrl(this.redirectUrl()), | ||||
| 		}); | ||||
| 	} | ||||
|  | ||||
| 	api() { | ||||
| 		return OneDriveApi.instance(); | ||||
|  | ||||
| 	redirectUrl() { | ||||
| 		return 'https://login.microsoftonline.com/common/oauth2/nativeclient'; | ||||
| 	} | ||||
| @@ -38,25 +36,15 @@ class OneDriveLoginScreenComponent extends React.Component { | ||||
| 		// at the moment so it's likely to change. | ||||
| 		const url = noIdeaWhatThisIs.url; | ||||
|  | ||||
| 		console.info('URL: ' + url); | ||||
| 		if (!this.authCode_ && url.indexOf(this.redirectUrl() + '?code=') === 0) { | ||||
| 			console.info('URL: ' + url); | ||||
|  | ||||
| 		if (!this.authCode_) { | ||||
| 			if (url.indexOf(this.redirectUrl() + '?code=') === 0) { | ||||
| 				let code = url.split('?code='); | ||||
| 				this.authCode_ = code[1]; | ||||
| 			let code = url.split('?code='); | ||||
| 			this.authCode_ = code[1]; | ||||
|  | ||||
| 				await this.api().execTokenRequest(this.authCode_, this.redirectUrl(), true); | ||||
| 				Setting.setValue('sync.onedrive.auth', JSON.stringify(this.api().auth())); | ||||
| 				oneDriveApi.on('authRefreshed', (a) => { | ||||
| 					Setting.setValue('sync.onedrive.auth', JSON.stringify(a)); | ||||
| 				}); | ||||
| 			await reg.oneDriveApi().execTokenRequest(this.authCode_, this.redirectUrl(), true); | ||||
|  | ||||
| 				let appDir = await this.api().appDirectory(); | ||||
|  | ||||
| 				Log.info('APP DIR: ' + appDir); | ||||
| 				// fileApi = new FileApi(appDir, driver); | ||||
| 				// fileApi.setLogger(logger); | ||||
| 			} | ||||
| 			this.authCode_ = null; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| @@ -65,8 +53,6 @@ class OneDriveLoginScreenComponent extends React.Component { | ||||
| 			uri: this.state.webviewUrl, | ||||
| 		} | ||||
|  | ||||
| 		// <Button title="Start" onPress={() => this.startButton_press()}></Button> | ||||
|  | ||||
| 		return ( | ||||
| 			<View style={{flex: 1}}> | ||||
| 				<ScreenHeader navState={this.props.navigation.state} /> | ||||
|   | ||||
| @@ -53,7 +53,7 @@ class SideMenuContentComponent extends Component { | ||||
| 		let buttons = []; | ||||
| 		for (let i = 0; i < this.props.folders.length; i++) { | ||||
| 			let f = this.props.folders[i]; | ||||
| 			let title = f.title; | ||||
| 			let title = f.title ? f.title : ''; | ||||
| 			buttons.push( | ||||
| 				<Button style={styles.button} title={title} onPress={() => { this.folder_press(f) }} key={f.id} /> | ||||
| 			); | ||||
|   | ||||
| @@ -5,10 +5,8 @@ import { OneDriveApi } from 'lib/onedrive-api.js'; | ||||
|  | ||||
| class FileApiDriverOneDrive { | ||||
|  | ||||
| 	//constructor(clientId, clientSecret) { | ||||
| 	constructor(api) { | ||||
| 		this.api_ = api; | ||||
| 		//this.api_ = new OneDriveApi(clientId, clientSecret); | ||||
| 	} | ||||
|  | ||||
| 	api() { | ||||
|   | ||||
| @@ -12,15 +12,6 @@ class OneDriveApi { | ||||
| 		}; | ||||
| 	} | ||||
|  | ||||
| 	static instance() { | ||||
| 		if (this.instance_) return this.instance_; | ||||
|  | ||||
| 		const CLIENT_ID = 'e09fc0de-c958-424f-83a2-e56a721d331b'; | ||||
| 		const CLIENT_SECRET = 'JA3cwsqSGHFtjMwd5XoF5L5'; | ||||
| 		this.instance_ = new OneDriveApi(CLIENT_ID, CLIENT_SECRET); | ||||
| 		return this.instance_; | ||||
| 	} | ||||
|  | ||||
| 	dispatch(eventName, param) { | ||||
| 		let ls = this.listeners_[eventName]; | ||||
| 		for (let i = 0; i < ls.length; i++) { | ||||
|   | ||||
							
								
								
									
										72
									
								
								ReactNativeClient/lib/registry.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								ReactNativeClient/lib/registry.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,72 @@ | ||||
| import { Logger } from 'lib/logger.js'; | ||||
| import { Setting } from 'lib/models/setting.js'; | ||||
| import { OneDriveApi } from 'lib/onedrive-api.js'; | ||||
| import { FileApi } from 'lib/file-api.js'; | ||||
| import { Synchronizer } from 'lib/synchronizer.js'; | ||||
| import { FileApiDriverOneDrive } from 'lib/file-api-driver-onedrive.js'; | ||||
|  | ||||
| const reg = {}; | ||||
|  | ||||
| reg.logger = () => { | ||||
| 	if (reg.logger_) return reg.logger_; | ||||
| 	reg.logger_ = new Logger(); | ||||
| 	reg.logger_.addTarget('console'); | ||||
| 	reg.logger_.setLevel(Logger.LEVEL_DEBUG); | ||||
| 	return reg.logger_; | ||||
| } | ||||
|  | ||||
| reg.oneDriveApi = () => { | ||||
| 	if (reg.oneDriveApi_) return reg.oneDriveApi_; | ||||
|  | ||||
| 	const CLIENT_ID = 'e09fc0de-c958-424f-83a2-e56a721d331b'; | ||||
| 	const CLIENT_SECRET = 'JA3cwsqSGHFtjMwd5XoF5L5'; | ||||
| 	reg.oneDriveApi_ = new OneDriveApi(CLIENT_ID, CLIENT_SECRET); | ||||
|  | ||||
| 	let auth = Setting.value('sync.onedrive.auth'); | ||||
| 	if (auth) { | ||||
| 		auth = JSON.parse(auth); | ||||
| 		reg.oneDriveApi_.setAuth(auth); | ||||
| 	} | ||||
|  | ||||
| 	reg.oneDriveApi_.on('authRefreshed', (a) => { | ||||
| 		Setting.setValue('sync.onedrive.auth', JSON.stringify(a)); | ||||
| 	}); | ||||
| 	 | ||||
| 	return reg.oneDriveApi_; | ||||
| } | ||||
|  | ||||
| reg.setFileApi = (v) => { | ||||
| 	reg.fileApi_ = v; | ||||
| } | ||||
|  | ||||
| reg.fileApi = async () => { | ||||
| 	if (reg.fileApi_) return reg.fileApi_; | ||||
|  | ||||
| 	let driver = new FileApiDriverOneDrive(reg.oneDriveApi()); | ||||
| 	let appDir = await reg.oneDriveApi().appDirectory(); | ||||
|  | ||||
| 	reg.fileApi_ = new FileApi(appDir, driver); | ||||
| 	reg.fileApi_.setLogger(reg.logger()); | ||||
|  | ||||
| 	return reg.fileApi_; | ||||
| } | ||||
|  | ||||
| reg.synchronizer = async () => { | ||||
| 	if (reg.synchronizer_) return reg.synchronizer_; | ||||
|  | ||||
| 	if (!reg.db()) throw new Error('Cannot initialize synchronizer: db not initialized'); | ||||
|  | ||||
| 	let fileApi = await reg.fileApi(); | ||||
| 	reg.synchronizer_ = new Synchronizer(reg.db(), fileApi); | ||||
| 	return reg.synchronizer_; | ||||
| } | ||||
|  | ||||
| reg.setDb = (v) => { | ||||
| 	reg.db_ = v; | ||||
| } | ||||
|  | ||||
| reg.db = () => { | ||||
| 	return reg.db_; | ||||
| } | ||||
|  | ||||
| export { reg } | ||||
| @@ -26,6 +26,7 @@ import { MenuContext } from 'react-native-popup-menu'; | ||||
| import { SideMenu } from 'lib/components/side-menu.js'; | ||||
| import { SideMenuContent } from 'lib/components/side-menu-content.js'; | ||||
| import { DatabaseDriverReactNative } from 'lib/database-driver-react-native'; | ||||
| import { reg } from 'lib/registry.js'; | ||||
|  | ||||
| let defaultState = { | ||||
| 	notes: [], | ||||
| @@ -192,7 +193,7 @@ class AppComponent extends React.Component { | ||||
|  | ||||
| 	componentDidMount() { | ||||
| 		let db = new Database(new DatabaseDriverReactNative()); | ||||
| 		//db.setDebugMode(false); | ||||
| 		reg.setDb(db); | ||||
|  | ||||
| 		BaseModel.dispatch = this.props.dispatch; | ||||
| 		NotesScreenUtils.dispatch = this.props.dispatch; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user