Repository navigation
Expand file tree
/
Copy pathuseDataLayer.ts
More file actions
148 lines (123 loc) · 4.08 KB
/
Copy pathuseDataLayer.ts
File metadata and controls
148 lines (123 loc) · 4.08 KB
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
144
145
146
147
148
/*
* Copyright (c) 2020-2022 Mateusz Falkowski (appidea.pl) and contributors. All rights reserved.
* This file is part of "react-native-hce" library: https://github.com/appidea/react-native-hce
* Licensed under the MIT license. See LICENSE file in the project root for details.
*/
import { useCallback, useContext, useEffect, useRef, useState } from 'react';
import {
HCESessionContext,
HCESession,
NFCTagType4NDEFContentType,
NFCTagType4,
} from 'react-native-hce';
import type {
DataLayer,
NFCTagReactStateProps,
LogEntry,
} from './DataLayerTypes';
const defaultProps: NFCTagReactStateProps = {
content: '',
type: NFCTagType4.stringFromContentType(NFCTagType4NDEFContentType.Text),
writable: false,
_pristine: true,
};
const createLogEntry = (eventName: string): LogEntry => ({
time: new Date().toISOString(),
message: eventName,
});
/**
* The hook encapsulating the data management layer.
*/
const useDataLayer = (): DataLayer => {
const { session } = useContext(HCESessionContext);
const [loading, setLoading] = useState<boolean>(false);
// ** Following section of code is responsible for: **
// Management of "Enabled" field state in the application
const [enabled, setEnabled] = useState<boolean>(false);
const switchSession = useCallback(
async (enable) => {
setLoading(true);
await session.setEnabled(enable);
setEnabled(enable);
setLoading(false);
},
[setLoading, session, setEnabled]
);
// ** Following section of code is responsible for: **
// Management of "HCE Application" - related fields state in the application
const [nfcTagProps, setNfcTagProps] =
useState<NFCTagReactStateProps>(defaultProps);
const updateProp = useCallback(
(prop: string, value: any) => {
setNfcTagProps((state: any) => ({
...state,
[prop]: value,
_pristine: false,
}));
},
[setNfcTagProps]
);
// ** Following section of code is responsible for: **
// Synchronization of state: APPLICATION ---> LIBRARY.
const timeout = useRef<ReturnType<typeof setTimeout>>();
const updateTag = useCallback(
async (localNfcTagProps) => {
setLoading(true);
const tag = new NFCTagType4({
type: NFCTagType4.contentTypeFromString(localNfcTagProps.type),
content: localNfcTagProps.content,
writable: localNfcTagProps.writable,
});
await session.setApplication(tag);
setLoading(false);
},
[setLoading, session]
);
useEffect(() => {
if (!nfcTagProps._pristine) {
const boundUpdateTag = updateTag.bind(null, nfcTagProps);
timeout.current = setTimeout(boundUpdateTag, 600);
}
return () => clearTimeout(timeout.current);
}, [nfcTagProps, updateTag]);
// ** Following section of code is responsible for: **
// Synchronization of state: LIBRARY ---> APPLICATION.
const updateApp = useCallback(() => {
const application = session.application;
if (application === null) {
return;
}
setNfcTagProps({
type: application.content.type,
content: application.content.content,
writable: application.content.writable,
_pristine: true,
});
setEnabled(session.enabled);
}, [session, setNfcTagProps, setEnabled]);
useEffect(() => {
const cancelSubscription = session.on(
HCESession.Events.HCE_STATE_WRITE_FULL,
updateApp
);
updateApp();
return () => cancelSubscription();
}, [session, setNfcTagProps, setEnabled, updateApp]);
// ** Following section of code is responsible for: **
// Logging the events to preview in "Events" pane.
const [log, setLog] = useState<Array<LogEntry>>([]);
const logger = useCallback(
(eventData) => {
setLog((msg) => [...msg, createLogEntry(eventData)]);
},
[setLog]
);
useEffect(() => {
const cancelSubscription = session.on(null, logger);
return () => cancelSubscription();
}, [session, logger]);
// ** Following section of code is responsible for: **
// Returning the hook result.
return { nfcTagProps, updateProp, switchSession, log, enabled, loading };
};
export default useDataLayer;