-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
app.state.ts
85 lines (71 loc) · 2.03 KB
/
app.state.ts
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
import { Action, NgxsOnInit, Selector, State, StateContext } from '@ngxs/store';
import { Inject } from '@angular/core';
import { WINDOW } from '../services/window.token';
export interface BeforeInstallPromptEvent extends Event {
readonly platforms: string[]; // ["web", "android", "windows"]
readonly userChoice: Promise<'accepted' | 'dismissed'>;
prompt(): void;
}
export class ChangeOnlineStatus {
static readonly type = '[App] Change Online Status';
}
export class ChangeInstallStatus {
static readonly type = '[App] Change Install Status';
constructor(public payload: boolean) {}
}
export class SetInstallPrompt {
static readonly type = '[App] Set Install Prompt';
constructor(public payload: BeforeInstallPromptEvent) {}
}
export class IsOnline {
static readonly type = '[App] Network Online';
}
export class IsOffline {
static readonly type = '[App] Network Offline';
}
export interface AppStateModel {
online: boolean;
installPrompt: BeforeInstallPromptEvent;
installed: boolean;
}
@State<AppStateModel>({
name: 'app',
defaults: {
online: window.navigator.onLine,
installPrompt: null,
installed: false,
},
})
export class AppState {
constructor(/*@Inject(WINDOW) private readonly window: Window*/) {}
@Selector()
static isOnline(state: AppStateModel) {
return state.online;
}
@Selector()
static installPrompt(state: AppStateModel) {
return state.installPrompt;
}
@Selector()
static installed(state: AppStateModel) {
return state.installed;
}
@Action(SetInstallPrompt)
setInstallPrompt({ patchState }: StateContext<AppStateModel>, { payload }: SetInstallPrompt) {
patchState({
installPrompt: payload,
});
}
@Action(ChangeInstallStatus)
changeInstallStatus({ patchState }: StateContext<AppStateModel>, { payload }: ChangeInstallStatus) {
patchState({
installed: payload,
});
}
@Action(ChangeOnlineStatus)
changeOnlineStatus({ patchState }: StateContext<AppStateModel>) {
patchState({
online: window.navigator.onLine,
});
}
}