Refactor capacitor plugins, clean up and organize android plugin classes

This commit is contained in:
advplyr
2022-04-04 19:08:27 -05:00
parent cc744bb975
commit 77ef0c119b
26 changed files with 376 additions and 385 deletions
-4
View File
@@ -1,4 +0,0 @@
import { registerPlugin } from '@capacitor/core';
const AudioDownloader = registerPlugin('AudioDownloader');
export default AudioDownloader;
+13
View File
@@ -0,0 +1,13 @@
import { registerPlugin, WebPlugin } from '@capacitor/core';
class AbsAudioPlayerWeb extends WebPlugin {
constructor() {
super()
}
}
const AbsAudioPlayer = registerPlugin('AbsAudioPlayer', {
web: () => new AbsAudioPlayerWeb()
})
export { AbsAudioPlayer }
@@ -1,11 +1,11 @@
import { registerPlugin, Capacitor, WebPlugin } from '@capacitor/core';
class DbWeb extends WebPlugin {
class AbsDatabaseWeb extends WebPlugin {
constructor() {
super()
}
async getDeviceData_WV() {
async getDeviceData() {
var dd = localStorage.getItem('device')
if (dd) {
return JSON.parse(dd)
@@ -18,8 +18,8 @@ class DbWeb extends WebPlugin {
return deviceData
}
async setCurrentServerConnectionConfig_WV(serverConnectionConfig) {
var deviceData = await this.getDeviceData_WV()
async setCurrentServerConnectionConfig(serverConnectionConfig) {
var deviceData = await this.getDeviceData()
var ssc = deviceData.serverConnectionConfigs.find(_ssc => _ssc.id == serverConnectionConfig.id)
if (ssc) {
@@ -44,20 +44,22 @@ class DbWeb extends WebPlugin {
return ssc
}
async removeServerConnectionConfig_WV(serverConnectionConfigCallObject) {
async removeServerConnectionConfig(serverConnectionConfigCallObject) {
var serverConnectionConfigId = serverConnectionConfigCallObject.serverConnectionConfigId
var deviceData = await this.getDeviceData_WV()
var deviceData = await this.getDeviceData()
deviceData.serverConnectionConfigs = deviceData.serverConnectionConfigs.filter(ssc => ssc.id == serverConnectionConfigId)
localStorage.setItem('device', JSON.stringify(deviceData))
}
logout_WV() {
// Nothing to do on web
async logout() {
var deviceData = await this.getDeviceData()
deviceData.lastServerConnectionConfigId = null
localStorage.setItem('device', JSON.stringify(deviceData))
}
}
const DbManager = registerPlugin('DbManager', {
web: () => new DbWeb()
const AbsDatabase = registerPlugin('AbsDatabase', {
web: () => new AbsDatabaseWeb()
})
export { DbManager }
export { AbsDatabase }
+13
View File
@@ -0,0 +1,13 @@
import { registerPlugin, WebPlugin } from '@capacitor/core';
class AbsDownloaderWeb extends WebPlugin {
constructor() {
super()
}
}
const AbsDownloader = registerPlugin('AbsDownloader', {
web: () => new AbsDownloaderWeb()
})
export { AbsDownloader }
+13
View File
@@ -0,0 +1,13 @@
import { registerPlugin, WebPlugin } from '@capacitor/core';
class AbsFileSystemWeb extends WebPlugin {
constructor() {
super()
}
}
const AbsFileSystem = registerPlugin('AbsFileSystem', {
web: () => new AbsFileSystemWeb()
})
export { AbsFileSystem }
+13
View File
@@ -0,0 +1,13 @@
import Vue from 'vue'
import { AbsAudioPlayer } from './AbsAudioPlayer'
import { AbsDownloader } from './AbsDownloader'
import { AbsFileSystem } from './AbsFileSystem'
import { Capacitor } from '@capacitor/core'
Vue.prototype.$platform = Capacitor.getPlatform()
export {
AbsAudioPlayer,
AbsDownloader,
AbsFileSystem
}
+12 -12
View File
@@ -1,5 +1,5 @@
import { Capacitor } from '@capacitor/core';
import { DbManager } from './capacitor/DbManager'
import { AbsDatabase } from './capacitor/AbsDatabase'
const isWeb = Capacitor.getPlatform() == 'web'
@@ -8,7 +8,7 @@ class DbService {
save(db, key, value) {
if (isWeb) return
return DbManager.saveFromWebview({ db, key, value }).then(() => {
return AbsDatabase.saveFromWebview({ db, key, value }).then(() => {
console.log('Saved data', db, key, JSON.stringify(value))
}).catch((error) => {
console.error('Failed to save data', error)
@@ -17,7 +17,7 @@ class DbService {
load(db, key) {
if (isWeb) return null
return DbManager.loadFromWebview({ db, key }).then((data) => {
return AbsDatabase.loadFromWebview({ db, key }).then((data) => {
console.log('Loaded data', db, key, JSON.stringify(data))
return data
}).catch((error) => {
@@ -27,33 +27,33 @@ class DbService {
}
getDeviceData() {
return DbManager.getDeviceData_WV().then((data) => {
return AbsDatabase.getDeviceData().then((data) => {
console.log('Loaded device data', JSON.stringify(data))
return data
})
}
setServerConnectionConfig(serverConnectionConfig) {
return DbManager.setCurrentServerConnectionConfig_WV(serverConnectionConfig).then((data) => {
return AbsDatabase.setCurrentServerConnectionConfig(serverConnectionConfig).then((data) => {
console.log('Set server connection config', JSON.stringify(data))
return data
})
}
removeServerConnectionConfig(serverConnectionConfigId) {
return DbManager.removeServerConnectionConfig_WV({ serverConnectionConfigId }).then((data) => {
return AbsDatabase.removeServerConnectionConfig({ serverConnectionConfigId }).then((data) => {
console.log('Removed server connection config', serverConnectionConfigId)
return true
})
}
logout() {
return DbManager.logout_WV()
return AbsDatabase.logout()
}
getLocalFolders() {
if (isWeb) return []
return DbManager.getLocalFolders_WV().then((data) => {
return AbsDatabase.getLocalFolders().then((data) => {
console.log('Loaded local folders', JSON.stringify(data))
if (data.folders && typeof data.folders == 'string') {
return JSON.parse(data.folders)
@@ -67,7 +67,7 @@ class DbService {
getLocalFolder(folderId) {
if (isWeb) return null
return DbManager.getLocalFolder_WV({ folderId }).then((data) => {
return AbsDatabase.getLocalFolder({ folderId }).then((data) => {
console.log('Got local folder', JSON.stringify(data))
return data
})
@@ -75,7 +75,7 @@ class DbService {
getLocalMediaItemsInFolder(folderId) {
if (isWeb) return []
return DbManager.getLocalMediaItemsInFolder_WV({ folderId }).then((data) => {
return AbsDatabase.getLocalMediaItemsInFolder({ folderId }).then((data) => {
console.log('Loaded local media items in folder', JSON.stringify(data))
if (data.localMediaItems && typeof data.localMediaItems == 'string') {
return JSON.parse(data.localMediaItems)
@@ -86,7 +86,7 @@ class DbService {
getLocalLibraryItems() {
if (isWeb) return []
return DbManager.getLocalLibraryItems_WV().then((data) => {
return AbsDatabase.getLocalLibraryItems().then((data) => {
console.log('Loaded all local media items', JSON.stringify(data))
if (data.localLibraryItems && typeof data.localLibraryItems == 'string') {
return JSON.parse(data.localLibraryItems)
@@ -97,7 +97,7 @@ class DbService {
getLocalLibraryItem(id) {
if (isWeb) return null
return DbManager.getLocalLibraryItem_WV({ id })
return AbsDatabase.getLocalLibraryItem({ id })
}
}
-7
View File
@@ -1,7 +0,0 @@
import Vue from 'vue'
import { registerPlugin, Capacitor } from '@capacitor/core';
Vue.prototype.$platform = Capacitor.getPlatform()
const MyNativeAudio = registerPlugin('MyNativeAudio');
export default MyNativeAudio;
+10 -3
View File
@@ -13,6 +13,16 @@ class ServerSocket extends EventEmitter {
this.token = null
}
$on(evt, callback) {
if (this.socket) this.socket.on(evt, callback)
else console.error('$on Socket not initialized')
}
$off(evt, callback) {
if (this.socket) this.socket.off(evt, callback)
else console.error('$off Socket not initialized')
}
connect(serverAddress, token) {
this.serverAddress = serverAddress
this.token = token
@@ -33,7 +43,6 @@ class ServerSocket extends EventEmitter {
}
setSocketListeners() {
if (!this.socket) return
this.socket.on('connect', this.onConnect.bind(this))
this.socket.on('disconnect', this.onDisconnect.bind(this))
this.socket.on('init', this.onInit.bind(this))
@@ -41,7 +50,6 @@ class ServerSocket extends EventEmitter {
this.socket.onAny((evt, args) => {
console.log(`[SOCKET] ${this.socket.id}: ${evt} ${JSON.stringify(args)}`)
})
}
onConnect() {
@@ -73,6 +81,5 @@ class ServerSocket extends EventEmitter {
}
export default ({ app, store }, inject) => {
console.log('Check event bus', this, Vue.prototype.$eventBus)
inject('socket', new ServerSocket(store))
}
-5
View File
@@ -1,5 +0,0 @@
import { registerPlugin } from '@capacitor/core';
const StorageManager = registerPlugin('StorageManager');
export default StorageManager;