- 版本:0.1.2
- GitHub: https://github.com/loloof64/react-native-chess-oex-scanner-android
- NPM: https://npmjs.net.cn/package/react-native-chess-oex-scanner-android
- 下载
- 前一天:0
- 上周:0
- 上个月:0
react-native ChessOexScannerAndroid
在您的 React Native 应用程序中,使用安装在 Android 设备上的棋类 OEX 引擎。
安装
npm install react-native-chess-oex-scanner-android
用法
这里有一个 TypeScript 的完整示例。首先,一些说明
-
所有方法都是异步的,返回一个
Promise
, -
在构造函数中,您调用
ChessOexScannerAndroid.setupEngineUtils()
,给它应用程序 ID, -
库管理我称之为
your store
和the installed libraries
。当 store 处理您可以复制到内部数据的内容时,安装的库是您选择复制的库。请注意,您只能启动安装在内部数据中的引擎, -
您可以通过调用
ChessOexScannerAndroid.getMyStoreEnginesNames()
列出 store 中的引擎名称,并通过ChessOexScannerAndroid.installEngineFromMyStore(index)
从 store 中安装一个引擎。请注意,传递给installEngineFromMyStore()
的索引将匹配从getMyStoreEnginesNames()
返回的引擎的顺序, -
您可以通过调用
ChessOexScannerAndroid.listInstalledEngines()
列出已安装的引擎,并通过ChessOexScannerAndroid.executeInstalledEngine(index)
执行一个引擎。请注意,这里传递给executeInstalledEngine()
的索引也将匹配从executeInstalledEngine()
返回的引擎的顺序, -
您应该定期调用
ChessOexScannerAndroid.readCurrentEnginePendingOutputs()
以读取来自引擎进程的输出, -
最后但同样重要的是,在释放您的组件/应用程序时,您应该调用
ChessOexScannerAndroid.stopCurrentRunningEngine()
以清理引擎进程。
import * as React from 'react';
import { StyleSheet, View, Text, ScrollView, Button } from 'react-native';
import Toast from 'react-native-easy-toast';
import Dialog from 'react-native-dialog';
import ChessOexScannerAndroid from 'react-native-chess-oex-scanner-android';
export default function App() {
const toast = React.useRef<Toast | null>(null);
const [commandValue, setCommandValue] = React.useState('');
const [commandDialogVisible, setCommandDialogVisible] = React.useState(false);
const [enginesStoreList, setEnginesStoreList] = React.useState<Array<string>>(
[]
);
const [enginesInstalledList, setEnginesInstalledList] = React.useState<
Array<string>
>([]);
function askForCommand(_e: any) {
setCommandDialogVisible(true);
}
async function readOutputsFromEngine() {
try {
const lines = await ChessOexScannerAndroid.readCurrentEnginePendingOutputs();
if (lines.length > 0) {
lines.forEach((singleLine) => console.log(singleLine));
}
} catch (err) {
console.error(err);
}
}
function handleCancelCommandDialog() {
setCommandDialogVisible(false);
}
async function sendCommandToEngine() {
setCommandDialogVisible(false);
try {
const command = commandValue;
await ChessOexScannerAndroid.sendCommandToRunningEngine(command);
} catch (err) {
console.error(err);
}
}
async function getStoreEnginesList() {
const engines = await ChessOexScannerAndroid.getMyStoreEnginesNames();
return engines;
}
async function getInstalledEnginesList() {
function stripNameFromLibName(libName: string) {
let result = libName;
result = result.substring(3);
result = result.substring(0, result.length - 3);
return result;
}
const engines = await ChessOexScannerAndroid.listInstalledEngines();
return engines.map((item) => stripNameFromLibName(item));
}
async function installStoreEngine(index: number) {
try {
await ChessOexScannerAndroid.installEngineFromMyStore(index);
toast.current?.show('Engine installed');
} catch (err) {
console.error(err);
toast.current?.show('Failed to install engine !');
}
}
async function playWithEngine(index: number) {
try {
const allEngines = await ChessOexScannerAndroid.listInstalledEngines();
const engineName = allEngines[index];
console.log('Launching engine ' + engineName);
await ChessOexScannerAndroid.executeInstalledEngine(index);
} catch (err) {
console.error(err);
toast.current?.show('Failed to launch engine !');
}
}
React.useEffect(() => {
async function setup() {
// In setupEngineUtils, you give the android application id
await ChessOexScannerAndroid.setupEngineUtils(
'com.example.reactnativechessoexscannerandroid'
);
}
setup()
.then(() => {})
.catch((err) => console.error(err));
getStoreEnginesList()
.then((engines) => setEnginesStoreList(engines))
.catch((err) => {
console.error(err);
toast.current?.show('Failed to get engines list from your store !');
});
getInstalledEnginesList()
.then((engines) => setEnginesInstalledList(engines))
.catch((err) => {
console.error(err);
toast.current?.show('Failed to get installed engines list !');
});
return function () {
ChessOexScannerAndroid.stopCurrentRunningEngine();
};
}, []);
React.useEffect(() => {
let timer = setInterval(readOutputsFromEngine, 1000);
return function () {
clearInterval(timer);
};
}, []);
return (
<View style={styles.container}>
<Toast ref={toast} />
<Dialog.Container visible={commandDialogVisible}>
<Dialog.Title>Send command</Dialog.Title>
<Dialog.Input
label="Command:"
value={commandValue}
onChangeText={setCommandValue}
/>
<Dialog.Button label="Cancel" onPress={handleCancelCommandDialog} />
<Dialog.Button label="Send" onPress={sendCommandToEngine} />
</Dialog.Container>
<Button onPress={askForCommand} title="Send command" />
<View style={styles.storeZone}>
<Text style={styles.listHeader}>Engines from store</Text>
<ScrollView>
{enginesStoreList.map((engineName, index) => {
return (
<Text
key={engineName}
onPress={() => installStoreEngine(index)}
style={styles.listText}
>
{engineName}
</Text>
);
})}
</ScrollView>
</View>
<View style={styles.installedZone}>
<Text style={styles.listHeader}>Engines installed</Text>
<ScrollView>
{enginesInstalledList.map((engineName, index) => {
return (
<Text
key={engineName}
onPress={() => playWithEngine(index)}
style={styles.listText}
>
{engineName}
</Text>
);
})}
</ScrollView>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
storeZone: {
width: '100%',
height: '40%',
backgroundColor: 'lightgreen',
marginTop: 10,
},
installedZone: {
width: '100%',
height: '40%',
backgroundColor: 'lightyellow',
},
listHeader: {
color: 'blue',
marginLeft: 'auto',
marginRight: 'auto',
marginTop: 5,
},
listText: {
marginLeft: 10,
marginTop: 8,
marginBottom: 8,
color: 'magenta',
},
});
贡献
查看贡献指南,了解如何为存储库和开发工作流程做出贡献。
许可证
MIT