Skip to content

Commit 26f8290

Browse files
committed
Server handling working properly
1 parent 57ff057 commit 26f8290

File tree

8 files changed

+169
-181
lines changed

8 files changed

+169
-181
lines changed

TODO.md

+4
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ Run debugging on android:
1111
```bash
1212
ionic capacitor run android -l --host=host
1313
```
14+
15+
## TODO
16+
17+
- By some reasons routing not working properly, when not using modals on Settings -> Servers and Media collections,

android/app/src/main/assets/capacitor.config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"webDir": "dist",
55
"bundledWebRuntime": false,
66
"server": {
7-
"url": "http://192.168.88.6:8100"
7+
"url": "http://192.168.88.4:8100"
88
}
99
}

src/Container.vue

+105-151
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
</template>
5353

5454
<script>
55-
import { computed } from "vue";
55+
import { computed, defineComponent, ref } from "vue";
5656
import {
5757
IonApp,
5858
IonContent,
@@ -68,10 +68,6 @@ import {
6868
IonSelect,
6969
IonSelectOption,
7070
} from "@ionic/vue";
71-
/* eslint-disable no-unused-vars */
72-
import { defineComponent, ref } from "vue";
73-
74-
import { useRoute } from "vue-router";
7571
import {
7672
playCircleOutline,
7773
cogOutline,
@@ -80,11 +76,10 @@ import {
8076
} from "ionicons/icons";
8177
import { getPlatforms } from "@ionic/vue";
8278
import { App } from "@capacitor/app";
83-
8479
import { useStore } from "vuex";
85-
import { apiInstance } from "./api";
86-
import appInfo from "./verinfo";
80+
8781
import { AndroindIntentActions } from "./enums";
82+
import { apiInstance } from "./api";
8883
8984
export default defineComponent({
9085
name: "App",
@@ -104,18 +99,11 @@ export default defineComponent({
10499
IonSelectOption,
105100
},
106101
setup() {
107-
// // Capacitor SQLite and Jeep init
108-
// // const app = getCurrentInstance();
109-
// // if (app != null) {
110-
// // app.appContext.config.globalProperties.$sqlite = useSQLite();
111-
// // }
112-
// // const sqlite = app?.appContext.config.globalProperties.$sqlite;
113102
const store = useStore();
114-
const route = useRoute();
115103
const platforms = getPlatforms();
116104
const selectedPageIndex = ref(0);
117105
118-
/*
106+
/*
119107
Side menu handler
120108
*/
121109
const appPages = [
@@ -159,15 +147,109 @@ export default defineComponent({
159147
"settings/setCurrentServer",
160148
store.getters["settings/currentServerId"]
161149
);
162-
// console.log("Get status");
163-
// apiInstance.get("/status").then((response) => {
164-
// store.commit("simpleapi/setPlayerData", response.data);
165-
// console.log(response.data);
166-
// });
167-
// store.dispatch("simpleapi/setPlaybackRefreshInterval");
150+
151+
if (platforms.includes("hybrid")) {
152+
console.log("Registering indent");
153+
registerBroadcastReceiver();
154+
window.plugins.intentShim.onIntent((intent) => {
155+
handleIntent(intent);
156+
});
157+
}
168158
};
169159
170-
initApp();
160+
/*
161+
Handling of Android intents.
162+
*/
163+
const handleSendIntent = async (intent) => {
164+
intent.clipItems.forEach((el) => {
165+
apiInstance.post("playlist", {
166+
filename: el.text,
167+
flag: "append-play",
168+
});
169+
});
170+
};
171+
const handleViewIntent = async (intent) => {
172+
let headerArray = [];
173+
if (Object.prototype.hasOwnProperty.call(intent.extras, "headers")) {
174+
for (var i = 0; i < intent.extras.headers.length - 1; i += 2) {
175+
headerArray.push(
176+
`${intent.extras.headers[i]}: ${intent.extras.headers[i + 1]}`
177+
);
178+
}
179+
}
180+
if (Object.prototype.hasOwnProperty.call(intent, "data")) {
181+
let reqData = {
182+
filename: intent.data,
183+
flag: "replace",
184+
};
185+
reqData["file-local-options"] = {};
186+
if (headerArray.length > 0)
187+
reqData["file-local-options"]["http-header-fields"] = headerArray;
188+
if (Object.prototype.hasOwnProperty.call(intent.extras, "title"))
189+
reqData["file-local-options"]["force-media-title"] =
190+
intent.extras.title;
191+
apiInstance.post("playlist", reqData);
192+
}
193+
};
194+
const handleIntent = async (intent) => {
195+
// console.log("Full intent object" + JSON.stringify(intent));
196+
if (Object.prototype.hasOwnProperty.call(intent, "action")) {
197+
switch (intent.action) {
198+
case AndroindIntentActions.SEND:
199+
handleSendIntent(intent);
200+
break;
201+
case AndroindIntentActions.VIEW:
202+
handleViewIntent(intent);
203+
break;
204+
default:
205+
break;
206+
}
207+
}
208+
};
209+
const registerBroadcastReceiver = () => {
210+
window.plugins.intentShim.registerBroadcastReceiver(
211+
{
212+
filterActions: [
213+
"com.darryncampbell.cordova.plugin.broadcastIntent.ACTION",
214+
],
215+
},
216+
(intent) => {
217+
// Broadcast received
218+
handleIntent(intent);
219+
}
220+
);
221+
};
222+
const unregisterBroadcastReceiver = () => {
223+
window.plugins.intentShim.unregisterBroadcastReceiver();
224+
};
225+
226+
// Listeners
227+
if (!platforms.includes("hybrid")) initApp();
228+
document.addEventListener("deviceReady", () => initApp());
229+
window.addEventListener("orientationchange", function () {
230+
store.commit("app/setScreenOrinetation", screen.orientation.type);
231+
});
232+
App.addListener("appStateChange", async ({ isActive }) => {
233+
if (isActive) {
234+
// Set screen orientation if active
235+
store.commit("app/setScreenOrinetation", screen.orientation.type);
236+
apiInstance.get("/status").then((response) => {
237+
store.commit("simpleapi/setPlayerData", response.data);
238+
});
239+
240+
if (!store.state.simpleapi.playbackRefreshInterval)
241+
store.dispatch("simpleapi/setPlaybackRefreshInterval");
242+
if (!store.state.settings.dbSession)
243+
await store.dispatch("settings/initDbSession");
244+
245+
if (platforms.includes("hybrid")) registerBroadcastReceiver();
246+
} else {
247+
// Battery saving stuff
248+
store.commit("simpleapi/clearPlaybackRefreshInterval");
249+
await store.dispatch("settings/closeDbConnection");
250+
if (platforms.includes("hybrid")) unregisterBroadcastReceiver();
251+
}
252+
});
171253
return {
172254
selectedPageIndex,
173255
appPages,
@@ -178,134 +260,6 @@ export default defineComponent({
178260
setCurrentServer: (serverId) =>
179261
store.dispatch("settings/setCurrentServer", serverId),
180262
};
181-
182-
// const handleSendIntent = async (intent) => {
183-
// intent.clipItems.forEach((el) => {
184-
// apiInstance.post("playlist", {
185-
// filename: el.text,
186-
// flag: "append-play",
187-
// });
188-
// });
189-
// };
190-
// const handleViewIntent = async (intent) => {
191-
// let headerArray = [];
192-
// if (Object.prototype.hasOwnProperty.call(intent.extras, "headers")) {
193-
// for (var i = 0; i < intent.extras.headers.length - 1; i += 2) {
194-
// headerArray.push(
195-
// `${intent.extras.headers[i]}: ${intent.extras.headers[i + 1]}`
196-
// );
197-
// }
198-
// }
199-
// if (Object.prototype.hasOwnProperty.call(intent, "data")) {
200-
// let reqData = {
201-
// filename: intent.data,
202-
// flag: "replace",
203-
// };
204-
// reqData["file-local-options"] = {};
205-
// if (headerArray.length > 0)
206-
// reqData["file-local-options"]["http-header-fields"] = headerArray;
207-
// if (Object.prototype.hasOwnProperty.call(intent.extras, "title"))
208-
// reqData["file-local-options"]["force-media-title"] =
209-
// intent.extras.title;
210-
// apiInstance.post("playlist", reqData);
211-
// }
212-
// };
213-
// const handleIntent = async (intent) => {
214-
// // console.log("Full intent object" + JSON.stringify(intent));
215-
// if (Object.prototype.hasOwnProperty.call(intent, "action")) {
216-
// switch (intent.action) {
217-
// case AndroindIntentActions.SEND:
218-
// handleSendIntent(intent);
219-
// break;
220-
// case AndroindIntentActions.VIEW:
221-
// handleViewIntent(intent);
222-
// break;
223-
// default:
224-
// break;
225-
// }
226-
// }
227-
// };
228-
// const registerBroadcastReceiver = () => {
229-
// window.plugins.intentShim.registerBroadcastReceiver(
230-
// {
231-
// filterActions: [
232-
// "com.darryncampbell.cordova.plugin.broadcastIntent.ACTION",
233-
// ],
234-
// },
235-
// (intent) => {
236-
// // Broadcast received
237-
// handleIntent(intent);
238-
// }
239-
// );
240-
// };
241-
// const unregisterBroadcastReceiver = () => {
242-
// window.plugins.intentShim.unregisterBroadcastReceiver();
243-
// };
244-
// const setServer = async (item) => {
245-
// console.log("Connecting to server:");
246-
// console.log(item.target);
247-
// // connect(item.detail.value);
248-
// };
249-
// const initApp = () => {
250-
// console.log("Init app");
251-
// store.dispatch("settings/loadSettings").then(async () => {
252-
// // await connect(store.state.settings.settings.currentServerId);
253-
// await store.dispatch(
254-
// "simpleapi/connectToServer",
255-
// store.getters["settings/currentServerId"]
256-
// );
257-
// // Register intent handling
258-
// // Intent handling only for mobile apps!
259-
// if (platforms.includes("hybrid")) {
260-
// console.log("Registering indent");
261-
// registerBroadcastReceiver();
262-
// window.plugins.intentShim.onIntent((intent) => {
263-
// handleIntent(intent);
264-
// });
265-
// }
266-
// apiInstance.get("/status").then((response) => {
267-
// store.commit("simpleapi/setPlayerData", response.data);
268-
// });
269-
// store.dispatch("simpleapi/setPlaybackRefreshInterval");
270-
// });
271-
// window.addEventListener("orientationchange", function () {
272-
// store.commit("app/setScreenOrinetation", screen.orientation.type);
273-
// });
274-
// };
275-
// // we should call initApp also on debug builds
276-
// if (!platforms.includes("hybrid")) initApp();
277-
// document.addEventListener("deviceReady", () => initApp());
278-
// App.addListener("appStateChange", ({ isActive }) => {
279-
// if (isActive) {
280-
// // Set screen orientation if active
281-
// store.commit("app/setScreenOrinetation", screen.orientation.type);
282-
// apiInstance.get("/status").then((response) => {
283-
// store.commit("simpleapi/setPlayerData", response.data);
284-
// });
285-
// if (!store.state.simpleapi.playbackRefreshInterval) {
286-
// store.dispatch("simpleapi/setPlaybackRefreshInterval");
287-
// }
288-
// if (platforms.includes("hybrid")) registerBroadcastReceiver();
289-
// } else {
290-
// // Battery saving stuff
291-
// store.commit("simpleapi/clearPlaybackRefreshInterval");
292-
// if (platforms.includes("hybrid")) unregisterBroadcastReceiver();
293-
// }
294-
// });
295-
// return {
296-
// selectedIndex,
297-
// appPages,
298-
// cogOutline,
299-
// informationCircleOutline,
300-
// listOutline,
301-
// playCircleOutline,
302-
// version: appInfo.version,
303-
// connectionState: store.getters["simpleapi/connectionState"],
304-
// servers: store.getters["settings/servers"],
305-
// currentServerId: store.getters["settings/currentServerId"],
306-
// setServer,
307-
// isSelected: (url) => (url === route.path ? "selected" : ""),
308-
// };
309263
},
310264
});
311265
</script>

src/dbcrud.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,14 @@ export const initDBTables = async (db) => {
2626

2727
export const createServer = async (db, data) => {
2828
try {
29-
console.log(`Creating server: ${JSON.stringify(data)}`);
3029
const res = await db.run(
3130
`INSERT INTO server (name, host, port) VALUES ("${data.name}", "${data.host}", ${data.port} );`
3231
);
3332

3433
if (res.changes && res.changes.changes && res.changes.changes < 0) {
35-
console.log("Failed");
3634
throw new Error(`Error: execute failed`);
3735
}
38-
console.log(res.changes);
36+
return res;
3937
} catch (e) {
4038
console.log("Failed creating server:");
4139
console.log(e);
@@ -63,3 +61,9 @@ export const updateServer = async (db, id, data) => {
6361
throw new Error(`Error: execute failed`);
6462
}
6563
};
64+
65+
export const deleteServer = async (db, id) => {
66+
const res = await db.run("DELETE FROM server WHERE id=?", [id]);
67+
console.log(res);
68+
return res;
69+
};

0 commit comments

Comments
 (0)