esoftplay 0.0.114-u → 0.0.114-x

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/bin/build.js CHANGED
@@ -229,6 +229,7 @@ import * as ErrorReport from 'esoftplay/error';
229
229
  import * as Notifications from 'expo-notifications';
230
230
  import React, { useEffect, useRef } from 'react';
231
231
  import { enableFreeze, enableScreens } from 'react-native-screens';
232
+ const { globalIdx } = require('esoftplay/global');
232
233
  enableScreens();
233
234
  enableFreeze();
234
235
 
@@ -236,7 +237,6 @@ Notifications.addNotificationResponseReceivedListener(x => LibNotification.onAct
236
237
 
237
238
  export default function App() {
238
239
  const Home = useRef(esp.home()).current
239
- const { globalIdx } = require('esoftplay/global');
240
240
 
241
241
  useEffect(() => {
242
242
  globalIdx.reset()
@@ -1,6 +1,8 @@
1
+ import { useGlobalReturn } from 'esoftplay';
1
2
  // noPage
2
3
 
3
- import { esp, LibCrypt, LibCurl, LibNavigation, UserClass, UserNotification, _global } from "esoftplay";
4
+ import { esp, LibCrypt, LibCurl, LibNavigation, LibObject, useGlobalState, UserClass, UserNotification, _global } from "esoftplay";
5
+ import { fastFilter } from "esoftplay/fast";
4
6
  import Constants from 'expo-constants';
5
7
  import * as Notifications from 'expo-notifications';
6
8
  import { Alert, Linking, Platform } from "react-native";
@@ -28,7 +30,6 @@ import moment from '../../moment';
28
30
 
29
31
  Notifications.setNotificationHandler({
30
32
  handleNotification: async (notif) => {
31
- UserNotification.user_notification_loadData();
32
33
  return ({
33
34
  shouldShowAlert: true,
34
35
  shouldPlaySound: true,
@@ -38,10 +39,151 @@ Notifications.setNotificationHandler({
38
39
  },
39
40
  });
40
41
 
42
+ const lastUrlState = useGlobalState<any>(undefined)
43
+
44
+ function mainUrl(): string {
45
+ const { protocol, domain, uri } = esp.config()
46
+ return protocol + "://" + domain + uri;
47
+ }
48
+
49
+ function readAll(ids: (string | number)[]) {
50
+ let url = mainUrl() + "user/push-read";
51
+ const { salt } = esp.config()
52
+ let post: any = {
53
+ notif_id: ids.join(','),
54
+ user_id: "",
55
+ secretkey: new LibCrypt().encode(salt + "|" + moment().format("YYYY-MM-DD hh:mm:ss"))
56
+ }
57
+ new LibCurl(url, post)
58
+ }
59
+
60
+ function splitArray(array: any[], maxLength: number): any[] {
61
+ const parts = maxLength // items per chunk
62
+ const splitedArray = array.reduce((resultArray: any, item, index) => {
63
+ const chunkIndex = Math.floor(index / parts)
64
+ if (!resultArray[chunkIndex]) {
65
+ resultArray[chunkIndex] = [] // start a new chunk
66
+ }
67
+ resultArray[chunkIndex].push(item)
68
+ return resultArray
69
+ }, [])
70
+ return splitedArray
71
+ }
72
+
41
73
  export default class m {
42
74
 
75
+ static state(): useGlobalReturn<any> {
76
+ return lastUrlState
77
+ }
78
+
79
+ static loadData(isFirst?: boolean): void {
80
+ // console.log('LOADDATA', isFirst)
81
+ let _uri = mainUrl() + "user/push-notif/desc"
82
+ const lastUrl = lastUrlState.get()
83
+ if (lastUrl == -1) {
84
+ return
85
+ }
86
+ if (lastUrl && !isFirst) {
87
+ _uri = lastUrl
88
+ }
89
+ const user = UserClass.state().get()
90
+ if (!user?.id) {
91
+ Notifications.setBadgeCountAsync(0)
92
+ }
93
+ const { salt } = esp.config()
94
+ let post: any = {
95
+ user_id: "",
96
+ secretkey: new LibCrypt().encode(salt + "|" + moment().format("YYYY-MM-DD hh:mm:ss"))
97
+ }
98
+ if (user) {
99
+ post["user_id"] = user.id || user.user_id
100
+ post["group_id"] = user.group_id || esp.config('group_id')
101
+ }
102
+
103
+ // console.log(esp.logColor.cyan, 'curl: ' + _uri, esp.logColor.reset)
104
+ new LibCurl(_uri, post,
105
+ (res: any) => {
106
+ // console.log(esp.logColor.green, 'next: ' + res.next, '\n length: ' + res?.list?.length, res.list[0].message, esp.logColor.reset)
107
+ if (res?.list?.length > 0) {
108
+ const urls: string[] = UserNotification.state().get().urls
109
+ // console.log(urls)
110
+ if (urls && urls.indexOf(_uri) < 0) {
111
+ let { data, unread } = UserNotification.state().get()
112
+ // console.log(nUnread+" => nUnread")
113
+ let nUnread
114
+ if (isFirst) {
115
+ nUnread = fastFilter(res.list, (row) => row.status != 2).length
116
+ UserNotification.state().set({
117
+ data: res.list,
118
+ urls: [],
119
+ unread: nUnread
120
+ })
121
+ } else {
122
+ nUnread = unread + fastFilter(res.list, (row) => row.status != 2).length
123
+ data.push(...res.list)
124
+ UserNotification.state().set({
125
+ data: data,
126
+ urls: [_uri, ...urls],
127
+ unread: nUnread
128
+ })
129
+ }
130
+ Notifications.setBadgeCountAsync(nUnread)
131
+ }
132
+ }
133
+ if (res.next) {
134
+ lastUrlState.set(res.next)
135
+ } else {
136
+ lastUrlState.set(-1)
137
+ }
138
+ }, (msg) => {
139
+
140
+ }
141
+ )
142
+ }
143
+
144
+ static add(id: number, user_id: number, group_id: number, title: string, message: string, params: string, status: 0 | 1 | 2, created?: string, updated?: string): void {
145
+ const item = { id, user_id, group_id, title, message, params, status, created, updated }
146
+ let data = UserNotification.state().get().data
147
+ data.unshift(item)
148
+ UserNotification.state().set({
149
+ ...UserNotification.state().get(),
150
+ data: data
151
+ })
152
+ }
153
+
154
+ static drop(): void {
155
+ UserNotification.state().reset()
156
+ }
157
+
158
+ static markRead(id?: string | number, ..._ids: (string | number)[]): void {
159
+ // console.log("markRead")
160
+ let { data, unread, urls } = UserNotification.state().get()
161
+ let nUnread = unread > 0 ? unread - 1 : 0
162
+ let ids = [id, ..._ids]
163
+ // console.log(ids)
164
+ ids.forEach((id) => {
165
+ const index = data.findIndex((row) => String(row.id) == String(id))
166
+ if (index > -1) {
167
+ data = LibObject.set(data, 2)(index, 'status')
168
+ nUnread = unread > 0 ? unread - 1 : 0
169
+ }
170
+ })
171
+ // console.log(JSON.stringify(data))
172
+ UserNotification.state().set({
173
+ urls,
174
+ data: data,
175
+ unread: nUnread
176
+ })
177
+ Notifications.setBadgeCountAsync(nUnread)
178
+ const idsToPush = splitArray(ids, 200)
179
+ idsToPush.forEach((ids) => {
180
+ readAll(ids)
181
+ })
182
+ }
183
+
184
+
43
185
  static onAction(notification: any): void {
44
- UserNotification.user_notification_loadData()
186
+ m.loadData(true)
45
187
  const data = m.getData(notification)
46
188
  function doOpen(data: any) {
47
189
  if (!_global.NavsIsReady) {
@@ -62,7 +204,7 @@ export default class m {
62
204
  return x?.notification?.request?.content?.data
63
205
  }
64
206
 
65
- static listen(dataRef: any): () => void {
207
+ static listen(dataRef: any): void {
66
208
  if (esp.config('notification') == 1) {
67
209
  if (Platform.OS == 'android')
68
210
  Notifications.setNotificationChannelAsync(
@@ -70,7 +212,7 @@ export default class m {
70
212
  {
71
213
  sound: 'default',
72
214
  enableLights: true,
73
- description: "this is description",
215
+ description: "Please enable notifications permissions",
74
216
  name: esp.appjson().expo.name,
75
217
  importance: Notifications.AndroidImportance.MAX,
76
218
  showBadge: true,
@@ -79,11 +221,8 @@ export default class m {
79
221
  }
80
222
  )
81
223
  UserClass.pushToken();
82
- dataRef.receive = Notifications.addNotificationReceivedListener((x) => {
83
- UserNotification.user_notification_loadData()
84
- })
224
+ dataRef.receive = Notifications.addNotificationReceivedListener(() => { })
85
225
  }
86
- else return () => { }
87
226
  }
88
227
 
89
228
  static requestPermission(callback?: (token: any) => void): Promise<any> {
@@ -130,18 +269,7 @@ export default class m {
130
269
  if (!data) return
131
270
  if (typeof data == 'string')
132
271
  data = JSON.parse(data)
133
- const crypt = new LibCrypt();
134
- const salt = esp.config("salt");
135
- const config = esp.config();
136
- let uri = config.protocol + "://" + config.domain + config.uri + "user/push-read"
137
- new LibCurl(uri, {
138
- notif_id: data.id,
139
- secretkey: crypt.encode(salt + "|" + moment().format("YYYY-MM-DD hh:mm:ss"))
140
- }, () => {
141
- UserNotification.user_notification_loadData();
142
- }, () => {
143
-
144
- })
272
+ m.markRead(data.id)
145
273
  let param = data;
146
274
  if (param.action)
147
275
  switch (param.action) {
@@ -175,17 +303,7 @@ export default class m {
175
303
  }
176
304
 
177
305
  static openNotif(data: any): void {
178
- const salt = esp.config("salt");
179
- const config = esp.config();
180
- let uri = config.protocol + "://" + config.domain + config.uri + "user/push-read"
181
- new LibCurl(uri, {
182
- notif_id: data.id,
183
- secretkey: new LibCrypt().encode(salt + "|" + moment().format("YYYY-MM-DD hh:mm:ss"))
184
- }, () => {
185
- UserNotification.user_notification_setRead(data.id)
186
- }, () => {
187
- // esp.log(msg)
188
- })
306
+ m.markRead(data.id)
189
307
  let param = JSON.parse(data.params)
190
308
  switch (param.action) {
191
309
  case "alert":
@@ -1,6 +1,6 @@
1
1
  // noPage
2
2
 
3
- import { esp, LibComponent, UserClass, UserNotification } from 'esoftplay';
3
+ import { esp, LibComponent, LibNotification, UserClass } from 'esoftplay';
4
4
  import React from 'react';
5
5
  import { InteractionManager, View } from 'react-native';
6
6
  import { WebView } from 'react-native-webview';
@@ -73,13 +73,12 @@ export default (() => {
73
73
  })
74
74
  }
75
75
  }
76
-
77
76
  componentDidMount(): void {
78
77
  super.componentDidMount();
79
78
  const user = UserClass.state().get()
80
79
  if (user){
81
80
  if(esp.config().notification == 1){
82
- UserNotification.user_notification_loadData()
81
+ LibNotification.loadData(true)
83
82
  }
84
83
  }
85
84
  }
@@ -1,8 +1,7 @@
1
1
  // noPage
2
2
 
3
3
 
4
- import { LibComponent, LibStyle, UserNotification } from "esoftplay";
5
- import React from "react";
4
+ import { LibComponent, LibStyle } from "esoftplay";
6
5
  import { Text, TouchableOpacity, View } from "react-native";
7
6
 
8
7
  export interface UserNotifbadgeProps {
@@ -25,7 +24,6 @@ export default class Enotifbadge extends LibComponent<UserNotifbadgeProps, UserN
25
24
 
26
25
  componentDidMount(): void {
27
26
  super.componentDidMount()
28
- UserNotification.user_notification_loadData()
29
27
  }
30
28
 
31
29
  render(): any {
@@ -1,15 +1,8 @@
1
1
  //
2
2
 
3
- import {
4
- esp, LibComponent, LibCrypt,
5
- LibCurl, LibIcon, LibList, LibNotification, LibObject, LibStatusbar, LibStyle, LibUtils, LibWorkloop, useGlobalReturn, useGlobalState,
6
- UserClass, UserNotification_item
7
- } from "esoftplay";
8
- import * as Notifications from 'expo-notifications';
3
+ import { LibComponent, LibIcon, LibList, LibNotification, LibStatusbar, LibStyle, useGlobalReturn, useGlobalState, UserNotification_item } from "esoftplay";
9
4
  import { Pressable, Text, TouchableOpacity, View } from "react-native";
10
- import { fastFilter } from "../../fast";
11
5
  //@ts-ignore
12
- import moment from "../../moment";
13
6
  export interface UserNotificationProps {
14
7
  navigation: any,
15
8
  data: any[]
@@ -25,109 +18,13 @@ const initState = {
25
18
  unread: 0
26
19
  };
27
20
 
28
- const state = useGlobalState(initState, { persistKey: 'user_notification', isUserData: true })
29
-
21
+ const state = useGlobalState<any>(initState, { persistKey: "user_notification_data", isUserData: true })
30
22
  class m extends LibComponent<UserNotificationProps, UserNotificationState> {
31
23
 
32
24
  static state(): useGlobalReturn<any> {
33
25
  return state
34
26
  }
35
27
 
36
- static add(id: number, user_id: number, group_id: number, title: string, message: string, params: string, status: 0 | 1 | 2, created?: string, updated?: string): void {
37
- const item = { id, user_id, group_id, title, message, params, status, created, updated }
38
- let data = state.get().data
39
- data.unshift(item)
40
- state.set({
41
- ...state.get(),
42
- data: data
43
- })
44
- }
45
-
46
- static drop(): void {
47
- state.set(initState)
48
- }
49
-
50
- static user_notification_loadData(): void {
51
- const { protocol, domain, uri, salt } = esp.config()
52
- let _uri = protocol + "://" + domain + uri + "user/push-notif"
53
- let data = state.get().data
54
- const user = UserClass.state().get()
55
- if (!user?.id) {
56
- Notifications.setBadgeCountAsync(0)
57
- }
58
- /* hapus yang lebih lama dari 3 bulan */
59
- const d = new Date();
60
- d.setMonth(d.getMonth() - 3);
61
- const created = LibUtils.moment(String(d.getDate())).format('YYYY-MM-DD HH:mm:ss')
62
- let cdata = fastFilter(data, (row) => row.created > created)
63
- if (cdata.length != data.length) {
64
- /* jika data tidak sama artinya ada yang expired > 3 bulan */
65
- state.set(LibObject.set(state.get(), cdata)('data'))
66
- }
67
- if (data && data.length > 0) {
68
- const lastData = data[0]
69
- if (lastData.id)
70
- _uri += "?last_id=" + lastData.id || 0
71
- }
72
- let post: any = {
73
- user_id: "",
74
- secretkey: new LibCrypt().encode(salt + "|" + moment().format("YYYY-MM-DD hh:mm:ss"))
75
- }
76
- if (user) {
77
- post["user_id"] = user.id || user.user_id
78
- post["group_id"] = user.group_id || esp.config('group_id')
79
- }
80
- m.user_notification_fetchData(_uri, post);
81
- }
82
-
83
- static user_notification_fetchData(uri: string, post: any): void {
84
- new LibCurl(uri, post,
85
- (res: any) => {
86
- m.user_notification_parseData(res.list, uri)
87
- if (res.next != "") {
88
- LibWorkloop.execNextTix(m.user_notification_fetchData, [res.next, post])
89
- }
90
- }, (msg) => {
91
- }
92
- )
93
- }
94
-
95
- static user_notification_parseData(res: any[], uri: string): void {
96
- if (res.length > 0) {
97
- const urls = state.get().urls
98
- if (urls && urls.indexOf(uri) < 0) {
99
- let { data, urls, unread } = state.get()
100
- const nUnread = unread + fastFilter(res, (row) => row.status != 2).length
101
- state.set({
102
- data: [...res.reverse(), ...data],
103
- urls: [uri, ...urls],
104
- unread: nUnread
105
- })
106
- Notifications.setBadgeCountAsync(nUnread)
107
- }
108
- }
109
- }
110
-
111
- static user_notification_setRead(id: string | number): void {
112
- let { data, unread, urls } = state.get()
113
- const index = data.findIndex((row) => row.id == String(id))
114
- if (index > -1) {
115
- const nUnread = unread > 0 ? unread - 1 : 0
116
- state.set({
117
- urls,
118
- data: LibObject.set(data, 2)(index, 'status'),
119
- unread: nUnread
120
- })
121
- Notifications.setBadgeCountAsync(nUnread)
122
- }
123
- }
124
-
125
- componentDidMount(): void {
126
- super.componentDidMount()
127
- moment().locale(esp.langId());
128
- m.user_notification_loadData()
129
- }
130
-
131
28
  render(): any {
132
29
  const { colorPrimary, colorAccent, STATUSBAR_HEIGHT } = LibStyle;
133
30
  const { goBack } = this.props.navigation
@@ -151,7 +48,7 @@ class m extends LibComponent<UserNotificationProps, UserNotificationState> {
151
48
  </View>
152
49
  <LibList
153
50
  data={data}
154
- onRefresh={() => m.user_notification_loadData()}
51
+ onRefresh={() => LibNotification.loadData(true)}
155
52
  renderItem={(item: any) => (
156
53
  <TouchableOpacity onPress={() => LibNotification.openNotif(item)} >
157
54
  <UserNotification_item {...item} />
@@ -1,11 +1,10 @@
1
1
  // noPage
2
2
 
3
3
  import { LibStyle, LibUtils } from 'esoftplay';
4
- import React from 'react';
5
4
  import { Text, View } from 'react-native';
6
5
 
7
6
 
8
- export interface UserNotificationProps {
7
+ export interface UserNotification_itemProps {
9
8
  created: string,
10
9
  id: number,
11
10
  message: string,
@@ -17,7 +16,7 @@ export interface UserNotificationProps {
17
16
  updated: string,
18
17
  user_id: number,
19
18
  }
20
- export default function m(props: UserNotificationProps): any {
19
+ export default function m(props: UserNotification_itemProps): any {
21
20
  return (
22
21
  <View style={[{ padding: 16, flexDirection: "row", backgroundColor: "white", marginBottom: 3, marginHorizontal: 0, width: LibStyle.width }, LibStyle.elevation(1.5)]} >
23
22
  <View style={{}} >
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "esoftplay",
3
- "version": "0.0.114-u",
3
+ "version": "0.0.114-x",
4
4
  "description": "embedding data from esoftplay framework (web based) into mobile app",
5
5
  "main": "cache/index.js",
6
6
  "types": "../../index.d.ts",
package/persist.ts CHANGED
@@ -16,7 +16,7 @@ export default (() => {
16
16
  if (value != undefined)
17
17
  AsyncStorage.setItem(key, JSON.stringify(value));
18
18
  else
19
- d()
19
+ del()
20
20
  fastLoop(setter[key], (cc) => cc(value))
21
21
  }
22
22
  }
@@ -51,7 +51,7 @@ export default (() => {
51
51
  updater()
52
52
  return () => {
53
53
  r.current = false
54
- setter[key] = fastFilter(setter[key], (x) => x !== b);
54
+ setter[key] = fastFilter(setter[key], (x) => x !== setState);
55
55
  }
56
56
  }, [])
57
57