aio-capacitor 1.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.css ADDED
@@ -0,0 +1,24 @@
1
+ .io-manager{
2
+ display: flex;
3
+ flex-direction: column;
4
+ align-items: center;
5
+ justify-content: center;
6
+ gap: 1em;
7
+ height:100%;
8
+ }
9
+ .io-manager-button{
10
+ background:#e9d171;
11
+ width:8em;
12
+ height:8em;
13
+ color:#111;
14
+ font-weight:bold;
15
+ border-radius:1em;
16
+ display:flex;
17
+ align-items: center;
18
+ justify-content: center;
19
+ }
20
+ .io-manager-button .aio-input{
21
+ background: none;
22
+ border: none;
23
+ padding: 0;
24
+ }
package/index.d.ts CHANGED
@@ -39,12 +39,10 @@ export declare class AIOCapacitor {
39
39
  storageLoad: (name: string, def: any) => Promise<any>;
40
40
  copy: (text: string) => Promise<void>;
41
41
  paste: () => Promise<string>;
42
- readText: (file: any) => Promise<any>;
43
- readPath: (path: string, type: 'text' | 'imageSrc' | 'arrayBuffer') => Promise<{
44
- success: boolean;
45
- result: any;
46
- message?: string;
47
- }>;
42
+ readFile: (path: string, directory: Directory) => Promise<any>;
43
+ uriToSrc: (uri: string) => Promise<any>;
44
+ pathToUri: (path: string, directory: Directory) => Promise<any>;
45
+ pathToSrc: (path: string, directory: Directory) => Promise<any>;
48
46
  writeFile: (data: string, path: string, encoding?: boolean) => Promise<any>;
49
47
  save: (data: string | File | Blob, path: string) => Promise<{
50
48
  success: boolean;
package/index.js CHANGED
@@ -148,26 +148,10 @@ export class AIOCapacitor {
148
148
  return '';
149
149
  }
150
150
  });
151
- this.readText = (file) => __awaiter(this, void 0, void 0, function* () {
152
- return yield new FileClass().readInput(file, 'text');
153
- });
154
- this.readPath = (path, type) => __awaiter(this, void 0, void 0, function* () {
155
- const fs = new FileClass();
156
- try {
157
- const res = yield Filesystem.readFile({ path, directory: Directory.Documents });
158
- if (!(res === null || res === void 0 ? void 0 : res.data)) {
159
- return { success: false, result: null, message: 'Empty file' };
160
- }
161
- const blobRes = fs.base64ToBlob(res.data);
162
- if (!blobRes.success) {
163
- return blobRes;
164
- }
165
- return fs.readInput(blobRes.result, type);
166
- }
167
- catch (e) {
168
- return { success: false, result: null, message: (e === null || e === void 0 ? void 0 : e.message) || 'Storage read failed' };
169
- }
170
- });
151
+ this.readFile = (path, directory) => __awaiter(this, void 0, void 0, function* () { return yield Filesystem.readFile({ path, directory }); });
152
+ this.uriToSrc = (uri) => __awaiter(this, void 0, void 0, function* () { return Capacitor.convertFileSrc(uri); });
153
+ this.pathToUri = (path, directory) => __awaiter(this, void 0, void 0, function* () { return (yield Filesystem.getUri({ path, directory })).uri; });
154
+ this.pathToSrc = (path, directory) => __awaiter(this, void 0, void 0, function* () { return yield this.uriToSrc(yield this.pathToUri(path, directory)); });
171
155
  this.writeFile = (data, path, encoding) => __awaiter(this, void 0, void 0, function* () { return yield Filesystem.writeFile({ path, data, directory: Directory.Documents, encoding: encoding ? Encoding.UTF8 : undefined }); });
