@saltcorn/mobile-app 1.1.1-beta.5 → 1.1.1-beta.7

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@saltcorn/mobile-app",
3
3
  "displayName": "Saltcorn mobile app",
4
- "version": "1.1.1-beta.5",
4
+ "version": "1.1.1-beta.7",
5
5
  "description": "Saltcorn mobile app for Android and iOS",
6
6
  "main": "index.js",
7
7
  "scripts": {
@@ -189,33 +189,15 @@ export async function getScreenOrientation() {
189
189
  return await ScreenOrientation.orientation();
190
190
  }
191
191
 
192
- export async function sendIntentCallback() {
193
- console.log("sendIntentCallback");
192
+ export async function finishShareIntent() {
193
+ SendIntent.finish();
194
+ }
195
+
196
+ export async function checkSendIntentReceived() {
194
197
  try {
195
- const received = await SendIntent.checkSendIntentReceived();
196
- console.log("received: ", received);
197
- if (received) {
198
- const response = await apiCall({
199
- method: "POST",
200
- path: "/notifications/share-handler",
201
- body: received,
202
- });
203
- console.log("Share data sent to server.");
204
- console.log(response);
205
- if (response.data.error) {
206
- errorAlert(response.data.error);
207
- } else {
208
- showAlerts([
209
- {
210
- type: "success",
211
- msg:
212
- "Shared: " +
213
- (received.title || received.text || received.url || ""),
214
- },
215
- ]);
216
- }
217
- }
198
+ return await SendIntent.checkSendIntentReceived();
218
199
  } catch (error) {
219
- console.log("Error in sendIntentCallback: ", error);
200
+ console.log("Error in checkSendIntentReceived: ", error);
201
+ return null;
220
202
  }
221
203
  }
package/src/init.js CHANGED
@@ -1,4 +1,4 @@
1
- /*global saltcorn */
1
+ /*global saltcorn, Capacitor */
2
2
 
3
3
  import {
4
4
  startOfflineMode,
@@ -22,7 +22,7 @@ import {
22
22
  gotoEntryView,
23
23
  addRoute,
24
24
  } from "./helpers/navigation.js";
25
- import { sendIntentCallback } from "./helpers/common.js";
25
+ import { checkSendIntentReceived } from "./helpers/common.js";
26
26
 
27
27
  import i18next from "i18next";
28
28
  import i18nextSprintfPostProcessor from "i18next-sprintf-postprocessor";
@@ -275,6 +275,22 @@ const takeLastLocation = () => {
275
275
  return result;
276
276
  };
277
277
 
278
+ const notEmpty = (shareData) => {
279
+ for (const value of Object.values(shareData)) {
280
+ if (typeof value === "string" && value.trim() !== "") return true;
281
+ }
282
+ };
283
+
284
+ const postShare = async (shareData) => {
285
+ const page = await router.resolve({
286
+ pathname: "post/notifications/share",
287
+ shareData,
288
+ fullWrap: true,
289
+ isSendIntentActivity: true,
290
+ });
291
+ return await replaceIframe(page.content);
292
+ };
293
+
278
294
  // device is ready
279
295
  export async function init({
280
296
  mobileConfig,
@@ -317,8 +333,18 @@ export async function init({
317
333
  const entryPoint = mobileConfig.entry_point;
318
334
  await initI18Next(translations);
319
335
  state.mobileConfig.encodedSiteLogo = siteLogo;
320
-
321
- state.mobileConfig.networkState = await Network.getStatus();
336
+ state.mobileConfig.networkState = (
337
+ await Network.getStatus()
338
+ ).connectionType;
339
+ if (Capacitor.platform === "android") {
340
+ const shareData = await checkSendIntentReceived();
341
+ if (shareData) return await postShare(shareData);
342
+ } else if (Capacitor.platform === "ios") {
343
+ window.addEventListener("sendIntentReceived", async () => {
344
+ const shareData = await checkSendIntentReceived();
345
+ if (shareData && notEmpty(shareData)) return await postShare(shareData);
346
+ });
347
+ }
322
348
  Network.addListener("networkStatusChange", networkChangeCallback);
323
349
 
324
350
  const networkDisabled = state.mobileConfig.networkState === "none";
@@ -367,11 +393,11 @@ export async function init({
367
393
  });
368
394
  }
369
395
  }
370
- if (state.mobileConfig.allowOfflineMode) {
371
- await sendIntentCallback();
372
- window.addEventListener("sendIntentReceived", sendIntentCallback);
373
- }
374
396
 
397
+ if (Capacitor.platform === "ios") {
398
+ const shareData = await checkSendIntentReceived();
399
+ if (shareData && notEmpty(shareData)) return await postShare(shareData);
400
+ }
375
401
  let page = null;
376
402
  if (!lastLocation) {
377
403
  addRoute({ route: entryPoint, query: undefined });
@@ -12,8 +12,8 @@ import {
12
12
  getAskDeleteOfflineData,
13
13
  getAskUploadNotEnded,
14
14
  } from "./routes/sync";
15
-
16
15
  import { postView, postViewRoute, getView } from "./routes/view";
16
+ import { postShare } from "./routes/notifications";
17
17
 
18
18
  const routes = [
19
19
  // api
@@ -80,6 +80,11 @@ const routes = [
80
80
  path: "get/sync/ask_delete_offline_data",
81
81
  action: getAskDeleteOfflineData,
82
82
  },
83
+ // notifications
84
+ {
85
+ path: "post/notifications/share",
86
+ action: postShare,
87
+ },
83
88
  // view
84
89
  {
85
90
  path: "post/view/:viewname",
@@ -0,0 +1,59 @@
1
+ /*global saltcorn, Capacitor */
2
+
3
+ import { wrapContents } from "../utils";
4
+ import { MobileRequest } from "../mocks/request";
5
+
6
+ import { checkJWT } from "../../helpers/auth.js";
7
+ import { apiCall } from "../../helpers/api";
8
+
9
+ const buildErrMsg = (msg) => {
10
+ return saltcorn.markup.tags.span(
11
+ msg,
12
+ saltcorn.markup.tags.i({
13
+ class: "ps-2 fas fa-times text-danger",
14
+ })
15
+ );
16
+ };
17
+
18
+ const buildSuccessMsg = (msg) => {
19
+ return saltcorn.markup.tags.span(
20
+ msg,
21
+ saltcorn.markup.tags.i({
22
+ class: "ps-2 fas fa-check text-success",
23
+ })
24
+ );
25
+ };
26
+
27
+ export const postShare = async (context) => {
28
+ let content = "";
29
+ const mobileCfg = saltcorn.data.state.getState().mobileConfig;
30
+ if (mobileCfg.networkState === "none")
31
+ content = buildErrMsg("No network connection");
32
+ else if (!(await checkJWT(mobileCfg.jwt)))
33
+ content = buildErrMsg("Not authenticated");
34
+ else {
35
+ const response = await apiCall({
36
+ method: "POST",
37
+ path: "/notifications/share-handler",
38
+ body: context.shareData,
39
+ });
40
+ if (response.error) content = buildErrMsg(response.error);
41
+ else content = buildSuccessMsg("Shared successfully");
42
+ }
43
+ return await wrapContents(
44
+ saltcorn.markup.tags.div(
45
+ content,
46
+ saltcorn.markup.tags.script(`
47
+ setTimeout(() => {
48
+ ${
49
+ Capacitor.platform === "android"
50
+ ? "parent.saltcorn.mobileApp.common.finishShareIntent();"
51
+ : "parent.saltcorn.mobileApp.navigation.gotoEntryView();"
52
+ }
53
+ }, 4000);`)
54
+ ),
55
+ "Share",
56
+ context,
57
+ new MobileRequest()
58
+ );
59
+ };
@@ -152,7 +152,7 @@ export const wrapContents = async (contents, title, context, req) => {
152
152
  body,
153
153
  alerts: prepareAlerts(context, req),
154
154
  role: state.mobileConfig.user.role_id || 100,
155
- menu: getMenu(req),
155
+ menu: context.isSendIntentActivity ? [] : getMenu(req),
156
156
  req,
157
157
  headers: getHeaders(),
158
158
  brand: {
package/webpack.config.js CHANGED
@@ -20,7 +20,18 @@ module.exports = {
20
20
  use: {
21
21
  loader: "babel-loader",
22
22
  options: {
23
- presets: ["@babel/preset-env"],
23
+ presets: [
24
+ [
25
+ "@babel/preset-env",
26
+ {
27
+ targets: {
28
+ esmodules: true,
29
+ browsers: ["last 2 Chrome versions", "not dead"],
30
+ },
31
+ exclude: ["@babel/plugin-transform-async-to-generator"],
32
+ },
33
+ ],
34
+ ],
24
35
  },
25
36
  },
26
37
  },