Skip to content

Commit 5f25721

Browse files
author
Ferenc Nánási
committed
Musiccontrols optimizations, changed versionCode to 1.0.8
1 parent 7360e96 commit 5f25721

9 files changed

+63
-28
lines changed

android/app/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ android {
66
applicationId "com.husudosu.mpvremote"
77
minSdkVersion rootProject.ext.minSdkVersion
88
targetSdkVersion rootProject.ext.targetSdkVersion
9-
versionCode 7
10-
versionName "1.0.7"
9+
versionCode 8
10+
versionName "1.0.8"
1111
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1212
aaptOptions {
1313
// Files and dirs to omit from the packaged assets dir, modified to accommodate modern web apps.

android/app/release/output-metadata.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
{
1111
"type": "SINGLE",
1212
"filters": [],
13-
"versionCode": 7,
14-
"versionName": "1.0.7",
13+
"versionCode": 8,
14+
"versionName": "1.0.8",
1515
"outputFile": "app-release.apk"
1616
}
1717
]

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mpv-remote",
3-
"version": "1.0.7",
3+
"version": "1.0.8",
44
"private": true,
55
"description": "MPV remote",
66
"scripts": {

src/Container.vue

-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ export default defineComponent({
159159
window.plugins.intentShim.onIntent((intent) => {
160160
handleIntent(intent);
161161
});
162-
// await store.dispatch("notificationController/createController");
163162
}
164163
};
165164

src/api.js

+28-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import axios from "axios";
22
import { toastController } from "@ionic/core";
33

44
import { store } from "./store";
5+
import musicControls from "cordova-plugin-music-controls2/www/MusicControls";
56

67
export const MINIMUM_API_VERSION = "1.0.6";
78

@@ -22,10 +23,21 @@ function requestOnRejected(error) {
2223
Promise.reject(error);
2324
}
2425

25-
export function disconnect() {
26-
store.commit("simpleapi/clearPlaybackRefreshInterval");
26+
// FIXME: App still creates musicController when it's disabled on settings.
27+
function handleDisconnect(auto_reconnect = false) {
28+
if (!auto_reconnect) store.commit("simpleapi/clearPlaybackRefreshInterval");
2729
store.commit("simpleapi/setConnectedState", false);
2830
store.commit("simpleapi/clearPlayerData");
31+
if (store.state.simpleapi.musicControlsActive) {
32+
console.log("remove music controls");
33+
store.commit("simpleapi/setMusicControlsActive", false);
34+
musicControls.destroy();
35+
}
36+
}
37+
38+
export function disconnect() {
39+
handleDisconnect();
40+
2941
if (callsPending > 0) {
3042
callsPending = 0;
3143
cancelSource.cancel("Cancel pending requests");
@@ -41,7 +53,7 @@ export function configureInstance(host, port) {
4153
}
4254

4355
apiInstance.interceptors.response.use(
44-
(response) => {
56+
async (response) => {
4557
if (!store.state.simpleapi.connected) {
4658
store.commit("simpleapi/setConnectedState", true);
4759
// Get MPV Info
@@ -56,6 +68,17 @@ apiInstance.interceptors.response.use(
5668
);
5769
}
5870
});
71+
console.log("Reconnected");
72+
// Get status after connecting immediately.
73+
await apiInstance.get("/status").then((response) => {
74+
console.log("Got initial status");
75+
store.commit("simpleapi/setPlayerData", response.data);
76+
});
77+
78+
if (store.getters["settings/androidNotificationEnabled"]) {
79+
console.log("Handle music controls");
80+
store.dispatch("simpleapi/handleMusicControls");
81+
}
5982
}
6083
callsPending--;
6184
return response;
@@ -65,8 +88,7 @@ apiInstance.interceptors.response.use(
6588
// Ignore 403 if getting drives
6689
callsPending--;
6790
if (error.message == "Network Error") {
68-
store.commit("simpleapi/setConnectedState", false);
69-
store.commit("simpleapi/clearPlayerData");
91+
handleDisconnect(true);
7092
return Promise.reject(error);
7193
} else if (
7294
error.response &&
@@ -88,8 +110,7 @@ apiInstance.interceptors.response.use(
88110
}
89111
} else {
90112
if (store.state.simpleapi.connected) {
91-
store.commit("simpleapi/setConnectedState", false);
92-
store.commit("simpleapi/clearPlayerData");
113+
handleDisconnect(true);
93114
}
94115
}
95116
return Promise.reject(error);

src/components/playerController.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ export default {
7070
key: "pause",
7171
value: !playerData.value.pause,
7272
});
73-
musicControls.updateIsPlaying(!playerData.value.pause);
73+
if (store.getters["settings/androidNotificationEnabled"] == true)
74+
musicControls.updateIsPlaying(!playerData.value.pause);
7475
}
7576
});
7677
};

src/store/settings.module.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ import {
1313
import { Capacitor } from "@capacitor/core";
1414
import { CapacitorSQLite, SQLiteConnection } from "@capacitor-community/sqlite";
1515
import { useSQLite } from "vue-sqlite-hook";
16-
import { apiInstance, configureInstance, disconnect } from "../api";
16+
import { configureInstance, disconnect } from "../api";
1717

1818
const initialState = {
1919
settings: {
2020
servers: [],
2121
currentServerId: null,
22+
androidNotificationEnabled: false,
2223
},
2324
configured: false,
2425
sqlite: null,
@@ -108,17 +109,19 @@ export const settings = {
108109
await Storage.remove({ key: "server_port" });
109110
}
110111

111-
const androidNotificationEnabled = await Storage.get({
112+
let androidNotificationEnabled = await Storage.get({
112113
key: "androidNotificationEnabled",
113114
});
114-
115115
if (androidNotificationEnabled.value == null) {
116116
await dispatch("setSetting", {
117117
key: "androidNotificationEnabled",
118118
value: false,
119119
});
120+
androidNotificationEnabled = false;
121+
} else {
122+
androidNotificationEnabled =
123+
androidNotificationEnabled.value === "true";
120124
}
121-
122125
const servers = await getServer(state.dbSession);
123126
const currentServerId = await Storage.get({ key: "currentServerId" });
124127
if (servers.length > 0) {
@@ -135,7 +138,7 @@ export const settings = {
135138
commit("setAppSettings", {
136139
servers,
137140
currentServerId: parseInt(currentServerId.value),
138-
androidNotificationEnabled: androidNotificationEnabled.value,
141+
androidNotificationEnabled: androidNotificationEnabled,
139142
});
140143
},
141144
setSetting: async function ({ commit }, payload) {
@@ -164,10 +167,6 @@ export const settings = {
164167
{ root: true }
165168
);
166169
configureInstance(server.host, server.port);
167-
// Get status after connecting immediately.
168-
apiInstance.get("/status").then((response) => {
169-
commit("simpleapi/setPlayerData", response.data, { root: true });
170-
});
171170
// Load fileman history
172171
await dispatch("loadFilemanHistory");
173172
await dispatch(

src/store/simpleapi.module.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const initialState = {
4242
musicControlsActive: false,
4343
musicControlStatus: {
4444
artist: "",
45+
pause: true,
4546
},
4647
};
4748

@@ -126,18 +127,23 @@ export const simpleapi = {
126127
},
127128
setMusicControlsActive(state, value) {
128129
state.musicControlsActive = value;
130+
if (!value) state.musicControlStatus.artist = "";
129131
},
130132
setMusicControlsTitle(state, value) {
131133
state.musicControlStatus.artist = value;
132134
},
135+
setMusicControlsPuasedState(state, value) {
136+
state.musicControlStatus.pause = value;
137+
},
133138
},
134139
actions: {
135140
setPlaybackRefreshInterval({ commit, state, dispatch, rootGetters }) {
136141
if (state.playbackRefreshInterval == null) {
137142
state.playbackRefreshInterval = setInterval(() => {
138143
apiInstance.get("/status").then((response) => {
139144
commit("setPlayerData", response.data);
140-
if (rootGetters["settings/androidNotificationEnabled"] === true) {
145+
if (rootGetters["settings/androidNotificationEnabled"] == true) {
146+
console.log("handle music controls");
141147
dispatch("handleMusicControls");
142148
}
143149
});
@@ -150,23 +156,31 @@ export const simpleapi = {
150156
state.playerData["media-title"] || state.playerData.filename;
151157

152158
if (title != state.musicControlStatus.artist) {
153-
console.log("Have to change title");
154-
155159
if (state.musicControlsActive) {
156160
musicControls.destroy();
157161
musicControls.create({ ...musicControlsSettings, track: title });
158162
musicControls.subscribe(musicEventHandler);
159163
musicControls.listen();
164+
musicControls.updateIsPlaying(!state.playerData.pause);
160165
} else {
161166
musicControls.create({ ...musicControlsSettings, track: title });
162167
musicControls.subscribe(musicEventHandler);
163168
musicControls.listen();
169+
musicControls.updateIsPlaying(!state.playerData.pause);
164170
}
165171
commit("setMusicControlsTitle", title);
166172
commit("setMusicControlsActive", true);
167-
musicControls.updateIsPlaying(!state.playerData.pause);
173+
/* Update isPlaying on some phones keeps sending notification to phone when it's on sleep.
174+
So we have handle that by only updating when the the playback paused/unpaused*/
175+
if (state.musicControlStatus.pause != state.playerData.pause) {
176+
musicControls.updateIsPlaying(!state.playerData.pause);
177+
commit("setMusicControlsPuasedState", state.playerData.pause);
178+
}
168179
} else {
169-
musicControls.updateIsPlaying(!state.playerData.pause);
180+
if (state.musicControlStatus.pause != state.playerData.pause) {
181+
musicControls.updateIsPlaying(!state.playerData.pause);
182+
commit("setMusicControlsPuasedState", state.playerData.pause);
183+
}
170184
}
171185
} else if (state.musicControlsActive) {
172186
console.log("Music controls should be destroyed");

src/views/Settings.vue

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export default {
101101
if (!event.target.checked) {
102102
console.log("Remove notification if exists");
103103
musicControls.destroy();
104+
store.commit("simpleapi/setMusicControlsActive", false);
104105
}
105106
};
106107
return {

0 commit comments

Comments
 (0)