172
156
  this.save = (data, path) => __awaiter(this, void 0, void 0, function* () {
173
157
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aio-capacitor",
3
- "version": "1.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/react.d.ts ADDED
@@ -0,0 +1,69 @@
1
+ import { type FC, type ReactNode } from "react";
2
+ import { type I_FileRecord } from "./index";
3
+ import './index.css';
4
+ export declare const useCapacitorStorage: <T>(p: {
5
+ key: string;
6
+ def: T;
7
+ debugId?: string;
8
+ }) => {
9
+ value: any;
10
+ save: (value: T) => Promise<void>;
11
+ getValue: () => any;
12
+ change: (obj: Partial<T>) => Promise<void>;
13
+ isLoaded: any;
14
+ };
15
+ export declare const useCapacitorStorageList: <T>(p: {
16
+ key: string;
17
+ def: T[];
18
+ getId: (row: T) => any;
19
+ }) => {
20
+ value: any;
21
+ add: (row: T) => Promise<void>;
22
+ edit: (row: T) => Promise<void>;
23
+ remove: (id: any) => Promise<void>;
24
+ getById: (id: any) => any;
25
+ change: (id: any, obj: Partial<T>) => Promise<void>;
26
+ };
27
+ export declare const useAwake: () => {
28
+ awake: any;
29
+ setAwake: (state?: boolean) => Promise<any>;
30
+ };
31
+ declare const useFileManager: (p: {
32
+ mock?: boolean;
33
+ fa?: boolean;
34
+ storageKey: string;
35
+ option?: {
36
+ text?: (o: I_FileRecord) => ReactNode;
37
+ subtext?: (o: I_FileRecord) => ReactNode;
38
+ before?: (o: I_FileRecord) => ReactNode;
39
+ onClick?: (o: I_FileRecord) => void;
40
+ };
41
+ }) => {
42
+ mainProps: {
43
+ mock?: boolean;
44
+ fa?: boolean;
45
+ storageKey: string;
46
+ option?: {
47
+ text?: (o: I_FileRecord) => ReactNode;
48
+ subtext?: (o: I_FileRecord) => ReactNode;
49
+ before?: (o: I_FileRecord) => ReactNode;
50
+ onClick?: (o: I_FileRecord) => void;
51
+ };
52
+ };
53
+ files: any;
54
+ registery: any;
55
+ click: (actionName: string, row: I_FileRecord) => void;
56
+ getActions: (row: I_FileRecord) => {
57
+ text: string;
58
+ onClick: () => void;
59
+ }[];
60
+ };
61
+ export declare const FileManager: FC<{
62
+ hook: ReturnType<typeof useFileManager>;
63
+ }>;
64
+ export declare const IOManager: <T>({ data, onImport, onExport }: {
65
+ data: T;
66
+ onImport?: (v: T) => void;
67
+ onExport?: () => void;
68
+ }) => import("react/jsx-runtime").JSX.Element;
69
+ export {};
package/react.js ADDED
@@ -0,0 +1,171 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
11
+ import { useEffect, useRef, useState } from "react";
12
+ import { AIOCapacitor, FileRegistry } from "./index";
13
+ import { GetSvg, FileClass } from "aio-utils";
14
+ import { Cards, AIFile } from "aio-input";
15
+ import './index.css';
16
+ export const useCapacitorStorage = (p) => {
17
+ const { key, def } = p;
18
+ const [data, setData] = useState({ isLoaded: false, value: def });
19
+ const dataRef = useRef(data);
20
+ dataRef.current = data;
21
+ const ac = new AIOCapacitor();
22
+ const isNative = ac.isNative();
23
+ const load = () => __awaiter(void 0, void 0, void 0, function* () {
24
+ if (!isNative) {
25
+ const newData = Object.assign(Object.assign({}, dataRef.current), { isLoaded: true });
26
+ setData(newData);
27
+ }
28
+ else {
29
+ const res = yield ac.storageLoad(key, def);
30
+ const newData = Object.assign(Object.assign({}, dataRef.current), { value: res, isLoaded: true });
31
+ setData(newData);
32
+ }
33
+ });
34
+ const save = (value) => __awaiter(void 0, void 0, void 0, function* () {
35
+ if (isNative) {
36
+ yield ac.storageSave(key, value);
37
+ }
38
+ const newData = Object.assign(Object.assign({}, dataRef.current), { value });
39
+ setData(newData);
40
+ });
41
+ const change = (obj) => __awaiter(void 0, void 0, void 0, function* () {
42
+ const oldData = dataRef.current;
43
+ const { value } = oldData;
44
+ save(Object.assign(Object.assign({}, value), obj));
45
+ });
46
+ const getValue = () => dataRef.current.value;
47
+ useEffect(() => { load(); }, []);
48
+ return { value: data.value, save, getValue, change, isLoaded: data.isLoaded };
49
+ };
50
+ export const useCapacitorStorageList = (p) => {
51
+ const { key, def } = p;
52
+ const [value, setValue] = useState(def);
53
+ const ac = new AIOCapacitor();
54
+ const isNative = ac.isNative();
55
+ const getById = (id) => value.filter((o) => p.getId(o) === id)[0];
56
+ const load = () => __awaiter(void 0, void 0, void 0, function* () {
57
+ if (!ac.isNative()) {
58
+ return;
59
+ }
60
+ const res = yield ac.storageLoad(key, def);
61
+ setValue(res);
62
+ });
63
+ const save = (value) => __awaiter(void 0, void 0, void 0, function* () {
64
+ if (isNative) {
65
+ yield ac.storageSave(key, value);
66
+ }
67
+ setValue(value);
68
+ });
69
+ const add = (row) => __awaiter(void 0, void 0, void 0, function* () { return yield save([...value, Object.assign({}, row)]); });
70
+ const edit = (row) => __awaiter(void 0, void 0, void 0, function* () {
71
+ const newValue = value.map((o) => {
72
+ if (p.getId(o) === p.getId(row)) {
73
+ return row;
74
+ }
75
+ return o;
76
+ });
77
+ save(newValue);
78
+ });
79
+ const change = (id, obj) => __awaiter(void 0, void 0, void 0, function* () {
80
+ const row = getById(id);
81
+ if (!row) {
82
+ return;
83
+ }
84
+ yield edit(Object.assign(Object.assign({}, row), obj));
85
+ });
86
+ const remove = (id) => __awaiter(void 0, void 0, void 0, function* () { return yield save(value.filter((o) => p.getId(o) !== id)); });
87
+ useEffect(() => { load(); }, []);
88
+ return { value, add, edit, remove, getById, change };
89
+ };
90
+ export const useAwake = () => {
91
+ const ac = new AIOCapacitor();
92
+ const [awake, setAwake] = useState(false);
93
+ const getAwake = () => __awaiter(void 0, void 0, void 0, function* () {
94
+ const awake = yield ac.isAwake();
95
+ setAwake(awake.isKeptAwake);
96
+ });
97
+ useEffect(() => {
98
+ getAwake();
99
+ }, []);
100
+ return { awake, setAwake: ac.awake };
101
+ };
102
+ const useFileManager = (p) => {
103
+ const [files, setFiles] = useState([]);
104
+ const registeryRef = useRef(undefined);
105
+ useEffect(() => {
106
+ const mock = [
107
+ { name: 'file1', id: '64563454564', path: './ddsd/sddgf', "createdAt": Date.now(), type: 'text' },
108
+ { name: 'file2', id: '7664564', path: './ddsd/sddgf', "createdAt": Date.now(), type: 'text' },
109
+ { name: 'file3', id: '876456464', path: './ddsd/sddgf', "createdAt": Date.now(), type: 'text' },
110
+ { name: 'file4', id: '544745345', path: './ddsd/sddgf', "createdAt": Date.now(), type: 'text' },
111
+ { name: 'file5', id: '8965565', path: './ddsd/sddgf', "createdAt": Date.now(), type: 'text' },
112
+ ];
113
+ registeryRef.current = new FileRegistry({ storageKey: p.storageKey, onChange: (v) => setFiles(v), mock: p.mock ? mock : undefined });
114
+ }, []);
115
+ const click = (actionName, row) => {
116
+ const registery = registeryRef.current;
117
+ if (registery) {
118
+ if (actionName === 'rename') {
119
+ registery.rename(row.id);
120
+ }
121
+ else if (actionName === 'remove') {
122
+ registery.remove(row.id);
123
+ }
124
+ }
125
+ };
126
+ const getActions = (row) => {
127
+ return [
128
+ { text: p.fa ? 'تغییر نام' : 'Rename', onClick: () => click('rename', row) },
129
+ { text: p.fa ? 'حذف' : 'Remove', onClick: () => click('remove', row) },
130
+ ];
131
+ };
132
+ return { mainProps: p, files, registery: registeryRef.current, click, getActions };
133
+ };
134
+ export const FileManager = ({ hook }) => {
135
+ const { mainProps } = hook;
136
+ const { option = {} } = mainProps;
137
+ return (_jsx("div", { className: "fr-list", children: _jsx(Cards, { items: hook.files, option: {
138
+ text: (row) => !option.text ? row.name : option.text(row),
139
+ subtext: (row) => !option.subtext ? undefined : option.subtext(row),
140
+ before: (row) => !(option === null || option === void 0 ? void 0 : option.before) ? new GetSvg().getIcon('mdiFile', 0.7) : option.before(row),
141
+ actions: (o) => hook.getActions(o)
142
+ } }) }));
143
+ };
144
+ export const IOManager = ({ data, onImport, onExport }) => {
145
+ const importData = (file) => __awaiter(void 0, void 0, void 0, function* () {
146
+ if (!onImport) {
147
+ return;
148
+ }
149
+ const res = yield new FileClass().readText(file);
150
+ if (res.success) {
151
+ onImport(JSON.parse(res.result));
152
+ }
153
+ else {
154
+ alert(res.message);
155
+ }
156
+ });
157
+ const exportData = () => __awaiter(void 0, void 0, void 0, function* () {
158
+ if (!onExport) {
159
+ return;
160
+ }
161
+ const f = new FileClass();
162
+ const res = yield f.saveFile(data, 'finance-data.json');
163
+ if (res.success) {
164
+ onExport();
165
+ }
166
+ else {
167
+ alert(res.message);
168
+ }
169
+ });
170
+ return (_jsxs("div", { className: "io-manager", children: [!!onImport && _jsx("div", { className: "io-manager-button", children: _jsx(AIFile, { text: 'Import', onChange: (file) => importData(file) }) }), !!onExport && _jsx("div", { className: "io-manager-button", onClick: () => exportData(), children: "Export" })] }));
171
+ };