1
1
import { apiInstance } from "../api" ;
2
- // import musicControls from "cordova-plugin-music-controls2/www/MusicControls.js";
2
+ import musicControls from "cordova-plugin-music-controls2/www/MusicControls.js" ;
3
3
const initialState = {
4
4
playerData : {
5
5
"audio-delay" : 0 , // <-- milliseconds
@@ -42,57 +42,31 @@ const initialState = {
42
42
musicControlsActive : false ,
43
43
musicControlStatus : {
44
44
artist : "" ,
45
- isPlaying : false ,
46
- filename : "" ,
47
- "media-title" : "" ,
48
45
} ,
49
46
} ;
50
47
51
- // async function postData(url = "", data = {}) {
52
- // // Default options are marked with *
53
- // const response = await fetch(url, {
54
- // method: "POST", // *GET, POST, PUT, DELETE, etc.
55
- // mode: "cors", // no-cors, *cors, same-origin
56
- // cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
57
- // credentials: "same-origin", // include, *same-origin, omit
58
- // headers: {
59
- // "Content-Type": "application/json",
60
- // // 'Content-Type': 'application/x-www-form-urlencoded',
61
- // },
62
- // redirect: "follow", // manual, *follow, error
63
- // referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
64
- // body: JSON.stringify(data), // body data type must match "Content-Type" header
65
- // });
66
- // return response.json(); // parses JSON response into native JavaScript objects
67
- // }
68
-
69
- // function events(action) {
70
- // const message = JSON.parse(action).message;
71
- // switch (message) {
72
- // case "music-controls-next":
73
- // apiInstance.post("controls/next");
74
- // break;
75
- // case "music-controls-previous":
76
- // apiInstance.post("controls/prev");
77
- // break;
78
- // case "music-controls-pause":
79
- // // Do something
80
- // console.log("MusicController: Pause");
81
- // musicControls.updateIsPlaying(false);
82
- // break;
83
- // case "music-controls-play":
84
- // console.log("MusicController: Play");
85
- // musicControls.updateIsPlaying(true);
86
- // break;
87
- // case "music-controls-destroy":
88
- // // Do something
89
- // console.log("Destroy music controls");
90
- // break;
91
- // default:
92
- // break;
93
- // }
94
- // }
48
+ const musicControlsSettings = {
49
+ hasPrev : false ,
50
+ hasNext : false ,
51
+ cover : null ,
52
+ } ;
95
53
54
+ const musicEventHandler = async ( action ) => {
55
+ const message = JSON . parse ( action ) . message ;
56
+ console . log ( `Message from controller: ${ message } ` ) ;
57
+ switch ( message ) {
58
+ case "music-controls-pause" :
59
+ await apiInstance
60
+ . post ( "/controls/play-pause" )
61
+ . then ( ( ) => musicControls . updateIsPlaying ( false ) ) ;
62
+ break ;
63
+ case "music-controls-play" :
64
+ await apiInstance
65
+ . post ( "/controls/play-pause" )
66
+ . then ( ( ) => musicControls . updateIsPlaying ( true ) ) ;
67
+ break ;
68
+ }
69
+ } ;
96
70
export const simpleapi = {
97
71
namespaced : true ,
98
72
state : { ...initialState } ,
@@ -130,43 +104,6 @@ export const simpleapi = {
130
104
mutations : {
131
105
setPlayerData ( state , value ) {
132
106
state . playerData = value ;
133
-
134
- // If music controls enabled
135
- // if (state.connected && state.playerData.filename) {
136
- // if (!state.musicControlsActive) {
137
- // musicControls.create(
138
- // {
139
- // artist:
140
- // state.playerData["media-title"] || state.playerData.filename,
141
- // dismissable: false,
142
- // },
143
- // () => {
144
- // state.musicControlsActive = true;
145
- // }
146
- // );
147
- // state.musicControlStatus["media-title"] =
148
- // state.playerData["media-title"];
149
- // state.musicControlStatus.filename = state.playerData.filename;
150
-
151
- // musicControls.updateIsPlaying(!state.playerData.pause);
152
- // musicControls.subscribe(events);
153
- // musicControls.listen();
154
- // } else {
155
- // // Media have changed.
156
- // if (
157
- // state.musicControlStatus["media-title"] !=
158
- // state.playerData["media-title"] ||
159
- // state.musicControlStatus.filename != state.playerData.filename
160
- // ) {
161
- // musicControls.destroy();
162
- // state.musicControlsActive = false;
163
- // // } else {
164
- // // console.log("T");
165
- // // // musicControls.updateIsPlaying(!state.playerData.pause);
166
- // // }
167
- // }
168
- // }
169
- // }
170
107
} ,
171
108
setPlayerDataProperty ( state , value ) {
172
109
state . playerData [ value . key ] = value . value ;
@@ -178,26 +115,64 @@ export const simpleapi = {
178
115
state . MPVInfo = value ;
179
116
} ,
180
117
clearPlaybackRefreshInterval ( state ) {
118
+ console . log ( "clear playback refresh interval" ) ;
181
119
if ( state . playbackRefreshInterval != null ) {
182
- console . log ( "Store playbackRefreshInterval should be cleared." ) ;
183
120
clearInterval ( state . playbackRefreshInterval ) ;
184
121
state . playbackRefreshInterval = null ;
185
122
}
186
123
} ,
187
124
clearPlayerData ( state ) {
188
125
state . playerData = { ...initialState . playerData } ;
189
126
} ,
127
+ setMusicControlsActive ( state , value ) {
128
+ state . musicControlsActive = value ;
129
+ } ,
130
+ setMusicControlsTitle ( state , value ) {
131
+ state . musicControlStatus . artist = value ;
132
+ } ,
190
133
} ,
191
134
actions : {
192
- setPlaybackRefreshInterval ( { commit, state } ) {
135
+ setPlaybackRefreshInterval ( { commit, state, dispatch , rootGetters } ) {
193
136
if ( state . playbackRefreshInterval == null ) {
194
137
state . playbackRefreshInterval = setInterval ( ( ) => {
195
138
apiInstance . get ( "/status" ) . then ( ( response ) => {
196
139
commit ( "setPlayerData" , response . data ) ;
140
+ if ( rootGetters [ "settings/androidNotificationEnabled" ] === true ) {
141
+ dispatch ( "handleMusicControls" ) ;
142
+ }
197
143
} ) ;
198
144
} , 1500 ) ;
199
145
}
200
- // Also setup local notifcations
146
+ } ,
147
+ handleMusicControls ( { commit, state } ) {
148
+ if ( state . playerData . filename ) {
149
+ const title =
150
+ state . playerData [ "media-title" ] || state . playerData . filename ;
151
+
152
+ if ( title != state . musicControlStatus . artist ) {
153
+ console . log ( "Have to change title" ) ;
154
+
155
+ if ( state . musicControlsActive ) {
156
+ musicControls . destroy ( ) ;
157
+ musicControls . create ( { ...musicControlsSettings , track : title } ) ;
158
+ musicControls . subscribe ( musicEventHandler ) ;
159
+ musicControls . listen ( ) ;
160
+ } else {
161
+ musicControls . create ( { ...musicControlsSettings , track : title } ) ;
162
+ musicControls . subscribe ( musicEventHandler ) ;
163
+ musicControls . listen ( ) ;
164
+ }
165
+ commit ( "setMusicControlsTitle" , title ) ;
166
+ commit ( "setMusicControlsActive" , true ) ;
167
+ musicControls . updateIsPlaying ( ! state . playerData . pause ) ;
168
+ } else {
169
+ musicControls . updateIsPlaying ( ! state . playerData . pause ) ;
170
+ }
171
+ } else if ( state . musicControlsActive ) {
172
+ console . log ( "Music controls should be destroyed" ) ;
173
+ musicControls . destroy ( ) ;
174
+ commit ( "setMusicControlsActive" , false ) ;
175
+ }
201
176
} ,
202
177
} ,
203
178
} ;
0 commit comments