-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprint_progres.js
143 lines (115 loc) · 3.49 KB
/
print_progres.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import * as React from 'react';
import {useEffect, useState} from 'react';
import {
View,
Text,
TouchableOpacity,
StyleSheet,
NativeEventEmitter,
NativeModules,
} from 'react-native';
import {RawBtProgress} from "./rawbtapi";
const {ReactNativeLoading} = NativeModules;
export type PrinterProgressProps = {
/**
* The tint color of the progress bar itself.
*
* @default #007aff
*/
color?: string;
/**
* The tint color of the progress bar track.
*
* @default transparent
*/
trackColor?: string;
/**
* Height of the loading indicator.
*
* @default 7
*/
height?: number;
/**
* Border radius of the loading indicator.
*
* @default height / 2
*/
borderRadius?: number;
};
let subscription ;
let timer = null;
function PrinterProgress({
height = 7,
borderRadius = height * 0.5,
color = '#007aff',
}: PrinterProgressProps) {
const [printerStatus:RawBtProgress,setPrinterStatus] = useState({status:'unknown',message:'no message',progress:0});
useEffect(()=>{
if(subscription === undefined) {
const loadingManagerEmitter = new NativeEventEmitter(ReactNativeLoading);
subscription = loadingManagerEmitter.addListener("RawBT", ({status,progress,message}) => {
setPrinterStatus({status:status,message:message,progress:progress});
});
}
});
if(timer != null){
clearTimeout(timer);
}
if(printerStatus.progress>0){
return (
<View style={styles.container}>
<View style={{borderRadius: borderRadius, borderWidth:1, borderColor:color,width:'100%',height:height+2}}
><View style={{borderRadius: borderRadius,
backgroundColor:color,
position: 'absolute',left:0,top:0,
width:printerStatus.progress+'%',height:height}} /></View>
</View>
);
}
if(printerStatus.status === "error"){
return (
<View style={[styles.container,{backgroundColor:'red'}]}>
<Text style={styles.title}>PRINTER ERROR</Text>
<Text style={{color: 'white'}}>{printerStatus.message}</Text>
<TouchableOpacity onPress={() => setPrinterStatus({status:'cancelled'})}>
<Text style={styles.btn}>CLOSE</Text>
</TouchableOpacity>
</View>
);
}
if(printerStatus.status === "info" || printerStatus.status === "connected") {
timer = setTimeout(() => setPrinterStatus({status: 'hide'}), 2000);
return (
<View style={styles.container}>
<Text>{printerStatus.message}</Text>
</View>
);
}
// success, canceled
return (<View/>);
}
const styles = StyleSheet.create({
container: {
flex: 1,
width: '100%',
padding: 16,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#EEE',
overflow: 'hidden',
position: 'absolute',
top: Platform.OS === "android" ? 25 : 0,
},
btn: {
paddingHorizontal: 16,
paddingVertical: 8,
backgroundColor: '#FFF',
color:'#F00',
marginTop:16
},
title : {
fontSize: 16,
color: '#FFF'
}
});
export default PrinterProgress